| author | paulson | 
| Mon, 16 May 2005 10:29:15 +0200 | |
| changeset 15965 | f422f8283491 | 
| parent 15959 | 366d39e95d3c | 
| child 16179 | fa7e70be26b0 | 
| permissions | -rw-r--r-- | 
| 15481 | 1 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) | 
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 2 | (* Title: isand.ML | 
| 15481 | 3 | Author: Lucas Dixon, University of Edinburgh | 
| 4 | lucas.dixon@ed.ac.uk | |
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 5 | Updated: 26 Apr 2005 | 
| 15481 | 6 | Date: 6 Aug 2004 | 
| 7 | *) | |
| 8 | (* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) | |
| 9 | (* DESCRIPTION: | |
| 10 | ||
| 11 | Natural Deduction tools | |
| 12 | ||
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 13 | For working with Isabelle theorems in a natural detuction style. | 
| 15481 | 14 | ie, not having to deal with meta level quantified varaibles, | 
| 15 | instead, we work with newly introduced frees, and hide the | |
| 16 | "all"'s, exporting results from theorems proved with the frees, to | |
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 17 | solve the all cases of the previous goal. This allows resolution | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 18 | to do proof search normally. | 
| 15481 | 19 | |
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 20 | Note: A nice idea: allow exporting to solve any subgoal, thus | 
| 15481 | 21 | allowing the interleaving of proof, or provide a structure for the | 
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 22 | ordering of proof, thus allowing proof attempts in parrell, but | 
| 15481 | 23 | recording the order to apply things in. | 
| 24 | ||
| 25 | debugging tools: | |
| 26 | ||
| 27 | fun asm_mk t = (assume (cterm_of (Theory.sign_of (the_context())) t)); | |
| 28 | fun asm_read s = | |
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 29 | (assume (read_cterm (Theory.sign_of (Context.the_context())) (s,propT))); | 
| 15481 | 30 | |
| 31 | THINK: are we really ok with our varify name w.r.t the prop - do | |
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 32 | we also need to avoid names in the hidden hyps? What about | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 33 | unification contraints in flex-flex pairs - might they also have | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 34 | extra free vars? | 
| 15481 | 35 | *) | 
| 36 | ||
| 37 | signature ISA_ND = | |
| 38 | sig | |
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 39 | |
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 40 | (* export data *) | 
| 15481 | 41 | datatype export = export of | 
| 42 |            {gth: Thm.thm, (* initial goal theorem *)
 | |
| 43 | sgid: int, (* subgoal id which has been fixed etc *) | |
| 44 | fixes: Thm.cterm list, (* frees *) | |
| 45 | assumes: Thm.cterm list} (* assumptions *) | |
| 46 | val fixes_of_exp : export -> Thm.cterm list | |
| 47 | val export_back : export -> Thm.thm -> Thm.thm Seq.seq | |
| 48 | val export_solution : export -> Thm.thm -> Thm.thm | |
| 49 | val export_solutions : export list * Thm.thm -> Thm.thm | |
| 50 | ||
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 51 | (* inserting meta level params for frees in the conditions *) | 
| 15481 | 52 | val allify_conditions : | 
| 53 | (Term.term -> Thm.cterm) -> | |
| 54 | (string * Term.typ) list -> Thm.thm -> Thm.thm * Thm.cterm list | |
| 55 | val allify_conditions' : | |
| 56 | (string * Term.typ) list -> Thm.thm -> Thm.thm * Thm.cterm list | |
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 57 | val assume_allified : | 
| 15928 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 58 | Sign.sg -> (string * Term.sort) list * (string * Term.typ) list | 
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 59 | -> Term.term -> (Thm.cterm * Thm.thm) | 
| 15481 | 60 | |
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 61 | (* meta level fixed params (i.e. !! vars) *) | 
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 62 | val fix_alls_in_term : Term.term -> Term.term * Term.term list | 
| 15481 | 63 | val fix_alls_term : int -> Term.term -> Term.term * Term.term list | 
| 64 | val fix_alls_cterm : int -> Thm.thm -> Thm.cterm * Thm.cterm list | |
| 65 | val fix_alls' : int -> Thm.thm -> Thm.thm * Thm.cterm list | |
| 66 | val fix_alls : int -> Thm.thm -> Thm.thm * export | |
| 67 | ||
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 68 | (* meta variables in types and terms *) | 
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 69 | val fix_tvars_to_tfrees_in_terms | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 70 | : string list (* avoid these names *) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 71 | -> Term.term list -> | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 72 | (((string * int) * Term.sort) * (string * Term.sort)) list (* renamings *) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 73 | val fix_vars_to_frees_in_terms | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 74 | : string list (* avoid these names *) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 75 | -> Term.term list -> | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 76 | (((string * int) * Term.typ) * (string * Term.typ)) list (* renamings *) | 
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 77 | val fix_tvars_to_tfrees : Thm.thm -> Thm.ctyp list * Thm.thm | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 78 | val fix_vars_to_frees : Thm.thm -> Thm.cterm list * Thm.thm | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 79 | val fix_vars_and_tvars : | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 80 | Thm.thm -> (Thm.cterm list * Thm.ctyp list) * Thm.thm | 
| 15928 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 81 | val fix_vars_upto_idx : int -> Thm.thm -> Thm.thm | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 82 | val fix_tvars_upto_idx : int -> Thm.thm -> Thm.thm | 
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 83 | val unfix_frees : Thm.cterm list -> Thm.thm -> Thm.thm | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 84 | val unfix_tfrees : Thm.ctyp list -> Thm.thm -> Thm.thm | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 85 | val unfix_frees_and_tfrees : | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 86 | (Thm.cterm list * Thm.ctyp list) -> Thm.thm -> Thm.thm | 
| 15481 | 87 | |
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 88 | (* assumptions/subgoals *) | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 89 | val assume_prems : | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 90 | int -> Thm.thm -> Thm.thm list * Thm.thm * Thm.cterm list | 
| 15481 | 91 | val fixed_subgoal_thms : Thm.thm -> Thm.thm list * (Thm.thm list -> Thm.thm) | 
| 92 | val fixes_and_assumes : int -> Thm.thm -> Thm.thm list * Thm.thm * export | |
| 93 | val hide_other_goals : Thm.thm -> Thm.thm * Thm.cterm list | |
| 94 | val hide_prems : Thm.thm -> Thm.thm * Thm.cterm list | |
| 95 | ||
| 96 | (* abstracts cterms (vars) to locally meta-all bounds *) | |
| 97 | val prepare_goal_export : string list * Thm.cterm list -> Thm.thm | |
| 98 | -> int * Thm.thm | |
| 99 | val solve_with : Thm.thm -> Thm.thm -> Thm.thm Seq.seq | |
| 100 | val subgoal_thms : Thm.thm -> Thm.thm list * (Thm.thm list -> Thm.thm) | |
| 101 | end | |
| 102 | ||
| 103 | ||
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 104 | structure IsaND | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 105 | : ISA_ND | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 106 | = struct | 
| 15481 | 107 | |
| 108 | (* Solve *some* subgoal of "th" directly by "sol" *) | |
| 109 | (* Note: this is probably what Markus ment to do upon export of a | |
| 110 | "show" but maybe he used RS/rtac instead, which would wrongly lead to | |
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 111 | failing if there are premices to the shown goal. | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 112 | |
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 113 | given: | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 114 | sol : Thm.thm = [| Ai... |] ==> Ci | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 115 | th : Thm.thm = | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 116 | [| ... [| Ai... |] ==> Ci ... |] ==> G | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 117 | results in: | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 118 | [| ... [| Ai-1... |] ==> Ci-1 | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 119 | [| Ai+1... |] ==> Ci+1 ... | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 120 | |] ==> G | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 121 | i.e. solves some subgoal of th that is identical to sol. | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 122 | *) | 
| 15481 | 123 | fun solve_with sol th = | 
| 124 | let fun solvei 0 = Seq.empty | |
| 125 | | solvei i = | |
| 126 | Seq.append (bicompose false (false,sol,0) i th, | |
| 127 | solvei (i - 1)) | |
| 128 | in | |
| 129 | solvei (Thm.nprems_of th) | |
| 130 | end; | |
| 131 | ||
| 132 | ||
| 133 | (* Given ctertmify function, (string,type) pairs capturing the free | |
| 134 | vars that need to be allified in the assumption, and a theorem with | |
| 135 | assumptions possibly containing the free vars, then we give back the | |
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 136 | assumptions allified as hidden hyps. | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 137 | |
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 138 | Given: x | 
| 15481 | 139 | th: A vs ==> B vs | 
| 140 | Results in: "B vs" [!!x. A x] | |
| 141 | *) | |
| 142 | fun allify_conditions ctermify Ts th = | |
| 143 | let | |
| 144 | val premts = Thm.prems_of th; | |
| 145 | ||
| 146 | fun allify_prem_var (vt as (n,ty),t) = | |
| 147 | (Term.all ty) $ (Abs(n,ty,Term.abstract_over (Free vt, t))) | |
| 148 | ||
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 149 | fun allify_prem Ts p = foldr allify_prem_var p Ts | 
| 15481 | 150 | |
| 151 | val cTs = map (ctermify o Free) Ts | |
| 152 | val cterm_asms = map (ctermify o allify_prem Ts) premts | |
| 153 | val allifyied_asm_thms = map (Drule.forall_elim_list cTs o Thm.assume) cterm_asms | |
| 154 | in | |
| 15570 | 155 | (Library.foldl (fn (x,y) => y COMP x) (th, allifyied_asm_thms), cterm_asms) | 
| 15481 | 156 | end; | 
| 157 | ||
| 158 | fun allify_conditions' Ts th = | |
| 159 | allify_conditions (Thm.cterm_of (Thm.sign_of_thm th)) Ts th; | |
| 160 | ||
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 161 | (* allify types *) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 162 | fun allify_typ ts ty = | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 163 | let | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 164 | fun trec (x as (TFree (s,srt))) = | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 165 | (case Library.find_first (fn (s2,srt2) => s = s2) ts | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 166 | of NONE => x | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 167 | | SOME (s2,_) => TVar ((s,0),srt)) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 168 | (* Maybe add in check here for bad sorts? | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 169 | if srt = srt2 then TVar ((s,0),srt) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 170 |                else raise  ("thaw_typ", ts, ty) *)
 | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 171 | | trec (Type (s,typs)) = Type (s, map trec typs) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 172 | | trec (v as TVar _) = v; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 173 | in trec ty end; | 
| 15481 | 174 | |
| 15924 | 175 | (* implicit types and term *) | 
| 176 | fun allify_term_typs ty = Term.map_term_types (allify_typ ty); | |
| 177 | ||
| 15928 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 178 | (* allified version of term, given frees to allify over. Note that we | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 179 | only allify over the types on the given allified cterm, we can't do | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 180 | this for the theorem as we are not allowed type-vars in the hyp. *) | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 181 | fun assume_allified sgn (tyvs,vs) t = | 
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 182 | let | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 183 | fun allify_var (vt as (n,ty),t) = | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 184 | (Term.all ty) $ (Abs(n,ty,Term.abstract_over (Free vt, t))) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 185 | fun allify Ts p = List.foldr allify_var p Ts | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 186 | |
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 187 | val ctermify = Thm.cterm_of sgn; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 188 | val cvars = map (fn (n,ty) => ctermify (Var ((n,0),ty))) vs | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 189 | val allified_term = t |> allify vs; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 190 | val ct = ctermify allified_term; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 191 | val typ_allified_ct = ctermify (allify_term_typs tyvs allified_term); | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 192 | in (typ_allified_ct, | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 193 | Drule.forall_elim_vars 0 (Thm.assume ct)) end; | 
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 194 | |
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 195 | |
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 196 | (* change type-vars to fresh type frees *) | 
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 197 | fun fix_tvars_to_tfrees_in_terms names ts = | 
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 198 | let | 
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 199 | val tfree_names = map fst (List.foldr Term.add_term_tfrees [] ts); | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 200 | val tvars = List.foldr Term.add_term_tvars [] ts; | 
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 201 | val (names',renamings) = | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 202 | List.foldr (fn (tv as ((n,i),s),(Ns,Rs)) => | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 203 | let val n2 = Term.variant Ns n in | 
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 204 | (n2::Ns, (tv, (n2,s))::Rs) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 205 | end) (tfree_names @ names,[]) tvars; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 206 | in renamings end; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 207 | fun fix_tvars_to_tfrees th = | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 208 | let | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 209 | val sign = Thm.sign_of_thm th; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 210 | val ctypify = Thm.ctyp_of sign; | 
| 15959 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 211 | val tpairs = Thm.terms_of_tpairs (Thm.tpairs_of th); | 
| 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 212 | val renamings = fix_tvars_to_tfrees_in_terms | 
| 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 213 | [] ((Thm.prop_of th) :: tpairs); | 
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 214 | val crenamings = | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 215 | map (fn (v,f) => (ctypify (TVar v), ctypify (TFree f))) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 216 | renamings; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 217 | val fixedfrees = map snd crenamings; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 218 | in (fixedfrees, Thm.instantiate (crenamings, []) th) end; | 
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 219 | |
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 220 | |
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 221 | (* change type-free's to type-vars *) | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 222 | fun unfix_tfrees ns th = | 
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 223 | let | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 224 | val varfiytfrees = (map (fn x => Term.dest_TFree (Thm.typ_of x)) ns) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 225 | val skiptfrees = Term.add_term_tfrees (Thm.prop_of th,[]) \\ varfiytfrees; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 226 | in fst (Thm.varifyT' skiptfrees th) end; | 
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 227 | |
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 228 | (* change schematic/meta vars to fresh free vars *) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 229 | fun fix_vars_to_frees_in_terms names ts = | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 230 | let | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 231 | val vars = map Term.dest_Var (List.foldr Term.add_term_vars [] ts); | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 232 | val Ns = List.foldr Term.add_term_names names ts; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 233 | val (_,renamings) = | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 234 | Library.foldl (fn ((Ns,Rs),v as ((n,i),ty)) => | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 235 | let val n2 = Term.variant Ns n in | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 236 | (n2 :: Ns, (v, (n2,ty)) :: Rs) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 237 | end) ((Ns,[]), vars); | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 238 | in renamings end; | 
| 15481 | 239 | fun fix_vars_to_frees th = | 
| 240 | let | |
| 241 | val ctermify = Thm.cterm_of (Thm.sign_of_thm th) | |
| 15959 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 242 | val tpairs = Thm.terms_of_tpairs (Thm.tpairs_of th); | 
| 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 243 | val renamings = fix_vars_to_frees_in_terms | 
| 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 244 | [] ([Thm.prop_of th] @ tpairs); | 
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 245 | val crenamings = | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 246 | map (fn (v,f) => (ctermify (Var v), ctermify (Free f))) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 247 | renamings; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 248 | val fixedfrees = map snd crenamings; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 249 | in (fixedfrees, Thm.instantiate ([], crenamings) th) end; | 
| 15481 | 250 | |
| 15928 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 251 | fun fix_tvars_upto_idx ix th = | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 252 | let | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 253 | val sgn = Thm.sign_of_thm th; | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 254 | val ctypify = Thm.ctyp_of sgn | 
| 15959 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 255 | val tpairs = Thm.terms_of_tpairs (Thm.tpairs_of th); | 
| 15928 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 256 | val prop = (Thm.prop_of th); | 
| 15959 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 257 | val tvars = List.foldr Term.add_term_tvars [] (prop :: tpairs); | 
| 15928 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 258 | val ctyfixes = | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 259 | Library.mapfilter | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 260 | (fn (v as ((s,i),ty)) => | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 261 | if i <= ix then SOME (ctypify (TVar v), ctypify (TFree (s,ty))) | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 262 | else NONE) tvars; | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 263 | in Thm.instantiate (ctyfixes, []) th end; | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 264 | fun fix_vars_upto_idx ix th = | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 265 | let | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 266 | val sgn = Thm.sign_of_thm th; | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 267 | val ctermify = Thm.cterm_of sgn | 
| 15959 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 268 | val tpairs = Thm.terms_of_tpairs (Thm.tpairs_of th); | 
| 15928 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 269 | val prop = (Thm.prop_of th); | 
| 15959 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 270 | val vars = map Term.dest_Var (List.foldr Term.add_term_vars | 
| 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 271 | [] (prop :: tpairs)); | 
| 15928 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 272 | val cfixes = | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 273 | Library.mapfilter | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 274 | (fn (v as ((s,i),ty)) => | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 275 | if i <= ix then SOME (ctermify (Var v), ctermify (Free (s,ty))) | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 276 | else NONE) vars; | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 277 | in Thm.instantiate ([], cfixes) th end; | 
| 
66b165ee016c
lucas - made clean unify smash unifiers so that when we get flex-flex constraints subst does not barf. Also added fix_vars_upto_idx to IsaND.
 dixon parents: 
15924diff
changeset | 278 | |
| 15481 | 279 | |
| 280 | (* make free vars into schematic vars with index zero *) | |
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 281 | fun unfix_frees frees = | 
| 15481 | 282 | apply (map (K (Drule.forall_elim_var 0)) frees) | 
| 283 | o Drule.forall_intr_list frees; | |
| 284 | ||
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 285 | (* fix term and type variables *) | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 286 | fun fix_vars_and_tvars th = | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 287 | let val (tvars, th') = fix_tvars_to_tfrees th | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 288 | val (vars, th'') = fix_vars_to_frees th' | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 289 | in ((vars, tvars), th'') end; | 
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 290 | |
| 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 291 | (* implicit Thm.thm argument *) | 
| 15959 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 292 | (* assumes: vars may contain fixed versions of the frees *) | 
| 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 293 | (* THINK: what if vs already has types varified? *) | 
| 15854 
1ae0a47dcccd
lucas - improved comments, clarified function behaviour, added missing functions for working with meta variables.
 dixon parents: 
15574diff
changeset | 294 | fun unfix_frees_and_tfrees (vs,tvs) = | 
| 15959 
366d39e95d3c
lucas - fixed bug with uninstantiated type contexts in eqsubst and added the automatic removal of duplicate subgoals (when there are no flex-flex constraints)
 dixon parents: 
15928diff
changeset | 295 | (unfix_tfrees tvs o unfix_frees vs); | 
| 15481 | 296 | |
| 297 | (* datatype to capture an exported result, ie a fix or assume. *) | |
| 298 | datatype export = | |
| 299 |          export of {fixes : Thm.cterm list, (* fixed vars *)
 | |
| 300 | assumes : Thm.cterm list, (* hidden hyps/assumed prems *) | |
| 301 | sgid : int, | |
| 302 | gth : Thm.thm}; (* subgoal/goalthm *) | |
| 303 | ||
| 304 | fun fixes_of_exp (export rep) = #fixes rep; | |
| 305 | ||
| 306 | (* export the result of the new goal thm, ie if we reduced teh | |
| 307 | subgoal, then we get a new reduced subtgoal with the old | |
| 308 | all-quantified variables *) | |
| 309 | local | |
| 310 | ||
| 311 | (* allify puts in a meta level univ quantifier for a free variavble *) | |
| 312 | fun allify_term (v, t) = | |
| 313 | let val vt = #t (Thm.rep_cterm v) | |
| 314 | val (n,ty) = Term.dest_Free vt | |
| 315 | in (Term.all ty) $ (Abs(n,ty,Term.abstract_over (vt, t))) end; | |
| 316 | ||
| 317 | fun allify_for_sg_term ctermify vs t = | |
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 318 | let val t_alls = foldr allify_term t vs; | 
| 15481 | 319 | val ct_alls = ctermify t_alls; | 
| 320 | in | |
| 321 | (ct_alls, Drule.forall_elim_list vs (Thm.assume ct_alls)) | |
| 322 | end; | |
| 323 | (* lookup type of a free var name from a list *) | |
| 324 | fun lookupfree vs vn = | |
| 325 | case Library.find_first (fn (n,ty) => n = vn) vs of | |
| 15531 | 326 |       NONE => raise ERROR_MESSAGE ("prepare_goal_export:lookupfree: " 
 | 
| 15481 | 327 | ^ vn ^ " does not occur in the term") | 
| 15531 | 328 | | SOME x => x; | 
| 15481 | 329 | in | 
| 330 | fun export_back (export {fixes = vs, assumes = hprems, 
 | |
| 331 | sgid = i, gth = gth}) newth = | |
| 332 | let | |
| 333 | val sgn = Thm.sign_of_thm newth; | |
| 334 | val ctermify = Thm.cterm_of sgn; | |
| 335 | ||
| 336 | val sgs = prems_of newth; | |
| 337 | val (sgallcts, sgthms) = | |
| 338 | Library.split_list (map (allify_for_sg_term ctermify vs) sgs); | |
| 339 | val minimal_newth = | |
| 15570 | 340 | (Library.foldl (fn ( newth', sgthm) => | 
| 15481 | 341 | Drule.compose_single (sgthm, 1, newth')) | 
| 342 | (newth, sgthms)); | |
| 343 | val allified_newth = | |
| 344 | minimal_newth | |
| 345 | |> Drule.implies_intr_list hprems | |
| 346 | |> Drule.forall_intr_list vs | |
| 347 | ||
| 348 | val newth' = Drule.implies_intr_list sgallcts allified_newth | |
| 349 | in | |
| 350 | bicompose false (false, newth', (length sgallcts)) i gth | |
| 351 | end; | |
| 352 | ||
| 353 | (* | |
| 354 | Given "vs" : names of free variables to abstract over, | |
| 355 | Given cterms : premices to abstract over (P1... Pn) in terms of vs, | |
| 356 | Given a thm of the form: | |
| 357 | P1 vs; ...; Pn vs ==> Goal(C vs) | |
| 358 | ||
| 359 | Gives back: | |
| 360 | (n, length of given cterms which have been allified | |
| 361 | [| !! vs. P1 vs; !! vs. Pn vs |] ==> !! C vs) the allified thm | |
| 362 | *) | |
| 363 | (* note: C may contain further premices etc | |
| 364 | Note that cterms is the assumed facts, ie prems of "P1" that are | |
| 365 | reintroduced in allified form. | |
| 366 | *) | |
| 367 | fun prepare_goal_export (vs, cterms) th = | |
| 368 | let | |
| 369 | val sgn = Thm.sign_of_thm th; | |
| 370 | val ctermify = Thm.cterm_of sgn; | |
| 371 | ||
| 372 | val allfrees = map Term.dest_Free (Term.term_frees (Thm.prop_of th)) | |
| 373 | val cfrees = map (ctermify o Free o lookupfree allfrees) vs | |
| 374 | ||
| 375 | val sgs = prems_of th; | |
| 376 | val (sgallcts, sgthms) = | |
| 377 | Library.split_list (map (allify_for_sg_term ctermify cfrees) sgs); | |
| 378 | ||
| 379 | val minimal_th = | |
| 15570 | 380 | (Library.foldl (fn ( th', sgthm) => | 
| 15481 | 381 | Drule.compose_single (sgthm, 1, th')) | 
| 382 | (th, sgthms)) RS Drule.rev_triv_goal; | |
| 383 | ||
| 384 | val allified_th = | |
| 385 | minimal_th | |
| 386 | |> Drule.implies_intr_list cterms | |
| 387 | |> Drule.forall_intr_list cfrees | |
| 388 | ||
| 389 | val th' = Drule.implies_intr_list sgallcts allified_th | |
| 390 | in | |
| 391 | ((length sgallcts), th') | |
| 392 | end; | |
| 393 | ||
| 394 | end; | |
| 395 | ||
| 396 | ||
| 397 | (* exporting function that takes a solution to the fixed/assumed goal, | |
| 398 | and uses this to solve the subgoal in the main theorem *) | |
| 399 | fun export_solution (export {fixes = cfvs, assumes = hcprems,
 | |
| 400 | sgid = i, gth = gth}) solth = | |
| 401 | let | |
| 402 | val solth' = | |
| 403 | solth |> Drule.implies_intr_list hcprems | |
| 404 | |> Drule.forall_intr_list cfvs | |
| 405 | in Drule.compose_single (solth', i, gth) end; | |
| 406 | ||
| 15574 
b1d1b5bfc464
Removed practically all references to Library.foldr.
 skalberg parents: 
15570diff
changeset | 407 | fun export_solutions (xs,th) = foldr (uncurry export_solution) th xs; | 
| 15481 | 408 | |
| 409 | ||
| 410 | (* fix parameters of a subgoal "i", as free variables, and create an | |
| 411 | exporting function that will use the result of this proved goal to | |
| 412 | show the goal in the original theorem. | |
| 413 | ||
| 414 | Note, an advantage of this over Isar is that it supports instantiation | |
| 415 | of unkowns in the earlier theorem, ie we can do instantiation of meta | |
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 416 | vars! | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 417 | |
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 418 | avoids constant, free and vars names. | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 419 | |
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 420 | loosely corresponds to: | 
| 15481 | 421 | Given "[| SG0; ... !! x. As ==> SGi x; ... SGm |] ==> G" : thm | 
| 422 | Result: | |
| 423 |   ("(As ==> SGi x') ==> (As ==> SGi x')" : thm, 
 | |
| 424 | expf : | |
| 425 |      ("As ==> SGi x'" : thm) -> 
 | |
| 426 |      ("[| SG0; ... SGi-1; SGi+1; ... SGm |] ==> G") : thm)
 | |
| 427 | *) | |
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 428 | fun fix_alls_in_term alledt = | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 429 | let | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 430 | val t = Term.strip_all_body alledt; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 431 | val alls = rev (Term.strip_all_vars alledt); | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 432 | val varnames = map (fst o fst o Term.dest_Var) (Term.term_vars t) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 433 | val names = Term.add_term_names (t,varnames); | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 434 | val fvs = map Free | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 435 | ((Term.variantlist (map fst alls, names)) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 436 | ~~ (map snd alls)); | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 437 | in ((subst_bounds (fvs,t)), fvs) end; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 438 | |
| 15481 | 439 | fun fix_alls_term i t = | 
| 440 | let | |
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 441 | val varnames = map (fst o fst o Term.dest_Var) (Term.term_vars t) | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 442 | val names = Term.add_term_names (t,varnames); | 
| 15481 | 443 | val gt = Logic.get_goal t i; | 
| 444 | val body = Term.strip_all_body gt; | |
| 445 | val alls = rev (Term.strip_all_vars gt); | |
| 446 | val fvs = map Free | |
| 447 | ((Term.variantlist (map fst alls, names)) | |
| 448 | ~~ (map snd alls)); | |
| 449 | in ((subst_bounds (fvs,body)), fvs) end; | |
| 450 | ||
| 451 | fun fix_alls_cterm i th = | |
| 452 | let | |
| 453 | val ctermify = Thm.cterm_of (Thm.sign_of_thm th); | |
| 454 | val (fixedbody, fvs) = fix_alls_term i (Thm.prop_of th); | |
| 455 | val cfvs = rev (map ctermify fvs); | |
| 456 | val ct_body = ctermify fixedbody | |
| 457 | in | |
| 458 | (ct_body, cfvs) | |
| 459 | end; | |
| 460 | ||
| 461 | fun fix_alls' i = | |
| 462 | (apfst Thm.trivial) o (fix_alls_cterm i); | |
| 463 | ||
| 464 | ||
| 465 | (* hide other goals *) | |
| 466 | (* note the export goal is rotated by (i - 1) and will have to be | |
| 467 | unrotated to get backto the originial position(s) *) | |
| 468 | fun hide_other_goals th = | |
| 469 | let | |
| 470 | (* tl beacuse fst sg is the goal we are interested in *) | |
| 471 | val cprems = tl (Drule.cprems_of th) | |
| 472 | val aprems = map Thm.assume cprems | |
| 473 | in | |
| 474 | (Drule.implies_elim_list (Drule.rotate_prems 1 th) aprems, | |
| 475 | cprems) | |
| 476 | end; | |
| 477 | ||
| 478 | (* a nicer version of the above that leaves only a single subgoal (the | |
| 479 | other subgoals are hidden hyps, that the exporter suffles about) | |
| 480 | namely the subgoal that we were trying to solve. *) | |
| 481 | (* loosely corresponds to: | |
| 482 | Given "[| SG0; ... !! x. As ==> SGi x; ... SGm |] ==> G" : thm | |
| 483 | Result: | |
| 484 |   ("(As ==> SGi x') ==> SGi x'" : thm, 
 | |
| 485 | expf : | |
| 486 |      ("SGi x'" : thm) -> 
 | |
| 487 |      ("[| SG0; ... SGi-1; SGi+1; ... SGm |] ==> G") : thm)
 | |
| 488 | *) | |
| 489 | fun fix_alls i th = | |
| 490 | let | |
| 491 | val (fixed_gth, fixedvars) = fix_alls' i th | |
| 492 | val (sml_gth, othergoals) = hide_other_goals fixed_gth | |
| 493 | in | |
| 494 |       (sml_gth, export {fixes = fixedvars, 
 | |
| 495 | assumes = othergoals, | |
| 496 | sgid = i, gth = th}) | |
| 497 | end; | |
| 498 | ||
| 499 | ||
| 500 | (* assume the premises of subgoal "i", this gives back a list of | |
| 501 | assumed theorems that are the premices of subgoal i, it also gives | |
| 502 | back a new goal thm and an exporter, the new goalthm is as the old | |
| 503 | one, but without the premices, and the exporter will use a proof of | |
| 504 | the new goalthm, possibly using the assumed premices, to shoe the | |
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 505 | orginial goal. | 
| 15481 | 506 | |
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 507 | Note: Dealing with meta vars, need to meta-level-all them in the | 
| 15481 | 508 | shyps, which we can later instantiate with a specific value.... ? | 
| 509 | think about this... maybe need to introduce some new fixed vars and | |
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 510 | then remove them again at the end... like I do with rw_inst. | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 511 | |
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 512 | loosely corresponds to: | 
| 15481 | 513 | Given "[| SG0; ... [| A0; ... An |] ==> SGi; ... SGm |] ==> G" : thm | 
| 514 | Result: | |
| 515 | (["A0" [A0], ... ,"An" [An]] : thm list, -- assumptions | |
| 516 | "SGi ==> SGi" : thm, -- new goal | |
| 517 | "SGi" ["A0" ... "An"] : thm -> -- export function | |
| 518 |     ("[| SG0 ... SGi-1, SGi+1, SGm |] ==> G" : thm) list)
 | |
| 519 | *) | |
| 520 | fun assume_prems i th = | |
| 521 | let | |
| 522 | val t = (prop_of th); | |
| 523 | val gt = Logic.get_goal t i; | |
| 524 | val _ = case Term.strip_all_vars gt of [] => () | |
| 525 | | _ => raise ERROR_MESSAGE "assume_prems: goal has params" | |
| 526 | val body = gt; | |
| 527 | val prems = Logic.strip_imp_prems body; | |
| 528 | val concl = Logic.strip_imp_concl body; | |
| 529 | ||
| 530 | val sgn = Thm.sign_of_thm th; | |
| 531 | val ctermify = Thm.cterm_of sgn; | |
| 532 | val cprems = map ctermify prems; | |
| 533 | val aprems = map Thm.assume cprems; | |
| 534 | val gthi = Thm.trivial (ctermify concl); | |
| 535 | ||
| 536 | (* fun explortf thi = | |
| 537 | Drule.compose (Drule.implies_intr_list cprems thi, | |
| 538 | i, th) *) | |
| 539 | in | |
| 540 | (aprems, gthi, cprems) | |
| 541 | end; | |
| 542 | ||
| 543 | ||
| 544 | (* first fix the variables, then assume the assumptions *) | |
| 545 | (* loosely corresponds to: | |
| 546 | Given | |
| 547 | "[| SG0; ... | |
| 548 | !! xs. [| A0 xs; ... An xs |] ==> SGi xs; | |
| 549 | ... SGm |] ==> G" : thm | |
| 550 | Result: | |
| 551 | (["A0 xs'" [A0 xs'], ... ,"An xs'" [An xs']] : thm list, -- assumptions | |
| 552 | "SGi xs' ==> SGi xs'" : thm, -- new goal | |
| 553 | "SGi xs'" ["A0 xs'" ... "An xs'"] : thm -> -- export function | |
| 554 |     ("[| SG0 ... SGi-1, SGi+1, SGm |] ==> G" : thm) list)
 | |
| 555 | *) | |
| 556 | ||
| 557 | (* Note: the fix_alls actually pulls through all the assumptions which | |
| 558 | means that the second export is not needed. *) | |
| 559 | fun fixes_and_assumes i th = | |
| 560 | let | |
| 561 | val (fixgth, exp1) = fix_alls i th | |
| 562 | val (assumps, goalth, _) = assume_prems 1 fixgth | |
| 563 | in | |
| 564 | (assumps, goalth, exp1) | |
| 565 | end; | |
| 566 | ||
| 567 | ||
| 568 | (* Fixme: allow different order of subgoals given to expf *) | |
| 569 | (* make each subgoal into a separate thm that needs to be proved *) | |
| 570 | (* loosely corresponds to: | |
| 571 | Given | |
| 572 | "[| SG0; ... SGm |] ==> G" : thm | |
| 573 | Result: | |
| 574 | (["SG0 ==> SG0", ... ,"SGm ==> SGm"] : thm list, -- goals | |
| 575 | ["SG0", ..., "SGm"] : thm list -> -- export function | |
| 576 | "G" : thm) | |
| 577 | *) | |
| 578 | fun subgoal_thms th = | |
| 579 | let | |
| 580 | val t = (prop_of th); | |
| 581 | ||
| 582 | val prems = Logic.strip_imp_prems t; | |
| 583 | ||
| 584 | val sgn = Thm.sign_of_thm th; | |
| 585 | val ctermify = Thm.cterm_of sgn; | |
| 586 | ||
| 587 | val aprems = map (Thm.trivial o ctermify) prems; | |
| 588 | ||
| 589 | fun explortf premths = | |
| 590 | Drule.implies_elim_list th premths | |
| 591 | in | |
| 592 | (aprems, explortf) | |
| 593 | end; | |
| 594 | ||
| 595 | ||
| 596 | (* make all the premices of a theorem hidden, and provide an unhide | |
| 597 | function, that will bring them back out at a later point. This is | |
| 598 | useful if you want to get back these premices, after having used the | |
| 599 | theorem with the premices hidden *) | |
| 600 | (* loosely corresponds to: | |
| 601 | Given "As ==> G" : thm | |
| 602 | Result: ("G [As]" : thm, 
 | |
| 603 | "G [As]" : thm -> "As ==> G" : thm | |
| 604 | *) | |
| 605 | fun hide_prems th = | |
| 606 | let | |
| 15915 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 607 | val cprems = Drule.cprems_of th; | 
| 
b0e8b37642a4
lucas - improved interface to isand.ML and cleaned up clean-unification code, and added some better comments.
 dixon parents: 
15854diff
changeset | 608 | val aprems = map Thm.assume cprems; | 
| 15481 | 609 | (* val unhidef = Drule.implies_intr_list cprems; *) | 
| 610 | in | |
| 611 | (Drule.implies_elim_list th aprems, cprems) | |
| 612 | end; | |
| 613 | ||
| 614 | ||
| 615 | ||
| 616 | ||
| 617 | (* Fixme: allow different order of subgoals in exportf *) | |
| 618 | (* as above, but also fix all parameters in all subgoals, and uses | |
| 619 | fix_alls, not fix_alls', ie doesn't leave extra asumptions as apparent | |
| 620 | subgoals. *) | |
| 621 | (* loosely corresponds to: | |
| 622 | Given | |
| 623 | "[| !! x0s. A0s x0s ==> SG0 x0s; | |
| 624 | ...; !! xms. Ams xms ==> SGm xms|] ==> G" : thm | |
| 625 | Result: | |
| 626 | (["(A0s x0s' ==> SG0 x0s') ==> SG0 x0s'", | |
| 627 | ... ,"(Ams xms' ==> SGm xms') ==> SGm xms'"] : thm list, -- goals | |
| 628 | ["SG0 x0s'", ..., "SGm xms'"] : thm list -> -- export function | |
| 629 | "G" : thm) | |
| 630 | *) | |
| 631 | (* requires being given solutions! *) | |
| 632 | fun fixed_subgoal_thms th = | |
| 633 | let | |
| 634 | val (subgoals, expf) = subgoal_thms th; | |
| 635 | (* fun export_sg (th, exp) = exp th; *) | |
| 636 | fun export_sgs expfs solthms = | |
| 637 | expf (map2 (op |>) (solthms, expfs)); | |
| 638 | (* expf (map export_sg (ths ~~ expfs)); *) | |
| 639 | in | |
| 640 | apsnd export_sgs (Library.split_list (map (apsnd export_solution o | |
| 641 | fix_alls 1) subgoals)) | |
| 642 | end; | |
| 643 | ||
| 644 | end; |