New function inst_morphism'.
1 (* Title: Pure/Isar/element.ML
5 Explicit data structures for some Isar language elements, with derived
11 datatype ('typ, 'term) stmt =
12 Shows of ((string * Attrib.src list) * ('term * 'term list) list) list |
13 Obtains of (string * ((string * 'typ option) list * 'term list)) list
14 type statement (*= (string, string) stmt*)
15 type statement_i (*= (typ, term) stmt*)
16 datatype ('typ, 'term, 'fact) ctxt =
17 Fixes of (string * 'typ option * mixfix) list |
18 Constrains of (string * 'typ) list |
19 Assumes of ((string * Attrib.src list) * ('term * 'term list) list) list |
20 Defines of ((string * Attrib.src list) * ('term * 'term list)) list |
21 Notes of string * ((string * Attrib.src list) * ('fact * Attrib.src list) list) list
22 type context (*= (string, string, thmref) ctxt*)
23 type context_i (*= (typ, term, thm list) ctxt*)
24 val facts_map: (('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt) ->
25 ((string * Attrib.src list) * ('fact * Attrib.src list) list) list ->
26 ((string * Attrib.src list) * ('c * Attrib.src list) list) list
27 val map_ctxt: {name: string -> string,
28 var: string * mixfix -> string * mixfix,
29 typ: 'typ -> 'a, term: 'term -> 'b, fact: 'fact -> 'c,
30 attrib: Attrib.src -> Attrib.src} -> ('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt
31 val map_ctxt_attrib: (Attrib.src -> Attrib.src) ->
32 ('typ, 'term, 'fact) ctxt -> ('typ, 'term, 'fact) ctxt
33 val morph_ctxt: morphism -> context_i -> context_i
34 val params_of: context_i -> (string * typ) list
35 val prems_of: context_i -> term list
36 val facts_of: theory -> context_i ->
37 ((string * Attrib.src list) * (thm list * Attrib.src list) list) list
38 val pretty_stmt: Proof.context -> statement_i -> Pretty.T list
39 val pretty_ctxt: Proof.context -> context_i -> Pretty.T list
40 val pretty_statement: Proof.context -> string -> thm -> Pretty.T
42 val map_witness: (term * thm -> term * thm) -> witness -> witness
43 val morph_witness: morphism -> witness -> witness
44 val witness_prop: witness -> term
45 val witness_hyps: witness -> term list
46 val assume_witness: theory -> term -> witness
47 val prove_witness: Proof.context -> term -> tactic -> witness
48 val conclude_witness: witness -> thm
49 val mark_witness: term -> term
50 val make_witness: term -> thm -> witness
51 val dest_witness: witness -> term * thm
52 val transfer_witness: theory -> witness -> witness
53 val refine_witness: Proof.state -> Proof.state Seq.seq
54 val pretty_witness: Proof.context -> witness -> Pretty.T
55 val rename: (string * (string * mixfix option)) list -> string -> string
56 val rename_var: (string * (string * mixfix option)) list -> string * mixfix -> string * mixfix
57 val rename_term: (string * (string * mixfix option)) list -> term -> term
58 val rename_thm: (string * (string * mixfix option)) list -> thm -> thm
59 val rename_morphism: (string * (string * mixfix option)) list -> morphism
60 val instT_type: typ Symtab.table -> typ -> typ
61 val instT_term: typ Symtab.table -> term -> term
62 val instT_thm: theory -> typ Symtab.table -> thm -> thm
63 val instT_morphism: theory -> typ Symtab.table -> morphism
64 val inst_term: typ Symtab.table * term Symtab.table -> term -> term
65 val inst_thm: theory -> typ Symtab.table * term Symtab.table -> thm -> thm
66 val inst_morphism: theory -> typ Symtab.table * term Symtab.table -> morphism
67 val inst_morphism': theory -> typ Symtab.table * term Symtab.table -> typ Symtab.table * term Symtab.table -> morphism
68 val satisfy_thm: witness list -> thm -> thm
69 val satisfy_morphism: witness list -> morphism
70 val satisfy_facts: witness list ->
71 ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
72 ((string * Attrib.src list) * (thm list * Attrib.src list) list) list
73 val generalize_facts: Proof.context -> Proof.context ->
74 ((string * Attrib.src list) * (thm list * Attrib.src list) list) list ->
75 ((string * Attrib.src list) * (thm list * Attrib.src list) list) list
78 structure Element: ELEMENT =
81 (** language elements **)
85 datatype ('typ, 'term) stmt =
86 Shows of ((string * Attrib.src list) * ('term * 'term list) list) list |
87 Obtains of (string * ((string * 'typ option) list * 'term list)) list;
89 type statement = (string, string) stmt;
90 type statement_i = (typ, term) stmt;
95 datatype ('typ, 'term, 'fact) ctxt =
96 Fixes of (string * 'typ option * mixfix) list |
97 Constrains of (string * 'typ) list |
98 Assumes of ((string * Attrib.src list) * ('term * 'term list) list) list |
99 Defines of ((string * Attrib.src list) * ('term * 'term list)) list |
100 Notes of string * ((string * Attrib.src list) * ('fact * Attrib.src list) list) list;
102 type context = (string, string, thmref) ctxt;
103 type context_i = (typ, term, thm list) ctxt;
105 fun facts_map f facts = Notes ("", facts) |> f |> (fn Notes (_, facts') => facts');
107 fun map_ctxt {name, var, typ, term, fact, attrib} =
108 fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) =>
109 let val (x', mx') = var (x, mx) in (x', Option.map typ T, mx') end))
110 | Constrains xs => Constrains (xs |> map (fn (x, T) => (#1 (var (x, NoSyn)), typ T)))
111 | Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) =>
112 ((name a, map attrib atts), propps |> map (fn (t, ps) => (term t, map term ps)))))
113 | Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) =>
114 ((name a, map attrib atts), (term t, map term ps))))
115 | Notes (kind, facts) => Notes (kind, facts |> map (fn ((a, atts), bs) =>
116 ((name a, map attrib atts), bs |> map (fn (ths, btts) => (fact ths, map attrib btts)))));
118 fun map_ctxt_attrib attrib =
119 map_ctxt {name = I, var = I, typ = I, term = I, fact = I, attrib = attrib};
121 fun morph_ctxt phi = map_ctxt
122 {name = Morphism.name phi,
123 var = Morphism.var phi,
124 typ = Morphism.typ phi,
125 term = Morphism.term phi,
126 fact = Morphism.fact phi,
127 attrib = Args.morph_values phi};
130 (* logical content *)
132 fun params_of (Fixes fixes) = fixes |> map
133 (fn (x, SOME T, _) => (x, T)
134 | (x, _, _) => raise TERM ("Untyped context element parameter " ^ quote x, []))
137 fun prems_of (Assumes asms) = maps (map fst o snd) asms
138 | prems_of (Defines defs) = map (fst o snd) defs
141 fun assume thy t = Assumption.assume (Thm.cterm_of thy t);
143 fun facts_of thy (Assumes asms) = map (apsnd (map (fn (t, _) => ([assume thy t], [])))) asms
144 | facts_of thy (Defines defs) = map (apsnd (fn (t, _) => [([assume thy t], [])])) defs
145 | facts_of _ (Notes (_, facts)) = facts
150 (** pretty printing **)
152 fun pretty_items _ _ [] = []
153 | pretty_items keyword sep (x :: ys) =
154 Pretty.block [Pretty.keyword keyword, Pretty.brk 1, x] ::
155 map (fn y => Pretty.block [Pretty.str " ", Pretty.keyword sep, Pretty.brk 1, y]) ys;
157 fun pretty_name_atts ctxt (name, atts) sep =
158 if name = "" andalso null atts then []
160 (Pretty.breaks (Pretty.str name :: Attrib.pretty_attribs ctxt atts @ [Pretty.str sep]))];
165 fun pretty_stmt ctxt =
167 val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt;
168 val prt_term = Pretty.quote o ProofContext.pretty_term ctxt;
169 val prt_terms = separate (Pretty.keyword "and") o map prt_term;
170 val prt_name_atts = pretty_name_atts ctxt;
172 fun prt_show (a, ts) =
173 Pretty.block (Pretty.breaks (prt_name_atts a ":" @ prt_terms (map fst ts)));
175 fun prt_var (x, SOME T) = Pretty.block [Pretty.str (x ^ " ::"), Pretty.brk 1, prt_typ T]
176 | prt_var (x, NONE) = Pretty.str x;
177 val prt_vars = separate (Pretty.keyword "and") o map prt_var;
179 fun prt_obtain (_, ([], ts)) = Pretty.block (Pretty.breaks (prt_terms ts))
180 | prt_obtain (_, (xs, ts)) = Pretty.block (Pretty.breaks
181 (prt_vars xs @ [Pretty.keyword "where"] @ prt_terms ts));
183 fn Shows shows => pretty_items "shows" "and" (map prt_show shows)
184 | Obtains obtains => pretty_items "obtains" "|" (map prt_obtain obtains)
190 fun pretty_ctxt ctxt =
192 val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt;
193 val prt_term = Pretty.quote o ProofContext.pretty_term ctxt;
194 val prt_thm = Pretty.backquote o ProofContext.pretty_thm ctxt;
195 val prt_name_atts = pretty_name_atts ctxt;
197 fun prt_mixfix NoSyn = []
198 | prt_mixfix mx = [Pretty.brk 2, Syntax.pretty_mixfix mx];
200 fun prt_fix (x, SOME T, mx) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 ::
201 prt_typ T :: Pretty.brk 1 :: prt_mixfix mx)
202 | prt_fix (x, NONE, mx) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_mixfix mx);
203 fun prt_constrain (x, T) = prt_fix (x, SOME T, NoSyn);
205 fun prt_asm (a, ts) =
206 Pretty.block (Pretty.breaks (prt_name_atts a ":" @ map (prt_term o fst) ts));
207 fun prt_def (a, (t, _)) =
208 Pretty.block (Pretty.breaks (prt_name_atts a ":" @ [prt_term t]));
210 fun prt_fact (ths, []) = map prt_thm ths
211 | prt_fact (ths, atts) = Pretty.enclose "(" ")"
212 (Pretty.breaks (map prt_thm ths)) :: Attrib.pretty_attribs ctxt atts;
213 fun prt_note (a, ths) =
214 Pretty.block (Pretty.breaks (flat (prt_name_atts a "=" :: map prt_fact ths)));
216 fn Fixes fixes => pretty_items "fixes" "and" (map prt_fix fixes)
217 | Constrains xs => pretty_items "constrains" "and" (map prt_constrain xs)
218 | Assumes asms => pretty_items "assumes" "and" (map prt_asm asms)
219 | Defines defs => pretty_items "defines" "and" (map prt_def defs)
220 | Notes ("", facts) => pretty_items "notes" "and" (map prt_note facts)
221 | Notes (kind, facts) => pretty_items ("notes " ^ kind) "and" (map prt_note facts)
225 (* pretty_statement *)
229 fun thm_name kind th prts =
231 if PureThy.has_name_hint th then
232 Pretty.block [Pretty.command kind,
233 Pretty.brk 1, Pretty.str (Sign.base_name (PureThy.get_name_hint th) ^ ":")]
234 else Pretty.command kind
235 in Pretty.block (Pretty.fbreaks (head :: prts)) end;
237 fun obtain prop ctxt =
239 val ((xs, prop'), ctxt') = Variable.focus prop ctxt;
240 val As = Logic.strip_imp_prems (Thm.term_of prop');
241 fun var (x, T) = (ProofContext.revert_skolem ctxt' x, SOME T);
242 in (("", (map (var o Term.dest_Free o Thm.term_of) xs, As)), ctxt') end;
246 fun pretty_statement ctxt kind raw_th =
248 val thy = ProofContext.theory_of ctxt;
249 val cert = Thm.cterm_of thy;
251 val th = MetaSimplifier.norm_hhf raw_th;
252 val is_elim = ObjectLogic.is_elim th;
254 val ((_, [th']), ctxt') = Variable.import_thms true [th] ctxt;
255 val prop = Thm.prop_of th';
256 val (prems, concl) = Logic.strip_horn prop;
257 val concl_term = ObjectLogic.drop_judgment thy concl;
259 val fixes = fold_aterms (fn v as Free (x, T) =>
260 if Variable.newly_fixed ctxt' ctxt x andalso not (v aconv concl_term)
261 then insert (op =) (x, T) else I | _ => I) prop []
262 |> rev |> map (apfst (ProofContext.revert_skolem ctxt'));
263 val (assumes, cases) = take_suffix (fn prem =>
264 is_elim andalso concl aconv Logic.strip_assums_concl prem) prems;
266 pretty_ctxt ctxt' (Fixes (map (fn (x, T) => (x, SOME T, NoSyn)) fixes)) @
267 pretty_ctxt ctxt' (Assumes (map (fn t => (("", []), [(t, [])])) assumes)) @
269 (if null cases then Shows [(("", []), [(concl, [])])]
270 else Obtains (#1 (fold_map (obtain o cert) cases ctxt')))
271 end |> thm_name kind raw_th;
277 (** logical operations **)
279 (* witnesses -- hypotheses as protected facts *)
281 datatype witness = Witness of term * thm;
283 fun map_witness f (Witness witn) = Witness (f witn);
285 fun morph_witness phi = map_witness (fn (t, th) => (Morphism.term phi t, Morphism.thm phi th));
287 fun witness_prop (Witness (t, _)) = t;
288 fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th);
290 fun assume_witness thy t =
291 Witness (t, Goal.protect (Thm.assume (Thm.cterm_of thy t)));
293 fun prove_witness ctxt t tac =
294 Witness (t, Goal.prove ctxt [] [] (Logic.protect t) (fn _ =>
295 Tactic.rtac Drule.protectI 1 THEN tac));
297 fun conclude_witness (Witness (_, th)) = MetaSimplifier.norm_hhf_protect (Goal.conclude th);
299 val mark_witness = Logic.protect;
301 fun make_witness t th = Witness (t, th);
302 fun dest_witness (Witness w) = w;
304 fun transfer_witness thy (Witness (t, th)) = Witness (t, Thm.transfer thy th);
307 Proof.refine (Method.Basic (K (Method.RAW_METHOD
310 (CONJUNCTS (TRYALL (Tactic.rtac Drule.protectI)))))))), Position.none));
312 fun pretty_witness ctxt witn =
313 let val prt_term = Pretty.quote o ProofContext.pretty_term ctxt in
314 Pretty.block (prt_term (witness_prop witn) ::
315 (if ! show_hyps then [Pretty.brk 2, Pretty.list "[" "]"
316 (map prt_term (witness_hyps witn))] else []))
322 fun instantiate_tfrees thy subst th =
324 val certT = Thm.ctyp_of thy;
325 val idx = Thm.maxidx_of th + 1;
326 fun cert_inst (a, (S, T)) = (certT (TVar ((a, idx), S)), certT T);
328 fun add_inst (a, S) insts =
329 if AList.defined (op =) insts a then insts
330 else (case AList.lookup (op =) subst a of NONE => insts | SOME T => (a, (S, T)) :: insts);
332 Term.fold_types (Term.fold_atyps (fn TFree v => add_inst v | _ => I))
333 (Thm.full_prop_of th) [];
336 |> Thm.generalize (map fst insts, []) idx
337 |> Thm.instantiate (map cert_inst insts, [])
340 fun instantiate_frees thy subst =
341 let val cert = Thm.cterm_of thy in
342 Drule.forall_intr_list (map (cert o Free o fst) subst) #>
343 Drule.forall_elim_list (map (cert o snd) subst)
346 fun hyps_rule rule th =
347 let val {hyps, ...} = Thm.crep_thm th in
348 Drule.implies_elim_list
349 (rule (Drule.implies_intr_list hyps th))
350 (map (Thm.assume o Drule.cterm_rule rule) hyps)
357 (case AList.lookup (op =) ren (x: string) of
359 | SOME (x', _) => x');
361 fun rename_var ren (x, mx) =
362 (case (AList.lookup (op =) ren (x: string), mx) of
364 | (SOME (x', NONE), Structure) => (x', mx)
365 | (SOME (x', SOME _), Structure) =>
366 error ("Attempt to change syntax of structure parameter " ^ quote x)
367 | (SOME (x', NONE), _) => (x', NoSyn)
368 | (SOME (x', SOME mx'), _) => (x', mx'));
370 fun rename_term ren (Free (x, T)) = Free (rename ren x, T)
371 | rename_term ren (t $ u) = rename_term ren t $ rename_term ren u
372 | rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t)
373 | rename_term _ a = a;
375 fun rename_thm ren th =
377 val thy = Thm.theory_of_thm th;
378 val subst = (Thm.fold_terms o Term.fold_aterms)
380 let val x' = rename ren x
381 in if x = x' then I else insert (eq_fst (op =)) ((x, T), Free (x', T)) end
384 if null subst then th
385 else th |> hyps_rule (instantiate_frees thy subst)
388 fun rename_morphism ren = Morphism.morphism
389 {name = I, var = rename_var ren, typ = I, term = rename_term ren, fact = map (rename_thm ren)};
392 (* instantiate types *)
395 if Symtab.is_empty env then I
396 else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x));
399 if Symtab.is_empty env then I
400 else Term.map_types (instT_type env);
402 fun instT_subst env th = (Thm.fold_terms o Term.fold_types o Term.fold_atyps)
403 (fn T as TFree (a, _) =>
404 let val T' = the_default T (Symtab.lookup env a)
405 in if T = T' then I else insert (op =) (a, T') end
408 fun instT_thm thy env th =
409 if Symtab.is_empty env then th
411 let val subst = instT_subst env th
412 in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end;
414 fun instT_morphism thy env =
415 let val thy_ref = Theory.check_thy thy in
418 typ = instT_type env,
419 term = instT_term env,
420 fact = map (fn th => instT_thm (Theory.deref thy_ref) env th)}
424 (* instantiate types and terms *)
426 fun inst_term (envT, env) =
427 if Symtab.is_empty env then instT_term envT
430 val instT = instT_type envT;
431 fun inst (Const (x, T)) = Const (x, instT T)
432 | inst (Free (x, T)) =
433 (case Symtab.lookup env x of
434 NONE => Free (x, instT T)
436 | inst (Var (xi, T)) = Var (xi, instT T)
437 | inst (b as Bound _) = b
438 | inst (Abs (x, T, t)) = Abs (x, instT T, inst t)
439 | inst (t $ u) = inst t $ inst u;
440 in Envir.beta_norm o inst end;
442 fun inst_thm thy (envT, env) th =
443 if Symtab.is_empty env then instT_thm thy envT th
446 val substT = instT_subst envT th;
447 val subst = (Thm.fold_terms o Term.fold_aterms)
450 val T' = instT_type envT T;
451 val t = Free (x, T');
452 val t' = the_default t (Symtab.lookup env x);
453 in if t aconv t' then I else insert (eq_fst (op =)) ((x, T'), t') end
456 if null substT andalso null subst then th
458 (instantiate_tfrees thy substT #>
459 instantiate_frees thy subst #>
460 Conv.fconv_rule (Thm.beta_conversion true))
463 fun inst_morphism thy envs =
464 let val thy_ref = Theory.check_thy thy in
467 typ = instT_type (#1 envs),
468 term = inst_term envs,
469 fact = map (fn th => inst_thm (Theory.deref thy_ref) envs th)}
472 (* separate instantiation for theorems -- cannot have vars *)
473 fun inst_morphism' thy envs envs' =
474 let val thy_ref = Theory.check_thy thy in
477 typ = instT_type (#1 envs),
478 term = inst_term envs,
479 fact = map (fn th => inst_thm (Theory.deref thy_ref) envs' th)}
483 (* satisfy hypotheses *)
485 fun satisfy_thm witns thm = thm |> fold (fn hyp =>
486 (case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of
488 | SOME (Witness (_, th)) => Drule.implies_intr_protected [hyp] #> Goal.comp_hhf th))
489 (#hyps (Thm.crep_thm thm));
491 fun satisfy_morphism witns = Morphism.thm_morphism (satisfy_thm witns);
493 fun satisfy_facts witns = facts_map (morph_ctxt (satisfy_morphism witns));
496 (* generalize type/term parameters *)
498 val maxidx_atts = fold Args.maxidx_values;
500 fun generalize_facts inner outer facts =
502 val thy = ProofContext.theory_of inner;
504 fold (fn ((_, atts), bs) => maxidx_atts atts #> fold (maxidx_atts o #2) bs) facts ~1;
505 val exp_fact = map (Thm.adjust_maxidx_thm maxidx) #> Variable.export inner outer;
506 val exp_term = Drule.term_rule thy (singleton exp_fact);
507 val exp_typ = Logic.type_map exp_term;
509 Morphism.morphism {name = I, var = I, typ = exp_typ, term = exp_term, fact = exp_fact};
510 in facts_map (morph_ctxt morphism) facts end;