| author | haftmann | 
| Tue, 04 May 2010 08:55:39 +0200 | |
| changeset 36634 | f9b43d197d16 | 
| parent 36043 | d149c3886e7e | 
| child 36610 | bafd82950e24 | 
| permissions | -rw-r--r-- | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 1 | (* Title: HOL/Tools/inductive_realizer.ML | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 2 | Author: Stefan Berghofer, TU Muenchen | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 3 | |
| 36043 | 4 | Program extraction from proofs involving inductive predicates: | 
| 29265 
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
 wenzelm parents: 
28965diff
changeset | 5 | Realizers for induction and elimination rules. | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 6 | *) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 7 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 8 | signature INDUCTIVE_REALIZER = | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 9 | sig | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 10 | val add_ind_realizers: string -> string list -> theory -> theory | 
| 18708 | 11 | val setup: theory -> theory | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 12 | end; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 13 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 14 | structure InductiveRealizer : INDUCTIVE_REALIZER = | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 15 | struct | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 16 | |
| 33671 | 17 | (* FIXME: Local_Theory.note should return theorems with proper names! *) (* FIXME ?? *) | 
| 22606 
962f824c2df9
- Tried to make name_of_thm more robust against changes of the
 berghofe parents: 
22596diff
changeset | 18 | fun name_of_thm thm = | 
| 28800 | 19 | (case Proofterm.fold_proof_atoms false (fn PThm (_, ((name, _, _), _)) => cons name | _ => I) | 
| 28814 | 20 | [Thm.proof_of thm] [] of | 
| 28800 | 21 | [name] => name | 
| 32091 
30e2ffbba718
proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
 wenzelm parents: 
31986diff
changeset | 22 |   | _ => error ("name_of_thm: bad proof of theorem\n" ^ Display.string_of_thm_without_context thm));
 | 
| 22271 | 23 | |
| 35364 | 24 | val all_simps = map (symmetric o mk_meta_eq) @{thms HOL.all_simps};
 | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 25 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 26 | fun prf_of thm = | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
26535diff
changeset | 27 | let | 
| 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
26535diff
changeset | 28 | val thy = Thm.theory_of_thm thm; | 
| 28814 | 29 | val thm' = Reconstruct.reconstruct_proof thy (Thm.prop_of thm) (Thm.proof_of thm); | 
| 26626 
c6231d64d264
rep_cterm/rep_thm: no longer dereference theory_ref;
 wenzelm parents: 
26535diff
changeset | 30 |   in Reconstruct.expand_proof thy [("", NONE)] thm' end; (* FIXME *)
 | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 31 | |
| 27330 | 32 | fun forall_intr_prf t prf = | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 33 | let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p) | 
| 15531 | 34 | in Abst (a, SOME T, Proofterm.prf_abstract_over t prf) end; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 35 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 36 | fun subsets [] = [[]] | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 37 | | subsets (x::xs) = | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 38 | let val ys = subsets xs | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 39 | in ys @ map (cons x) ys end; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 40 | |
| 22271 | 41 | val pred_of = fst o dest_Const o head_of; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 42 | |
| 22271 | 43 | fun strip_all' used names (Const ("all", _) $ Abs (s, T, t)) =
 | 
| 44 | let val (s', names') = (case names of | |
| 45 | [] => (Name.variant used s, []) | |
| 46 | | name :: names' => (name, names')) | |
| 47 | in strip_all' (s'::used) names' (subst_bound (Free (s', T), t)) end | |
| 48 |   | strip_all' used names ((t as Const ("==>", _) $ P) $ Q) =
 | |
| 49 | t $ strip_all' used names Q | |
| 50 | | strip_all' _ _ t = t; | |
| 51 | ||
| 29281 | 52 | fun strip_all t = strip_all' (Term.add_free_names t []) [] t; | 
| 22271 | 53 | |
| 54 | fun strip_one name (Const ("all", _) $ Abs (s, T, Const ("==>", _) $ P $ Q)) =
 | |
| 55 | (subst_bound (Free (name, T), P), subst_bound (Free (name, T), Q)) | |
| 56 |   | strip_one _ (Const ("==>", _) $ P $ Q) = (P, Q);
 | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 57 | |
| 30190 | 58 | fun relevant_vars prop = List.foldr (fn | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 59 | (Var ((a, i), T), vs) => (case strip_type T of | 
| 35364 | 60 |         (_, Type (s, _)) => if s mem [@{type_name bool}] then (a, T) :: vs else vs
 | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 61 | | _ => vs) | 
| 29265 
5b4247055bd7
moved old add_term_vars, add_term_frees etc. to structure OldTerm;
 wenzelm parents: 
28965diff
changeset | 62 | | (_, vs) => vs) [] (OldTerm.term_vars prop); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 63 | |
| 22271 | 64 | fun dt_of_intrs thy vs nparms intrs = | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 65 | let | 
| 29271 
1d685baea08e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
 wenzelm parents: 
29265diff
changeset | 66 | val iTs = OldTerm.term_tvars (prop_of (hd intrs)); | 
| 22271 | 67 | val (Const (s, _), ts) = strip_comb (HOLogic.dest_Trueprop | 
| 68 | (Logic.strip_imp_concl (prop_of (hd intrs)))); | |
| 33957 | 69 | val params = map dest_Var (take nparms ts); | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 70 | val tname = Binding.name (space_implode "_" (Long_Name.base_name s ^ "T" :: vs)); | 
| 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 71 | fun constr_of_intr intr = (Binding.name (Long_Name.base_name (name_of_thm intr)), | 
| 35845 
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
 wenzelm parents: 
35625diff
changeset | 72 | map (Logic.unvarifyT_global o snd) (subtract (op =) params (rev (Term.add_vars (prop_of intr) []))) @ | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 73 | filter_out (equal Extraction.nullT) (map | 
| 35845 
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
 wenzelm parents: 
35625diff
changeset | 74 | (Logic.unvarifyT_global o Extraction.etype_of thy vs []) (prems_of intr)), | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 75 | NoSyn); | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 76 | in (map (fn a => "'" ^ a) vs @ map (fst o fst) iTs, tname, NoSyn, | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 77 | map constr_of_intr intrs) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 78 | end; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 79 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 80 | fun mk_rlz T = Const ("realizes", [T, HOLogic.boolT] ---> HOLogic.boolT);
 | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 81 | |
| 22271 | 82 | (** turn "P" into "%r x. realizes r (P x)" **) | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 83 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 84 | fun gen_rvar vs (t as Var ((a, 0), T)) = | 
| 22271 | 85 | if body_type T <> HOLogic.boolT then t else | 
| 86 | let | |
| 87 |           val U = TVar (("'" ^ a, 0), HOLogic.typeS)
 | |
| 88 | val Ts = binder_types T; | |
| 89 | val i = length Ts; | |
| 90 | val xs = map (pair "x") Ts; | |
| 91 | val u = list_comb (t, map Bound (i - 1 downto 0)) | |
| 92 | in | |
| 93 | if a mem vs then | |
| 94 |             list_abs (("r", U) :: xs, mk_rlz U $ Bound i $ u)
 | |
| 95 | else list_abs (xs, mk_rlz Extraction.nullT $ Extraction.nullt $ u) | |
| 96 | end | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 97 | | gen_rvar _ t = t; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 98 | |
| 22271 | 99 | fun mk_realizes_eqn n vs nparms intrs = | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 100 | let | 
| 22271 | 101 | val concl = HOLogic.dest_Trueprop (concl_of (hd intrs)); | 
| 29271 
1d685baea08e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
 wenzelm parents: 
29265diff
changeset | 102 | val iTs = OldTerm.term_tvars concl; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 103 | val Tvs = map TVar iTs; | 
| 22271 | 104 | val (h as Const (s, T), us) = strip_comb concl; | 
| 105 | val params = List.take (us, nparms); | |
| 106 | val elTs = List.drop (binder_types T, nparms); | |
| 107 | val predT = elTs ---> HOLogic.boolT; | |
| 108 | val used = map (fst o fst o dest_Var) params; | |
| 109 | val xs = map (Var o apfst (rpair 0)) | |
| 110 | (Name.variant_list used (replicate (length elTs) "x") ~~ elTs); | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 111 | val rT = if n then Extraction.nullT | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 112 | else Type (space_implode "_" (s ^ "T" :: vs), | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 113 |         map (fn a => TVar (("'" ^ a, 0), HOLogic.typeS)) vs @ Tvs);
 | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 114 | val r = if n then Extraction.nullt else Var ((Long_Name.base_name s, 0), rT); | 
| 22271 | 115 | val S = list_comb (h, params @ xs); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 116 | val rvs = relevant_vars S; | 
| 33040 | 117 | val vs' = subtract (op =) vs (map fst rvs); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 118 | val rname = space_implode "_" (s ^ "R" :: vs); | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 119 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 120 | fun mk_Tprem n v = | 
| 17485 | 121 | let val T = (the o AList.lookup (op =) rvs) v | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 122 |       in (Const ("typeof", T --> Type ("Type", [])) $ Var ((v, 0), T),
 | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 123 | Extraction.mk_typ (if n then Extraction.nullT | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 124 |           else TVar (("'" ^ v, 0), HOLogic.typeS)))
 | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 125 | end; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 126 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 127 | val prems = map (mk_Tprem true) vs' @ map (mk_Tprem false) vs; | 
| 22271 | 128 | val ts = map (gen_rvar vs) params; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 129 | val argTs = map fastype_of ts; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 130 | |
| 22271 | 131 |   in ((prems, (Const ("typeof", HOLogic.boolT --> Type ("Type", [])) $ S,
 | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 132 | Extraction.mk_typ rT)), | 
| 22271 | 133 | (prems, (mk_rlz rT $ r $ S, | 
| 134 | if n then list_comb (Const (rname, argTs ---> predT), ts @ xs) | |
| 135 | else list_comb (Const (rname, argTs @ [rT] ---> predT), ts @ [r] @ xs)))) | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 136 | end; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 137 | |
| 22271 | 138 | fun fun_of_prem thy rsets vs params rule ivs intr = | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 139 | let | 
| 22271 | 140 | val ctxt = ProofContext.init thy | 
| 141 | val args = map (Free o apfst fst o dest_Var) ivs; | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 142 | val args' = map (Free o apfst fst) | 
| 33040 | 143 | (subtract (op =) params (Term.add_vars (prop_of intr) [])); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 144 | val rule' = strip_all rule; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 145 | val conclT = Extraction.etype_of thy vs [] (Logic.strip_imp_concl rule'); | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 146 | val used = map (fst o dest_Free) args; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 147 | |
| 29271 
1d685baea08e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
 wenzelm parents: 
29265diff
changeset | 148 | val is_rec = exists_Const (fn (c, _) => member (op =) rsets c); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 149 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 150 |     fun is_meta (Const ("all", _) $ Abs (s, _, P)) = is_meta P
 | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 151 |       | is_meta (Const ("==>", _) $ _ $ Q) = is_meta Q
 | 
| 35364 | 152 |       | is_meta (Const (@{const_name Trueprop}, _) $ t) =
 | 
| 153 | (case head_of t of | |
| 154 | Const (s, _) => can (Inductive.the_inductive ctxt) s | |
| 155 | | _ => true) | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 156 | | is_meta _ = false; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 157 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 158 | fun fun_of ts rts args used (prem :: prems) = | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 159 | let | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 160 | val T = Extraction.etype_of thy vs [] prem; | 
| 20071 
8f3e1ddb50e6
replaced Term.variant(list) by Name.variant(_list);
 wenzelm parents: 
19806diff
changeset | 161 | val [x, r] = Name.variant_list used ["x", "r"] | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 162 | in if T = Extraction.nullT | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 163 | then fun_of ts rts args used prems | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 164 | else if is_rec prem then | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 165 | if is_meta prem then | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 166 | let | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 167 | val prem' :: prems' = prems; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 168 | val U = Extraction.etype_of thy vs [] prem'; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 169 | in if U = Extraction.nullT | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 170 | then fun_of (Free (x, T) :: ts) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 171 | (Free (r, binder_types T ---> HOLogic.unitT) :: rts) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 172 | (Free (x, T) :: args) (x :: r :: used) prems' | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 173 | else fun_of (Free (x, T) :: ts) (Free (r, U) :: rts) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 174 | (Free (r, U) :: Free (x, T) :: args) (x :: r :: used) prems' | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 175 | end | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 176 | else (case strip_type T of | 
| 35364 | 177 |                   (Ts, Type (@{type_name "*"}, [T1, T2])) =>
 | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 178 | let | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 179 | val fx = Free (x, Ts ---> T1); | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 180 | val fr = Free (r, Ts ---> T2); | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 181 | val bs = map Bound (length Ts - 1 downto 0); | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 182 | val t = list_abs (map (pair "z") Ts, | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 183 | HOLogic.mk_prod (list_comb (fx, bs), list_comb (fr, bs))) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 184 | in fun_of (fx :: ts) (fr :: rts) (t::args) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 185 | (x :: r :: used) prems | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 186 | end | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 187 | | (Ts, U) => fun_of (Free (x, T) :: ts) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 188 | (Free (r, binder_types T ---> HOLogic.unitT) :: rts) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 189 | (Free (x, T) :: args) (x :: r :: used) prems) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 190 | else fun_of (Free (x, T) :: ts) rts (Free (x, T) :: args) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 191 | (x :: used) prems | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 192 | end | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 193 | | fun_of ts rts args used [] = | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 194 | let val xs = rev (rts @ ts) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 195 | in if conclT = Extraction.nullT | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 196 | then list_abs_free (map dest_Free xs, HOLogic.unit) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 197 | else list_abs_free (map dest_Free xs, list_comb | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 198 |               (Free ("r" ^ Long_Name.base_name (name_of_thm intr),
 | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 199 | map fastype_of (rev args) ---> conclT), rev args)) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 200 | end | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 201 | |
| 13921 
69c627b6b28d
Fixed problem in add_elim_realizer which caused bound variables to
 berghofe parents: 
13725diff
changeset | 202 | in fun_of args' [] (rev args) used (Logic.strip_imp_prems rule') end; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 203 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 204 | fun indrule_realizer thy induct raw_induct rsets params vs rec_names rss intrs dummies = | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 205 | let | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 206 | val concls = HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of raw_induct)); | 
| 31986 | 207 | val premss = map_filter (fn (s, rs) => if member (op =) rsets s then | 
| 208 | SOME (rs, map (fn (_, r) => nth (prems_of raw_induct) | |
| 209 | (find_index (fn prp => prp = prop_of r) (map prop_of intrs))) rs) else NONE) rss; | |
| 22271 | 210 | val fs = maps (fn ((intrs, prems), dummy) => | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 211 | let | 
| 22271 | 212 | val fs = map (fn (rule, (ivs, intr)) => | 
| 213 | fun_of_prem thy rsets vs params rule ivs intr) (prems ~~ intrs) | |
| 35364 | 214 | in | 
| 215 |         if dummy then Const (@{const_name default},
 | |
| 216 | HOLogic.unitT --> body_type (fastype_of (hd fs))) :: fs | |
| 22271 | 217 | else fs | 
| 218 | end) (premss ~~ dummies); | |
| 16861 | 219 | val frees = fold Term.add_frees fs []; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 220 | val Ts = map fastype_of fs; | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 221 | fun name_of_fn intr = "r" ^ Long_Name.base_name (name_of_thm intr) | 
| 22271 | 222 | in | 
| 223 | fst (fold_map (fn concl => fn names => | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 224 | let val T = Extraction.etype_of thy vs [] concl | 
| 22271 | 225 | in if T = Extraction.nullT then (Extraction.nullt, names) else | 
| 226 | let | |
| 227 |           val Type ("fun", [U, _]) = T;
 | |
| 228 | val a :: names' = names | |
| 32952 | 229 |         in (list_abs_free (("x", U) :: map_filter (fn intr =>
 | 
| 22271 | 230 | Option.map (pair (name_of_fn intr)) | 
| 231 | (AList.lookup (op =) frees (name_of_fn intr))) intrs, | |
| 232 |           list_comb (Const (a, Ts ---> T), fs) $ Free ("x", U)), names')
 | |
| 233 | end | |
| 234 | end) concls rec_names) | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 235 | end; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 236 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 237 | fun add_dummy name dname (x as (_, (vs, s, mfx, cs))) = | 
| 30345 | 238 | if Binding.eq_name (name, s) then (true, (vs, s, mfx, (dname, [HOLogic.unitT], NoSyn) :: cs)) | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 239 | else x; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 240 | |
| 18314 | 241 | fun add_dummies f [] _ thy = | 
| 242 | (([], NONE), thy) | |
| 243 | | add_dummies f dts used thy = | |
| 244 | thy | |
| 245 | |> f (map snd dts) | |
| 30345 | 246 | |-> (fn dtinfo => pair (map fst dts, SOME dtinfo)) | 
| 33968 
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
 haftmann parents: 
33957diff
changeset | 247 | handle Datatype_Aux.Datatype_Empty name' => | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 248 | let | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 249 | val name = Long_Name.base_name name'; | 
| 30345 | 250 | val dname = Name.variant used "Dummy"; | 
| 18314 | 251 | in | 
| 252 | thy | |
| 30345 | 253 | |> add_dummies f (map (add_dummy (Binding.name name) (Binding.name dname)) dts) (dname :: used) | 
| 14888 
99ac3eb0f84e
add_dummies no longer uses transform_error but handles specific
 berghofe parents: 
13928diff
changeset | 254 | end; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 255 | |
| 22271 | 256 | fun mk_realizer thy vs (name, rule, rrule, rlz, rt) = | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 257 | let | 
| 13725 
12404b452034
Changed format of realizers / correctness proofs.
 berghofe parents: 
13710diff
changeset | 258 | val rvs = map fst (relevant_vars (prop_of rule)); | 
| 16861 | 259 | val xs = rev (Term.add_vars (prop_of rule) []); | 
| 13725 
12404b452034
Changed format of realizers / correctness proofs.
 berghofe parents: 
13710diff
changeset | 260 | val vs1 = map Var (filter_out (fn ((a, _), _) => a mem rvs) xs); | 
| 16861 | 261 | val rlzvs = rev (Term.add_vars (prop_of rrule) []); | 
| 17485 | 262 | val vs2 = map (fn (ixn, _) => Var (ixn, (the o AList.lookup (op =) rlzvs) ixn)) xs; | 
| 22271 | 263 | val rs = map Var (subtract (op = o pairself fst) xs rlzvs); | 
| 27330 | 264 | val rlz' = fold_rev Logic.all (vs2 @ rs) (prop_of rrule); | 
| 265 | val rlz'' = fold_rev Logic.all vs2 rlz | |
| 22271 | 266 | in (name, (vs, | 
| 33338 | 267 | if rt = Extraction.nullt then rt else fold_rev lambda vs1 rt, | 
| 22271 | 268 | ProofRewriteRules.un_hhf_proof rlz' rlz'' | 
| 27330 | 269 | (fold_rev forall_intr_prf (vs2 @ rs) (prf_of rrule)))) | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 270 | end; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 271 | |
| 24157 
409cd6eaa7ea
Added renaming function to prevent correctness proof for realizer
 berghofe parents: 
23590diff
changeset | 272 | fun rename tab = map (fn x => the_default x (AList.lookup op = tab x)); | 
| 
409cd6eaa7ea
Added renaming function to prevent correctness proof for realizer
 berghofe parents: 
23590diff
changeset | 273 | |
| 33244 | 274 | fun add_ind_realizer rsets intrs induct raw_induct elims vs thy = | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 275 | let | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 276 | val qualifier = Long_Name.qualifier (name_of_thm induct); | 
| 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 277 | val inducts = PureThy.get_thms thy (Long_Name.qualify qualifier "inducts"); | 
| 29271 
1d685baea08e
moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
 wenzelm parents: 
29265diff
changeset | 278 | val iTs = OldTerm.term_tvars (prop_of (hd intrs)); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 279 | val ar = length vs + length iTs; | 
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31668diff
changeset | 280 | val params = Inductive.params_of raw_induct; | 
| 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31668diff
changeset | 281 | val arities = Inductive.arities_of raw_induct; | 
| 22271 | 282 | val nparms = length params; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 283 | val params' = map dest_Var params; | 
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31668diff
changeset | 284 | val rss = Inductive.partition_rules raw_induct intrs; | 
| 22271 | 285 | val rss' = map (fn (((s, rs), (_, arity)), elim) => | 
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31668diff
changeset | 286 | (s, (Inductive.infer_intro_vars elim arity rs ~~ rs))) | 
| 22790 
e1cff9268177
Moved functions infer_intro_vars, arities_of, params_of, and
 berghofe parents: 
22606diff
changeset | 287 | (rss ~~ arities ~~ elims); | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 288 | val (prfx, _) = split_last (Long_Name.explode (fst (hd rss))); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 289 | val tnames = map (fn s => space_implode "_" (s ^ "T" :: vs)) rsets; | 
| 16123 | 290 | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 291 | val thy1 = thy |> | 
| 24712 
64ed05609568
proper Sign operations instead of Theory aliases;
 wenzelm parents: 
24157diff
changeset | 292 | Sign.root_path |> | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 293 | Sign.add_path (Long_Name.implode prfx); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 294 | val (ty_eqs, rlz_eqs) = split_list | 
| 22271 | 295 | (map (fn (s, rs) => mk_realizes_eqn (not (s mem rsets)) vs nparms rs) rss); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 296 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 297 | val thy1' = thy1 |> | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 298 | Theory.copy |> | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 299 | Sign.add_types (map (fn s => (Binding.name (Long_Name.base_name s), ar, NoSyn)) tnames) |> | 
| 24926 
bcb6b098df11
AxClass.axiomatize: renamed XXX_i to XXX, and XXX to XXX_cmd;
 wenzelm parents: 
24816diff
changeset | 300 | fold (fn s => AxClass.axiomatize_arity | 
| 19510 | 301 | (s, replicate ar HOLogic.typeS, HOLogic.typeS)) tnames |> | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 302 | Extraction.add_typeof_eqns_i ty_eqs; | 
| 32952 | 303 | val dts = map_filter (fn (s, rs) => if s mem rsets then | 
| 22271 | 304 | SOME (dt_of_intrs thy1' vs nparms rs) else NONE) rss; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 305 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 306 | (** datatype representing computational content of inductive set **) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 307 | |
| 31783 
cfbe9609ceb1
add_datatypes does not yield particular rules any longer
 haftmann parents: 
31781diff
changeset | 308 | val ((dummies, some_dt_names), thy2) = | 
| 18008 | 309 | thy1 | 
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31668diff
changeset | 310 | |> add_dummies (Datatype.add_datatype | 
| 32125 | 311 |            { strict = false, quiet = false } (map (Binding.name_of o #2) dts))
 | 
| 18314 | 312 | (map (pair false) dts) [] | 
| 313 | ||> Extraction.add_typeof_eqns_i ty_eqs | |
| 314 | ||> Extraction.add_realizes_eqns_i rlz_eqs; | |
| 31783 
cfbe9609ceb1
add_datatypes does not yield particular rules any longer
 haftmann parents: 
31781diff
changeset | 315 | val dt_names = these some_dt_names; | 
| 31784 | 316 | val case_thms = map (#case_rewrites o Datatype.the_info thy2) dt_names; | 
| 31783 
cfbe9609ceb1
add_datatypes does not yield particular rules any longer
 haftmann parents: 
31781diff
changeset | 317 | val rec_thms = if null dt_names then [] | 
| 31784 | 318 | else (#rec_rewrites o Datatype.the_info thy2) (hd dt_names); | 
| 19046 
bc5c6c9b114e
removed distinct, renamed gen_distinct to distinct;
 wenzelm parents: 
18929diff
changeset | 319 | val rec_names = distinct (op =) (map (fst o dest_Const o head_of o fst o | 
| 31781 
861e675f01e6
add_datatype interface yields type names and less rules
 haftmann parents: 
31723diff
changeset | 320 | HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) rec_thms); | 
| 31458 | 321 | val (constrss, _) = fold_map (fn (s, rs) => fn (recs, dummies) => | 
| 322 | if member (op =) rsets s then | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 323 | let | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 324 | val (d :: dummies') = dummies; | 
| 19473 | 325 | val (recs1, recs2) = chop (length rs) (if d then tl recs else recs) | 
| 31458 | 326 | in (map (head_of o hd o rev o snd o strip_comb o fst o | 
| 327 | HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) recs1, (recs2, dummies')) | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 328 | end | 
| 31458 | 329 | else (replicate (length rs) Extraction.nullt, (recs, dummies))) | 
| 31781 
861e675f01e6
add_datatype interface yields type names and less rules
 haftmann parents: 
31723diff
changeset | 330 | rss (rec_thms, dummies); | 
| 18929 | 331 | val rintrs = map (fn (intr, c) => Envir.eta_contract | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 332 | (Extraction.realizes_of thy2 vs | 
| 22271 | 333 | (if c = Extraction.nullt then c else list_comb (c, map Var (rev | 
| 33040 | 334 | (subtract (op =) params' (Term.add_vars (prop_of intr) []))))) (prop_of intr))) | 
| 32952 | 335 | (maps snd rss ~~ flat constrss); | 
| 30345 | 336 | val (rlzpreds, rlzpreds') = | 
| 337 | rintrs |> map (fn rintr => | |
| 22271 | 338 | let | 
| 30345 | 339 | val Const (s, T) = head_of (HOLogic.dest_Trueprop (Logic.strip_assums_concl rintr)); | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 340 | val s' = Long_Name.base_name s; | 
| 35845 
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
 wenzelm parents: 
35625diff
changeset | 341 | val T' = Logic.unvarifyT_global T; | 
| 30345 | 342 | in (((s', T'), NoSyn), (Const (s, T'), Free (s', T'))) end) | 
| 343 | |> distinct (op = o pairself (#1 o #1)) | |
| 344 | |> map (apfst (apfst (apfst Binding.name))) | |
| 345 | |> split_list; | |
| 346 | ||
| 35845 
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
 wenzelm parents: 
35625diff
changeset | 347 | val rlzparams = map (fn Var ((s, _), T) => (s, Logic.unvarifyT_global T)) | 
| 22271 | 348 | (List.take (snd (strip_comb | 
| 349 | (HOLogic.dest_Trueprop (Logic.strip_assums_concl (hd rintrs)))), nparms)); | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 350 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 351 | (** realizability predicate **) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 352 | |
| 22271 | 353 | val (ind_info, thy3') = thy2 |> | 
| 33726 
0878aecbf119
eliminated slightly odd name space grouping -- now managed by Isar toplevel;
 wenzelm parents: 
33671diff
changeset | 354 | Inductive.add_inductive_global | 
| 33669 | 355 |         {quiet_mode = false, verbose = false, alt_name = Binding.empty, coind = false,
 | 
| 356 | no_elim = false, no_ind = false, skip_mono = false, fork_mono = false} | |
| 22271 | 357 | rlzpreds rlzparams (map (fn (rintr, intr) => | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 358 | ((Binding.name (Long_Name.base_name (name_of_thm intr)), []), | 
| 35845 
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
 wenzelm parents: 
35625diff
changeset | 359 | subst_atomic rlzpreds' (Logic.unvarify_global rintr))) | 
| 22271 | 360 | (rintrs ~~ maps snd rss)) [] ||> | 
| 30435 
e62d6ecab6ad
explicit Binding.qualified_name -- prevents implicitly qualified bstring;
 wenzelm parents: 
30364diff
changeset | 361 | Sign.root_path; | 
| 26663 | 362 | val thy3 = fold (PureThy.hide_fact false o name_of_thm) (#intrs ind_info) thy3'; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 363 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 364 | (** realizer for induction rule **) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 365 | |
| 31458 | 366 | val Ps = map_filter (fn _ $ M $ P => if pred_of M mem rsets then | 
| 15531 | 367 | SOME (fst (fst (dest_Var (head_of P)))) else NONE) | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 368 | (HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of raw_induct))); | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 369 | |
| 33244 | 370 | fun add_ind_realizer Ps thy = | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 371 | let | 
| 24157 
409cd6eaa7ea
Added renaming function to prevent correctness proof for realizer
 berghofe parents: 
23590diff
changeset | 372 | val vs' = rename (map (pairself (fst o fst o dest_Var)) | 
| 
409cd6eaa7ea
Added renaming function to prevent correctness proof for realizer
 berghofe parents: 
23590diff
changeset | 373 | (params ~~ List.take (snd (strip_comb (HOLogic.dest_Trueprop | 
| 
409cd6eaa7ea
Added renaming function to prevent correctness proof for realizer
 berghofe parents: 
23590diff
changeset | 374 | (hd (prems_of (hd inducts))))), nparms))) vs; | 
| 22271 | 375 | val rs = indrule_realizer thy induct raw_induct rsets params' | 
| 24157 
409cd6eaa7ea
Added renaming function to prevent correctness proof for realizer
 berghofe parents: 
23590diff
changeset | 376 | (vs' @ Ps) rec_names rss' intrs dummies; | 
| 
409cd6eaa7ea
Added renaming function to prevent correctness proof for realizer
 berghofe parents: 
23590diff
changeset | 377 | val rlzs = map (fn (r, ind) => Extraction.realizes_of thy (vs' @ Ps) r | 
| 22271 | 378 | (prop_of ind)) (rs ~~ inducts); | 
| 29281 | 379 | val used = fold Term.add_free_names rlzs []; | 
| 22271 | 380 | val rnames = Name.variant_list used (replicate (length inducts) "r"); | 
| 381 | val rnames' = Name.variant_list | |
| 382 | (used @ rnames) (replicate (length intrs) "s"); | |
| 383 | val rlzs' as (prems, _, _) :: _ = map (fn (rlz, name) => | |
| 384 | let | |
| 35845 
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
 wenzelm parents: 
35625diff
changeset | 385 | val (P, Q) = strip_one name (Logic.unvarify_global rlz); | 
| 22271 | 386 | val Q' = strip_all' [] rnames' Q | 
| 387 | in | |
| 388 | (Logic.strip_imp_prems Q', P, Logic.strip_imp_concl Q') | |
| 389 | end) (rlzs ~~ rnames); | |
| 390 | val concl = HOLogic.mk_Trueprop (foldr1 HOLogic.mk_conj (map | |
| 391 | (fn (_, _ $ P, _ $ Q) => HOLogic.mk_imp (P, Q)) rlzs')); | |
| 31781 
861e675f01e6
add_datatype interface yields type names and less rules
 haftmann parents: 
31723diff
changeset | 392 | val rews = map mk_meta_eq (fst_conv :: snd_conv :: rec_thms); | 
| 26711 | 393 |         val thm = Goal.prove_global thy [] prems concl (fn {prems, ...} => EVERY
 | 
| 22271 | 394 | [rtac (#raw_induct ind_info) 1, | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 395 | rewrite_goals_tac rews, | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 396 | REPEAT ((resolve_tac prems THEN_ALL_NEW EVERY' | 
| 35625 | 397 | [K (rewrite_goals_tac rews), Object_Logic.atomize_prems_tac, | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 398 | DEPTH_SOLVE_1 o FIRST' [atac, etac allE, etac impE]]) 1)]); | 
| 30435 
e62d6ecab6ad
explicit Binding.qualified_name -- prevents implicitly qualified bstring;
 wenzelm parents: 
30364diff
changeset | 399 | val (thm', thy') = PureThy.store_thm (Binding.qualified_name (space_implode "_" | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 400 | (Long_Name.qualify qualifier "induct" :: vs' @ Ps @ ["correctness"])), thm) thy; | 
| 22271 | 401 | val thms = map (fn th => zero_var_indexes (rotate_prems ~1 (th RS mp))) | 
| 33968 
f94fb13ecbb3
modernized structures and tuned headers of datatype package modules; joined former datatype.ML and datatype_rep_proofs.ML
 haftmann parents: 
33957diff
changeset | 402 | (Datatype_Aux.split_conj_thm thm'); | 
| 22271 | 403 | val ([thms'], thy'') = PureThy.add_thmss | 
| 30435 
e62d6ecab6ad
explicit Binding.qualified_name -- prevents implicitly qualified bstring;
 wenzelm parents: 
30364diff
changeset | 404 | [((Binding.qualified_name (space_implode "_" | 
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 405 | (Long_Name.qualify qualifier "inducts" :: vs' @ Ps @ | 
| 29579 | 406 | ["correctness"])), thms), [])] thy'; | 
| 22271 | 407 | val realizers = inducts ~~ thms' ~~ rlzs ~~ rs; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 408 | in | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 409 | Extraction.add_realizers_i | 
| 22271 | 410 | (map (fn (((ind, corr), rlz), r) => | 
| 24157 
409cd6eaa7ea
Added renaming function to prevent correctness proof for realizer
 berghofe parents: 
23590diff
changeset | 411 | mk_realizer thy' (vs' @ Ps) (Thm.get_name ind, ind, corr, rlz, r)) | 
| 22271 | 412 | realizers @ (case realizers of | 
| 413 | [(((ind, corr), rlz), r)] => | |
| 30364 
577edc39b501
moved basic algebra of long names from structure NameSpace to Long_Name;
 wenzelm parents: 
30345diff
changeset | 414 | [mk_realizer thy' (vs' @ Ps) (Long_Name.qualify qualifier "induct", | 
| 22271 | 415 | ind, corr, rlz, r)] | 
| 416 | | _ => [])) thy'' | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 417 | end; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 418 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 419 | (** realizer for elimination rules **) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 420 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 421 | val case_names = map (fst o dest_Const o head_of o fst o HOLogic.dest_eq o | 
| 31781 
861e675f01e6
add_datatype interface yields type names and less rules
 haftmann parents: 
31723diff
changeset | 422 | HOLogic.dest_Trueprop o prop_of o hd) case_thms; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 423 | |
| 13921 
69c627b6b28d
Fixed problem in add_elim_realizer which caused bound variables to
 berghofe parents: 
13725diff
changeset | 424 | fun add_elim_realizer Ps | 
| 
69c627b6b28d
Fixed problem in add_elim_realizer which caused bound variables to
 berghofe parents: 
13725diff
changeset | 425 | (((((elim, elimR), intrs), case_thms), case_name), dummy) thy = | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 426 | let | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 427 | val (prem :: prems) = prems_of elim; | 
| 22271 | 428 | fun reorder1 (p, (_, intr)) = | 
| 33244 | 429 | fold (fn ((s, _), T) => Logic.all (Free (s, T))) | 
| 430 | (subtract (op =) params' (Term.add_vars (prop_of intr) [])) | |
| 431 | (strip_all p); | |
| 22271 | 432 | fun reorder2 ((ivs, intr), i) = | 
| 33040 | 433 | let val fs = subtract (op =) params' (Term.add_vars (prop_of intr) []) | 
| 33244 | 434 | in fold (lambda o Var) fs (list_comb (Bound (i + length ivs), ivs)) end; | 
| 13921 
69c627b6b28d
Fixed problem in add_elim_realizer which caused bound variables to
 berghofe parents: 
13725diff
changeset | 435 | val p = Logic.list_implies | 
| 
69c627b6b28d
Fixed problem in add_elim_realizer which caused bound variables to
 berghofe parents: 
13725diff
changeset | 436 | (map reorder1 (prems ~~ intrs) @ [prem], concl_of elim); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 437 | val T' = Extraction.etype_of thy (vs @ Ps) [] p; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 438 | val T = if dummy then (HOLogic.unitT --> body_type T') --> T' else T'; | 
| 13921 
69c627b6b28d
Fixed problem in add_elim_realizer which caused bound variables to
 berghofe parents: 
13725diff
changeset | 439 | val Ts = map (Extraction.etype_of thy (vs @ Ps) []) (prems_of elim); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 440 | val r = if null Ps then Extraction.nullt | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 441 | else list_abs (map (pair "x") Ts, list_comb (Const (case_name, T), | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 442 | (if dummy then | 
| 35364 | 443 |                [Abs ("x", HOLogic.unitT, Const (@{const_name default}, body_type T))]
 | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 444 | else []) @ | 
| 13921 
69c627b6b28d
Fixed problem in add_elim_realizer which caused bound variables to
 berghofe parents: 
13725diff
changeset | 445 | map reorder2 (intrs ~~ (length prems - 1 downto 0)) @ | 
| 
69c627b6b28d
Fixed problem in add_elim_realizer which caused bound variables to
 berghofe parents: 
13725diff
changeset | 446 | [Bound (length prems)])); | 
| 22271 | 447 | val rlz = Extraction.realizes_of thy (vs @ Ps) r (prop_of elim); | 
| 35845 
e5980f0ad025
renamed varify/unvarify operations to varify_global/unvarify_global to emphasize that these only work in a global situation;
 wenzelm parents: 
35625diff
changeset | 448 | val rlz' = strip_all (Logic.unvarify_global rlz); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 449 | val rews = map mk_meta_eq case_thms; | 
| 22271 | 450 | val thm = Goal.prove_global thy [] | 
| 26711 | 451 |           (Logic.strip_imp_prems rlz') (Logic.strip_imp_concl rlz') (fn {prems, ...} => EVERY
 | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 452 | [cut_facts_tac [hd prems] 1, | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 453 | etac elimR 1, | 
| 22271 | 454 | ALLGOALS (asm_simp_tac HOL_basic_ss), | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 455 | rewrite_goals_tac rews, | 
| 35625 | 456 | REPEAT ((resolve_tac prems THEN_ALL_NEW (Object_Logic.atomize_prems_tac THEN' | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 457 | DEPTH_SOLVE_1 o FIRST' [atac, etac allE, etac impE])) 1)]); | 
| 30435 
e62d6ecab6ad
explicit Binding.qualified_name -- prevents implicitly qualified bstring;
 wenzelm parents: 
30364diff
changeset | 458 | val (thm', thy') = PureThy.store_thm (Binding.qualified_name (space_implode "_" | 
| 29579 | 459 | (name_of_thm elim :: vs @ Ps @ ["correctness"])), thm) thy | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 460 | in | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 461 | Extraction.add_realizers_i | 
| 22271 | 462 | [mk_realizer thy' (vs @ Ps) (name_of_thm elim, elim, thm', rlz, r)] thy' | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 463 | end; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 464 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 465 | (** add realizers to theory **) | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 466 | |
| 33244 | 467 | val thy4 = fold add_ind_realizer (subsets Ps) thy3; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 468 | val thy5 = Extraction.add_realizers_i | 
| 22271 | 469 | (map (mk_realizer thy4 vs) (map (fn (((rule, rrule), rlz), c) => | 
| 470 | (name_of_thm rule, rule, rrule, rlz, | |
| 33040 | 471 | list_comb (c, map Var (subtract (op =) params' (rev (Term.add_vars (prop_of rule) [])))))) | 
| 32952 | 472 | (maps snd rss ~~ #intrs ind_info ~~ rintrs ~~ flat constrss))) thy4; | 
| 473 | val elimps = map_filter (fn ((s, intrs), p) => | |
| 22271 | 474 | if s mem rsets then SOME (p, intrs) else NONE) | 
| 475 | (rss' ~~ (elims ~~ #elims ind_info)); | |
| 33244 | 476 | val thy6 = | 
| 477 | fold (fn p as (((((elim, _), _), _), _), _) => | |
| 478 | add_elim_realizer [] p #> | |
| 479 | add_elim_realizer [fst (fst (dest_Var (HOLogic.dest_Trueprop (concl_of elim))))] p) | |
| 480 | (elimps ~~ case_thms ~~ case_names ~~ dummies) thy5; | |
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 481 | |
| 24712 
64ed05609568
proper Sign operations instead of Theory aliases;
 wenzelm parents: 
24157diff
changeset | 482 | in Sign.restore_naming thy thy6 end; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 483 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 484 | fun add_ind_realizers name rsets thy = | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 485 | let | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 486 |     val (_, {intrs, induct, raw_induct, elims, ...}) =
 | 
| 31723 
f5cafe803b55
discontinued ancient tradition to suffix certain ML module names with "_package"
 haftmann parents: 
31668diff
changeset | 487 | Inductive.the_inductive (ProofContext.init thy) name; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 488 | val vss = sort (int_ord o pairself length) | 
| 22271 | 489 | (subsets (map fst (relevant_vars (concl_of (hd intrs))))) | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 490 | in | 
| 33244 | 491 | fold (add_ind_realizer rsets intrs induct raw_induct elims) vss thy | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 492 | end | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 493 | |
| 20897 | 494 | fun rlz_attrib arg = Thm.declaration_attribute (fn thm => Context.mapping | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 495 | let | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 496 | fun err () = error "ind_realizer: bad rule"; | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 497 | val sets = | 
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 498 | (case HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of thm)) of | 
| 22271 | 499 | [_] => [pred_of (HOLogic.dest_Trueprop (hd (prems_of thm)))] | 
| 500 | | xs => map (pred_of o fst o HOLogic.dest_imp) xs) | |
| 15570 | 501 | handle TERM _ => err () | Empty => err (); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 502 | in | 
| 18728 | 503 | add_ind_realizers (hd sets) | 
| 504 | (case arg of | |
| 15531 | 505 | NONE => sets | SOME NONE => [] | 
| 33040 | 506 | | SOME (SOME sets') => subtract (op =) sets' sets) | 
| 20897 | 507 | end I); | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 508 | |
| 18708 | 509 | val setup = | 
| 30722 
623d4831c8cf
simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
 wenzelm parents: 
30528diff
changeset | 510 |   Attrib.setup @{binding ind_realizer}
 | 
| 
623d4831c8cf
simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
 wenzelm parents: 
30528diff
changeset | 511 | ((Scan.option (Scan.lift (Args.$$$ "irrelevant") |-- | 
| 35402 | 512 | Scan.option (Scan.lift (Args.colon) |-- Scan.repeat1 (Args.const true)))) >> rlz_attrib) | 
| 30722 
623d4831c8cf
simplified attribute and method setup: eliminating bottom-up styles makes it easier to keep things in one place, and also SML/NJ happy;
 wenzelm parents: 
30528diff
changeset | 513 | "add realizers for inductive set"; | 
| 13710 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 514 | |
| 
75bec2c1bfd5
New package for constructing realizers for introduction and elimination
 berghofe parents: diff
changeset | 515 | end; | 
| 15706 | 516 |