Session Automatic_Refinement

Theory Refine_Util_Bootstrap1

theory Refine_Util_Bootstrap1
imports Main
ML infix 1 ##

  signature BASIC_REFINE_UTIL = sig
    val map_option: ('a -> 'b) -> 'a option -> 'b option

    val map_fold: ('a -> 'b -> 'c * 'b) -> 'a list -> 'b -> 'c list * 'b

    val split: ('a -> bool) -> 'a list -> 'a list * 'a list
    val split_matching: ('a -> 'b -> bool) -> 'a list -> 'b list -> ('b list * 'b list) option

    val yield_singleton2: ('a list -> 'b -> ('c * 'd list) * 'e) -> 'a -> 'b ->
      ('c * 'd) * 'e

    val ## : ('a -> 'c) * ('b -> 'd) -> ('a * 'b) -> ('c * 'd)

    val seq_is_empty: 'a Seq.seq -> bool * 'a Seq.seq


  structure Basic_Refine_Util : BASIC_REFINE_UTIL = struct
    fun map_option _ NONE = NONE
      | map_option f (SOME x) = SOME (f x)

    fun map_fold _ [] s = ([],s)
      | map_fold f (x::xs) s = 
          val (x',s') = f x s
          val (xs',s') = map_fold f xs s'

    fun yield_singleton2 f x y = case f [x] y of
      ((r1,[r2]),r3) => ((r1,r2),r3)
    | _ => error "INTERNAL: yield_singleton2"

    fun (f ## g) (a,b) = (f a, g b)
    fun seq_is_empty seq = case Seq.pull seq of
      NONE => (true, seq)
    | SOME (a,seq) => (false, Seq.cons a seq)

      fun split P l = (filter P l, filter_out P l)

      fun split_matching R xs ys = let
        exception EXC
        fun fs _ [] = raise EXC
          | fs x (y::ys) = 
              if R x y then (y,ys) 
              else let val (y',ys) = fs x ys in (y',y::ys) end
        fun fm [] ys = ([],ys)      
          | fm (x::xs) ys = let
              val (y,ys) = fs x ys
              val (ys1,ys2) = fm xs ys
        try (fm xs) ys


  open Basic_Refine_Util  


Theory Mpat_Antiquot

section ‹Matching-Pattern Antiquotation›
theory Mpat_Antiquot
imports Refine_Util_Bootstrap1

typedecl term_struct
typedecl typ_struct
typedecl sort

definition mpaq_AS_PAT :: "'a  'a  'a" (infixl "ASp" 900)
  where "a ASp s  a"

definition mpaq_STRUCT :: "term_struct  'a" where "mpaq_STRUCT _ = undefined"

abbreviation mpaq_AS_STRUCT :: "'a  term_struct  'a" (infixl "ASs" 900)
  where "a ASs s  a ASp mpaq_STRUCT s"

  mpaq_Const :: "string  typ_struct  term_struct"
  mpaq_Free :: "string  typ_struct  term_struct"
  mpaq_Var :: "string*nat  typ_struct  term_struct"
  mpaq_Bound :: "nat  term_struct"
  mpaq_Abs :: "string  typ_struct  term_struct  term_struct"
  mpaq_App :: "term_struct  term_struct  term_struct" (infixl "$$" 900)

  mpaq_TFree :: "string  sort  typ_struct"
  mpaq_TVar :: "string*nat  sort  typ_struct"
  mpaq_Type :: "string  typ_struct list  typ_struct"

ML (*
    Antiquotation to generate a term-pattern in ML. 
      * Schematic term variables are translated to ML-variables of same name.
      * Non-dummy variables in lambda-abstractions are bound to variables of 
        same name, and type of λx. t is bound to x_T
      * In (typs) mode, schematic type variables are translated if they start
        with 'v_, where the prefix is removed. Otherwise, types are completely
        ignored and translated to ML dummy pattern.
      * Dummy variables are translated to ML dummy patterns
      * Supports AS to define alternative pattern,
        and STRUCT to reflect term structure
      * Non-linear patterns are not allowed, due to SML limitation.
        For term variables, it will result in an error. Type variables, 
        however, are silently linearized and only the first occurrence is bound.
      * Indexes <>0 on term variables are not allowed. 
        On type variables, such indexes are silently ignored.
      * Sorts are completely ignored
      * Due to the type-inference on patterns, only innermost types can be 
        bound to names.
      * Patterns are not localized. Currently, we issue an error if pattern
        contains free variables.

      * We could also bind the type of a term-var, where the name for the 
        type ML-var is encoded into the term-var name

    Examples: See below


    fun replace_dummy' (Const (@{const_name Pure.dummy_pattern}, T)) i =
          (Var (("_dummy_", i), T), i + 1)
      | replace_dummy' (Abs (x, T, t)) i =
          let val (t', i') = replace_dummy' t i
          in (Abs (x, T, t'), i') end
      | replace_dummy' (t $ u) i =
            val (t', i') = replace_dummy' t i;
            val (u', i'') = replace_dummy' u i';
          in (t' $ u', i'') end
      | replace_dummy' a i = (a, i);

    fun prepare_dummies' ts = #1 (fold_map replace_dummy' ts 1);

    fun tcheck (Type (name,Ts)) tnames = 
          val (Ts,tnames) = map_fold tcheck Ts tnames
          (Type (name,Ts),tnames)
      | tcheck (TFree (name,_)) _ = 
          error ("Pattern contains free type variable: " ^ name)
      | tcheck (TVar ((name,idx),S)) tnames = 
          if String.isPrefix "'v_" name andalso not (member (op =) tnames name) 
          then (TVar ((unprefix "'v_" name,idx),S),name::tnames)
          else (TVar (("_",0),S),tnames)

    fun vcheck (Const (n,T)) (vnames,tnames) = 
          val (T,tnames) = tcheck T tnames
        in (Const (n,T),(vnames,tnames)) end
      | vcheck (Free (n,_)) _ = 
          error ("Pattern contains free variable: " ^ n)
      | vcheck (Var (("_dummy_",_),T)) (vnames,tnames) = 
          val (T,tnames) = tcheck T tnames
        in (Var (("_",0),T),(vnames,tnames)) end
      | vcheck (Var ((name,idx),T)) (vnames,tnames) = 
          val (T,tnames) = tcheck T tnames
          val _ = idx <> 0 andalso error ("Variable index greater zero: "
            ^ Term.string_of_vname (name,idx)) 
          val _ = member (op =) vnames name andalso error ("Non-linear pattern: "
            ^ Term.string_of_vname (name,idx))
        in (Var ((name,0),T),(name::vnames,tnames)) end
      | vcheck (Bound i) p = (Bound i,p)
      | vcheck (Abs ("uu_",T,t)) (vnames,tnames) = let
          val (T,tnames) = tcheck T tnames
          val (t,(vnames,tnames)) = vcheck t (vnames,tnames)
        in (Abs ("_",T,t),(vnames,tnames)) end
      | vcheck (Abs (x,T,t)) (vnames,tnames) = let
          val (T,tnames) = tcheck T tnames
          val (t,(vnames,tnames)) = vcheck t (vnames,tnames)
        in (Abs (x,T,t),(vnames,tnames)) end
      | vcheck (t1$t2) p = let
          val (t1,p) = vcheck t1 p
          val (t2,p) = vcheck t2 p
        in (t1$t2,p) end

    val read = Scan.lift (Args.mode "typs" )
      -- (Args.context -- Scan.lift (Parse.position Args.embedded_inner_syntax))

    fun write (with_types, t) = let
      fun twr_aux (Type (name,Ts)) 
            = "Type (" ^ ML_Syntax.print_string name ^ ", " ^ ML_Syntax.print_list twr_aux Ts ^ ")"
        | twr_aux (TFree (name,_)) = "TFree (" ^ ML_Syntax.print_string name ^ ", _)"
        | twr_aux (TVar ((name,_),_)) = name
      val twr = if with_types then twr_aux else K "_"

      fun s_string (Var ((name,_),_)) = name
        | s_string t = case try HOLogic.dest_string t of
            SOME s => ML_Syntax.print_string s
          | NONE => raise TERM ("Expected var or string literal",[t])

      fun s_index (Var ((name,_),_)) = name
        | s_index t = case try HOLogic.dest_nat t of
            SOME n => ML_Syntax.print_int n
          | NONE => raise TERM ("Expected var or nat literal",[t])

      fun s_indexname (Var ((name,_),_)) = name
        | s_indexname (Const (@{const_name Pair},_)$n$i) =
            "(" ^ s_string n ^ ", " ^ s_index i ^ ")"
        | s_indexname t = raise TERM ("Expected var or indexname-pair",[t])

      fun s_typ (Var ((name,_),_)) = name
        | s_typ (Const(@{const_name "mpaq_TFree"},_)$name$typ) 
            = "TFree (" ^ s_string name ^ "," ^ s_typ typ ^ ")"
        | s_typ (Const(@{const_name "mpaq_TVar"},_)$name$typ)
            = "TVar (" ^ s_indexname name ^ "," ^ s_typ typ ^ ")"
        | s_typ (Const(@{const_name "mpaq_Type"},_)$name$args)
            = "Type (" ^ s_string name ^ "," ^ s_args args ^ ")"
        | s_typ t = raise TERM ("Expected var or type structure",[t])
      and s_args (Var ((name,_),_)) = name
        | s_args t = ( case try HOLogic.dest_list t of
            SOME l => ML_Syntax.print_list s_typ l
          | NONE => raise TERM ("Expected variable or type argument list",[t])

      fun swr (Const(@{const_name "mpaq_Const"},_)$name$typ) 
            = "Const (" ^ s_string name ^ ", " ^ s_typ typ ^ ")"
        | swr (Const(@{const_name "mpaq_Free"},_)$name$typ)
            = "Free (" ^ s_string name ^ ", " ^ s_typ typ ^ ")"
        | swr (Const(@{const_name "mpaq_Var"},_)$name$typ)
            = "Var (" ^ s_indexname name ^ ", " ^ s_typ typ ^ ")"
        | swr (Const(@{const_name "mpaq_Bound"},_)$idx)
            = "Bound " ^ s_index idx
        | swr (Const(@{const_name "mpaq_Abs"},_)$name$T$t)
            = "Abs (" ^ s_string name ^ ", " ^ s_typ T ^ ", " ^ swr t ^ ")"
        | swr (Const(@{const_name "mpaq_App"},_)$t1$t2)
            = "(" ^ swr t1 ^ ") $ (" ^ swr t2 ^ ")"
        | swr t = raise TERM ("Expected var or term structure",[t])

      fun vwr (Const (name,T)) 
            = "Const (" ^ ML_Syntax.print_string name ^ ", " ^ twr T ^ ")"
        | vwr (Var ((name,_),_)) = name
        | vwr (Free (name,T)) = "Free (" ^ name ^ ", " ^ twr T ^ ")"
        | vwr (Bound i) = "Bound " ^ ML_Syntax.print_int i
        | vwr (Abs ("_",T,t)) = "Abs (_," ^ twr T ^ "," ^ vwr t ^ ")"
        | vwr (Abs (x,T,t)) = "Abs (" ^ x ^ "," 
                ^ x ^ "_T as " ^ twr T ^ "," 
                ^ vwr t ^ ")"
        | vwr (Const(@{const_name mpaq_STRUCT},_)$t) = (
            swr t
        | vwr (trm as Const(@{const_name "mpaq_AS_PAT"},_)$t$s) = ( 
            case t of
              (Var ((name,_),_)) => name ^ " as (" ^ vwr s ^ ")"
            | _ => raise TERM ("_AS_ must have identifier on LHS",[trm])
        | vwr (t1$t2) = "(" ^ vwr t1 ^ ") $ (" ^ vwr t2 ^ ")"

      val (t,_) = vcheck t ([],[])
      val s = vwr t
      "(" ^ s ^ ")"

    fun process (with_types,(ctxt,(raw_t,pos))) = (let
      val ctxt' = Context.proof_map (
        Syntax_Phases.term_check 90 "Prepare dummies" (K prepare_dummies')
      ) ctxt
      val t = Proof_Context.read_term_pattern ctxt' raw_t
      val res = write (with_types,t)
      (*tracing res;*)
    handle ERROR msg => error (msg ^ pos)

    val mpat_antiquot = read >> process

setup ‹
  ML_Antiquotation.inline @{binding "mpat"} mpat_antiquot

subsection ‹Examples›

ML_val fun dest_pair_singleton @{mpat "{(?a,_)}"} = (a)
    | dest_pair_singleton t = raise TERM ("dest_pair_singleton",[t])

  fun dest_nat_pair_singleton @{mpat (typs) "{(?a::nat,?b::nat)}"} = (a,b)
    | dest_nat_pair_singleton t = raise TERM ("dest_nat_pair_singleton",[t])

  fun dest_pair_singleton_T @{mpat (typs) "{(?a::_  ?'v_Ta,?b::?'v_Tb)}"} = 
    | dest_pair_singleton_T t = raise TERM ("dest_pair_singleton_T",[t])

  fun dest_pair_lambda @{mpat "{(λa _ _. ?Ta, λb. ?Tb)}"} = (a,a_T,b,b_T,Ta,Tb)
    | dest_pair_lambda t = raise TERM ("dest_pair_lambda",[t])

  fun foo @{mpat "[?a,?b ASs mpaq_Bound ?i,?c ASp [?n]]"} = 
  | foo t = raise TERM ("foo",[t])

hide_type (open) term_struct typ_struct sort


Theory Mk_Term_Antiquot

section ‹Antiquotation to Build Terms›
theory Mk_Term_Antiquot
imports Refine_Util_Bootstrap1

ML (*
  Antiquotation to generate a term from a template.

  This antiquotation takes a term with schematic variables, 
  interprets the schematic variables as names of term-valued ML-variables and generates
  code to create the ML-level representation of the term with the schematics replaced
  by the ML-variables.  

  All type variables in the term must be inferable from the types of the schematics.

    * The type-inference is not complete, i.e., it may fail to detect some type-errors,
      resulting in untypable terms to be created.

    * Does not work if inserted terms contain loose bound variables (FIXME)

    * We could also provide explicit type variables

  Examples: See below


    add_nv_tvars (Const (_,T)) l = Term.add_tvarsT T l
  | add_nv_tvars (Free (_,T)) l =  Term.add_tvarsT T l
  | add_nv_tvars (Abs (_,T,t)) l = add_nv_tvars t (Term.add_tvarsT T l)
  | add_nv_tvars (t1$t2) l = add_nv_tvars t1 (add_nv_tvars t2 l)
  | add_nv_tvars _ l = l

  fun prepare env t ctxt = let
    val tvars = add_nv_tvars t []
    val vars = Term.add_vars t []
    val vtvars = fold (Term.add_tvarsT o #2) vars []
    fun is_expl_tvar (n,i) = i=0 andalso String.isPrefix "'v_" n
    val expl_tvars = Term.add_tvars t []
      |> filter (is_expl_tvar o #1)
    val spec_tvars = union (op =) vtvars expl_tvars
    val _ = subset (op =) (tvars, spec_tvars)
      orelse let
        val loose = subtract (op =) spec_tvars tvars 
          |> map (TVar #> Syntax.pretty_typ ctxt)
          |> Pretty.commas |> Pretty.block

        val pretty_t = Syntax.pretty_term ctxt t
        val msg = Pretty.block [
            Pretty.str "mk_term: Loose type variables in",
            Pretty.brk 1,pretty_t,Pretty.str ":",Pretty.brk 1,loose
          ] |> Pretty.string_of
      in error msg end

    val _ = fold 
      (fn v as ((_,i),_) => fn () => if i<>0 then 
          Pretty.block [
            Pretty.str "mk_term: Variable indices must be zero:", Pretty.brk 1,
            Syntax.pretty_term ctxt (Var v)
          ] |> Pretty.string_of |> error
        else ()
      vars ()
    (* ARGH!!! "subtract eq a b" computes "b - a" *)
    val unused_tvars = subtract (op =) tvars vtvars 
      |> map #1
    val _ = tracing ("tvars: " ^ @{make_string} tvars)
    val _ = tracing ("vtvars: " ^ @{make_string} vtvars)
    val _ = tracing ("unused_tvars: " ^ @{make_string} unused_tvars)
      lin_type (TFree f) Ts = (TFree f, Ts)
    | lin_type (TVar (iname,S)) Ts = 
        if is_expl_tvar iname orelse member (op =) Ts iname then (TVar (("_",0),S), Ts)
        else (TVar (iname,S), iname::Ts)
    | lin_type (Type (name,args)) Ts = let
        val (args,Ts) = map_fold lin_type args Ts
        (Type (name,args),Ts)

    val (vars,_) = map_fold 
      ( fn (iname,T) => fn Ts => 
          let val (T,Ts) = lin_type T Ts in ((iname,T),Ts) end
      vars unused_tvars

    fun name_of_T (name,idx) = 
      if is_expl_tvar (name,idx) then unprefix "'v_" name
      else "T_" ^ name ^ "_" ^ string_of_int idx

      fun wrv (TVar (("_",_),_)) = "_"
        | wrv (TVar (iname,_)) = name_of_T iname
        | wrv (T as TFree _) = ML_Syntax.print_typ T
        | wrv (Type arg) =
            "Type " ^ ML_Syntax.print_pair ML_Syntax.print_string (ML_Syntax.print_list wrv) arg

      fun is_special (TVar _) = false | is_special _ = true
      fun mk_error_msg name T = 
        Pretty.block [
          Pretty.str ("mk_term type error: Argument for ?" ^ name ^ " does not match type"),
          Pretty.brk 1, Syntax.pretty_typ ctxt T
      |> Pretty.unformatted_string_of |> ML_Syntax.print_string
      fun pr_fastype name = case env of
        SOME env => "fastype_of1 (" ^ env ^ ", " ^ name ^ ")"
      | _ => "fastype_of " ^ name
      fun matcher ((name,_),T) rest = 
        "case " ^ pr_fastype name ^ " of " ^ wrv T ^ " => (" ^ rest ^ ")"
        ^ ( if is_special T then "| _ => raise TERM ("^mk_error_msg name T^",["^name^"])" 
            else "")

      fun matchers [] rest = rest
        | matchers (v::vs) rest = matcher v (matchers vs rest)


      fun print_typ (Type arg) =
            "Type " ^ ML_Syntax.print_pair ML_Syntax.print_string (ML_Syntax.print_list print_typ) arg
        | print_typ (TFree arg) =
            "TFree " ^ ML_Syntax.print_pair ML_Syntax.print_string ML_Syntax.print_sort arg
        | print_typ (TVar (iname, _)) = name_of_T iname;

      fun print_term (Const arg) =
            "Const " ^ ML_Syntax.print_pair ML_Syntax.print_string print_typ arg
        | print_term (Free arg) =
            "Free " ^ ML_Syntax.print_pair ML_Syntax.print_string ML_Syntax.print_typ arg
        | print_term (Var ((name, _), _)) = name
        | print_term (Bound i) = "Bound " ^ ML_Syntax.print_int i
        | print_term (Abs (s, T, t)) = 
            if String.isPrefix "v_" s then
              "Abs (" ^ unprefix "v_" s ^ ", " ^ print_typ T ^ ", " ^ print_term t ^ ")"
              "Abs (" ^ ML_Syntax.print_string s ^ ", " ^ print_typ T ^ ", " ^ print_term t ^ ")"
        | print_term (t1 $ t2) =
            ML_Syntax.atomic (print_term t1) ^ " $ " ^ ML_Syntax.atomic (print_term t2);
      val et = print_term t
      val e = "(" ^ matchers vars et ^ ")"

  val read = Scan.lift (Scan.option ( --| Args.colon)) -- 
    (Args.context -- Scan.lift (Parse.position Args.embedded_inner_syntax))

  fun process (env,(ctxt,(raw_t,pos))) = let
    val t = Proof_Context.read_term_pattern ctxt raw_t
    prepare env t ctxt
  end handle ERROR msg => error (msg ^ pos)

  val mk_term_antiquot = read >> process

setup ‹ML_Antiquotation.inline @{binding mk_term} mk_term_antiquot

subsection "Examples"

ML_val (* The mk_term antiquotation can replace the omnipresent mk_xxx functions, and 
    easily works with complex patterns *)
  fun mk_2elem_list a b = @{mk_term "[?a,?b]"}
  fun mk_compr s P = @{mk_term "{ x?s. ?P x}"}

  val test1 = mk_2elem_list @{term "1::nat"} @{term "2::nat"} |> Thm.cterm_of @{context}
  val test2 = mk_compr @{term "{1,2,3::nat}"} @{term "(<) (2::nat)"} |> Thm.cterm_of @{context}

  val test3 = let 
    val x = Bound 0 
    val env = [@{typ nat}]
    @{mk_term env: "?x+?x"}

  val test4 = let 
    val x = Bound 0 
    val T = @{typ nat}
    ctd here: Handle bounds below lambdas!
    @{mk_term "λx::?'v_T. ?x"}


Theory Refine_Util

section "General Utilities"
theory Refine_Util
imports Refine_Util_Bootstrap1 Mpat_Antiquot Mk_Term_Antiquot
definition conv_tag where "conv_tag n x == x" 
  ― ‹Used internally for @{text "pat_conv"}-conversion›

lemma shift_lambda_left: "(f  λx. g x)  (x. f x  g x)" by simp
  infix 3 ->>

  signature BASIC_REFINE_UTIL = sig
    (* Resolution with matching *)
    val RSm: Proof.context -> thm -> thm -> thm

    val is_Abs: term -> bool
    val is_Comb: term -> bool
    val has_Var: term -> bool

    val is_TFree: typ -> bool

    val is_def_thm: thm -> bool

    (* Tacticals *)
    type tactic' = int -> tactic
    type itactic = int -> int -> tactic

    val IF_EXGOAL: (int -> tactic) -> tactic'
    val COND': (term -> bool) -> tactic'
    val CONCL_COND': (term -> bool) -> tactic'
    val THEN_ELSE': tactic' * (tactic' * tactic') -> tactic'
    val THEN_ELSE_COMB': 
      tactic' * ((tactic'*tactic'->tactic') * tactic' * tactic') -> tactic'

    val INTERVAL_FWD: tactic' -> int -> int -> tactic
    val THEN_ALL_NEW_FWD: tactic' * tactic' -> tactic'
    val REPEAT_ALL_NEW_FWD: tactic' -> tactic'
    val REPEAT_DETERM': tactic' -> tactic'
    val REPEAT': tactic' -> tactic'
    val ALL_GOALS_FWD': tactic' -> tactic'
    val ALL_GOALS_FWD: tactic' -> tactic

    val APPEND_LIST': tactic' list -> tactic'

    val SINGLE_INTERVAL: itactic -> tactic'
    val THEN_INTERVAL: itactic * itactic -> itactic
    val ORELSE_INTERVAL: itactic * itactic -> itactic

    val CAN': tactic' -> tactic'

    val NTIMES': tactic' -> int -> tactic'

    (* Only consider results that solve subgoal. If none, return all results unchanged. *)
    val TRY_SOLVED': tactic' -> tactic'

    (* Case distinction with tactics. Generalization of THEN_ELSE to lists. *)
    val CASES': (tactic' * tactic) list -> tactic'

    (* Tactic that depends on subgoal term structure *)
    val WITH_subgoal: (term -> tactic') -> tactic'
    (* Tactic that depends on subgoal's conclusion term structure *)
    val WITH_concl: (term -> tactic') -> tactic'

    (* Tactic version of Import, apply tactic, and export results.
      One effect is that schematic variables in the goal are fixed, and thus cannot 
      be instantiated by the tactic.
    val TRADE: (Proof.context -> tactic') -> Proof.context -> tactic'

    (* Tactics *)
    val fo_rtac: thm -> Proof.context -> tactic'
    val fo_resolve_tac: thm list -> Proof.context -> tactic'
    val rprems_tac: Proof.context -> tactic'
    val rprem_tac: int -> Proof.context -> tactic'
    val elim_all_tac: Proof.context -> thm list -> tactic

    val prefer_tac: int -> tactic

    val insert_subgoal_tac: cterm -> tactic'
    val insert_subgoals_tac: cterm list -> tactic'

    val eqsubst_inst_tac: Proof.context -> bool -> int list 
      -> ((indexname * Position.T) * string) list -> thm -> int -> tactic

    val eqsubst_inst_meth: (Proof.context -> Proof.method) context_parser

    (* Parsing *)
    val ->> : 'a context_parser *('a * Context.generic -> 'b * Context.generic)
      -> 'b context_parser


  signature REFINE_UTIL = sig

    val order_by: ('a * 'a -> order) -> ('b -> 'a) -> 'b list -> 'b list
    val build_res_net: thm list -> (int * thm)

    (* Terms *)
    val fo_matchp: theory -> cterm -> term -> term list option
    val fo_matches: theory -> cterm -> term -> bool

    val anorm_typ: typ -> typ
    val anorm_term: term -> term

    val import_cterms: bool -> cterm list -> Proof.context -> 
      cterm list * Proof.context

    val subsume_sort: ('a -> term) -> theory -> 'a list -> 'a list
    val subsume_sort_gen: ('a -> term) -> Context.generic 
      -> 'a list -> 'a list

    val mk_compN1: typ list -> int -> term -> term -> term
    val mk_compN: int -> term -> term -> term

    val dest_itselfT: typ -> typ
    val dummify_tvars: term -> term

    (* a≡λx. f x  ↦  a ?x ≡ f ?x *)
    val shift_lambda_left: thm -> thm
    val shift_lambda_leftN: int -> thm -> thm

    (* Left-Bracketed Structures *)

    (* Map [] to z, and [x1,...,xN] to f(...f(f(x1,x2),x3)...) *)
    val list_binop_left: 'a -> ('a * 'a -> 'a) -> 'a list -> 'a
    (* Map [] to z, [x] to i x, [x1,...,xN] to f(...f(f(x1,x2),x3)...), thread state *)
    val fold_binop_left: ('c -> 'b * 'c) -> ('a -> 'c -> 'b * 'c) -> ('b * 'b -> 'b) 
          -> 'a list -> 'c -> 'b * 'c

    (* Tuples, handling () as empty tuple *)      
    val strip_prodT_left: typ -> typ list
    val list_prodT_left: typ list -> typ
    val mk_ltuple: term list -> term
    (* Fix a tuple of new frees *)
    val fix_left_tuple_from_Ts: string -> typ list -> Proof.context -> term * Proof.context

    (* HO-Patterns with tuples *)
    (* Lambda-abstraction over list of terms, recognizing tuples *)
    val lambda_tuple: term list -> term -> term
    (* Instantiate tuple-types in specified variables *)
    val instantiate_tuples: Proof.context -> (indexname*typ) list -> thm -> thm
    (* Instantiate tuple-types in variables from given term *)
    val instantiate_tuples_from_term_tac: Proof.context -> term -> tactic
    (* Instantiate tuple types in variables of subgoal *)
    val instantiate_tuples_subgoal_tac: Proof.context -> tactic'

    (* Rules *)
    val abs_def: Proof.context -> thm -> thm

    (* Rule combinators *)
    (* Iterate rule on theorem until it fails *)  
    val repeat_rule: (thm -> thm) -> thm -> thm
    (* Apply rule on theorem and assert that theorem was changed *)
    val changed_rule: (thm -> thm) -> thm -> thm
    (* Try rule on theorem, return theorem unchanged if rule fails *)
    val try_rule: (thm -> thm) -> thm -> thm
    (* Singleton version of *)
    val trade_rule: (Proof.context -> thm -> thm) -> Proof.context -> thm -> thm
    (* Combine with first matching theorem *)
    val RS_fst: thm -> thm list -> thm
    (* Instantiate first matching theorem *)
    val OF_fst: thm list -> thm list -> thm

    (* Conversion *)
    val trace_conv: conv
    val monitor_conv: string -> conv -> conv
    val monitor_conv': string -> (Proof.context -> conv) -> Proof.context -> conv

    val fixup_vars: cterm -> thm -> thm
    val fixup_vars_conv: conv -> conv
    val fixup_vars_conv': (Proof.context -> conv) -> Proof.context -> conv

    val pat_conv': cterm -> (string -> Proof.context -> conv) -> Proof.context
      -> conv
    val pat_conv: cterm -> (Proof.context -> conv) -> Proof.context -> conv

    val HOL_concl_conv: (Proof.context -> conv) -> Proof.context -> conv

    val import_conv: (Proof.context -> conv) -> Proof.context -> conv

    val fix_conv: Proof.context -> conv -> conv
    val ite_conv: conv -> conv -> conv -> conv

    val cfg_trace_f_tac_conv: bool Config.T
    val f_tac_conv: Proof.context -> (term -> term) -> tactic -> conv

    (* Conversion combinators to choose first matching position *)
    (* Try argument, then function *)
    val fcomb_conv: conv -> conv
    (* Descend over function or abstraction *)
    val fsub_conv: (Proof.context -> conv) -> Proof.context -> conv 
    (* Apply to topmost matching position *)
    val ftop_conv: (Proof.context -> conv) -> Proof.context -> conv

    (* Parsing *)
    val parse_bool_config: string -> bool Config.T -> bool context_parser
    val parse_paren_list: 'a context_parser -> 'a list context_parser
    val parse_paren_lists: 'a context_parser -> 'a list list context_parser

    (* 2-step configuration parser *)
    (* Parse boolean config, name or no_name. *)
    val parse_bool_config': string -> bool Config.T -> Token.T list -> (bool Config.T * bool) * Token.T list
    (* Parse optional (p1,...,pn). Empty list if nothing parsed. *)
    val parse_paren_list': 'a parser -> Token.T list -> 'a list * Token.T list
    (* Apply list of (config,value) pairs *)
    val apply_configs: ('a Config.T * 'a) list -> Proof.context -> Proof.context


  structure Refine_Util: REFINE_UTIL = struct
    open Basic_Refine_Util

    fun RSm ctxt thA thB = let
      val (thA, ctxt') = ctxt
        |> Variable.declare_thm thA
        |> Variable.declare_thm thB
        |> yield_singleton (apfst snd oo Variable.import true) thA
      val thm = thA RS thB
      val thm = singleton (Variable.export ctxt' ctxt) thm
        |> Drule.zero_var_indexes

    fun is_Abs (Abs _) = true | is_Abs _ = false
    fun is_Comb (_$_) = true | is_Comb _ = false

    fun has_Var (Var _) = true
      | has_Var (Abs (_,_,t)) = has_Var t
      | has_Var (t1$t2) = has_Var t1 orelse has_Var t2
      | has_Var _ = false

    fun is_TFree (TFree _) = true
      | is_TFree _ = false

    fun is_def_thm thm = case thm |> Thm.prop_of of
      Const (@{const_name "Pure.eq"},_)$_$_ => true | _ => false

    type tactic' = int -> tactic
    type itactic = int -> int -> tactic

    (* Fail if subgoal does not exist *)
    fun IF_EXGOAL tac i st = if i <= Thm.nprems_of st then
      tac i st
    else no_tac st;

    fun COND' P = IF_EXGOAL (fn i => fn st => 
      (if P (Thm.prop_of st |> curry Logic.nth_prem i) then
      all_tac st else no_tac st) 
      handle TERM _ => no_tac st
      | Pattern.MATCH => no_tac st

    (* FIXME: Subtle difference between Logic.concl_of_goal and this:
         concl_of_goal converts loose bounds to frees!
    fun CONCL_COND' P = COND' (strip_all_body #> Logic.strip_imp_concl #> P)

    fun (tac1 THEN_ELSE' (tac2,tac3)) x = tac1 x THEN_ELSE (tac2 x,tac3 x);  

    (* If first tactic succeeds, combine its effect with "comb tac2", 
      otherwise use tac_else. Example: 
        tac1 THEN_ELSE_COMB ((THEN_ALL_NEW),tac2,tac_else)  *)
    fun (tac1 THEN_ELSE_COMB' (comb,tac2,tac_else)) i st = let
      val rseq = tac1 i st
      case seq_is_empty rseq of
        (true,_) => tac_else i st
      | (false,rseq) => comb (K(K( rseq )), tac2) i st


    (* Apply tactic to subgoals in interval, in a forward manner, skipping over
      emerging subgoals *)
    fun INTERVAL_FWD tac l u st =
      if l>u then all_tac st 
      else (tac l THEN (fn st' => let
          val ofs = Thm.nprems_of st' - Thm.nprems_of st;
          if ofs < ~1 then raise THM (
            "INTERVAL_FWD: Tac solved more than one goal",~1,[st,st'])
          else INTERVAL_FWD tac (l+1+ofs) (u+ofs) st'
        end)) st;

    (* Apply tac2 to all subgoals emerged from tac1, in forward manner. *)
    fun (tac1 THEN_ALL_NEW_FWD tac2) i st =
      (tac1 i 
        THEN (fn st' => INTERVAL_FWD tac2 i (i + Thm.nprems_of st' - Thm.nprems_of st) st')
      ) st;

    fun REPEAT_ALL_NEW_FWD tac =
      tac THEN_ALL_NEW_FWD (TRY o (fn i => REPEAT_ALL_NEW_FWD tac i));

    (* Repeat tac on subgoal. Determinize each step. 
       Stop if tac fails or subgoal is solved. *)
    fun REPEAT_DETERM' tac i st = let
      val n = Thm.nprems_of st 
      REPEAT_DETERM (COND (has_fewer_prems n) no_tac (tac i)) st

    fun REPEAT' tac i st = let
      val n = Thm.nprems_of st 
      REPEAT (COND (has_fewer_prems n) no_tac (tac i)) st

    (* Apply tactic to all goals in a forward manner.
      Newly generated goals are ignored.
    fun ALL_GOALS_FWD' tac i st =
      (tac i THEN (fn st' => 
          val i' = i + Thm.nprems_of st' + 1 - Thm.nprems_of st;
          if i' <= Thm.nprems_of st' then
            ALL_GOALS_FWD' tac i' st'
            all_tac st'
      )) st;

    fun ALL_GOALS_FWD tac = ALL_GOALS_FWD' tac 1;

    fun APPEND_LIST' tacs = fold_rev (curry (op APPEND')) tacs (K no_tac);

    fun SINGLE_INTERVAL tac i = tac i i

    fun ((tac1:itactic) THEN_INTERVAL (tac2:itactic)) = 
      (fn i => fn j => fn st =>
        ( tac1 i j
          THEN (fn st' => tac2 i (j + Thm.nprems_of st' - Thm.nprems_of st) st')
        ) st

    fun tac1 ORELSE_INTERVAL tac2 = (fn i => fn j => tac1 i j ORELSE tac2 i j)

    (* Fail if tac fails, otherwise do nothing *)
    fun CAN' tac i st = 
      case tac i st |> Seq.pull of
        NONE => Seq.empty
      | SOME _ => Seq.single st

    (* Repeat tactic n times *)
    fun NTIMES' _ 0 _ st = Seq.single st
      | NTIMES' tac n i st = (tac THEN' NTIMES' tac (n-1)) i st

    (* Resolve with rule. Use first-order unification.
      From cookbook, added exception handling *)
    fun fo_rtac thm = Subgoal.FOCUS (fn {context = ctxt, concl, ...} => 
      val concl_pat = Drule.strip_imp_concl (Thm.cprop_of thm)
      val insts = Thm.first_order_match (concl_pat, concl)
      resolve_tac ctxt [Drule.instantiate_normalize insts thm] 1
    end handle Pattern.MATCH => no_tac )

    fun fo_resolve_tac thms ctxt = 
      FIRST' (map (fn thm => fo_rtac thm ctxt) thms);

    (* Resolve with premises. Copied and adjusted from Goal.assume_rule_tac. *)
    fun rprems_tac ctxt = Goal.norm_hhf_tac ctxt THEN' CSUBGOAL (fn (goal, i) =>
        fun non_atomic (Const (@{const_name Pure.imp}, _) $ _ $ _) = true
          | non_atomic (Const (@{const_name Pure.all}, _) $ _) = true
          | non_atomic _ = false;

        val ((_, goal'), ctxt') = Variable.focus_cterm NONE goal ctxt;
        val goal'' = Drule.cterm_rule 
          (singleton (Variable.export ctxt' ctxt)) goal';
        val Rs = filter (non_atomic o Thm.term_of) 
          (Drule.strip_imp_prems goal'');

        val ethms = Rs |> map (fn R =>
          (Simplifier.norm_hhf ctxt (Thm.trivial R)));
      in eresolve_tac ctxt ethms i end

    (* Resolve with premise. Copied and adjusted from Goal.assume_rule_tac. *)
    fun rprem_tac n ctxt = Goal.norm_hhf_tac ctxt THEN' CSUBGOAL (fn (goal, i) =>
        val ((_, goal'), ctxt') = Variable.focus_cterm NONE goal ctxt;
        val goal'' = Drule.cterm_rule 
          (singleton (Variable.export ctxt' ctxt)) goal';

        val R = nth (Drule.strip_imp_prems goal'') (n - 1)
        val rl = Simplifier.norm_hhf ctxt (Thm.trivial R)
        eresolve_tac ctxt [rl] i

    fun elim_all_tac ctxt thms = ALLGOALS (REPEAT_ALL_NEW (ematch_tac ctxt thms))

    fun prefer_tac i = defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1)

    fun order_by ord f = sort (ord o apply2 f)

    (* CLONE from tactic.ML *)
      (*insert one tagged rl into the net*)
      fun insert_krl (krl as (_,th)) =
        Net.insert_term (K false) (Thm.concl_of th, krl);
      (*build a net of rules for resolution*)
      fun build_res_net rls =
        fold_rev insert_krl (tag_list 1 rls) Net.empty;

    fun insert_subgoals_tac cts i = PRIMITIVE (
      Thm.permute_prems 0 (i - 1)
      #> fold_rev Thm.implies_intr cts
      #> Thm.permute_prems 0 (~i + 1)

    fun insert_subgoal_tac ct i = insert_subgoals_tac [ct] i

    (* Substitution with dynamic instantiation of parameters.
       By Lars Noschinski. *)
    fun eqsubst_tac' ctxt asm =
      if asm then EqSubst.eqsubst_asm_tac ctxt else EqSubst.eqsubst_tac ctxt

    fun subst_method inst_tac tac =
      Args.goal_spec --
      Scan.lift (Args.mode "asm" -- Scan.optional (Args.parens (Scan.repeat Parse.nat)) [0]) --
      Scan.optional (Scan.lift
          (Parse.position Args.var -- (Args.$$$ "=" |-- Parse.!!! Args.embedded_inner_syntax)) --|
          Args.$$$ "in")) [] --
      Attrib.thms >>
      (fn (((quant, (asm, occL)), insts), thms) => fn ctxt => METHOD 
        (fn facts =>
          if null insts then 
            quant (Method.insert_tac ctxt facts THEN' tac ctxt asm occL thms)
            (case thms of
              [thm] => quant (
                Method.insert_tac ctxt facts THEN' inst_tac ctxt asm occL insts thm)
            | _ => error "Cannot have instantiations with multiple rules")));

    fun eqsubst_inst_tac ctxt asm occL insts thm = 
      Subgoal.FOCUS (
        fn {context=ctxt,...} => let
          val ctxt' = ctxt |> Proof_Context.set_mode Proof_Context.mode_schematic  (* FIXME !? *)
          val thm' = thm |> Rule_Insts.read_instantiate ctxt' insts []
        in eqsubst_tac' ctxt asm occL [thm'] 1 end
      ) ctxt

    val eqsubst_inst_meth = subst_method eqsubst_inst_tac eqsubst_tac'

    (* Match pattern against term, and return list of values for non-dummy
      variables. A variable is considered dummy if its name starts with 
      an underscore (_)*)
    fun fo_matchp thy cpat t = let
      fun ignore (Var ((name,_),_)) = String.isPrefix "_" name
        | ignore _ = true;

      val pat = Thm.term_of cpat;
      val pvars = fold_aterms (
        fn t => fn l => if is_Var t andalso not (ignore t)
          then t::l else l
      ) pat [] |> rev
      val inst = Pattern.first_order_match thy (pat,t) 
    in SOME (map (Envir.subst_term inst) pvars) end 
    handle Pattern.MATCH => NONE;

    val fo_matches = is_some ooo fo_matchp

    fun anorm_typ ty = let
      val instT = Term.add_tvarsT ty []
      |> map_index (fn (i,(n,s)) => (n,TVar (("t"^string_of_int i,0),s)))
      val ty = Term.typ_subst_TVars instT ty;
    in ty end;

    fun anorm_term t = let
      val instT = Term.add_tvars t []
      |> map_index (fn (i,(n,s)) => (n,TVar (("t"^string_of_int i,0),s)))
      val t = Term.subst_TVars instT t;
      val inst = Term.add_vars t []
      |> map_index (fn (i,(n,s)) => (n,Var (("v"^string_of_int i,0),s)))
      val t = Term.subst_Vars inst t;
    in t end;

    fun import_cterms is_open cts ctxt = let
      val ts = map Thm.term_of cts
      val (ts', ctxt') = Variable.import_terms is_open ts ctxt
      val cts' = map (Thm.cterm_of ctxt) ts'
    in (cts', ctxt') end

    (* Order a list of items such that more specific items come
       before less specific ones. The term to be compared is
       extracted by a function that is given as parameter.
    fun subsume_sort f thy items = let
      val rhss = map (Envir.beta_eta_contract o f) items
      fun freqf thy net rhs = Net.match_term net rhs 
        |> filter (fn p => Pattern.matches thy (p,rhs))
        |> length

      val net = fold 
        (fn rhs => Net.insert_term_safe (op =) (rhs,rhs)) rhss Net.empty 

      val freqs = map (freqf thy net) rhss

      val res = freqs ~~ items 
        |> sort (rev_order o int_ord o apply2 fst)
        |> map snd
    in res end

    fun subsume_sort_gen f = subsume_sort f o Context.theory_of

    fun mk_comp1 env (f, g) =
        val fT = fastype_of1 (env, f);
        val gT = fastype_of1 (env, g);
        val compT = fT --> gT --> domain_type gT --> range_type fT;
      in Const ("Fun.comp", compT) $ f $ g end;

    fun mk_compN1 _   0 f g = f$g
      | mk_compN1 env 1 f g = mk_comp1 env (f, g)
      | mk_compN1 env n f g = let
          val T = fastype_of1 (env, g) |> domain_type
          val g = incr_boundvars 1 g $ Bound 0
          val env = T::env
          Abs ("x"^string_of_int n,T,mk_compN1 env (n-1) f g)

    val mk_compN = mk_compN1 []    

    fun abs_def ctxt = Local_Defs.meta_rewrite_rule ctxt #> Drule.abs_def

    fun trace_conv ct = (tracing (@{make_string} ct); Conv.all_conv ct);

    fun monitor_conv msg conv ct = let
      val _ = tracing (msg ^ " (gets): " ^ @{make_string} ct);
      val res = conv ct 
        handle exc =>
         (if Exn.is_interrupt exc then Exn.reraise exc
          else tracing (msg ^ " (raises): " ^ @{make_string} exc);
          Exn.reraise exc)
      val _ = tracing (msg ^ " (yields): " ^ @{make_string} res);
    in res end

    fun monitor_conv' msg conv ctxt ct = monitor_conv msg (conv ctxt) ct

    fun fixup_vars ct thm = let
      val lhs = Thm.lhs_of thm
      val inst = Thm.first_order_match (lhs,ct)
      val thm' = Thm.instantiate inst thm
    in thm' end

    fun fixup_vars_conv conv ct = fixup_vars ct (conv ct)

    fun fixup_vars_conv' conv ctxt = fixup_vars_conv (conv ctxt)

      fun tag_ct ctxt name ct = let
        val t = Thm.term_of ct;
        val ty = fastype_of t;
        val t' = Const (@{const_name conv_tag},@{typ unit}-->ty-->ty)
          $Free (name,@{typ unit})$t;
        val ct' = Thm.cterm_of ctxt t';
      in ct' end

      fun mpat_conv pat ctxt ct = let
        val (tym,tm) = Thm.first_order_match (pat,ct);
        val tm' = map (fn (pt as ((name, _), _),ot) => (pt, tag_ct ctxt name ot)) tm;
        val ct' = Thm.instantiate_cterm (tym,tm') pat;
        val rthm =
          Goal.prove_internal ctxt []
            (Thm.cterm_of ctxt (Logic.mk_equals (apply2 Thm.term_of (ct, ct'))))
            (K (simp_tac (put_simpset HOL_basic_ss ctxt addsimps @{thms conv_tag_def}) 1))
        |> Goal.norm_result ctxt
        fixup_vars ct rthm 
      end handle Pattern.MATCH 
        => raise (CTERM ("mpat_conv: No match",[pat,ct]));

      fun tag_conv cnv ctxt ct = case Thm.term_of ct of
        Const (@{const_name conv_tag},_)$Free(name,_)$_ => (
          (Conv.rewr_conv (@{thm conv_tag_def}) then_conv (cnv name) ctxt) ct)
      | _ => Conv.all_conv ct;

      fun all_tag_conv cnv = Conv.bottom_conv (tag_conv cnv);
      (* Match against pattern, and apply parameter conversion to matches of
         variables prefixed by hole_prefix.
      fun pat_conv' cpat cnv ctxt = 
        mpat_conv cpat ctxt
        then_conv (all_tag_conv cnv ctxt);

      fun pat_conv cpat conv = pat_conv' cpat 
        (fn name => case name of "HOLE" => conv | _ => K Conv.all_conv);

    fun HOL_concl_conv cnv = Conv.params_conv ~1 
      (fn ctxt => Conv.concl_conv ~1 (
        HOLogic.Trueprop_conv (cnv ctxt)));

    fun import_conv conv ctxt ct = let
      val (ct',ctxt') = yield_singleton (import_cterms true) ct ctxt
      val res = conv ctxt' ct'
      val res' = singleton (Variable.export ctxt' ctxt) res |> fixup_vars ct
    in res' end

    fun fix_conv ctxt conv ct = let
      val thm = conv ct
      val eq = Logic.mk_equals (Thm.term_of ct, Thm.term_of ct) |> head_of
    in if (Thm.term_of (Thm.lhs_of thm) aconv Thm.term_of ct)
      then thm
      else thm RS Thm.trivial (Thm.mk_binop (Thm.cterm_of ctxt eq) ct (Thm.rhs_of thm))

    fun ite_conv cv cv1 cv2 ct =
        val eq1 = SOME (cv ct) 
          handle THM _ => NONE
            | CTERM _ => NONE
            | TERM _ => NONE
            | TYPE _ => NONE;
        val res = case eq1 of
          NONE => cv2 ct
        | SOME eq1 => let val eq2 = cv1 (Thm.rhs_of eq1) in 
            if Thm.is_reflexive eq1 then eq2
            else if Thm.is_reflexive eq2 then eq1
            else Thm.transitive eq1 eq2
      in res end

      val cfg_trace_f_tac_conv = 
        Attrib.setup_config_bool @{binding trace_f_tac_conv} (K false)

      (* Transform term and prove equality to original by tactic *)
      fun f_tac_conv ctxt f tac ct = let
        val t = Thm.term_of ct
        val t' = f t
        val goal = Logic.mk_equals (t,t')
        val _ = if Config.get ctxt cfg_trace_f_tac_conv then
          tracing (Syntax.string_of_term ctxt goal)
        else ()

        val goal = Thm.cterm_of ctxt goal

        val thm = Goal.prove_internal ctxt [] goal (K tac)

    (* Apply function to result and context *)
    fun (p->>f) ctks = let
      val (res,(context,tks)) = p ctks
      val (res,context) = f (res, context)

    fun parse_bool_config name cfg = (
      Scan.lift (Args.$$$ name)
        ->> (apsnd (Config.put_generic cfg true) #>> K true)
      Scan.lift (Args.$$$ ("no_"^name))
        ->> (apsnd (Config.put_generic cfg false) #>> K false)

    fun parse_paren_list p = 
      Scan.lift (
        Args.$$$ "(") |-- Parse.enum1' "," p --| Scan.lift (Args.$$$ ")"

    fun parse_paren_lists p = Scan.repeat (parse_paren_list p)

    val _ = Theory.setup
      (Method.setup @{binding fo_rule} 
        (Attrib.thms >> (fn thms => fn ctxt => SIMPLE_METHOD' (
          fo_resolve_tac thms ctxt))) 
        "resolve using first-order matching"
      Method.setup @{binding rprems} 
        (Scan.lift (Scan.option Parse.nat) >> (fn i => fn ctxt => 
          SIMPLE_METHOD' (
            case i of
              NONE => rprems_tac ctxt
            | SOME i => rprem_tac i ctxt
        "resolve with premises"
      #> Method.setup @{binding elim_all}
         (Attrib.thms >> (fn thms => fn ctxt => SIMPLE_METHOD (elim_all_tac ctxt thms)))
         "repeteadly apply elimination rules to all subgoals"
      #> Method.setup @{binding subst_tac} eqsubst_inst_meth
         "single-step substitution (dynamic instantiation)"
      #> Method.setup @{binding clarsimp_all} (
           Method.sections Clasimp.clasimp_modifiers >> K (fn ctxt => SIMPLE_METHOD (
             CHANGED_PROP (ALLGOALS (Clasimp.clarsimp_tac ctxt))))
         ) "simplify and clarify all subgoals")


      (* Filter alternatives that solve a subgoal. 
        If no alternative solves goal, return result sequence unchanged *)
      fun TRY_SOLVED' tac i st = let
        val res = tac i st
        val solved = Seq.filter (fn st' => Thm.nprems_of st' < Thm.nprems_of st) res
        case Seq.pull solved of
          SOME _ => solved
        | NONE => res  
        fun CASES_aux [] = no_tac
          | CASES_aux ((tac1, tac2)::cs) = tac1 1 THEN_ELSE (tac2, CASES_aux cs)    
          Accepts a list of pairs of (pattern_tac', worker_tac), and applies
          worker_tac to results of first successful pattern_tac'.
        val CASES' = SELECT_GOAL o CASES_aux

      (* TODO/FIXME: There seem to be no guarantees when eta-long forms are introduced by unification.
        So, we have to expect eta-long forms everywhere, which may be a problem when matching terms
      fun WITH_subgoal tac = 
        CONVERSION Thm.eta_conversion THEN' 
        IF_EXGOAL (fn i => fn st => tac (nth (Thm.prems_of st) (i - 1)) i st)
      fun WITH_concl tac = 
        CONVERSION Thm.eta_conversion THEN' 
        IF_EXGOAL (fn i => fn st => 
          tac (Logic.concl_of_goal (Thm.prop_of st) i) i st

      fun TRADE tac ctxt i st = let
        val orig_ctxt = ctxt
        val (st,ctxt) = yield_singleton (apfst snd oo Variable.import true) st ctxt
        val seq = tac ctxt i st
          |> (singleton (Variable.export ctxt orig_ctxt))

      (* Try argument, then function *)
      fun fcomb_conv conv = let open Conv in
        arg_conv conv else_conv fun_conv conv
      (* Descend over function or abstraction *)
      fun fsub_conv conv ctxt = let 
        open Conv 
        fcomb_conv (conv ctxt) else_conv
        abs_conv (conv o snd) ctxt else_conv
      (* Apply to topmost matching position *)
      fun ftop_conv conv ctxt ct = 
        (conv ctxt else_conv fsub_conv (ftop_conv conv) ctxt) ct
      (* Iterate rule on theorem until it fails *)  
      fun repeat_rule n thm = case try n thm of
        SOME thm => repeat_rule n thm
      | NONE => thm
      (* Apply rule on theorem and assert that theorem was changed *)
      fun changed_rule n thm = let
        val thm' = n thm
        if Thm.eq_thm_prop (thm, thm') then raise THM ("Same",~1,[thm,thm'])
        else thm'

      (* Try rule on theorem *)
      fun try_rule n thm = case try n thm of
        SOME thm => thm | NONE => thm

      fun trade_rule f ctxt thm = 
        singleton ( (map o f) ctxt) thm

      fun RS_fst thm thms = let
        fun r [] = raise THM ("RS_fst, no matches",~1,thm::thms)
          | r (thm'::thms) = case try (op RS) (thm,thm') of
              NONE => r thms | SOME thm => thm
        r thms

      fun OF_fst thms insts = let
        fun r [] = raise THM ("OF_fst, no matches",length thms,thms@insts)
          | r (thm::thms) = case try (op OF) (thm,insts) of
              NONE => r thms | SOME thm => thm
        r thms

      (* Map [] to z, and [x1,...,xN] to f(...f(f(x1,x2),x3)...) *)
      fun list_binop_left z f = let
        fun r [] = z
          | r [T] = T
          | r (T::Ts) = f (r Ts,T)
        fn l => r (rev l)

      (* Map [] to z, [x] to i x, [x1,...,xN] to f(...f(f(x1,x2),x3)...), thread state *)
      fun fold_binop_left z i f = let
        fun r [] ctxt = z ctxt
          | r [T] ctxt = i T ctxt
          | r (T::Ts) ctxt = let 
              val (Ti,ctxt) = i T ctxt
              val (Tsi,ctxt) = r Ts ctxt
              (f (Tsi,Ti),ctxt)
        fn l => fn ctxt => r (rev l) ctxt

      fun strip_prodT_left (Type (@{type_name},[A,B])) = strip_prodT_left A @ [B]
        | strip_prodT_left (Type (@{type_name Product_Type.unit},[])) = []
        | strip_prodT_left T = [T]
      val list_prodT_left = list_binop_left HOLogic.unitT HOLogic.mk_prodT

      (* Make tuple with left-bracket structure *)
      val mk_ltuple = list_binop_left HOLogic.unit HOLogic.mk_prod

      (* Fix a tuple of new frees *)
      fun fix_left_tuple_from_Ts name = fold_binop_left
        (fn ctxt => (@{term "()"},ctxt))
        (fn T => fn ctxt => let 
            val (x,ctxt) = yield_singleton Variable.variant_fixes name ctxt
            val x = Free (x,T)

      (* Replace all type-vars by dummyT *)
      val dummify_tvars = map_types (map_type_tvar (K dummyT))

      fun dest_itselfT (Type (@{type_name itself},[A])) = A
        | dest_itselfT T = raise TYPE("dest_itselfT",[T],[])

      fun shift_lambda_left thm = thm RS @{thm shift_lambda_left}
      fun shift_lambda_leftN i = funpow i shift_lambda_left

      (* TODO: Naming should be without ' for basic parse, and with ' for context_parser! *)
      fun parse_bool_config' name cfg =
           (Args.$$$ name #>> K (cfg,true))
        || (Args.$$$ ("no_"^name) #>> K (cfg,false))  
      fun parse_paren_list' p = Scan.optional (Args.parens (Parse.enum1 "," p)) []
      fun apply_configs l ctxt = fold (fn (cfg,v) => fn ctxt => Config.put cfg v ctxt) l ctxt
      fun lambda_tuple [] t = t
        | lambda_tuple (@{mpat "(?a,?b)"}::l) t = let
            val body = lambda_tuple (a::b::l) t
            @{mk_term "case_prod ?body"}
        | lambda_tuple (x::l) t = lambda x (lambda_tuple l t)
      fun get_tuple_inst ctxt (iname,T) = let
        val (argTs,T) = strip_type T
        fun cr (Type (@{type_name prod},[T1,T2])) ctxt = let
              val (x1,ctxt) = cr T1 ctxt
              val (x2,ctxt) = cr T2 ctxt
              (HOLogic.mk_prod (x1,x2), ctxt)
          | cr T ctxt = let
              val (name, ctxt) = yield_singleton Variable.variant_fixes "x" ctxt
              (Free (name,T),ctxt)
        val ctxt = Variable.set_body false ctxt (* Prevent generation of skolem-names *)

        val (args,ctxt) = fold_map cr argTs ctxt
        fun fl (@{mpat "(?x,?y)"}) = fl x @ fl y
          | fl t = [t]
        val fargs = flat (map fl args)
        val fTs = map fastype_of fargs
        val v = Var (iname,fTs ---> T)
        val v = list_comb (v,fargs)
        val v = lambda_tuple args v
        Thm.cterm_of ctxt v
      fun instantiate_tuples ctxt inTs = let
        val inst = inTs ~~ map (get_tuple_inst ctxt) inTs
        Thm.instantiate ([],inst)
      val _ = COND'
      fun instantiate_tuples_from_term_tac ctxt t st = let
        val vars = Term.add_vars t []
        PRIMITIVE (instantiate_tuples ctxt vars) st
      fun instantiate_tuples_subgoal_tac ctxt = WITH_subgoal (fn t => K (instantiate_tuples_from_term_tac ctxt t))


  structure Basic_Refine_Util: BASIC_REFINE_UTIL = Refine_Util
  open Basic_Refine_Util

attribute_setup zero_var_indexes = ‹
  Scan.succeed (Thm.rule_attribute [] (K Drule.zero_var_indexes)) "Set variable indexes to zero, renaming to avoid clashes"


Theory Attr_Comb

section ‹Attribute Combinators›
theory Attr_Comb
imports Refine_Util

ML infixr 5 THEN_ATTR
  infixr 4 ELSE_ATTR

  signature ATTR_COMB = sig
    exception ATTR_EXC of string

    val NO_ATTR: attribute
    val ID_ATTR: attribute

    val ITE_ATTR': attribute -> attribute -> (exn -> attribute) -> attribute  
    val ITE_ATTR: attribute -> attribute -> attribute -> attribute  

    val THEN_ATTR: attribute * attribute -> attribute
    val ELSE_ATTR: attribute * attribute -> attribute

    val TRY_ATTR: attribute -> attribute
    val RPT_ATTR: attribute -> attribute
    val RPT1_ATTR: attribute -> attribute

    val EFF_ATTR: (Context.generic * thm -> 'a) -> attribute
    val WARN_ATTR: Context.generic -> string -> attribute
    val TRACE_ATTR: string -> attribute -> attribute

    val IGNORE_THM: attribute -> attribute

    val CHECK_PREPARE: (Context.generic * thm -> bool) -> attribute -> attribute

    val COND_attr: (Context.generic * thm -> bool) -> attribute
    val RS_attr: thm -> attribute
    val RSm_attr: thm -> attribute

  structure Attr_Comb :ATTR_COMB = struct

    exception ATTR_EXC of string
    fun NO_ATTR _ = raise ATTR_EXC "NO_ATTR"
    fun ID_ATTR _ = (NONE,NONE)

    fun ITE_ATTR' a b c (context,thm) = let
      fun dflt v NONE = SOME v | dflt _ (SOME v) = SOME v
      val ccxt' = (true,a (context,thm)) 
        handle (e as ATTR_EXC _) => (false,c e (context,thm))
      case ccxt' of
        (false,cxt')       => cxt'
      | (_,(NONE        , NONE    )) => b (context, thm)
      | (_,(SOME context, NONE    )) => b (context, thm) |>> dflt context
      | (_,(NONE        , SOME thm)) => b (context, thm) ||> dflt thm
      | (_,(SOME context, SOME thm)) => 
          b (context, thm) |>> dflt context ||> dflt thm

    fun ITE_ATTR a b c = ITE_ATTR' a b (K c)

    fun (a THEN_ATTR b) = ITE_ATTR' a b Exn.reraise
    fun (a ELSE_ATTR b) = ITE_ATTR a ID_ATTR b

    fun RPT_ATTR a cxt = (ITE_ATTR a (RPT_ATTR a) ID_ATTR) cxt
    fun RPT1_ATTR a = a THEN_ATTR RPT_ATTR a

    fun EFF_ATTR f cxt = (f cxt; (NONE,NONE))

    fun WARN_ATTR context msg = EFF_ATTR (fn (_,thm) => warning (msg ^ ": " 
      ^ Thm.string_of_thm (Context.proof_of context) thm))

    fun TRACE_ATTR msg a cxt = let
      val _ = tracing (msg ^ "\n" ^ @{make_string} cxt)
      val r = a cxt handle ATTR_EXC m => (
        tracing ("EXC "^m^"("^msg^")"); 
        raise ATTR_EXC m)
      val _ = tracing ("YIELDS (" ^ msg ^ ") " ^ @{make_string} r)
    in r end
    fun IGNORE_THM a = a #> apsnd (K NONE)

    fun COND_attr cond cxt = if cond cxt then (NONE,NONE) else 
      raise ATTR_EXC "COND_attr"

    fun CHECK_PREPARE check prep = 
      ITE_ATTR (COND_attr check) 
        (prep THEN_ATTR COND_attr check)

    fun RS_attr thm = 
      Thm.rule_attribute [thm] (fn _ => fn thm' => (
        thm' RS thm handle (exc as THM _) => 
          raise ATTR_EXC ("RS_attr: " ^ @{make_string} exc)))

    fun RSm_attr thm = 
      Thm.rule_attribute [thm] (fn context => fn thm' => (
        RSm (Context.proof_of context) thm' thm handle (exc as THM _) => 
          raise ATTR_EXC ("RSm_attr: " ^ @{make_string} exc)))



Theory Named_Sorted_Thms

section ‹Named Theorems with Explicit Filtering and Sorting›
theory Named_Sorted_Thms
imports Attr_Comb
    val member: Proof.context -> thm -> bool
    val get: Proof.context -> thm list
    val add_thm: thm -> Context.generic -> Context.generic
    val del_thm: thm -> Context.generic -> Context.generic
    val add: attribute
    val del: attribute
    val setup: theory -> theory

  functor Named_Sorted_Thms(
    val name: binding 
    val description: string
    val sort: Context.generic -> thm list -> thm list
    val transform: Context.generic -> thm -> thm list 
      (* Raise THM on invalid thm *)

    structure Data = Generic_Data
      type T = thm Item_Net.T;
      val empty = Thm.full_rules;
      val extend = I;
      val merge = Item_Net.merge;

    val member = Item_Net.member o Data.get o Context.Proof;

    fun content context = sort context (Item_Net.content (Data.get context));
    val get = content o Context.Proof;

    fun wrap upd = Thm.declaration_attribute (fn thm => fn context => let
      fun warn (msg,i,thms) = let
        val ctxt = Context.proof_of context
        val pt = Pretty.block [
          Pretty.str msg, 
          Pretty.brk 1,
            [Pretty.str "(",Pretty.str (string_of_int i),Pretty.str ")"],
          Pretty.brk 1,
          Pretty.block (Pretty.fbreaks (map (Thm.pretty_thm ctxt) thms))
        warning (Pretty.string_of pt)

      val thms = (transform context thm) handle THM e => (warn e; [])
      fold upd thms context

    val add = wrap ( o Item_Net.update)
    val del = wrap ( o Item_Net.remove)

    fun add_thm thm = Thm.apply_attribute (add) thm #> snd
    fun del_thm thm = Thm.apply_attribute (del) thm #> snd

    val setup =
      Attrib.setup name (Attrib.add_del add del) 
        ("declaration of " ^ description) 
      #> Global_Theory.add_thms_dynamic (name, content);



Theory Prio_List

section ‹Priority Lists›
theory Prio_List
imports Main
ML (*
    We provide a list of items with insertion operation relative to other
    items (after, before) and relative to absolute positions (first, last).
  signature PRIO_LIST = sig
    type T
    type item

    val empty: T
    val add_first: T -> item -> T
    val add_last: T -> item -> T
    val add_before: T -> item -> item -> T
    val add_after: T -> item -> item -> T

    val delete: item -> T -> T

    val prio_of: (item -> bool) -> (item * item -> bool) -> T -> int
    val contains: T -> item -> bool

    val dest: T -> item list

    val merge: T * T -> T  (* Ignore conflicts *)
    val merge': T * T -> item list * T (* Return conflicting items *)


  functor Prio_List (
    type item; 
    val eq: item * item -> bool
  ): PRIO_LIST = struct
    type item = item
    type T = item list

    val empty = []
    fun add_first l e = remove eq e l@[e]
    fun add_last l e = e::remove eq e l

    fun add_before l e a = let 
      val l = remove eq e l
      val (l1,(l2,l3)) = chop_prefix (fn x => not (eq (x,a))) l ||> chop 1
    in l1@l2@e::l3 end;

    fun add_after l e b = let 
      val l = remove eq e l
      val (l1,l2) = chop_prefix (fn x => not (eq (x,b))) l
    in l1@e::l2 end

    val delete = remove eq

    fun prio_of P prefer l = let
      fun do_prefer _ NONE = true
        | do_prefer x (SOME (_,y)) = prefer (x,y)

      fun pr [] _ st = (case st of NONE => ~1 | SOME (i,_) => i)
        | pr (x::l) i st = if P x andalso do_prefer x st then
            pr l (i+1) (SOME (i,x))
            pr l (i+1) st

      pr l 0 NONE

    val contains = member eq

    fun dest l = l

    fun merge' (l1,l2) = let
      val l1' = map (fn ty => (Library.member eq l2 ty,ty)) l1;
      val l2' = map (fn ty => (Library.member eq l1 ty,ty)) l2;

      fun m []                []               = []
        | m []                l                = map (apfst (K false)) l
        | m l                 []               = map (apfst (K false)) l
        | m ((true,t1)::l1)   ((true,t2)::l2)  = 
          (not (eq (t1,t2)),t2) :: m l1 l2 
        | m ((false,t1)::l1)  ((true,t2)::l2)  = 
          (false,t1) :: m l1 ((true,t2)::l2)
        | m ((true,t1)::l1)   ((false,t2)::l2) = 
          (false,t2) :: m ((true,t1)::l1) l2
        | m ((false,t1)::l1)  ((false,t2)::l2) = 
          (false,t2)::(false,t1)::m l1 l2
      val r = m l1' l2'
      (map #2 (filter #1 r), map #2 r)

    fun merge (l1,l2) = #2 (merge' (l1,l2))



Theory Tagged_Solver

theory Tagged_Solver
imports Refine_Util

  A solver is some named entity, and it should be possible to
  reference it by its short/long name like a constant or a theorem!

ML signature TAGGED_SOLVER = sig
    type solver = thm list * string * string * (Proof.context -> tactic')
    val get_solvers: Proof.context -> solver list
    val declare_solver: thm list -> binding -> string 
      -> (Proof.context -> tactic') -> morphism 
      -> Context.generic -> Context.generic

    val lookup_solver: string -> Context.generic -> solver option
    val add_triggers: string -> thm list -> morphism ->
      Context.generic -> Context.generic

    val delete_solver: string -> morphism -> Context.generic -> Context.generic

    val tac_of_solver: Proof.context -> solver -> tactic'

    val get_potential_solvers: Proof.context -> int -> thm -> solver list
    val get_potential_tacs: Proof.context -> int -> thm -> tactic' list

    val solve_greedy_step_tac: Proof.context -> tactic'
    val solve_greedy_tac: Proof.context -> tactic'
    val solve_greedy_keep_tac: Proof.context -> tactic'

    val solve_full_step_tac: Proof.context -> tactic'
    val solve_full_tac: Proof.context -> tactic'
    val solve_full_keep_tac: Proof.context -> tactic'

    val cfg_keep: bool Config.T
    val cfg_trace: bool Config.T
    val cfg_full: bool Config.T
    val cfg_step: bool Config.T

    val solve_tac: Proof.context -> tactic'

    val pretty_solvers: Proof.context -> Pretty.T

  structure Tagged_Solver : TAGGED_SOLVER = struct
    type solver = thm list * string * string * (Proof.context -> tactic')

    structure solvers = Generic_Data (
      type T = solver Item_Net.T * solver Symtab.table
      val empty = (Item_Net.init 
        ((op =) o apply2 #2) 
        (fn p:solver => #1 p |> map Thm.concl_of)
      fun merge ((n1,t1),(n2,t2)) 
        = (Item_Net.merge (n1,n2), Symtab.merge ((op =) o apply2 #2) (t1,t2))
      val extend = I 

    fun get_solvers ctxt = solvers.get (Context.Proof ctxt) 
      |> #2 |> Symtab.dest |> map #2

    fun lookup_solver n context = let
      val tab = solvers.get context |> #2
      Symtab.lookup tab n

    fun add_triggers n thms phi context = 
      case lookup_solver n context of
        NONE => 
          error ("Undefined solver: " ^ n)
      | SOME (trigs,n,desc,tac) => let
          val thms = map (Morphism.thm phi) thms
          val trigs = thms @ trigs
          val solver = (trigs,n,desc,tac)
 (Item_Net.update solver ## Symtab.update (n, solver)) 

    fun declare_solver thms n desc tac phi context = let
      val thms = map (Morphism.thm phi) thms
      val n = Morphism.binding phi n
      val n = Context.cases Sign.full_name Proof_Context.full_name context n
      val _ = 
        if Symtab.defined (solvers.get context |> #2) n then
          error ("Duplicate solver " ^ n)
        else ()
      val solver = (thms,n,desc,tac)
    in (Item_Net.update solver ## Symtab.update (n,solver)) context

    fun delete_solver n _ context = 
      case lookup_solver n context of
        NONE => error ("Undefined solver: " ^ n)
      | SOME solver => 
 (Item_Net.remove solver ## Symtab.delete (#2 solver))

    val cfg_keep = 
      Attrib.setup_config_bool @{binding tagged_solver_keep} (K false)

    val cfg_trace = 
      Attrib.setup_config_bool @{binding tagged_solver_trace} (K false)

    val cfg_step = 
      Attrib.setup_config_bool @{binding tagged_solver_step} (K false)

    val cfg_full = 
      Attrib.setup_config_bool @{binding tagged_solver_full} (K false)

    (* Get potential solvers. Overapproximation caused by net *)
    fun get_potential_solvers ctxt i st = 
        val concl = Logic.concl_of_goal (Thm.prop_of st) i
        val net = solvers.get (Context.Proof ctxt) |> #1
        val solvers = Item_Net.retrieve net concl
      in solvers end

    fun notrace_tac_of_solver ctxt (thms,_,_,tac) = 
      match_tac ctxt thms THEN' tac ctxt

    fun trace_tac_of_solver ctxt (thms,name,_,tac) i st = 
        val _ = tracing ("Trying solver " ^ name)
        val r = match_tac ctxt thms i st
        case Seq.pull r of 
          NONE => (tracing "  No trigger"; Seq.empty)
        | SOME _ => let 
            val r = Seq.maps (tac ctxt i) r
            case Seq.pull r of 
              NONE => (tracing ("  No solution (" ^ name ^ ")"); Seq.empty)
            | SOME _ => (tracing ("  OK (" ^ name ^ ")"); r)

    fun tac_of_solver ctxt = 
      if Config.get ctxt cfg_trace then
        trace_tac_of_solver ctxt
        notrace_tac_of_solver ctxt
    fun get_potential_tacs ctxt i st = 
      if i <= Thm.nprems_of st then
        eq_assume_tac :: (
          get_potential_solvers ctxt i st
          |> map (tac_of_solver ctxt)
      else []

    fun solve_greedy_step_tac ctxt i st = 
      (FIRST' (get_potential_tacs ctxt i st)) i st

    fun solve_full_step_tac ctxt i st = 
      (APPEND_LIST' (get_potential_tacs ctxt i st) i st)

    (* Try to solve, take first matching tactic, but allow backtracking over
       its results *)
    fun solve_greedy_tac ctxt i st = let
      val tacs = get_potential_tacs ctxt i st
      (FIRST' tacs THEN_ALL_NEW_FWD solve_greedy_tac ctxt) i st

    (* Try to solve. Allow backtracking over matching tactics. *)
    fun solve_full_tac ctxt i st = let
      val tacs = get_potential_tacs ctxt i st
      (APPEND_LIST' tacs THEN_ALL_NEW_FWD solve_full_tac ctxt) i st

    fun solve_greedy_keep_tac ctxt i st = let
      val tacs = get_potential_tacs ctxt i st
      (FIRST' tacs THEN_ALL_NEW_FWD (TRY o solve_greedy_keep_tac ctxt)) i st

    fun solve_full_keep_tac ctxt i st = let
      val tacs = get_potential_tacs ctxt i st
      (APPEND_LIST' tacs THEN_ALL_NEW_FWD (TRY o solve_full_keep_tac ctxt)) i st

    fun solve_tac ctxt = 
      case (Config.get ctxt cfg_keep, Config.get ctxt cfg_step, 
            Config.get ctxt cfg_full) of
        (_,true,false) => solve_greedy_step_tac ctxt
      | (_,true,true) => solve_full_step_tac ctxt
      | (true,false,false) => solve_greedy_keep_tac ctxt
      | (false,false,false) => solve_greedy_tac ctxt
      | (true,false,true) => solve_full_keep_tac ctxt
      | (false,false,true) => solve_full_tac ctxt

    fun pretty_solvers ctxt = let
      fun pretty_solver (ts,name,desc,_) = Pretty.block (
        Pretty.str (name ^ ": " ^ desc) :: Pretty.fbrk 
        :: Pretty.str ("Triggers: ")
        :: Pretty.commas (map (Thm.pretty_thm ctxt) ts))

      val solvers = get_solvers ctxt
      Pretty.big_list "Solvers:" (map pretty_solver solvers)

method_setup tagged_solver = let
  open Refine_Util
  val flags = 
        parse_bool_config "keep" Tagged_Solver.cfg_keep
    ||  parse_bool_config "trace" Tagged_Solver.cfg_trace
    ||  parse_bool_config "full" Tagged_Solver.cfg_full
    ||  parse_bool_config "step" Tagged_Solver.cfg_step

  parse_paren_lists flags >> (fn _ => fn ctxt => 
    SIMPLE_METHOD' (Tagged_Solver.solve_tac ctxt)
end "Select tactic to solve goal by pattern"

term True
(* Localization Test *)
locale foo = 
  fixes A b
  assumes A: "A x = True"
  definition "B == A"

  lemma AI: "A x" unfolding A ..
  lemma A_trig: "A x ==> A x" .
  lemma BI: "A x ==> B x" unfolding B_def .
  lemma B_trig: "B x ==> B x" .
  declaration {* fn phi =>
    Tagged_Solver.declare_solver @{thms A_trig} 
      @{binding "A_solver"} "description" 
      (K (rtac (Morphism.thm phi @{thm AI}))) phi

  ML_val {*
    Tagged_Solver.pretty_solvers @{context} |> Pretty.writeln

  (* FIXME: Does not work because of improper naming!
  declaration {*
    Tagged_Solver.add_triggers "local.A_solver" @{thms A_trig}

  declaration {* fn phi =>
    Tagged_Solver.declare_solver @{thms B_trig} 
      @{binding "B_solver"} "description" 
      (K (rtac (Morphism.thm phi @{thm BI}))) phi

  ML_val {*
    Tagged_Solver.pretty_solvers @{context} |> Pretty.writeln


definition "TAG x == True"
interpretation tag: foo TAG 1
  apply unfold_locales
  unfolding TAG_def by simp

ML_val {*
  Tagged_Solver.pretty_solvers @{context} |> Pretty.writeln

definition "TAG' x == True"
interpretation tag': foo TAG' 2
  apply unfold_locales
  unfolding TAG'_def by simp

interpretation tag2: foo TAG 3
  by unfold_locales

ML_val {*
  Tagged_Solver.pretty_solvers @{context} |> Pretty.writeln

lemma "tag.B undefined"
  by (tagged_solver (keep))

declaration {* Tagged_Solver.delete_solver "Tagged_Solver.tag.B_solver" *}

ML_val {*
  Tagged_Solver.pretty_solvers @{context} |> Pretty.writeln


Theory Anti_Unification

section ‹Anti-Unification›
theory Anti_Unification
imports Refine_Util
  text ‹We implement a simple anti-unification.
    Currently, we do not handle lambdas, nor sorts, and avoid
    higher-order variables, i.e.,
    f x and g x will be unified to ?v, not ?v x, and existing higher-order
    patterns will be collapsed, e.g.: ?f x and ?f x will become ?v.

ML signature ANTI_UNIFICATION = sig
    type typ_env
    type term_env
    type env = typ_env * term_env

    val empty_typ: typ_env
    val empty_term: term_env
    val empty: env

    val anti_unifyT: typ * typ -> typ_env -> typ * typ_env
    val anti_unify_env: term * term -> env -> term * env
    val anti_unify: term * term -> term
    val anti_unify_list: term list -> term

    val specialize_tac: Proof.context -> thm list -> tactic'
    val specialize_net_tac: Proof.context -> (int * thm) -> tactic'

  structure Anti_Unification :ANTI_UNIFICATION = struct
    structure Term2tab = Table (
      type key = term * term
      val ord = prod_ord Term_Ord.fast_term_ord Term_Ord.fast_term_ord

    structure Typ2tab = Table (
      type key = typ * typ
      val ord = prod_ord Term_Ord.typ_ord Term_Ord.typ_ord

    type typ_env = (typ Typ2tab.table * int)
    type term_env = (term Term2tab.table * int)
    type env = typ_env * term_env

    val empty_typ = (Typ2tab.empty,0)
    val empty_term = (Term2tab.empty,0)
    val empty = (empty_typ,empty_term)

    fun tvar_pair p (vtab,idx) =
      case Typ2tab.lookup vtab p of
        NONE => let
          val tv = TVar (("T",idx),[])
          val vtab = Typ2tab.update (p,tv) vtab
      | SOME tv => (tv,(vtab,idx))

    fun anti_unifyT (TFree v1, TFree v2) dt =
        if v1 = v2 then (TFree v1,dt)
        else tvar_pair (TFree v1, TFree v2) dt
      | anti_unifyT (p as (Type (n1,l1), Type (n2,l2))) dt =
        if n1 = n2 andalso (length l1 = length l2) then
            val (l,dt) = fold_map anti_unifyT (l1~~l2) dt
          in (Type (n1,l),dt) end
          tvar_pair p dt
      | anti_unifyT p dt = tvar_pair p dt

    fun var_pair p (tdt,(vtab,idx)) =
      case Term2tab.lookup vtab p of
        NONE => let
          val (T,tdt) = anti_unifyT (apply2 fastype_of p) tdt
          val v = Var (("v",idx),T)
          val vtab = Term2tab.update (p,v) vtab
      | SOME v => (v,(tdt,(vtab,idx)))

    fun anti_unify_env (p as (Free (n1,T1), Free (n2,T2))) (tdt,dt) =
        if n1 = n2 then
            val (T,tdt) = anti_unifyT (T1,T2) tdt
          in (Free (n1,T),(tdt,dt)) end
          var_pair p (tdt,dt)
      | anti_unify_env (p as (Const (n1,T1), Const (n2,T2))) (tdt,dt) =
        if n1 = n2 then
            val (T,tdt) = anti_unifyT (T1,T2) tdt
          in (Const (n1,T),(tdt,dt)) end
          var_pair p (tdt,dt)
      | anti_unify_env (p as (f1$x1,f2$x2)) dtp =
          val (f,dtp) = anti_unify_env (f1,f2) dtp
          if is_Var f then
            var_pair p dtp
          else let
            val (x,dtp) = anti_unify_env (x1,x2) dtp
          in (f$x,dtp) end
      | anti_unify_env p dtp = var_pair p dtp

    fun anti_unify p = anti_unify_env p empty |> #1

    fun anti_unify_list l = let
      fun f [] _ = raise TERM ("anti_unify_list: Empty",[])
        | f [t] dt = (t,dt)
        | f (t1::t2::ts) dt = let
            val (t,dt) = anti_unify_env (t1,t2) dt
          in f (t::ts) dt end
      f l empty |> #1

      fun specialize_aux_tac ctxt get_candidates i st = let
        val maxidx = Thm.maxidx_of st
        val concl = Logic.concl_of_goal (Thm.prop_of st) i
        val pre_candidates = get_candidates concl
          |> map (fn thm =>
                 val tidx = Thm.maxidx_of thm
                 val t = Thm.incr_indexes (maxidx + 1) thm |> Thm.concl_of
               in (maxidx + tidx + 1,t) end)

        fun unifies (idx,t)
          = can (Pattern.unify (Context.Proof ctxt) (t, concl)) (Envir.empty idx)

        val candidates = filter unifies pre_candidates |> map #2
        case candidates of
          [] => Seq.single st
        | _ => let
            val pattern = anti_unify_list candidates
              |> Thm.cterm_of ctxt |> Thm.trivial
            resolve_tac ctxt [pattern] i st
      fun specialize_tac ctxt thms = let
        fun get_candidates concl =
          filter (fn thm => Term.could_unify (Thm.concl_of thm, concl)) thms
        specialize_aux_tac ctxt get_candidates

      fun specialize_net_tac ctxt net = let
        fun get_candidates concl = Net.unify_term net concl |> map #2
        specialize_aux_tac ctxt get_candidates



Theory Misc

(*  Title:       Miscellaneous Definitions and Lemmas
    Author:      Peter Lammich <>
    Maintainer:  Peter Lammich <>
                 Thomas Tuerk <>

    2010-05-09: Removed AC, AI locales, they are superseeded by concepts
                  from OrderedGroups
    2010-09-22: Merges with ext/Aux
    2017-06-12: Added a bunch of lemmas from various other projects


section ‹Miscellaneous Definitions and Lemmas›

theory Misc
imports Main
text_raw ‹\label{thy:Misc}›

subsection ‹Isabelle Distribution Move Proposals›  

subsubsection ‹Pure›  
lemma TERMI: "TERM x" unfolding Pure.term_def .
subsubsection ‹HOL›  
(* Stronger disjunction elimination rules. *)
lemma disjE1: " P  Q; P  R; ¬P;Q  R   R"
  by metis
lemma disjE2: " P  Q; P; ¬Q  R; Q  R   R"
  by blast

lemma imp_mp_iff[simp]: 
  "a  (a  b)  a  b" 
  "(a  b)  a  a  b" (* is Inductive.imp_conj_iff, but not in simpset by default *)
  by blast+
lemma atomize_Trueprop_eq[atomize]: "(Trueprop x  Trueprop y)  Trueprop (x=y)"
  apply rule
  apply (rule)
  apply (erule equal_elim_rule1)
  apply assumption
  apply (erule equal_elim_rule2)
  apply assumption
  apply simp
subsubsection ‹Set›    
lemma inter_compl_diff_conv[simp]: "A  -B = A - B" by auto
lemma pair_set_inverse[simp]: "{(a,b). P a b}¯ = {(b,a). P a b}"
  by auto

lemma card_doubleton_eq_2_iff[simp]: "card {a,b} = 2  ab" by auto

subsubsection ‹List›  
(* TODO: Move, analogous to List.length_greater_0_conv *) 
thm List.length_greater_0_conv  
lemma length_ge_1_conv[iff]: "Suc 0  length l  l[]"
  by (cases l) auto
― ‹Obtains a list from the pointwise characterization of its elements›
lemma obtain_list_from_elements:
  assumes A: "i<n. (li. P li i)"
  obtains l where
    "length l = n"
    "i<n. P (l!i) i"
proof -
  from A have "l. length l=n  (i<n. P (l!i) i)"
  proof (induct n)
    case 0 thus ?case by simp
    case (Suc n)
    then obtain l where IH: "length l = n" "(i<n. P(l!i) i)" by auto
    moreover from Suc.prems obtain ln where "P ln n" by auto
    ultimately have "length (l@[ln]) = Suc n" "(i<Suc n. P((l@[ln])!i) i)"
      by (auto simp add: nth_append dest: less_antisym)
    thus ?case by blast
  thus ?thesis using that by (blast)
lemma distinct_sorted_mono:
  assumes S: "sorted l"
  assumes D: "distinct l"
  assumes B: "i<j" "j<length l"
  shows "l!i < l!j"
proof -
  from S B have "l!i  l!j"
    by (simp add: sorted_iff_nth_mono)
  also from nth_eq_iff_index_eq[OF D] B have "l!i  l!j"
    by auto
  finally show ?thesis .

lemma distinct_sorted_strict_mono_iff:
  assumes "distinct l" "sorted l"
  assumes "i<length l" "j<length l"
  shows "l!i<l!j  i<j"
  using assms
  by (metis distinct_sorted_mono leI less_le_not_le

lemma distinct_sorted_mono_iff:
  assumes "distinct l" "sorted l"
  assumes "i<length l" "j<length l"
  shows "l!il!j  ij"
  by (metis assms distinct_sorted_strict_mono_iff leD le_less_linear)
(* List.thy has:
 declare map_eq_Cons_D [dest!]  Cons_eq_map_D [dest!] 

  We could, analogously, declare rules for "map _ _ = _@_" as dest!,
  or use elim!, or declare the _conv-rule as simp
lemma map_eq_appendE: 
  assumes "map f ls = fl@fl'"
  obtains l l' where "ls=l@l'" and "map f l=fl" and  "map f l' = fl'"
  using assms  
proof (induction fl arbitrary: ls thesis)
  case (Cons x xs)
    then obtain l ls' where [simp]: "ls = l#ls'" "f l = x" by force
    with Cons.prems(2) have "map f ls' = xs @ fl'" by simp
    from Cons.IH[OF _ this] guess ll ll' .
    with Cons.prems(1)[of "l#ll" ll'] show thesis by simp
qed simp

lemma map_eq_append_conv: "map f ls = fl@fl'  (l l'. ls = l@l'  map f l = fl  map f l' = fl')"
  by (auto elim!: map_eq_appendE)
lemmas append_eq_mapE = map_eq_appendE[OF sym]
lemma append_eq_map_conv: "fl@fl' = map f ls  (l l'. ls = l@l'  map f l = fl  map f l' = fl')"
  by (auto elim!: append_eq_mapE)
lemma distinct_mapI: "distinct (map f l)  distinct l"
  by (induct l) auto
lemma map_distinct_upd_conv: 
  "i<length l; distinct l  (map f l)[i := x] = map (f(l!i := x)) l"
  ― ‹Updating a mapped distinct list is equal to updating the 
    mapping function›
  by (auto simp: nth_eq_iff_index_eq intro: nth_equalityI)

lemma zip_inj: "length a = length b; length a' = length b'; zip a b = zip a' b'  a=a'  b=b'"
proof (induct a b arbitrary: a' b' rule: list_induct2)
  case Nil
  then show ?case by (cases a'; cases b'; auto)
  case (Cons x xs y ys)
  then show ?case by (cases a'; cases b'; auto)

lemma zip_eq_zip_same_len[simp]:
  " length a = length b; length a' = length b'  
  zip a b = zip a' b'  a=a'  b=b'"
  by (auto dest: zip_inj)
lemma upt_merge[simp]: "ij  jk  [i..<j]@[j..<k] = [i..<k]"
  by (metis le_Suc_ex upt_add_eq_append)
(* Maybe this should go into List.thy, next to snoc_eq_iff_butlast *)
lemma snoc_eq_iff_butlast':
  "(ys = xs @ [x])  (ys  []  butlast ys = xs  last ys = x)"
  by auto

(* Case distinction how two elements of a list can be related to each other *)
lemma list_match_lel_lel:
  assumes "c1 @ qs # c2 = c1' @ qs' # c2'"
    (left) c21' where "c1 = c1' @ qs' # c21'" "c2' = c21' @ qs # c2"
  | (center) "c1' = c1" "qs' = qs" "c2' = c2"
  | (right) c21 where "c1' = c1 @ qs # c21" "c2 = c21 @ qs' # c2'"
  using assms  
  apply (clarsimp simp: append_eq_append_conv2)  
  subgoal for us by (cases us) auto
lemma xy_in_set_cases[consumes 2, case_names EQ XY YX]:
  assumes A: "xset l" "yset l"
  and C:
  "!!l1 l2.  x=y; l=l1@y#l2   P"
  "!!l1 l2 l3.  xy; l=l1@x#l2@y#l3   P"
  "!!l1 l2 l3.  xy; l=l1@y#l2@x#l3   P"
  shows P
proof (cases "x=y")
  case True with A(1) obtain l1 l2 where "l=l1@y#l2" by (blast dest: split_list)
  with C(1) True show ?thesis by blast
  case False
  from A(1) obtain l1 l2 where S1: "l=l1@x#l2" by (blast dest: split_list)
  from A(2) obtain l1' l2' where S2: "l=l1'@y#l2'" by (blast dest: split_list)
  from S1 S2 have M: "l1@x#l2 = l1'@y#l2'" by simp
  thus P proof (cases rule: list_match_lel_lel[consumes 1, case_names 1 2 3])
    case (1 c) with S1 have "l=l1'@y#c@x#l2" by simp
    with C(3) False show ?thesis by blast
    case 2 with False have False by blast
    thus ?thesis ..
    case (3 c) with S1 have "l=l1@x#c@y#l2'" by simp
    with C(2) False show ?thesis by blast
lemma list_e_eq_lel[simp]:
  "[e] = l1@e'#l2  l1=[]  e'=e  l2=[]"
  "l1@e'#l2 = [e]  l1=[]  e'=e  l2=[]"
  apply (cases l1, auto) []
  apply (cases l1, auto) []

lemma list_ee_eq_leel[simp]:
  "([e1,e2] = l1@e1'#e2'#l2)  (l1=[]  e1=e1'  e2=e2'  l2=[])"
  "(l1@e1'#e2'#l2 = [e1,e2])  (l1=[]  e1=e1'  e2=e2'  l2=[])"
  apply (cases l1, auto) []
  apply (cases l1, auto) []

subsubsection ‹Transitive Closure›

text ‹A point-free induction rule for elements reachable from an initial set›
lemma rtrancl_reachable_induct[consumes 0, case_names base step]:
  assumes I0: "I  INV"
  assumes IS: "E``INV  INV"
  shows "E*``I  INV"
  by (metis I0 IS Image_closed_trancl Image_mono subset_refl)

lemma acyclic_empty[simp, intro!]: "acyclic {}" by (unfold acyclic_def) auto

lemma acyclic_union:
  "acyclic (AB)  acyclic A"
  "acyclic (AB)  acyclic B"
  by (metis Un_upper1 Un_upper2 acyclic_subset)+
subsubsection ‹Lattice Syntax›    
(* Providing the syntax in a locale makes it more usable, without polluting the global namespace*)    
locale Lattice_Syntax begin
    bot ("") and
    top ("") and
    inf  (infixl "" 70) and
    sup  (infixl "" 65) and
    Inf  ("_" [900] 900) and
    Sup  ("_" [900] 900)

text ‹Here we provide a collection of miscellaneous definitions and helper lemmas›

subsection "Miscellaneous (1)"

text ‹This stuff is used in this theory itself, and thus occurs in first place or is simply not sorted into any other section of this theory.›

lemma IdD: "(a,b)Id  a=b" by simp

text ‹Conversion Tag›    
definition [simp]: "CNV x y  x=y"
lemma CNV_I: "CNV x x" by simp
lemma CNV_eqD: "CNV x y  x=y" by simp
lemma CNV_meqD: "CNV x y  xy" by simp
(* TODO: Move. Shouldn't this be detected by simproc? *)
lemma ex_b_b_and_simp[simp]: "(b. b  Q b)  Q True" by auto
lemma ex_b_not_b_and_simp[simp]: "(b. ¬b  Q b)  Q False" by auto
method repeat_all_new methods m = m;(repeat_all_new m)?
subsubsection "AC-operators"

text ‹Locale to declare AC-laws as simplification rules›
locale Assoc =
  fixes f
  assumes assoc[simp]: "f (f x y) z = f x (f y z)"

locale AC = Assoc +
  assumes commute[simp]: "f x y = f y x"

lemma (in AC) left_commute[simp]: "f x (f y z) = f y (f x z)"
  by (simp only: assoc[symmetric]) simp

lemmas (in AC) AC_simps = commute assoc left_commute

text ‹Locale to define functions from surjective, unique relations›
locale su_rel_fun =
  fixes F and f
  assumes unique: "(A,B)F; (A,B')F  B=B'"
  assumes surjective: "!!B. (A,B)F  P  P"
  assumes f_def: "f A == THE B. (A,B)F"

lemma (in su_rel_fun) repr1: "(A,f A)F" proof (unfold f_def)
  obtain B where "(A,B)F" by (rule surjective)
  with theI[where P="λB. (A,B)F", OF this] show "(A, THE x. (A, x)  F)  F" by (blast intro: unique)

lemma (in su_rel_fun) repr2: "(A,B)F  B=f A" using repr1
  by (blast intro: unique)

lemma (in su_rel_fun) repr: "(f A = B) = ((A,B)F)" using repr1 repr2
  by (blast)

    ― ‹Contract quantification over two variables to pair›
lemma Ex_prod_contract: "(a b. P a b)  (z. P (fst z) (snd z))"
  by auto

lemma All_prod_contract: "(a b. P a b)  (z. P (fst z) (snd z))"
  by auto

lemma nat_geq_1_eq_neqz: "x1  x(0::nat)"
  by auto

lemma nat_in_between_eq:
  "(a<b  bSuc a)  b = Suc a"
  "(ab  b<Suc a)  b = a"
  by auto

lemma Suc_n_minus_m_eq: " nm; m>1   Suc (n - m) = n - (m - 1)"
  by simp

lemma Suc_to_right: "Suc n = m  n = m - Suc 0" by simp
lemma Suc_diff[simp]: "n m. nm  m1  Suc (n - m) = n - (m - 1)"
  by simp

lemma if_not_swap[simp]: "(if ¬c then a else b) = (if c then b else a)" by auto
lemma all_to_meta: "Trueprop (a. P a)  (a. P a)"
  apply rule
  by auto

lemma imp_to_meta: "Trueprop (PQ)  (PQ)"
  apply rule
  by auto

(* for some reason, there is no such rule in HOL *)
lemma iffI2: "P  Q; ¬ P  ¬ Q  P  Q"
by metis

lemma iffExI:
  " x. P x  Q x; x. Q x  P x   (x. P x)  (x. Q x)"
by metis

lemma bex2I[intro?]: " (a,b)S; (a,b)S  P a b   a b. (a,b)S  P a b"
  by blast

(* TODO: Move lemma to HOL! *)  
lemma cnv_conj_to_meta: "(P  Q  PROP X)  (P;Q  PROP X)"
  by (rule BNF_Fixpoint_Base.conj_imp_eq_imp_imp)

subsection ‹Sets›
  lemma remove_subset: "xS  S-{x}  S" by auto

  lemma subset_minus_empty: "AB  A-B = {}" by auto

  lemma insert_minus_eq: "xy  insert x A - {y} = insert x (A - {y})" by auto
  lemma set_notEmptyE: "S{}; !!x. xS  P  P"
    by (metis equals0I)

  lemma subset_Collect_conv: "S  Collect P  (xS. P x)"
    by auto

  lemma memb_imp_not_empty: "xS  S{}"
    by auto

  lemma disjoint_mono: " aa'; bb'; a'b'={}   ab={}" by auto

  lemma disjoint_alt_simp1: "A-B = A  AB = {}" by auto
  lemma disjoint_alt_simp2: "A-B  A  AB  {}" by auto
  lemma disjoint_alt_simp3: "A-B  A  AB  {}" by auto

  lemma disjointI[intro?]: " x. xa; xb  False   ab={}"
    by auto

  lemmas set_simps = subset_minus_empty disjoint_alt_simp1 disjoint_alt_simp2 disjoint_alt_simp3 Un_absorb1 Un_absorb2

  lemma set_minus_singleton_eq: "xX  X-{x} = X"
    by auto

  lemma set_diff_diff_left: "A-B-C = A-(BC)"
    by auto

  lemma image_update[simp]: "xA  f(x:=n)`A = f`A"
    by auto

  lemma eq_or_mem_image_simp[simp]: "{f l |l. l = a  lB} = insert (f a) {f l|l. lB}" by blast
  lemma set_union_code [code_unfold]:
    "set xs  set ys = set (xs @ ys)"
    by auto

  lemma in_fst_imageE:
    assumes "x  fst`S"
    obtains y where "(x,y)S"
    using assms by auto

  lemma in_snd_imageE:
    assumes "y  snd`S"
    obtains x where "(x,y)S"
    using assms by auto

  lemma fst_image_mp: "fst`A  B; (x,y)A   xB"
    by (metis Domain.DomainI fst_eq_Domain in_mono)

  lemma snd_image_mp: "snd`A  B; (x,y)A   yB"
    by (metis Range.intros rev_subsetD snd_eq_Range)

  lemma inter_eq_subsetI: " SS'; AS' = BS'   AS = BS"
    by auto

text ‹
  Decompose general union over sum types.
lemma Union_plus:
  "( x  A <+> B. f x) = ( a  A. f (Inl a))  (b  B. f (Inr b))"
by auto

lemma Union_sum:
  "(x. f (x::'a+'b)) = (l. f (Inl l))  (r. f (Inr r))"
  (is "?lhs = ?rhs")
proof -
  have "?lhs = (x  UNIV <+> UNIV. f x)"
    by simp
  thus ?thesis
    by (simp only: Union_plus)


  subsubsection ‹Finite Sets›

  lemma card_1_singletonI: "finite S; card S = 1; xS  S={x}"
  proof (safe, rule ccontr, goal_cases)
    case prems: (1 x')
    hence "finite (S-{x})" "S-{x}  {}" by auto
    hence "card (S-{x})  0" by auto
    moreover from prems(1-3) have "card (S-{x}) = 0" by auto
    ultimately have False by simp
    thus ?case ..

  lemma card_insert_disjoint': "finite A; x  A  card (insert x A) - Suc 0 = card A"
    by (drule (1) card_insert_disjoint) auto

  lemma card_eq_UNIV[simp]: "card (S::'a::finite set) = card (UNIV::'a set)  S=UNIV"
  proof (auto)
    fix x
    assume A: "card S = card (UNIV::'a set)"
    show "xS" proof (rule ccontr)
      assume "xS" hence "SUNIV" by auto
      with psubset_card_mono[of UNIV S] have "card S < card (UNIV::'a set)" by auto
      with A show False by simp

  lemma card_eq_UNIV2[simp]: "card (UNIV::'a set) = card (S::'a::finite set)  S=UNIV"
    using card_eq_UNIV[of S] by metis

  lemma card_ge_UNIV[simp]: "card (UNIV::'a::finite set)  card (S::'a set)  S=UNIV"
    using card_mono[of "UNIV::'a::finite set" S, simplified]
    by auto

  lemmas length_remdups_card = length_remdups_concat[of "[l]", simplified] for l

  lemma fs_contract: "fst ` { p | p. f (fst p) (snd p)  S } = { a . b. f a b  S }"
    by (simp add: image_Collect)

lemma finite_Collect: "finite S  inj f  finite {a. f a : S}"
by(simp add: finite_vimageI vimage_def[symmetric])

  ― ‹Finite sets have an injective mapping to an initial segments of the
      natural numbers›
  (* This lemma is also in the standard library (from Isabelle2009-1 on)
      as @{thm [source] Finite_Set.finite_imp_inj_to_nat_seg}. However, it is formulated with HOL's
      ∃ there rather then with the meta-logic obtain *)
  lemma finite_imp_inj_to_nat_seg':
    fixes A :: "'a set"
    assumes A: "finite A"
    obtains f::"'a  nat" and n::"nat" where
      "f`A = {i. i<n}"
      "inj_on f A"
    by (metis A finite_imp_inj_to_nat_seg)

  lemma lists_of_len_fin1: "finite P  finite (lists P  { l. length l = n })"
  proof (induct n)
    case 0 thus ?case by auto
    case (Suc n)
    have "lists P  { l. length l = Suc n }
          = (λ(a,l). a#l) ` (P × (lists P  {l. length l = n}))"
      apply auto
      apply (case_tac x)
      apply auto
    moreover from Suc have "finite " by auto
    ultimately show ?case by simp

  lemma lists_of_len_fin2: "finite P  finite (lists P  { l. n = length l })"
  proof -
    assume A: "finite P"
    have S: "{ l. n = length l } = { l. length l = n }" by auto
    have "finite (lists P  { l. n = length l })
       finite (lists P  { l. length l = n })"
      by (subst S) simp

    thus ?thesis using lists_of_len_fin1[OF A] by auto

  lemmas lists_of_len_fin = lists_of_len_fin1 lists_of_len_fin2

  (* Try (simp only: cset_fin_simps, fastforce intro: cset_fin_intros) when reasoning about finiteness of collected sets *)
  lemmas cset_fin_simps = Ex_prod_contract fs_contract[symmetric] image_Collect[symmetric]
  lemmas cset_fin_intros = finite_imageI finite_Collect inj_onI

lemma Un_interval:
  fixes b1 :: "'a::linorder"
  assumes "b1b2" and "b2b3"
  shows "{ f i | i. b1i  i<b2 }  { f i | i. b2i  i<b3 }
    = {f i | i. b1i  i<b3}"
  using assms
  apply -
  apply rule
  apply safe []
  apply (rule_tac x=i in exI, auto) []
  apply (rule_tac x=i in exI, auto) []
  apply rule
  apply simp
  apply (elim exE, simp)
  apply (case_tac "i<b2")
  apply (rule disjI1)
  apply (rule_tac x=i in exI, auto) []
  apply (rule disjI2)
  apply (rule_tac x=i in exI, auto) []

text ‹
  The standard library proves that a generalized union is finite
  if the index set is finite and if for every index the component
  set is itself finite. Conversely, we show that every component
  set must be finite when the union is finite.
lemma finite_UNION_then_finite:
  "finite ((B ` A))  a  A  finite (B a)"
by (metis Set.set_insert UN_insert Un_infinite)

lemma finite_if_eq_beyond_finite: "finite S  finite {s. s - S = s' - S}"
proof (rule finite_subset[where B="(λs. s  (s' - S)) ` Pow S"], clarsimp)
  fix s
  have "s = (s  S)  (s - S)"
    by auto
  also assume "s - S = s' - S"
  finally show "s  (λs. s  (s' - S)) ` Pow S" by blast
qed blast

lemma distinct_finite_subset:
  assumes "finite x"
  shows "finite {ys. set ys  x  distinct ys}" (is "finite ?S")
proof (rule finite_subset)
  from assms show "?S  {ys. set ys  x  length ys  card x}"
    by clarsimp (metis distinct_card card_mono)
  from assms show "finite ..." by (rule finite_lists_length_le)

lemma distinct_finite_set:
  shows "finite {ys. set ys = x  distinct ys}" (is "finite ?S")
proof (cases "finite x")
  case False hence "{ys. set ys = x} = {}" by auto
  thus ?thesis by simp
  case True show ?thesis
  proof (rule finite_subset)
    show "?S  {ys. set ys  x  length ys  card x}"
      using distinct_card by force
    from True show "finite ..." by (rule finite_lists_length_le)

lemma finite_set_image:
  assumes f: "finite (set ` A)"
  and dist: "xs. xs  A  distinct xs"
  shows "finite A"
proof (rule finite_subset)
  from f show "finite (set -` (set ` A)  {xs. distinct xs})"
  proof (induct rule: finite_induct)
    case (insert x F)
    have "finite (set -` {x}  {xs. distinct xs})"
      apply (simp add: vimage_def)
      by (metis Collect_conj_eq distinct_finite_set)
    with insert show ?case
      apply (subst vimage_insert)
      apply (subst Int_Un_distrib2)
      apply (rule finite_UnI)
        apply simp_all
  qed simp
  moreover from dist show "A  ..."
    by (auto simp add: vimage_image_eq)

subsubsection ‹Infinite Set›
lemma INFM_nat_inductI:
  assumes P0: "P (0::nat)"
  assumes PS: "i. P i  j>i. P j  Q j"
  shows "i. Q i"
proof -
  have "i. j>i. P j  Q j" proof
    fix i
    show "j>i. P j  Q j"
      apply (induction i)
      using PS[OF P0] apply auto []
      by (metis PS Suc_lessI)
  thus ?thesis unfolding INFM_nat by blast

subsection ‹Functions›

lemma fun_neq_ext_iff: "mm'  (x. m x  m' x)" by auto  

definition "inv_on f A x == SOME y. yA  f y = x"

lemma inv_on_f_f[simp]: "inj_on f A; xA  inv_on f A (f x) = x"
  by (auto simp add: inv_on_def inj_on_def)

lemma f_inv_on_f: " yf`A   f (inv_on f A y) = y"
  by (auto simp add: inv_on_def intro: someI2)

lemma inv_on_f_range: " y  f`A   inv_on f A y  A"
  by (auto simp add: inv_on_def intro: someI2)

lemma inj_on_map_inv_f [simp]: "set l  A; inj_on f A  map (inv_on f A) (map f l) = l"
  apply (simp)
  apply (induct l)
  apply auto

lemma comp_cong_right: "x = y  f o x = f o y" by (simp)
lemma comp_cong_left: "x = y  x o f = y o f" by (simp)

lemma fun_comp_eq_conv: "f o g = fg  (x. f (g x) = fg x)"
  by auto

abbreviation comp2 (infixl "oo" 55) where "f oo g  λx. f o (g x)"
abbreviation comp3 (infixl "ooo" 55) where "f ooo g  λx. f oo (g x)"

  comp2  (infixl "∘∘" 55) and
  comp3  (infixl "∘∘∘" 55)

definition [code_unfold, simp]: "swap_args2 f x y  f y x"

subsection ‹Multisets›

  The following is a syntax extension for multisets. Unfortunately, it depends on a change in the Library/Multiset.thy, so it is commented out here, until it will be incorporated
  into Library/Multiset.thy by its maintainers.

  The required change in Library/Multiset.thy is removing the syntax for single:
     - single :: "'a => 'a multiset"    ("{#_#}")
     + single :: "'a => 'a multiset"

  And adding the following translations instead:

     + syntax
     + "_multiset" :: "args ⇒ 'a multiset" ("{#(_)#}")

     + translations
     +   "{#x, xs#}" == "{#x#} + {#xs#}"
     +   "{# x #}" == "single x"

  This translates "{# … #}" into a sum of singletons, that is parenthesized to the right. ?? Can we also achieve left-parenthesizing ??


  (* Let's try what happens if declaring AC-rules for multiset union as simp-rules *)
(*declare union_ac[simp] -- don't do it !*)

lemma count_mset_set_finite_iff:
  "finite S  count (mset_set S) a = (if a  S then 1 else 0)"
  by simp

lemma ex_Melem_conv: "(x. x ∈# A) = (A  {#})"
  by (simp add: ex_in_conv)

subsubsection ‹Count›
        lemma count_ne_remove: " x ~= t  count S x = count (S-{#t#}) x"
          by (auto)
  lemma mset_empty_count[simp]: "(p. count M p = 0) = (M={#})"
    by (auto simp add: multiset_eq_iff)

subsubsection ‹Union, difference and intersection›

  lemma size_diff_se: "t ∈# S  size S = size (S - {#t#}) + 1" proof (unfold size_multiset_overloaded_eq)
                let ?SIZE = "sum (count S) (set_mset S)"
                assume A: "t ∈# S"
                from A have SPLITPRE: "finite (set_mset S) & {t}(set_mset S)" by auto
                hence "?SIZE = sum (count S) (set_mset S - {t}) + sum (count S) {t}" by (blast dest: sum.subset_diff)
                hence "?SIZE = sum (count S) (set_mset S - {t}) + count (S) t" by auto
                moreover with A have "count S t = count (S-{#t#}) t + 1" by auto
                ultimately have D: "?SIZE = sum (count S) (set_mset S - {t}) + count (S-{#t#}) t + 1" by (arith)
                moreover have "sum (count S) (set_mset S - {t}) = sum (count (S-{#t#})) (set_mset S - {t})" proof -
                        have "x(set_mset S - {t}). count S x = count (S-{#t#}) x" by (auto iff add: count_ne_remove)
                        thus ?thesis by simp
                ultimately have D: "?SIZE = sum (count (S-{#t#})) (set_mset S - {t}) + count (S-{#t#}) t + 1" by (simp)
                { assume CASE: "t ∉# S - {#t#}"
                        from CASE have "set_mset S - {t} = set_mset (S - {#t#})"
                          by (auto simp add: in_diff_count split: if_splits)
                        with CASE D have "?SIZE = sum (count (S-{#t#})) (set_mset (S - {#t#})) + 1"
                          by (simp add: not_in_iff)
                { assume CASE: "t ∈# S - {#t#}"
                        from CASE have "t ∈# S" by (rule in_diffD)
                        with CASE have 1: "set_mset S = set_mset (S-{#t#})"
                          by (auto simp add: in_diff_count split: if_splits)
                        moreover from D have "?SIZE = sum (count (S-{#t#})) (set_mset S - {t}) + sum (count (S-{#t#})) {t} + 1" by simp
                        moreover from SPLITPRE sum.subset_diff have "sum (count (S-{#t#})) (set_mset S) = sum (count (S-{#t#})) (set_mset S - {t}) + sum (count (S-{#t#})) {t}" by (blast)
                        ultimately have "?SIZE = sum (count (S-{#t#})) (set_mset (S-{#t#})) + 1" by simp
                ultimately show "?SIZE = sum (count (S-{#t#})) (set_mset (S - {#t#})) + 1" by blast

  (* TODO: Check whether this proof can be done simpler *)
  lemma mset_union_diff_comm: "t ∈# S  T + (S - {#t#}) = (T + S) - {#t#}" proof -
    assume "t ∈# S"
    then obtain S' where S: "S = add_mset t S'"
      by (metis insert_DiffM)
    then show ?thesis
      by auto

(*  lemma mset_diff_diff_left: "A-B-C = A-((B::'a multiset)+C)" proof -
    have "∀e . count (A-B-C) e = count (A-(B+C)) e" by auto
    thus ?thesis by (simp add: multiset_eq_conv_count_eq)

  lemma mset_diff_commute: "A-B-C = A-C-(B::'a multiset)" proof -
    have "A-B-C = A-(B+C)" by (simp add: mset_diff_diff_left)
    also have "… = A-(C+B)" by (simp add: union_commute)
    thus ?thesis by (simp add: mset_diff_diff_left)

  lemma mset_diff_same_empty[simp]: "(S::'a multiset) - S = {#}"
  proof -
    have "∀e . count (S-S) e = 0" by auto
    hence "∀e . ~ (e : set_mset (S-S))" by auto
    hence "set_mset (S-S) = {}" by blast
    thus ?thesis by (auto)
  lemma mset_right_cancel_union: "a ∈# A+B; ~(a ∈# B)  a∈#A"
    by (simp)
  lemma mset_left_cancel_union: "a ∈# A+B; ~(a ∈# A)  a∈#B"
    by (simp)

  lemmas mset_cancel_union = mset_right_cancel_union mset_left_cancel_union

  lemma mset_right_cancel_elem: "a ∈# A+{#b#}; a~=b  a∈#A"
    by simp

  lemma mset_left_cancel_elem: "a ∈# {#b#}+A; a~=b  a∈#A"
    by simp

  lemmas mset_cancel_elem = mset_right_cancel_elem mset_left_cancel_elem

  lemma mset_diff_cancel1elem[simp]: "~(a ∈# B)  {#a#}-B = {#a#}"
    by (auto simp add: not_in_iff intro!: multiset_eqI)

(*  lemma diff_union_inverse[simp]: "A + B - B = (A::'a multiset)"
    by (auto iff add: multiset_eq_conv_count_eq)

  lemma diff_union_inverse2[simp]: "B + A - B = (A::'a multiset)"
    by (auto iff add: multiset_eq_conv_count_eq)
  (*lemma union_diff_assoc_se2: "t ∈# A ⟹ (A+B)-{#t#} = (A-{#t#}) + B"
    by (auto iff add: multiset_eq_conv_count_eq)
  lemmas union_diff_assoc_se = union_diff_assoc_se1 union_diff_assoc_se2*)

        lemma union_diff_assoc: "C-B={#}  (A+B)-C = A + (B-C)"
          by (simp add: multiset_eq_iff)

  lemmas mset_neutral_cancel1 = union_left_cancel[where N="{#}", simplified] union_right_cancel[where N="{#}", simplified]
  declare mset_neutral_cancel1[simp]

  lemma mset_union_2_elem: "{#a, b#} = add_mset c M  {#a#}=M & b=c | a=c & {#b#}=M"
    by (auto simp: add_eq_conv_diff)

  lemma mset_un_cases[cases set, case_names left right]:
    "a ∈# A + B; a ∈# A  P; a ∈# B  P  P"
    by (auto)

  lemma mset_unplusm_dist_cases[cases set, case_names left right]:
    assumes A: "{#s#}+A = B+C"
    assumes L: "B={#s#}+(B-{#s#}); A=(B-{#s#})+C  P"
    assumes R: "C={#s#}+(C-{#s#}); A=B+(C-{#s#})  P"
    shows P
  proof -
    from A[symmetric] have "s ∈# B+C" by simp
    thus ?thesis proof (cases rule: mset_un_cases)
      case left hence 1: "B={#s#}+(B-{#s#})" by simp
      with A have "{#s#}+A = {#s#}+((B-{#s#})+C)" by (simp add: union_ac)
      hence 2: "A = (B-{#s#})+C" by (simp)
      from L[OF 1 2] show ?thesis .
      case right hence 1: "C={#s#}+(C-{#s#})" by simp
      with A have "{#s#}+A = {#s#}+(B+(C-{#s#}))" by (simp add: union_ac)
      hence 2: "A = B+(C-{#s#})" by (simp)
      from R[OF 1 2] show ?thesis .

  lemma mset_unplusm_dist_cases2[cases set, case_names left right]:
    assumes A: "B+C = {#s#}+A"
    assumes L: "B={#s#}+(B-{#s#}); A=(B-{#s#})+C  P"
    assumes R: "C={#s#}+(C-{#s#}); A=B+(C-{#s#})  P"
    shows P
    using mset_unplusm_dist_cases[OF A[symmetric]] L R by blast

  lemma mset_single_cases[cases set, case_names loc env]:
    assumes A: "add_mset s c = add_mset r' c'"
    assumes CASES: "s=r'; c=c'  P" "c'={#s#}+(c'-{#s#}); c={#r'#}+(c-{#r'#}); c-{#r'#} = c'-{#s#}   P"
    shows "P"
  proof -
    { assume CASE: "s=r'"
      with A have "c=c'" by simp
      with CASE CASES have ?thesis by auto
    } moreover {
      assume CASE: "sr'"
      have "s ∈# {#s#}+c" by simp
      with A have "s ∈# {#r'#}+c'" by simp
      with CASE have "s ∈# c'" by simp
      hence 1: "c' = {#s#} + (c' - {#s#})" by simp
      with A have "{#s#}+c = {#s#}+({#r'#}+(c' - {#s#}))" by (auto simp add: union_ac)
      hence 2: "c={#r'#}+(c' - {#s#})" by (auto)
      hence 3: "c-{#r'#} = (c' - {#s#})" by auto
      from 1 2 3 CASES have ?thesis by auto
    } ultimately show ?thesis by blast

  lemma mset_single_cases'[cases set, case_names loc env]:
    assumes A: "add_mset s c = add_mset r' c'"
    assumes CASES: "s=r'; c=c'  P" "!!cc. c'={#s#}+cc; c={#r'#}+cc; c'-{#s#}=cc; c-{#r'#}=cc  P"
    shows "P"
    using A  CASES by (auto elim!: mset_single_cases)

  lemma mset_single_cases2[cases set, case_names loc env]:
    assumes A: "add_mset s c = add_mset r' c'"
    assumes CASES: "s=r'; c=c'  P" "c'=(c'-{#s#})+{#s#}; c=(c-{#r'#})+{#r'#}; c-{#r'#} = c'-{#s#}   P"
    shows "P"
  proof -
    from A have "add_mset s c = add_mset r' c'" by (simp add: union_ac)
    thus ?thesis using CASES by (cases rule: mset_single_cases) simp_all

  lemma mset_single_cases2'[cases set, case_names loc env]:
    assumes A: "add_mset s c = add_mset r' c'"
    assumes CASES: "s=r'; c=c'  P" "!!cc. c'=cc+{#s#}; c=cc+{#r'#}; c'-{#s#}=cc; c-{#r'#}=cc  P"
    shows "P"
    using A  CASES by (auto elim!: mset_single_cases2)

  lemma mset_un_single_un_cases [consumes 1, case_names left right]:
    assumes A: "add_mset a A = B + C"
      and CASES: "a ∈# B  A = (B - {#a#}) + C  P"
        "a ∈# C  A = B + (C - {#a#})  P" shows "P"
  proof -
    have "a ∈# A+{#a#}" by simp
    with A have "a ∈# B+C" by auto
    thus ?thesis proof (cases rule: mset_un_cases)
      case left hence "B=B-{#a#}+{#a#}" by auto
      with A have "A+{#a#} = (B-{#a#})+C+{#a#}" by (auto simp add: union_ac)
      hence "A=(B-{#a#})+C" by simp
      with CASES(1)[OF left] show ?thesis by blast
      case right hence "C=C-{#a#}+{#a#}" by auto
      with A have "A+{#a#} = B+(C-{#a#})+{#a#}" by (auto simp add: union_ac)
      hence "A=B+(C-{#a#})" by simp
      with CASES(2)[OF right] show ?thesis by blast

      (* TODO: Can this proof be done more automatically ? *)
  lemma mset_distrib[consumes 1, case_names dist]: assumes A: "(A::'a multiset)+B = M+N" "!!Am An Bm Bn. A=Am+An; B=Bm+Bn; M=Am+Bm; N=An+Bn  P" shows "P"
  proof -
    have BN_MA: "B - N = M - A"
      by (metis (no_types) add_diff_cancel_right assms(1) union_commute)
    have H: "A = A∩# C + (A - C) ∩# D" if "A + B = C + D" for A B C D :: "'a multiset"
      by (metis add.commute diff_intersect_left_idem mset_subset_eq_add_left subset_eq_diff_conv
          subset_mset.add_diff_inverse subset_mset.inf_absorb1 subset_mset.inf_le1 that)
    have A': "A = A∩# M + (A - M) ∩# N"
      using A(1) H by blast
    moreover have B': "B = (B - N) ∩# M + B∩# N"
      using A(1) H[of B A N M] by (auto simp: ac_simps)
    moreover have "M = A ∩# M + (B - N) ∩# M"
      using H[of M N A B] BN_MA[symmetric] A(1) by (metis (no_types) diff_intersect_left_idem
          diff_union_cancelR multiset_inter_commute subset_mset.diff_add subset_mset.inf.cobounded1
    moreover have "N = (A - M) ∩# N + B ∩# N"
      by (metis A' assms(1) diff_union_cancelL inter_union_distrib_left inter_union_distrib_right
          mset_subset_eq_multiset_union_diff_commute subset_mset.inf.cobounded1 subset_mset.inf.commute)
    ultimately show P
      using A(2) by blast

subsubsection ‹Singleton multisets›

lemma mset_size_le1_cases[case_names empty singleton,consumes 1]: " size M  Suc 0; M={#}  P; !!m. M={#m#}  P   P"
  by (cases M) auto

lemma diff_union_single_conv2: "a ∈# J  J + I - {#a#} = (J - {#a#}) + I"
  by simp

lemmas diff_union_single_convs = diff_union_single_conv diff_union_single_conv2

lemma mset_contains_eq: "(m ∈# M) = ({#m#}+(M-{#m#})=M)"
  using diff_single_trivial by fastforce

subsubsection ‹Pointwise ordering›

  (*declare mset_le_trans[trans] Seems to be in there now. Why is this not done in Multiset.thy or order-class ? *)

  lemma mset_le_incr_right1: "a⊆#(b::'a multiset)  a⊆#b+c" using mset_subset_eq_mono_add[of a b "{#}" c, simplified] .
  lemma mset_le_incr_right2: "a⊆#(b::'a multiset)  a⊆#c+b" using mset_le_incr_right1
    by (auto simp add: union_commute)
  lemmas mset_le_incr_right = mset_le_incr_right1 mset_le_incr_right2

  lemma mset_le_decr_left1: "a+c⊆#(b::'a multiset)  a⊆#b" using mset_le_incr_right1 mset_subset_eq_mono_add_right_cancel
    by blast
  lemma mset_le_decr_left2: "c+a⊆#(b::'a multiset)  a⊆#b" using mset_le_decr_left1
    by (auto simp add: union_ac)
  lemma mset_le_add_mset_decr_left1: "add_mset c a⊆#(b::'a multiset)  a⊆#b"
    by (simp add: mset_subset_eq_insertD subset_mset.dual_order.strict_implies_order)
  lemma mset_le_add_mset_decr_left2: "add_mset c a⊆#(b::'a multiset)  {#c#}⊆#b"
    by (simp add: mset_subset_eq_insertD subset_mset.dual_order.strict_implies_order)

  lemmas mset_le_decr_left = mset_le_decr_left1 mset_le_decr_left2 mset_le_add_mset_decr_left1

  lemma mset_le_subtract: "A⊆#B  A-C ⊆# B-(C::'a multiset)"
    apply (unfold subseteq_mset_def count_diff)
    apply clarify
    apply (subgoal_tac "count A a  count B a")
    apply arith
    apply simp

  lemma mset_union_subset: "A+B ⊆# C  A⊆#C  B⊆#(C::'a multiset)"
    by (auto dest: mset_le_decr_left)

  lemma mset_le_add_mset: "add_mset x B ⊆# C  {#x#}⊆#C  B⊆#(C::'a multiset)"
    by (auto dest: mset_le_decr_left)

  lemma mset_le_subtract_left: "A+B ⊆# (X::'a multiset)  B ⊆# X-A  A⊆#X"
    by (auto dest: mset_le_subtract[of "A+B" "X" "A"] mset_union_subset)
  lemma mset_le_subtract_right: "A+B ⊆# (X::'a multiset)  A ⊆# X-B  B⊆#X"
    by (auto dest: mset_le_subtract[of "A+B" "X" "B"] mset_union_subset)

  lemma mset_le_subtract_add_mset_left: "add_mset x B ⊆# (X::'a multiset)  B ⊆# X-{#x#}  {#x#}⊆#X"
    by (auto dest: mset_le_subtract[of "add_mset x B" "X" "{#x#}"] mset_le_add_mset)

  lemma mset_le_subtract_add_mset_right: "add_mset x B ⊆# (X::'a multiset)  {#x#} ⊆# X-B  B⊆#X"
    by (auto dest: mset_le_subtract[of "add_mset x B" "X" "B"] mset_le_add_mset)

  lemma mset_le_addE: " xs ⊆# (ys::'a multiset); !!zs. ys=xs+zs  P   P" using mset_subset_eq_exists_conv
    by blast

  declare subset_mset.diff_add[simp, intro]

  lemma mset_2dist2_cases:
    assumes A: "{#a#}+{#b#} ⊆# A+B"
    assumes CASES: "{#a#}+{#b#} ⊆# A  P" "{#a#}+{#b#} ⊆# B  P" "a ∈# A; b ∈# B  P" "a ∈# B; b ∈# A  P"
    shows "P"
  proof -
    { assume C: "a ∈# A" "b ∈# A-{#a#}"
      with mset_subset_eq_mono_add[of "{#a#}" "{#a#}" "{#b#}" "A-{#a#}"] have "{#a#}+{#b#} ⊆# A" by auto
    } moreover {
      assume C: "a ∈# A" "¬ (b ∈# A-{#a#})"
      with A have "b ∈# B"
        by (metis diff_union_single_conv2 mset_le_subtract_left mset_subset_eq_insertD mset_un_cases)
    } moreover {
      assume C: "¬ (a ∈# A)" "b ∈# B-{#a#}"
      with A have "a ∈# B"
        by (auto dest: mset_subset_eqD)
       with C mset_subset_eq_mono_add[of "{#a#}" "{#a#}" "{#b#}" "B-{#a#}"] have "{#a#}+{#b#} ⊆# B" by auto
    } moreover {
      assume C: "¬ (a ∈# A)" "¬ (b ∈# B-{#a#})"
      with A have "a ∈# B  b ∈# A"
        apply (intro conjI)
         apply (auto dest!: mset_subset_eq_insertD simp: insert_union_subset_iff; fail)[]
        by (metis mset_diff_cancel1elem mset_le_subtract_left multiset_diff_union_assoc
            single_subset_iff subset_eq_diff_conv)
    } ultimately show P using CASES by blast

  lemma mset_union_subset_s: "{#a#}+B ⊆# C  a ∈# C  B ⊆# C"
    by (drule mset_union_subset) simp

  (* TODO: Check which of these lemmas are already introduced by order-classes ! *)

  lemma mset_le_single_cases[consumes 1, case_names empty singleton]: "M⊆#{#a#}; M={#}  P; M={#a#}  P  P"
    by (induct M) auto

  lemma mset_le_distrib[consumes 1, case_names dist]: "(X::'a multiset)⊆#A+B; !!Xa Xb. X=Xa+Xb; Xa⊆#A; Xb⊆#B  P   P"
    by (auto elim!: mset_le_addE mset_distrib)

lemma mset_le_mono_add_single: "a ∈# ys; b ∈# ws  {#a#} + {#b#} ⊆# ys + ws"
  by (meson mset_subset_eq_mono_add single_subset_iff)

  lemma mset_size1elem: "size P  1; q ∈# P  P={#q#}"
    by (auto elim: mset_size_le1_cases)
  lemma mset_size2elem: "size P  2; {#q#}+{#q'#} ⊆# P  P={#q#}+{#q'#}"
    by (auto elim: mset_le_addE)

subsubsection ‹Image under function›

notation image_mset (infixr "`#" 90)

lemma mset_map_split_orig: "!!M1 M2. f `# P = M1+M2; !!P1 P2. P=P1+P2; f `# P1 = M1; f `# P2 = M2  Q   Q"
  by (induct P) (force elim!: mset_un_single_un_cases)+

lemma mset_map_id: "!!x. f (g x) = x  f `# g `# X = X"
  by (induct X) auto

text ‹The following is a very specialized lemma. Intuitively, it splits the original multiset
  by a splitting of some pointwise supermultiset of its image.

  This lemma came in handy when proving the correctness of a constraint system that collects at most k sized submultisets of the sets of spawned threads.
lemma mset_map_split_orig_le: assumes A: "f `# P ⊆# M1+M2" and EX: "!!P1 P2. P=P1+P2; f `# P1 ⊆# M1; f `# P2 ⊆# M2  Q" shows "Q"
  using A EX by (auto elim: mset_le_distrib mset_map_split_orig)

subsection ‹Lists›

lemma len_greater_imp_nonempty[simp]: "length l > x  l[]"
  by auto

lemma list_take_induct_tl2:
  "length xs = length ys; n<length xs. P (ys ! n) (xs ! n)
     n < length (tl xs). P ((tl ys) ! n) ((tl xs) ! n)"
by (induct xs ys rule: list_induct2) auto

lemma not_distinct_split_distinct:
  assumes "¬ distinct xs"
  obtains y ys zs where "distinct ys" "y  set ys" "xs = ys@[y]@zs"
using assms
proof (induct xs rule: rev_induct)
  case Nil thus ?case by simp
  case (snoc x xs) thus ?case by (cases "distinct xs") auto

lemma distinct_length_le:
  assumes d: "distinct ys"
  and eq: "set ys = set xs"
  shows "length ys  length xs"
proof -
  from d have "length ys = card (set ys)" by (simp add: distinct_card)
  also from eq List.card_set have "card (set ys) = length (remdups xs)" by simp
  also have "...  length xs" by simp
  finally show ?thesis .

lemma find_SomeD:
  "List.find P xs = Some x  P x"
  "List.find P xs = Some x  xset xs"
  by (auto simp add: find_Some_iff)

lemma in_hd_or_tl_conv[simp]: "l[]  x=hd l  xset (tl l)  xset l"
  by (cases l) auto

lemma length_dropWhile_takeWhile:
  assumes "x < length (dropWhile P xs)"
  shows "x + length (takeWhile P xs) < length xs"
  using assms
  by (induct xs) auto

text ‹Elim-version of @{thm neq_Nil_conv}.›  
lemma neq_NilE: 
  assumes "l[]"
  obtains x xs where "l=x#xs" 
  using assms by (metis list.exhaust)

lemma length_Suc_rev_conv: "length xs = Suc n  (ys y. xs=ys@[y]  length ys = n)"
  by (cases xs rule: rev_cases) auto

subsubsection ‹List Destructors›
lemma not_hd_in_tl:
  "x  hd xs  x  set xs  x  set (tl xs)"
by (induct xs) simp_all

lemma distinct_hd_tl:
  "distinct xs  x = hd xs  x  set (tl (xs))"
by (induct xs) simp_all

lemma in_set_tlD: "x  set (tl xs)  x  set xs"
by (induct xs) simp_all

lemma nth_tl: "xs  []  tl xs ! n = xs ! Suc n"
by (induct xs) simp_all

lemma tl_subset:
  "xs  []  set xs  A  set (tl xs)  A"
by (metis in_set_tlD rev_subsetD subsetI)

lemma tl_last:
  "tl xs  []  last xs = last (tl xs)"
by (induct xs) simp_all

lemma tl_obtain_elem:
  assumes "xs  []" "tl xs = []"
  obtains e where "xs = [e]"
using assms
by (induct xs rule: list_nonempty_induct) simp_all

lemma butlast_subset:
  "xs  []  set xs  A  set (butlast xs)  A"
by (metis in_set_butlastD rev_subsetD subsetI)

lemma butlast_rev_tl:
  "xs  []  butlast (rev xs) = rev (tl xs)"
by (induct xs rule: rev_induct) simp_all

lemma hd_butlast:
  "length xs > 1  hd (butlast xs) = hd xs"
by (induct xs) simp_all

lemma butlast_upd_last_eq[simp]: "length l  2 
  (butlast l) [ length l - 2 := x ] = take (length l - 2) l @ [x]"
  apply (case_tac l rule: rev_cases)
  apply simp
  apply simp
  apply (case_tac ys rule: rev_cases)
  apply simp
  apply simp

lemma distinct_butlast_swap[simp]: 
  "distinct pq  distinct (butlast (pq[i := last pq]))"
  apply (cases pq rule: rev_cases)
  apply (auto simp: list_update_append distinct_list_update split: nat.split)

subsubsection ‹Splitting list according to structure of other list›
context begin
private definition "SPLIT_ACCORDING m l  length l = length m"

private lemma SPLIT_ACCORDINGE: 
  assumes "length m = length l"
  obtains "SPLIT_ACCORDING m l"
  unfolding SPLIT_ACCORDING_def using assms by auto

private lemma SPLIT_ACCORDING_simp:
  "SPLIT_ACCORDING m (l1@l2)  (m1 m2. m=m1@m2  SPLIT_ACCORDING m1 l1  SPLIT_ACCORDING m2 l2)"
  "SPLIT_ACCORDING m (x#l')  (y m'. m=y#m'  SPLIT_ACCORDING m' l')"
  apply (fastforce simp: SPLIT_ACCORDING_def intro: exI[where x = "take (length l1) m"] exI[where x = "drop (length l1) m"])
  apply (cases m;auto simp: SPLIT_ACCORDING_def)

text ‹Split structure of list @{term m} according to structure of list @{term l}.›
method split_list_according for m :: "'a list" and l :: "'b list" =
  (rule SPLIT_ACCORDINGE[of m l],
    (simp; fail),
    ( simp only: SPLIT_ACCORDING_simp,
      elim exE conjE, 
      simp only: SPLIT_ACCORDING_def
subsubsection list_all2›
lemma list_all2_induct[consumes 1, case_names Nil Cons]:
  assumes "list_all2 P l l'"
  assumes "Q [] []"
  assumes "x x' ls ls'.  P x x'; list_all2 P ls ls'; Q ls ls' 
     Q (x#ls) (x'#ls')"
  shows "Q l l'"
  using list_all2_lengthD[OF assms(1)] assms
  apply (induct rule: list_induct2)
  apply auto

subsubsection ‹Indexing›    

lemma ran_nth_set_encoding_conv[simp]: 
  "ran (λi. if i<length l then Some (l!i) else None) = set l"
  apply safe
  apply (auto simp: ran_def split: if_split_asm) []
  apply (auto simp: in_set_conv_nth intro: ranI) []

lemma nth_image_indices[simp]: "(!) l ` {0..<length l} = set l"
  by (auto simp: in_set_conv_nth)

lemma nth_update_invalid[simp]:"¬i<length l  l[j:=x]!i = l!i"  
  apply (induction l arbitrary: i j)
  apply (auto split: nat.splits)

lemma nth_list_update': "l[i:=x]!j = (if i=j  i<length l then x else l!j)"  
  by auto

lemma last_take_nth_conv: "n  length l  n0  last (take n l) = l!(n - 1)"
  apply (induction l arbitrary: n)
  apply (auto simp: take_Cons split: nat.split)

lemma nth_append_first[simp]: "i<length l  (l@l')!i = l!i"
  by (simp add: nth_append) 
lemma in_set_image_conv_nth: "f x  f`set l  (i<length l. f (l!i) = f x)"
  by (auto simp: in_set_conv_nth) (metis image_eqI nth_mem)

lemma set_image_eq_pointwiseI: 
  assumes "length l = length l'"
  assumes "i. i<length l  f (l!i) = f (l'!i)"  
  shows "f`set l = f`set l'"
  using assms
  by (fastforce simp: in_set_conv_nth in_set_image_conv_nth)
lemma insert_swap_set_eq: "i<length l  insert (l!i) (set (l[i:=x])) = insert x (set l)"
  by (auto simp: in_set_conv_nth nth_list_update split: if_split_asm)
subsubsection ‹Reverse lists›
  lemma neq_Nil_revE: 
    assumes "l[]" 
    obtains ll e  where "l = ll@[e]"
    using assms by (cases l rule: rev_cases) auto
  lemma neq_Nil_rev_conv: "l[]  (xs x. l = xs@[x])"
    by (cases l rule: rev_cases) auto

  text ‹Caution: Same order of case variables in snoc-case as @{thm [source] rev_exhaust}, the other way round than @{thm [source] rev_induct} !›
  lemma length_compl_rev_induct[case_names Nil snoc]: "P []; !! l e . !! ll . length ll <= length l  P ll  P (l@[e])  P l"
    apply(induct_tac l rule: length_induct)
    apply(case_tac "xs" rule: rev_cases)

  lemma list_append_eq_Cons_cases[consumes 1]: "ys@zs = x#xs; ys=[]; zs=x#xs  P; !!ys'.  ys=x#ys'; ys'@zs=xs   P   P"
    by (auto iff add: append_eq_Cons_conv)
  lemma list_Cons_eq_append_cases[consumes 1]: "x#xs = ys@zs; ys=[]; zs=x#xs  P; !!ys'.  ys=x#ys'; ys'@zs=xs   P   P"
    by (auto iff add: Cons_eq_append_conv)

lemma map_of_rev_distinct[simp]:
  "distinct (map fst m)  map_of (rev m) = map_of m"
  apply (induct m)
    apply simp

    apply simp
    apply (subst map_add_comm)
      apply force
      apply simp

― ‹Tail-recursive, generalized @{const rev}. May also be used for
      tail-recursively getting a list with all elements of the two
      operands, if the order does not matter, e.g. when implementing
      sets by lists.›
fun revg where
  "revg [] b = b" |
  "revg (a#as) b = revg as (a#b)"

lemma revg_fun[simp]: "revg a b = rev a @ b"
  by (induct a arbitrary: b)

lemma rev_split_conv[simp]:
  "l  []  rev (tl l) @ [hd l] = rev l"
by (induct l) simp_all

lemma rev_butlast_is_tl_rev: "rev (butlast l) = tl (rev l)"
  by (induct l) auto

lemma hd_last_singletonI:
  "xs  []; hd xs = last xs; distinct xs  xs = [hd xs]"
  by (induct xs rule: list_nonempty_induct) auto

lemma last_filter:
  "xs  []; P (last xs)  last (filter P xs) = last xs"
  by (induct xs rule: rev_nonempty_induct) simp_all

(* As the following two rules are similar in nature to list_induct2',
   they are named accordingly. *)
lemma rev_induct2' [case_names empty snocl snocr snoclr]:
  assumes empty: "P [] []"
  and snocl: "x xs. P (xs@[x]) []"
  and snocr: "y ys. P [] (ys@[y])"
  and snoclr: "x xs y ys.  P xs ys   P (xs@[x]) (ys@[y])"
  shows "P xs ys"
proof (induct xs arbitrary: ys rule: rev_induct)
  case Nil thus ?case using empty snocr
    by (cases ys rule: rev_exhaust) simp_all
  case snoc thus ?case using snocl snoclr
    by (cases ys rule: rev_exhaust) simp_all

lemma rev_nonempty_induct2' [case_names single snocl snocr snoclr, consumes 2]:
  assumes "xs  []" "ys  []"
  assumes single': "x y. P [x] [y]"
  and snocl: "x xs y. xs  []  P (xs@[x]) [y]"
  and snocr: "x y ys. ys  []  P [x] (ys@[y])"
  and snoclr: "x xs y ys. P xs ys; xs  []; ys[]   P (xs@[x]) (ys@[y])"
  shows "P xs ys"
  using assms(1,2)
proof (induct xs arbitrary: ys rule: rev_nonempty_induct)
  case single then obtain z zs where "ys = zs@[z]" by (metis rev_exhaust)
  thus ?case using single' snocr
    by (cases "zs = []") simp_all
  case (snoc x xs) then obtain z zs where zs: "ys = zs@[z]" by (metis rev_exhaust)
  thus ?case using snocl snoclr snoc
    by (cases "zs = []") simp_all

subsubsection "Folding"

text "Ugly lemma about foldl over associative operator with left and right neutral element"
lemma foldl_A1_eq: "!!i.  !! e. f n e = e; !! e. f e n = e; !!a b c. f a (f b c) = f (f a b) c   foldl f i ww = f i (foldl f n ww)"
proof (induct ww)
  case Nil thus ?case by simp
  case (Cons a ww i) note IHP[simplified]=this
  have "foldl f i (a # ww) = foldl f (f i a) ww" by simp
  also from IHP have " = f (f i a) (foldl f n ww)" by blast
  also from IHP(4) have " = f i (f a (foldl f n ww))" by simp
  also from IHP(1)[OF IHP(2,3,4), where i=a] have " = f i (foldl f a ww)" by simp
  also from IHP(2)[of a] have " = f i (foldl f (f n a) ww)" by simp
  also have " = f i (foldl f n (a#ww))" by simp
  finally show ?case .

lemmas foldl_conc_empty_eq = foldl_A1_eq[of "(@)" "[]", simplified]
lemmas foldl_un_empty_eq = foldl_A1_eq[of "(∪)" "{}", simplified, OF Un_assoc[symmetric]]

lemma foldl_set: "foldl (∪) {} l = {x. xset l}"
  apply (induct l)
  apply simp_all
  apply (subst foldl_un_empty_eq)
  apply auto

lemma (in monoid_mult) foldl_absorb1: "x*foldl (*) 1 zs = foldl (*) x zs"
  apply (rule sym)
  apply (rule foldl_A1_eq)
  apply (auto simp add: mult.assoc)

text ‹Towards an invariant rule for foldl›
lemma foldl_rule_aux:
  fixes I :: "  'a list  bool"
  assumes initial: "I σ0 l0"
  assumes step: "!!l1 l2 x σ.  l0=l1@x#l2; I σ (x#l2)   I (f σ x) l2"
  shows "I (foldl f σ0 l0) []"
  using initial step
  apply (induct l0 arbitrary: σ0)
  apply auto

lemma foldl_rule_aux_P:
  fixes I :: "  'a list  bool"
  assumes initial: "I σ0 l0"
  assumes step: "!!l1 l2 x σ.  l0=l1@x#l2; I σ (x#l2)   I (f σ x) l2"
  assumes final: "!!σ. I σ []  P σ"
  shows "P (foldl f σ0 l0)"
using foldl_rule_aux[of I σ0 l0, OF initial, OF step] final
by simp

lemma foldl_rule:
  fixes I :: "  'a list  'a list  bool"
  assumes initial: "I σ0 [] l0"
  assumes step: "!!l1 l2 x σ.  l0=l1@x#l2; I σ l1 (x#l2)   I (f σ x) (l1@[x]) l2"
  shows "I (foldl f σ0 l0) l0 []"
  using initial step
  apply (rule_tac I="λσ lr. ll. l0=ll@lr  I σ ll lr" in foldl_rule_aux_P)
  apply auto

text ‹
  Invariant rule for foldl. The invariant is parameterized with
  the state, the list of items that have already been processed and
  the list of items that still have to be processed.
lemma foldl_rule_P:
  fixes I :: "  'a list  'a list  bool"
  ― ‹The invariant holds for the initial state, no items processed yet and all items to be processed:›
  assumes initial: "I σ0 [] l0"
  ― ‹The invariant remains valid if one item from the list is processed›
  assumes step: "!!l1 l2 x σ.  l0=l1@x#l2; I σ l1 (x#l2)   I (f σ x) (l1@[x]) l2"
  ― ‹The proposition follows from the invariant in the final state, i.e. all items processed and nothing to be processed›
  assumes final: "!!σ. I σ l0 []  P σ"
  shows "P (foldl f σ0 l0)"
  using foldl_rule[of I, OF initial step] by (simp add: final)

text ‹Invariant reasoning over @{const foldl} for distinct lists. Invariant rule makes no
  assumptions about ordering.›
lemma distinct_foldl_invar:
  " distinct S; I (set S) σ0;
     x it σ. x  it; it  set S; I it σ  I (it - {x}) (f σ x)
     I {} (foldl f σ0 S)"
proof (induct S arbitrary: σ0)
  case Nil thus ?case by auto
  case (Cons x S)

  note [simp] = Cons.prems(1)[simplified]

  show ?case
    apply simp
    apply (rule Cons.hyps)
  proof -
    from Cons.prems(1) show "distinct S" by simp
    from Cons.prems(3)[of x "set (x#S)", simplified,
                       OF Cons.prems(2)[simplified]]
    show "I (set S) (f σ0 x)" .
    fix xx it σ
    assume A: "xxit" "it  set S" "I it σ"
    show "I (it - {xx}) (f σ xx)" using A(2)
      apply (rule_tac Cons.prems(3))
      apply (simp_all add: A(1,3))
      apply blast

lemma foldl_length_aux: "foldl (λi x. Suc i) a l = a + length l"
  by (induct l arbitrary: a) auto

lemmas foldl_length[simp] = foldl_length_aux[where a=0, simplified]

lemma foldr_length_aux: "foldr (λx i. Suc i) l a = a + length l"
  by (induct l arbitrary: a rule: rev_induct) auto

lemmas foldr_length[simp] = foldr_length_aux[where a=0, simplified]

context comp_fun_commute begin

lemma foldl_f_commute: "f a (foldl (λa b. f b a) b xs) = foldl (λa b. f b a) (f a b) xs"
by(induct xs arbitrary: b)(simp_all add: fun_left_comm)

lemma foldr_conv_foldl: "foldr f xs a = foldl (λa b. f b a) a xs"
by(induct xs arbitrary: a)(simp_all add: foldl_f_commute)


lemma filter_conv_foldr:
  "filter P xs = foldr (λx xs. if P x then x # xs else xs) xs []"
by(induct xs) simp_all

lemma foldr_Cons: "foldr Cons xs [] = xs"
by(induct xs) simp_all

lemma foldr_snd_zip:
  "length xs  length ys  foldr (λ(x, y). f y) (zip xs ys) b = foldr f ys b"
proof(induct ys arbitrary: xs)
  case (Cons y ys) thus ?case by(cases xs) simp_all
qed simp

lemma foldl_snd_zip:
  "length xs  length ys  foldl (λb (x, y). f b y) b (zip xs ys) = foldl f b ys"
proof(induct ys arbitrary: xs b)
  case (Cons y ys) thus ?case by(cases xs) simp_all
qed simp

lemma fst_foldl: "fst (foldl (λ(a, b) x. (f a x, g a b x)) (a, b) xs) = foldl f a xs"
by(induct xs arbitrary: a b) simp_all

lemma foldl_foldl_conv_concat: "foldl (foldl f) a xs = foldl f a (concat xs)"
by(induct xs arbitrary: a) simp_all

lemma foldl_list_update:
  "n < length xs  foldl f a (xs[n := x]) = foldl f (f (foldl f a (take n xs)) x) (drop (Suc n) xs)"
by(simp add: upd_conv_take_nth_drop)

lemma map_by_foldl:
  fixes l :: "'a list" and f :: "'a  'b"
  shows "foldl (λl x. l@[f x]) [] l = map f l"
proof -
    fix l'
    have "foldl (λl x. l@[f x]) l' l = l'@map f l"
      by (induct l arbitrary: l') auto
  } thus ?thesis by simp

subsubsection ‹Sorting›

lemma sorted_in_between:
  assumes A: "0i" "i<j" "j<length l"
  assumes S: "sorted l"
  assumes E: "l!i  x" "x<l!j"
  obtains k where "ik" and "k<j" and "l!kx" and "x<l!(k+1)"
proof -
  from A E have "k. ik  k<j  l!kx  x<l!(k+1)"
  proof (induct "j-i" arbitrary: i j)
    case (Suc d)
    show ?case proof (cases "l!(i+1)  x")
      case True
      from True Suc.hyps have "d = j - (i + 1)" by simp
      moreover from True have "i+1 < j"
        by (metis Suc.prems Suc_eq_plus1 Suc_lessI not_less)
      moreover from True have "0i+1" by simp
      ultimately obtain k where
        "i+1k" "k<j" "l!k  x" "x<l!(k+1)"
        using Suc.hyps(1)[of j "i+1"] Suc.prems True
        by auto
      thus ?thesis by (auto dest: Suc_leD)
      case False
      show ?thesis proof (cases "x<(l!(j - 1))")
        case True
        from True Suc.hyps have "d = j - (i + 1)" by simp
        moreover from True Suc.prems have "i < j - 1"
          by (metis Suc_eq_plus1 Suc_lessI diff_Suc_1 less_diff_conv not_le)
        moreover from True Suc.prems have "j - 1 < length l" by simp
        ultimately obtain k where
          "ik" "k<j - 1" "l!k  x" "x<l!(k+1)"
          using Suc.hyps(1)[of "j - 1" i] Suc.prems True
          by auto
        thus ?thesis by (auto dest: Suc_leD)
        case False thus ?thesis using Suc
          apply clarsimp
          by (metis Suc_leI add_0_iff add_diff_inverse diff_Suc_1 le_add2 lessI
              not0_implies_Suc not_less)
  qed simp
  thus ?thesis by (blast intro: that)

lemma sorted_hd_last:
  "sorted l; l[]  hd l  last l"
by (metis eq_iff hd_Cons_tl last_in_set not_hd_in_tl sorted.simps(2))

lemma (in linorder) sorted_hd_min:
  "xs  []; sorted xs  x  set xs. hd xs  x"
by (induct xs, auto)

lemma sorted_append_bigger:
  "sorted xs; x  set xs. x  y  sorted (xs @ [y])"
proof (induct xs)
  case Nil
  then show ?case by simp
  case (Cons x xs)
  then have s: "sorted xs" by (cases xs) simp_all
  from Cons have a: "xset xs. x  y" by simp
  from Cons(1)[OF s a] Cons(2-) show ?case by (cases xs) simp_all

lemma sorted_filter':
  "sorted l  sorted (filter P l)"
  using sorted_filter[where f=id, simplified] .

subsubsection ‹Map›
(* List.thy has:
 declare map_eq_Cons_D [dest!]  Cons_eq_map_D [dest!] *)  
lemma map_eq_consE: "map f ls = fa#fl; !!a l.  ls=a#l; f a=fa; map f l = fl   P  P"
  by auto

lemma map_fst_mk_snd[simp]: "map fst (map (λx. (x,k)) l) = l" by (induct l) auto
lemma map_snd_mk_fst[simp]: "map snd (map (λx. (k,x)) l) = l" by (induct l) auto
lemma map_fst_mk_fst[simp]: "map fst (map (λx. (k,x)) l) = replicate (length l) k" by (induct l) auto
lemma map_snd_mk_snd[simp]: "map snd (map (λx. (x,k)) l) = replicate (length l) k" by (induct l) auto

lemma map_zip1: "map (λx. (x,k)) l = zip l (replicate (length l) k)" by (induct l) auto
lemma map_zip2: "map (λx. (k,x)) l = zip (replicate (length l) k) l" by (induct l) auto
lemmas map_zip=map_zip1 map_zip2

(* TODO/FIXME: hope nobody changes nth to be underdefined! *)
lemma map_eq_nth_eq:
  assumes A: "map f l = map f l'"
  shows "f (l!i) = f (l'!i)"
proof -
  from A have "length l = length l'"
    by (metis length_map)
  thus ?thesis using A
    apply (induct arbitrary: i rule: list_induct2)
    apply simp
    apply (simp add: nth_def split: nat.split)

lemma map_upd_eq:
  "i<length l  f (l!i) = f x  map f (l[i:=x]) = map f l"
  by (metis list_update_beyond list_update_id map_update not_le_imp_less)

lemma inj_map_inv_f [simp]: "inj f  map (inv f) (map f l) = l"
  by (simp)

lemma inj_on_map_the: "D  dom m; inj_on m D  inj_on (them) D"
  apply (rule inj_onI)
  apply simp
  apply (case_tac "m x")
  apply (case_tac "m y")
  apply (auto intro: inj_onD) [1]
  apply (auto intro: inj_onD) [1]
  apply (case_tac "m y")
  apply (auto intro: inj_onD) [1]
  apply simp
  apply (rule inj_onD)
  apply assumption
  apply auto


lemma map_consI:
  "w=map f ww  f a#w = map f (a#ww)"
  "w@l=map f ww@l  f a#w@l = map f (a#ww)@l"
  by auto

lemma restrict_map_subset_eq:
  fixes R
  shows "m |` R = m'; R'R  m|` R' = m' |` R'"
  by (auto simp add: Int_absorb1)

lemma restrict_map_self[simp]: "m |` dom m = m"
  apply (rule ext)
  apply (case_tac "m x")
  apply (auto simp add: restrict_map_def)

lemma restrict_map_UNIV[simp]: "f |` UNIV = f"
  by (auto simp add: restrict_map_def)

lemma restrict_map_inv[simp]: "f |` (- dom f) = Map.empty"
  by (auto simp add: restrict_map_def intro: ext)

lemma restrict_map_upd: "(f |` S)(k  v) = f(kv) |` (insert k S)"
  by (auto simp add: restrict_map_def intro: ext)

lemma map_upd_eq_restrict: "m (x:=None) = m |` (-{x})"
  by (auto intro: ext)

declare Map.finite_dom_map_of [simp, intro!]

lemma dom_const'[simp]: "dom (λx. Some (f x)) = UNIV"
  by auto

lemma restrict_map_eq :
  "((m |` A) k = None)  (k  dom m  A)"
  "((m |` A) k = Some v)  (m k = Some v  k  A)"
unfolding restrict_map_def
by (simp_all add: dom_def)

definition "rel_of m P == {(k,v). m k = Some v  P (k, v)}"
lemma rel_of_empty[simp]: "rel_of Map.empty P = {}"
    by (auto simp add: rel_of_def)

lemma remove1_tl: "xs  []  remove1 (hd xs) xs = tl xs"
  by (cases xs) auto

lemma set_oo_map_alt: "(set ∘∘ map) f = (λl. f ` set l)" by auto
subsubsection "Filter and Revert"
primrec filter_rev_aux where
  "filter_rev_aux a P [] = a"
| "filter_rev_aux a P (x#xs) = (
     if P x then filter_rev_aux (x#a) P xs else filter_rev_aux a P xs)"

lemma filter_rev_aux_alt: "filter_rev_aux a P l = filter P (rev l) @ a"
  by (induct l arbitrary: a) auto

definition "filter_rev == filter_rev_aux []"
lemma filter_rev_alt: "filter_rev P l = filter P (rev l)"
  unfolding filter_rev_def by (simp add: filter_rev_aux_alt)

definition "remove_rev x == filter_rev (Not o (=) x)"
lemma remove_rev_alt_def :
  "remove_rev x xs = (filter (λy. y  x) (rev xs))"
  unfolding remove_rev_def
  apply (simp add: filter_rev_alt comp_def)
  by metis

subsubsection "zip"

declare zip_map_fst_snd[simp]  

lemma pair_list_split: " !!l1 l2.  l = zip l1 l2; length l1=length l2; length l=length l2   P   P"
proof (induct l arbitrary: P)
  case Nil thus ?case by auto
  case (Cons a l) from Cons.hyps obtain l1 l2 where IHAPP: "l=zip l1 l2" "length l1 = length l2" "length l=length l2" .
  obtain a1 a2 where [simp]: "a=(a1,a2)" by (cases a) auto
  from IHAPP have "a#l = zip (a1#l1) (a2#l2)" "length (a1#l1) = length (a2#l2)" "length (a#l) = length (a2#l2)"
    by (simp_all only:) (simp_all (no_asm_use))
  with Cons.prems show ?case by blast

lemma set_zip_cart: "xset (zip l l')  xset l × set l'"
  by (auto simp add: set_zip)

lemma map_prod_fun_zip: "map (λ(x, y). (f x, g y)) (zip xs ys) = zip (map f xs) (map g ys)"
proof(induct xs arbitrary: ys)
  case Nil thus ?case by simp
  case (Cons x xs) thus ?case by(cases ys) simp_all

subsubsection ‹Generalized Zip›
text ‹Zip two lists element-wise, where the combination of two elements is specified by a function. Note that this function is underdefined for lists of different length.›
fun zipf :: "('a'b'c)  'a list  'b list  'c list" where
  "zipf f [] [] = []" |
  "zipf f (a#as) (b#bs) = f a b # zipf f as bs"

lemma zipf_zip: "length l1 = length l2  zipf Pair l1 l2 = zip l1 l2"
  apply (induct l1 arbitrary: l2)
  apply auto
  apply (case_tac l2)
  apply auto

  ― ‹All quantification over zipped lists›
fun list_all_zip where
  "list_all_zip P [] []  True" |
  "list_all_zip P (a#as) (b#bs)  P a b  list_all_zip P as bs" |
  "list_all_zip P _ _  False"

lemma list_all_zip_alt: "list_all_zip P as bs  length as = length bs  (i<length as. P (as!i) (bs!i))"
  apply (induct PP as bs rule: list_all_zip.induct)
  apply auto
  apply (case_tac i)
  apply auto

lemma list_all_zip_map1: "list_all_zip P ( f as) bs  list_all_zip (λa b. P (f a) b) as bs"
  apply (induct as arbitrary: bs)
  apply (case_tac bs)
  apply auto [2]
  apply (case_tac bs)
  apply auto [2]

lemma list_all_zip_map2: "list_all_zip P as ( f bs)  list_all_zip (λa b. P a (f b)) as bs"
  apply (induct as arbitrary: bs)
  apply (case_tac bs)
  apply auto [2]
  apply (case_tac bs)
  apply auto [2]

declare list_all_zip_alt[mono]

lemma lazI[intro?]: " length a = length b; !!i. i<length b  P (a!i) (b!i) 
   list_all_zip P a b"
  by (auto simp add: list_all_zip_alt)

lemma laz_conj[simp]: "list_all_zip (λx y. P x y  Q x y) a b
                        list_all_zip P a b  list_all_zip Q a b"
  by (auto simp add: list_all_zip_alt)

lemma laz_len: "list_all_zip P a b  length a = length b"
  by (simp add: list_all_zip_alt)

lemma laz_eq: "list_all_zip (=) a b  a=b"
  apply (induct a arbitrary: b)
  apply (case_tac b)
  apply simp
  apply simp
  apply (case_tac b)
  apply simp
  apply simp

lemma laz_swap_ex:
  assumes A: "list_all_zip (λa b. c. P a b c) A B"
  obtains C where
    "list_all_zip (λa c. b. P a b c) A C"
    "list_all_zip (λb c. a. P a b c) B C"
proof -
  from A have
    [simp]: "length A = length B" and
    IC: "i<length B. ci. P (A!i) (B!i) ci"
    by (auto simp add: list_all_zip_alt)
  from obtain_list_from_elements[OF IC] obtain C where
    "length C = length B"
    "i<length B. P (A!i) (B!i) (C!i)" .
  thus ?thesis
    by (rule_tac that) (auto simp add: list_all_zip_alt)

lemma laz_weak_Pa[simp]:
  "list_all_zip (λa b. P a) A B  (length A = length B)  (aset A. P a)"
  by (auto simp add: list_all_zip_alt set_conv_nth)

lemma laz_weak_Pb[simp]:
  "list_all_zip (λa b. P b) A B  (length A = length B)  (bset B. P b)"
  by (force simp add: list_all_zip_alt set_conv_nth)

subsubsection "Collecting Sets over Lists"

definition "list_collect_set f l == { f a | a. aset l }"
lemma list_collect_set_simps[simp]:
  "list_collect_set f [] = {}"
  "list_collect_set f [a] = f a"
  "list_collect_set f (a#l) = f a  list_collect_set f l"
  "list_collect_set f (l@l') = list_collect_set f l  list_collect_set f l'"
by (unfold list_collect_set_def) auto

lemma list_collect_set_map_simps[simp]:
  "list_collect_set f (map x []) = {}"
  "list_collect_set f (map x [a]) = f (x a)"
  "list_collect_set f (map x (a#l)) = f (x a)  list_collect_set f (map x l)"
  "list_collect_set f (map x (l@l')) = list_collect_set f (map x l)  list_collect_set f (map x l')"
by simp_all

lemma list_collect_set_alt: "list_collect_set f l = { f (l!i) | i. i<length l }"
  apply (induct l)
  apply simp
  apply safe
  apply auto
  apply (rule_tac x="f (l!i)" in exI)
  apply simp
  apply (rule_tac x="Suc i" in exI)
  apply simp
  apply (case_tac i)
  apply auto

lemma list_collect_set_as_map: "list_collect_set f l = (set (map f l))"
  by (unfold list_collect_set_def) auto

subsubsection ‹Sorted List with arbitrary Relations›

lemma (in linorder) sorted_wrt_rev_linord [simp] :
  "sorted_wrt (≥) l  sorted (rev l)"
by (simp add: sorted_sorted_wrt sorted_wrt_rev)

lemma (in linorder) sorted_wrt_map_linord [simp] :
  "sorted_wrt (λ(x::'a × 'b) y. fst x  fst y) l
   sorted (map fst l)"
by (simp add: sorted_sorted_wrt sorted_wrt_map)

lemma (in linorder) sorted_wrt_map_rev_linord [simp] :
  "sorted_wrt (λ(x::'a × 'b) y. fst x  fst y) l
   sorted (rev (map fst l))"
by (induct l) (auto simp add: sorted_append)

subsubsection ‹Take and Drop›
  lemma take_update[simp]: "take n (l[i:=x]) = (take n l)[i:=x]"
    apply (induct l arbitrary: n i)
    apply (auto split: nat.split)
    apply (case_tac n)
    apply simp_all
    apply (case_tac n)
    apply simp_all

  lemma take_update_last: "length list>n  (take (Suc n) list) [n:=x] = take n list @ [x]"
    by (induct list arbitrary: n)
       (auto split: nat.split)

  lemma drop_upd_irrelevant: "m < n  drop n (l[m:=x]) = drop n l"
    apply (induct n arbitrary: l m)
    apply simp
    apply (case_tac l)
    apply (auto split: nat.split)

lemma set_drop_conv:
  "set (drop n l) =  { l!i | i. ni  i < length l }" (is "?L=?R")
proof (intro equalityI subsetI)
  fix x
  assume "x?L"
  then obtain i where L: "i<length l - n" and X: "x = drop n l!i"
    by (auto simp add: in_set_conv_nth)
  note X
  also have " = l!(n+i)" using L by simp
  finally show "x?R" using L by auto
  fix x
  assume "x?R"
  then obtain i where L: "ni" "i<length l" and X: "x=l!i" by blast
  note X
  moreover have "l!i = drop n l ! (i - n)" and "(i-n) < length l - n" using L
    by (auto)
  ultimately show "x?L"
    by (auto simp add: in_set_conv_nth)

lemma filter_upt_take_conv:
  "[i[n..<m]. P (take m l ! i) ] = [i[n..<m]. P (l ! i) ]"
  by (rule filter_cong) (simp_all)

lemma in_set_drop_conv_nth: "xset (drop n l)  (i. ni  i<length l  x = l!i)"
  apply (clarsimp simp: in_set_conv_nth)
  apply safe
  apply simp
  apply (metis le_add2 less_diff_conv add.commute)
  apply (rule_tac x="i-n" in exI)
  apply auto []

lemma Union_take_drop_id: "(set (drop n l))  (set (take n l)) = (set l)"
  by (metis Union_Un_distrib append_take_drop_id set_union_code sup_commute)

lemma Un_set_drop_extend: "jSuc 0; j < length l
   l ! (j - Suc 0)  (set (drop j l)) = (set (drop (j - Suc 0) l))"
  apply safe
  apply simp_all
  apply (metis diff_Suc_Suc diff_zero gr0_implies_Suc in_set_drop_conv_nth
    le_refl less_eq_Suc_le order.strict_iff_order)
  apply (metis Nat.diff_le_self set_drop_subset_set_drop subset_code(1))
  by (metis diff_Suc_Suc gr0_implies_Suc in_set_drop_conv_nth
    less_eq_Suc_le order.strict_iff_order minus_nat.diff_0)

lemma drop_take_drop_unsplit:
  "ij  drop i (take j l) @ drop j l = drop i l"
proof -
  assume "i  j"
  then obtain skf where "i + skf = j"
    by (metis le_iff_add)
  thus "drop i (take j l) @ drop j l = drop i l"
    by (metis append_take_drop_id diff_add_inverse drop_drop drop_take

lemma drop_last_conv[simp]: "l[]  drop (length l - Suc 0) l = [last l]"
  by (cases l rule: rev_cases) auto

lemma take_butlast_conv[simp]: "take (length l - Suc 0) l = butlast l"
  by (cases l rule: rev_cases) auto

  lemma drop_takeWhile:
    assumes "ilength (takeWhile P l)"
    shows "drop i (takeWhile P l) = takeWhile P (drop i l)"  
    using assms
  proof (induction l arbitrary: i)
    case Nil thus ?case by auto
    case (Cons x l) thus ?case  
      by (cases i) auto
lemma less_length_takeWhile_conv: "i < length (takeWhile P l)  (i<length l  (ji. P (l!j)))"  
  apply safe
  subgoal using length_takeWhile_le less_le_trans by blast
  subgoal by (metis dual_order.strict_trans2 nth_mem set_takeWhileD takeWhile_nth)
  subgoal by (meson less_le_trans not_le_imp_less nth_length_takeWhile)
lemma eq_len_takeWhile_conv: "i=length (takeWhile P l) 
   ilength l  (j<i. P (l!j))  (i<length l  ¬P (l!i))"  
  apply safe
  subgoal using length_takeWhile_le less_le_trans by blast
  subgoal by (auto simp: less_length_takeWhile_conv)
  subgoal using nth_length_takeWhile by blast 
  subgoal by (metis length_takeWhile_le nth_length_takeWhile order.order_iff_strict) 
  subgoal by (metis dual_order.strict_trans2 leI less_length_takeWhile_conv linorder_neqE_nat nth_length_takeWhile) 
subsubsection ‹Up-to›

lemma upt_eq_append_conv: "ij  [i..<j] = xs@ys  (k. ik  kj  [i..<k] = xs  [k..<j] = ys)"
proof (rule iffI)
  assume "[i..<j] = xs @ ys"  
  and "ij"
  thus "ki. k  j  [i..<k] = xs  [k..<j] = ys"
    apply (induction xs arbitrary: i)
    apply (auto; fail)
    apply (clarsimp simp: upt_eq_Cons_conv)
    by (meson Suc_le_eq less_imp_le_nat)
qed auto
lemma map_nth_upt_drop_take_conv: "N  length l  map (nth l) [M..<N] = drop M (take N l)"
  by (induction N) (auto simp: take_Suc_conv_app_nth)
lemma upt_eq_lel_conv:
  "[l..<h] = is1@i#is2  is1 = [l..<i]  is2 = [Suc i..<h]  li  i<h"
  apply (rule)
    apply (induction is1 arbitrary: l)
    apply (auto simp: upt_eq_Cons_conv) []
    apply (clarsimp simp: upt_eq_Cons_conv)
    using Suc_le_eq upt_rec by auto
  subgoal by (auto simp: upt_conv_Cons[symmetric])

lemma map_add_upt': "map (λi. i + ofs) [a..<b] = [a+ofs..<b + ofs]"
  by (induct b) simp_all
subsubsection ‹Last and butlast›
lemma butlast_upt: "butlast [m..<n] = [m..<n - 1]"
  apply (cases "m<n")
    apply (cases n)
      apply simp
    apply simp
  apply simp

(*lemma butlast_upt: "n<m ⟹ butlast [n..<m] = [n..<m - 1]"
  apply (cases "[n..<m]" rule: rev_cases)
  apply simp
  apply (cases m)
  apply simp
  apply simp

lemma butlast_update': "(butlast l) [i:=x] = butlast (l[i:=x])"
  by (metis butlast_conv_take butlast_list_update length_butlast take_update)

lemma take_minus_one_conv_butlast:
  "nlength l  take (n - Suc 0) l = butlast (take n l)"
  by (simp add: butlast_take)

lemma butlast_eq_cons_conv: "butlast l = x#xs  (xl. l=x#xs@[xl])"
  by (metis Cons_eq_appendI append_butlast_last_id butlast.simps
    butlast_snoc eq_Nil_appendI)

lemma butlast_eq_consE:
  assumes "butlast l = x#xs"
  obtains xl where "l=x#xs@[xl]"
  using assms
  by (auto simp: butlast_eq_cons_conv)

lemma drop_eq_ConsD: "drop n xs = x # xs'  drop (Suc n) xs = xs'"
by(induct xs arbitrary: n)(simp_all add: drop_Cons split: nat.split_asm)

subsubsection ‹List Slices›  
  text ‹Based on Lars Hupel's code.›  
definition slice :: "nat  nat  'a list  'a list" where
"slice from to list = take (to - from) (drop from list)"

lemma slice_len[simp]: " from  to; to  length xs   length (slice from to xs) = to - from"
unfolding slice_def
by simp

lemma slice_head: " from < to; to  length xs   hd (slice from to xs) = xs ! from"
unfolding slice_def
proof -
  assume a1: "from < to"
  assume "to  length xs"
  then have "n. to - (to - n)  length (take to xs)"
    by (metis (no_types) slice_def diff_le_self drop_take length_drop slice_len)
  then show "hd (take (to - from) (drop from xs)) = xs ! from"
    using a1 by (metis diff_diff_cancel drop_take hd_drop_conv_nth leI le_antisym less_or_eq_imp_le nth_take)

lemma slice_eq_bounds_empty[simp]: "slice i i xs = []"
unfolding slice_def by auto

lemma slice_nth: " from < to; to  length xs; i < to - from   slice from to xs ! i = xs ! (from + i)"
unfolding slice_def
by (induction "to - from" arbitrary: "from" to i) simp+

lemma slice_prepend: " i  k; k  length xs   let p = length ys in slice i k xs = slice (i + p) (k + p) (ys @ xs)"
unfolding slice_def Let_def
by force

lemma slice_Nil[simp]: "slice begin end [] = []"  
  unfolding slice_def by auto
lemma slice_Cons: "slice begin end (x#xs) 
  = (if begin=0  end>0 then x#slice begin (end-1) xs else slice (begin - 1) (end - 1) xs)"
  unfolding slice_def
  by (auto simp: take_Cons' drop_Cons')

lemma slice_complete[simp]: "slice 0 (length xs) xs = xs"
  unfolding slice_def
  by simp

subsubsection ‹Miscellaneous›
  lemma length_compl_induct[case_names Nil Cons]: "P []; !! e l . !! ll . length ll <= length l  P ll  P (e#l)  P l"
    apply(induct_tac l rule: length_induct)
    apply(case_tac "xs")

  lemma in_set_list_format: " eset l; !!l1 l2. l=l1@e#l2  P   P"
  proof (induct l arbitrary: P)
    case Nil thus ?case by auto
    case (Cons a l) show ?case proof (cases "a=e")
      case True with Cons show ?thesis by force
      case False with Cons.prems(1) have "eset l" by auto
      with Cons.hyps obtain l1 l2 where "l=l1@e#l2" by blast
      hence "a#l = (a#l1)@e#l2" by simp
      with Cons.prems(2) show P by blast

lemma in_set_upd_cases:
  assumes "xset (l[i:=y])"
  obtains "i<length l" and "x=y" | "xset l"
  by (metis assms in_set_conv_nth length_list_update nth_list_update_eq

lemma in_set_upd_eq_aux:
  assumes "i<length l"
  shows "xset (l[i:=y])  x=y  (y. xset (l[i:=y]))"
  by (metis in_set_upd_cases assms list_update_overwrite

lemma in_set_upd_eq:
  assumes "i<length l"
  shows "xset (l[i:=y])  x=y  (xset l  (y. xset (l[i:=y])))"
  by (metis in_set_upd_cases in_set_upd_eq_aux assms)

  text ‹Simultaneous induction over two lists, prepending an element to one of the lists in each step›
  lemma list_2pre_induct[case_names base left right]: assumes BASE: "P [] []" and LEFT: "!!e w1' w2. P w1' w2  P (e#w1') w2" and RIGHT: "!!e w1 w2'. P w1 w2'  P w1 (e#w2')" shows "P w1 w2"
  proof -
    { ― ‹The proof is done by induction over the sum of the lengths of the lists›
      fix n
      have "!!w1 w2. length w1 + length w2 = n; P [] []; !!e w1' w2. P w1' w2  P (e#w1') w2; !!e w1 w2'. P w1 w2'  P w1 (e#w2')   P w1 w2 "
        apply (induct n)
        apply simp
        apply (case_tac w1)
        apply auto
        apply (case_tac w2)
        apply auto
    } from this[OF _ BASE LEFT RIGHT] show ?thesis by blast

  lemma list_decomp_1: "length l=1  a. l=[a]"
    by (case_tac l, auto)

  lemma list_decomp_2: "length l=2  a b. l=[a,b]"
    by (case_tac l, auto simp add: list_decomp_1)

  lemma list_rest_coinc: "length s2  length s1; s1@r1 = s2@r2  r1p. r2=r1p@r1"
    by (metis append_eq_append_conv_if)

  lemma list_tail_coinc: "n1#r1 = n2#r2  n1=n2 & r1=r2"
    by (auto)

  lemma last_in_set[intro]: "l[]  last l  set l"
    by (induct l) auto

  lemma empty_append_eq_id[simp]: "(@) [] = (λx. x)" by auto
  lemma op_conc_empty_img_id[simp]: "((@) [] ` L) = L" by auto

  lemma distinct_match: " distinct (al@e#bl)   (al@e#bl = al'@e#bl')  (al=al'  bl=bl')"
  proof (rule iffI, induct al arbitrary: al')
    case Nil thus ?case by (cases al') auto
    case (Cons a al) note Cprems=Cons.prems note Chyps=Cons.hyps
    show ?case proof (cases al')
      case Nil with Cprems have False by auto
      thus ?thesis ..
      case [simp]: (Cons a' all')
      with Cprems have [simp]: "a=a'" and P: "al@e#bl = all'@e#bl'" by auto
      from Cprems(1) have D: "distinct (al@e#bl)" by auto
      from Chyps[OF D P] have [simp]: "al=all'" "bl=bl'" by auto
      show ?thesis by simp
  qed simp

  lemma prop_match: " list_all P al; ¬P e; ¬P e'; list_all P bl   (al@e#bl = al'@e'#bl')  (al=al'  e=e'  bl=bl')"
    apply (rule iffI, induct al arbitrary: al')
    apply (case_tac al', fastforce, fastforce)+

  lemmas prop_matchD = rev_iffD1[OF _ prop_match[where P=P]] for P

  declare distinct_tl[simp]

lemma list_se_match[simp]:
  "l1  []  l1@l2 = [a]  l1 = [a]  l2 = []"
  "l2  []  l1@l2 = [a]  l1 = []  l2 = [a]"
  "l1  []  [a] = l1@l2  l1 = [a]  l2 = []"
  "l2  []  [a] = l1@l2  l1 = []  l2 = [a]"
  apply (cases l1, simp_all)
  apply (cases l1, simp_all)
  apply (cases l1, auto) []
  apply (cases l1, auto)