author | wenzelm |
Tue, 05 Dec 2006 00:30:38 +0100 | |
changeset 21646 | c07b5b0e8492 |
parent 21605 | 4e7307e229b3 |
child 21965 | 7120ef5bc378 |
permissions | -rw-r--r-- |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/Isar/element.ML |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
2 |
ID: $Id$ |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
3 |
Author: Makarius |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
4 |
|
19777 | 5 |
Explicit data structures for some Isar language elements, with derived |
6 |
logical operations. |
|
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
7 |
*) |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
8 |
|
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
9 |
signature ELEMENT = |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
10 |
sig |
19259 | 11 |
datatype ('typ, 'term) stmt = |
19585 | 12 |
Shows of ((string * Attrib.src list) * ('term * 'term list) list) list | |
19259 | 13 |
Obtains of (string * ((string * 'typ option) list * 'term list)) list |
14 |
type statement (*= (string, string) stmt*) |
|
15 |
type statement_i (*= (typ, term) stmt*) |
|
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
16 |
datatype ('typ, 'term, 'fact) ctxt = |
18669 | 17 |
Fixes of (string * 'typ option * mixfix) list | |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
18 |
Constrains of (string * 'typ) list | |
19585 | 19 |
Assumes of ((string * Attrib.src list) * ('term * 'term list) list) list | |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
20 |
Defines of ((string * Attrib.src list) * ('term * 'term list)) list | |
21440 | 21 |
Notes of string * ((string * Attrib.src list) * ('fact * Attrib.src list) list) list |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
22 |
type context (*= (string, string, thmref) ctxt*) |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
23 |
type context_i (*= (typ, term, thm list) ctxt*) |
21581 | 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 |
|
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
27 |
val map_ctxt: {name: string -> string, |
18669 | 28 |
var: string * mixfix -> string * mixfix, |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
29 |
typ: 'typ -> 'a, term: 'term -> 'b, fact: 'fact -> 'c, |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
30 |
attrib: Attrib.src -> Attrib.src} -> ('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt |
21528 | 31 |
val map_ctxt_attrib: (Attrib.src -> Attrib.src) -> |
32 |
('typ, 'term, 'fact) ctxt -> ('typ, 'term, 'fact) ctxt |
|
21481 | 33 |
val morph_ctxt: morphism -> context_i -> context_i |
19808 | 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 |
|
19777 | 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 |
|
41 |
type witness |
|
42 |
val map_witness: (term * thm -> term * thm) -> witness -> witness |
|
21481 | 43 |
val morph_witness: morphism -> witness -> witness |
19777 | 44 |
val witness_prop: witness -> term |
45 |
val witness_hyps: witness -> term list |
|
46 |
val assume_witness: theory -> term -> witness |
|
20058 | 47 |
val prove_witness: Proof.context -> term -> tactic -> witness |
19777 | 48 |
val conclude_witness: witness -> thm |
49 |
val mark_witness: term -> term |
|
50 |
val make_witness: term -> thm -> witness |
|
19931
fb32b43e7f80
Restructured locales with predicates: import is now an interpretation.
ballarin
parents:
19897
diff
changeset
|
51 |
val dest_witness: witness -> term * thm |
20068
19c7361db4a3
New function transfer_witness lifting Thm.transfer to witnesses.
ballarin
parents:
20058
diff
changeset
|
52 |
val transfer_witness: theory -> witness -> witness |
19808 | 53 |
val refine_witness: Proof.state -> Proof.state Seq.seq |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
54 |
val rename: (string * (string * mixfix option)) list -> string -> string |
18669 | 55 |
val rename_var: (string * (string * mixfix option)) list -> string * mixfix -> string * mixfix |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
56 |
val rename_term: (string * (string * mixfix option)) list -> term -> term |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
57 |
val rename_thm: (string * (string * mixfix option)) list -> thm -> thm |
21481 | 58 |
val rename_morphism: (string * (string * mixfix option)) list -> morphism |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
59 |
val instT_type: typ Symtab.table -> typ -> typ |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
60 |
val instT_term: typ Symtab.table -> term -> term |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
61 |
val instT_thm: theory -> typ Symtab.table -> thm -> thm |
21481 | 62 |
val instT_morphism: theory -> typ Symtab.table -> morphism |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
63 |
val inst_term: typ Symtab.table * term Symtab.table -> term -> term |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
64 |
val inst_thm: theory -> typ Symtab.table * term Symtab.table -> thm -> thm |
21481 | 65 |
val inst_morphism: theory -> typ Symtab.table * term Symtab.table -> morphism |
19777 | 66 |
val satisfy_thm: witness list -> thm -> thm |
21481 | 67 |
val satisfy_morphism: witness list -> morphism |
20264 | 68 |
val satisfy_facts: witness list -> |
69 |
((string * Attrib.src list) * (thm list * Attrib.src list) list) list -> |
|
70 |
((string * Attrib.src list) * (thm list * Attrib.src list) list) list |
|
21581 | 71 |
val generalize_facts: Proof.context -> Proof.context -> |
20264 | 72 |
((string * Attrib.src list) * (thm list * Attrib.src list) list) list -> |
73 |
((string * Attrib.src list) * (thm list * Attrib.src list) list) list |
|
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
74 |
end; |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
75 |
|
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
76 |
structure Element: ELEMENT = |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
77 |
struct |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
78 |
|
19777 | 79 |
(** language elements **) |
80 |
||
81 |
(* statement *) |
|
19259 | 82 |
|
83 |
datatype ('typ, 'term) stmt = |
|
19585 | 84 |
Shows of ((string * Attrib.src list) * ('term * 'term list) list) list | |
19259 | 85 |
Obtains of (string * ((string * 'typ option) list * 'term list)) list; |
86 |
||
87 |
type statement = (string, string) stmt; |
|
88 |
type statement_i = (typ, term) stmt; |
|
89 |
||
90 |
||
19777 | 91 |
(* context *) |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
92 |
|
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
93 |
datatype ('typ, 'term, 'fact) ctxt = |
18669 | 94 |
Fixes of (string * 'typ option * mixfix) list | |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
95 |
Constrains of (string * 'typ) list | |
19585 | 96 |
Assumes of ((string * Attrib.src list) * ('term * 'term list) list) list | |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
97 |
Defines of ((string * Attrib.src list) * ('term * 'term list)) list | |
21440 | 98 |
Notes of string * ((string * Attrib.src list) * ('fact * Attrib.src list) list) list; |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
99 |
|
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
100 |
type context = (string, string, thmref) ctxt; |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
101 |
type context_i = (typ, term, thm list) ctxt; |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
102 |
|
21581 | 103 |
fun facts_map f facts = Notes ("", facts) |> f |> (fn Notes (_, facts') => facts'); |
104 |
||
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
105 |
fun map_ctxt {name, var, typ, term, fact, attrib} = |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
106 |
fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) => |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
107 |
let val (x', mx') = var (x, mx) in (x', Option.map typ T, mx') end)) |
18669 | 108 |
| Constrains xs => Constrains (xs |> map (fn (x, T) => (#1 (var (x, NoSyn)), typ T))) |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
109 |
| Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) => |
19585 | 110 |
((name a, map attrib atts), propps |> map (fn (t, ps) => (term t, map term ps))))) |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
111 |
| Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) => |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
112 |
((name a, map attrib atts), (term t, map term ps)))) |
21440 | 113 |
| Notes (kind, facts) => Notes (kind, facts |> map (fn ((a, atts), bs) => |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
114 |
((name a, map attrib atts), bs |> map (fn (ths, btts) => (fact ths, map attrib btts))))); |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
115 |
|
21528 | 116 |
fun map_ctxt_attrib attrib = |
117 |
map_ctxt {name = I, var = I, typ = I, term = I, fact = I, attrib = attrib}; |
|
118 |
||
21481 | 119 |
fun morph_ctxt phi = map_ctxt |
120 |
{name = Morphism.name phi, |
|
121 |
var = Morphism.var phi, |
|
122 |
typ = Morphism.typ phi, |
|
123 |
term = Morphism.term phi, |
|
21521 | 124 |
fact = Morphism.fact phi, |
21481 | 125 |
attrib = Args.morph_values phi}; |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
126 |
|
19808 | 127 |
|
128 |
(* logical content *) |
|
129 |
||
19777 | 130 |
fun params_of (Fixes fixes) = fixes |> map |
131 |
(fn (x, SOME T, _) => (x, T) |
|
132 |
| (x, _, _) => raise TERM ("Untyped context element parameter " ^ quote x, [])) |
|
133 |
| params_of _ = []; |
|
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
134 |
|
19777 | 135 |
fun prems_of (Assumes asms) = maps (map fst o snd) asms |
136 |
| prems_of (Defines defs) = map (fst o snd) defs |
|
137 |
| prems_of _ = []; |
|
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
138 |
|
20233 | 139 |
fun assume thy t = Assumption.assume (Thm.cterm_of thy t); |
19808 | 140 |
|
141 |
fun facts_of thy (Assumes asms) = map (apsnd (map (fn (t, _) => ([assume thy t], [])))) asms |
|
142 |
| facts_of thy (Defines defs) = map (apsnd (fn (t, _) => [([assume thy t], [])])) defs |
|
21440 | 143 |
| facts_of _ (Notes (_, facts)) = facts |
19808 | 144 |
| facts_of _ _ = []; |
145 |
||
18894 | 146 |
|
147 |
||
19259 | 148 |
(** pretty printing **) |
149 |
||
19267 | 150 |
fun pretty_items _ _ [] = [] |
151 |
| pretty_items keyword sep (x :: ys) = |
|
152 |
Pretty.block [Pretty.keyword keyword, Pretty.brk 1, x] :: |
|
153 |
map (fn y => Pretty.block [Pretty.str " ", Pretty.keyword sep, Pretty.brk 1, y]) ys; |
|
19259 | 154 |
|
155 |
fun pretty_name_atts ctxt (name, atts) sep = |
|
156 |
if name = "" andalso null atts then [] |
|
19731 | 157 |
else [Pretty.block |
21032 | 158 |
(Pretty.breaks (Pretty.str name :: Attrib.pretty_attribs ctxt atts @ [Pretty.str sep]))]; |
19259 | 159 |
|
160 |
||
161 |
(* pretty_stmt *) |
|
162 |
||
163 |
fun pretty_stmt ctxt = |
|
164 |
let |
|
165 |
val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt; |
|
166 |
val prt_term = Pretty.quote o ProofContext.pretty_term ctxt; |
|
19267 | 167 |
val prt_terms = separate (Pretty.keyword "and") o map prt_term; |
19259 | 168 |
val prt_name_atts = pretty_name_atts ctxt; |
169 |
||
170 |
fun prt_show (a, ts) = |
|
19267 | 171 |
Pretty.block (Pretty.breaks (prt_name_atts a ":" @ prt_terms (map fst ts))); |
19259 | 172 |
|
173 |
fun prt_var (x, SOME T) = Pretty.block [Pretty.str (x ^ " ::"), Pretty.brk 1, prt_typ T] |
|
174 |
| prt_var (x, NONE) = Pretty.str x; |
|
19585 | 175 |
val prt_vars = separate (Pretty.keyword "and") o map prt_var; |
19259 | 176 |
|
19267 | 177 |
fun prt_obtain (_, ([], ts)) = Pretty.block (Pretty.breaks (prt_terms ts)) |
19259 | 178 |
| prt_obtain (_, (xs, ts)) = Pretty.block (Pretty.breaks |
19585 | 179 |
(prt_vars xs @ [Pretty.keyword "where"] @ prt_terms ts)); |
19259 | 180 |
in |
19267 | 181 |
fn Shows shows => pretty_items "shows" "and" (map prt_show shows) |
182 |
| Obtains obtains => pretty_items "obtains" "|" (map prt_obtain obtains) |
|
19259 | 183 |
end; |
184 |
||
18894 | 185 |
|
19259 | 186 |
(* pretty_ctxt *) |
187 |
||
188 |
fun pretty_ctxt ctxt = |
|
189 |
let |
|
190 |
val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt; |
|
191 |
val prt_term = Pretty.quote o ProofContext.pretty_term ctxt; |
|
192 |
val prt_thm = Pretty.backquote o ProofContext.pretty_thm ctxt; |
|
193 |
val prt_name_atts = pretty_name_atts ctxt; |
|
194 |
||
19267 | 195 |
fun prt_mixfix NoSyn = [] |
196 |
| prt_mixfix mx = [Pretty.brk 2, Syntax.pretty_mixfix mx]; |
|
197 |
||
19259 | 198 |
fun prt_fix (x, SOME T, mx) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 :: |
199 |
prt_typ T :: Pretty.brk 1 :: prt_mixfix mx) |
|
200 |
| prt_fix (x, NONE, mx) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_mixfix mx); |
|
201 |
fun prt_constrain (x, T) = prt_fix (x, SOME T, NoSyn); |
|
18894 | 202 |
|
19259 | 203 |
fun prt_asm (a, ts) = |
204 |
Pretty.block (Pretty.breaks (prt_name_atts a ":" @ map (prt_term o fst) ts)); |
|
205 |
fun prt_def (a, (t, _)) = |
|
206 |
Pretty.block (Pretty.breaks (prt_name_atts a ":" @ [prt_term t])); |
|
207 |
||
208 |
fun prt_fact (ths, []) = map prt_thm ths |
|
209 |
| prt_fact (ths, atts) = Pretty.enclose "(" ")" |
|
21032 | 210 |
(Pretty.breaks (map prt_thm ths)) :: Attrib.pretty_attribs ctxt atts; |
19259 | 211 |
fun prt_note (a, ths) = |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
212 |
Pretty.block (Pretty.breaks (flat (prt_name_atts a "=" :: map prt_fact ths))); |
19259 | 213 |
in |
19267 | 214 |
fn Fixes fixes => pretty_items "fixes" "and" (map prt_fix fixes) |
215 |
| Constrains xs => pretty_items "constrains" "and" (map prt_constrain xs) |
|
216 |
| Assumes asms => pretty_items "assumes" "and" (map prt_asm asms) |
|
217 |
| Defines defs => pretty_items "defines" "and" (map prt_def defs) |
|
21440 | 218 |
| Notes ("", facts) => pretty_items "notes" "and" (map prt_note facts) |
219 |
| Notes (kind, facts) => pretty_items ("notes " ^ kind) "and" (map prt_note facts) |
|
19259 | 220 |
end; |
18894 | 221 |
|
19267 | 222 |
|
223 |
(* pretty_statement *) |
|
224 |
||
225 |
local |
|
226 |
||
227 |
fun thm_name kind th prts = |
|
228 |
let val head = |
|
21646
c07b5b0e8492
thm/prf: separate official name vs. additional tags;
wenzelm
parents:
21605
diff
changeset
|
229 |
(case PureThy.get_name_hint th of |
19267 | 230 |
"" => Pretty.command kind |
231 |
| a => Pretty.block [Pretty.command kind, Pretty.brk 1, Pretty.str (Sign.base_name a ^ ":")]) |
|
232 |
in Pretty.block (Pretty.fbreaks (head :: prts)) end; |
|
233 |
||
234 |
fun obtain prop ctxt = |
|
235 |
let |
|
20150 | 236 |
val ((xs, prop'), ctxt') = Variable.focus prop ctxt; |
237 |
val As = Logic.strip_imp_prems (Thm.term_of prop'); |
|
238 |
fun var (x, T) = (ProofContext.revert_skolem ctxt' x, SOME T); |
|
20218
be3bfb0699ba
Variable.import(T): result includes fixed types/terms;
wenzelm
parents:
20150
diff
changeset
|
239 |
in (("", (map (var o Term.dest_Free o Thm.term_of) xs, As)), ctxt') end; |
19267 | 240 |
|
241 |
in |
|
242 |
||
243 |
fun pretty_statement ctxt kind raw_th = |
|
244 |
let |
|
245 |
val thy = ProofContext.theory_of ctxt; |
|
20150 | 246 |
val cert = Thm.cterm_of thy; |
247 |
||
21605 | 248 |
val th = MetaSimplifier.norm_hhf raw_th; |
20150 | 249 |
val is_elim = ObjectLogic.is_elim th; |
19267 | 250 |
|
20218
be3bfb0699ba
Variable.import(T): result includes fixed types/terms;
wenzelm
parents:
20150
diff
changeset
|
251 |
val ((_, [th']), ctxt') = Variable.import true [th] ctxt; |
20150 | 252 |
val prop = Thm.prop_of th'; |
253 |
val (prems, concl) = Logic.strip_horn prop; |
|
254 |
val concl_term = ObjectLogic.drop_judgment thy concl; |
|
19267 | 255 |
|
20150 | 256 |
val fixes = fold_aterms (fn v as Free (x, T) => |
257 |
if Variable.newly_fixed ctxt' ctxt x andalso not (v aconv concl_term) |
|
258 |
then insert (op =) (x, T) else I | _ => I) prop [] |
|
259 |
|> rev |> map (apfst (ProofContext.revert_skolem ctxt')); |
|
260 |
val (assumes, cases) = take_suffix (fn prem => |
|
261 |
is_elim andalso concl aconv Logic.strip_assums_concl prem) prems; |
|
19267 | 262 |
in |
20150 | 263 |
pretty_ctxt ctxt' (Fixes (map (fn (x, T) => (x, SOME T, NoSyn)) fixes)) @ |
264 |
pretty_ctxt ctxt' (Assumes (map (fn t => (("", []), [(t, [])])) assumes)) @ |
|
265 |
pretty_stmt ctxt' |
|
19585 | 266 |
(if null cases then Shows [(("", []), [(concl, [])])] |
20150 | 267 |
else Obtains (#1 (fold_map (obtain o cert) cases ctxt'))) |
19267 | 268 |
end |> thm_name kind raw_th; |
269 |
||
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
270 |
end; |
19267 | 271 |
|
19777 | 272 |
|
273 |
||
274 |
(** logical operations **) |
|
275 |
||
276 |
(* witnesses -- hypotheses as protected facts *) |
|
277 |
||
278 |
datatype witness = Witness of term * thm; |
|
279 |
||
280 |
fun map_witness f (Witness witn) = Witness (f witn); |
|
281 |
||
21481 | 282 |
fun morph_witness phi = map_witness (fn (t, th) => (Morphism.term phi t, Morphism.thm phi th)); |
283 |
||
19777 | 284 |
fun witness_prop (Witness (t, _)) = t; |
285 |
fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th); |
|
286 |
||
287 |
fun assume_witness thy t = |
|
288 |
Witness (t, Goal.protect (Thm.assume (Thm.cterm_of thy t))); |
|
289 |
||
20058 | 290 |
fun prove_witness ctxt t tac = |
291 |
Witness (t, Goal.prove ctxt [] [] (Logic.protect t) (fn _ => |
|
19777 | 292 |
Tactic.rtac Drule.protectI 1 THEN tac)); |
293 |
||
21605 | 294 |
fun conclude_witness (Witness (_, th)) = MetaSimplifier.norm_hhf_protect (Goal.conclude th); |
19777 | 295 |
|
296 |
val mark_witness = Logic.protect; |
|
297 |
||
298 |
fun make_witness t th = Witness (t, th); |
|
19931
fb32b43e7f80
Restructured locales with predicates: import is now an interpretation.
ballarin
parents:
19897
diff
changeset
|
299 |
fun dest_witness (Witness w) = w; |
fb32b43e7f80
Restructured locales with predicates: import is now an interpretation.
ballarin
parents:
19897
diff
changeset
|
300 |
|
20068
19c7361db4a3
New function transfer_witness lifting Thm.transfer to witnesses.
ballarin
parents:
20058
diff
changeset
|
301 |
fun transfer_witness thy (Witness (t, th)) = Witness (t, Thm.transfer thy th); |
19c7361db4a3
New function transfer_witness lifting Thm.transfer to witnesses.
ballarin
parents:
20058
diff
changeset
|
302 |
|
19777 | 303 |
val refine_witness = |
304 |
Proof.refine (Method.Basic (K (Method.RAW_METHOD |
|
305 |
(K (ALLGOALS |
|
306 |
(PRECISE_CONJUNCTS ~1 (ALLGOALS |
|
19808 | 307 |
(PRECISE_CONJUNCTS ~1 (TRYALL (Tactic.rtac Drule.protectI)))))))))); |
19777 | 308 |
|
309 |
||
310 |
(* derived rules *) |
|
311 |
||
20007 | 312 |
fun instantiate_tfrees thy subst th = |
19777 | 313 |
let |
314 |
val certT = Thm.ctyp_of thy; |
|
20007 | 315 |
val idx = Thm.maxidx_of th + 1; |
316 |
fun cert_inst (a, (S, T)) = (certT (TVar ((a, idx), S)), certT T); |
|
317 |
||
318 |
fun add_inst (a, S) insts = |
|
319 |
if AList.defined (op =) insts a then insts |
|
320 |
else (case AList.lookup (op =) subst a of NONE => insts | SOME T => (a, (S, T)) :: insts); |
|
321 |
val insts = |
|
322 |
Term.fold_types (Term.fold_atyps (fn TFree v => add_inst v | _ => I)) |
|
323 |
(Thm.full_prop_of th) []; |
|
19777 | 324 |
in |
20007 | 325 |
th |
326 |
|> Thm.generalize (map fst insts, []) idx |
|
327 |
|> Thm.instantiate (map cert_inst insts, []) |
|
19777 | 328 |
end; |
329 |
||
330 |
fun instantiate_frees thy subst = |
|
331 |
let val cert = Thm.cterm_of thy in |
|
332 |
Drule.forall_intr_list (map (cert o Free o fst) subst) #> |
|
333 |
Drule.forall_elim_list (map (cert o snd) subst) |
|
334 |
end; |
|
335 |
||
336 |
fun hyps_rule rule th = |
|
21521 | 337 |
let val {hyps, ...} = Thm.crep_thm th in |
19777 | 338 |
Drule.implies_elim_list |
339 |
(rule (Drule.implies_intr_list hyps th)) |
|
21521 | 340 |
(map (Thm.assume o Drule.cterm_rule rule) hyps) |
19777 | 341 |
end; |
342 |
||
343 |
||
344 |
(* rename *) |
|
345 |
||
346 |
fun rename ren x = |
|
347 |
(case AList.lookup (op =) ren (x: string) of |
|
348 |
NONE => x |
|
349 |
| SOME (x', _) => x'); |
|
350 |
||
351 |
fun rename_var ren (x, mx) = |
|
352 |
(case (AList.lookup (op =) ren (x: string), mx) of |
|
353 |
(NONE, _) => (x, mx) |
|
354 |
| (SOME (x', NONE), Structure) => (x', mx) |
|
355 |
| (SOME (x', SOME _), Structure) => |
|
356 |
error ("Attempt to change syntax of structure parameter " ^ quote x) |
|
357 |
| (SOME (x', NONE), _) => (x', NoSyn) |
|
358 |
| (SOME (x', SOME mx'), _) => (x', mx')); |
|
359 |
||
360 |
fun rename_term ren (Free (x, T)) = Free (rename ren x, T) |
|
361 |
| rename_term ren (t $ u) = rename_term ren t $ rename_term ren u |
|
362 |
| rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t) |
|
363 |
| rename_term _ a = a; |
|
364 |
||
365 |
fun rename_thm ren th = |
|
366 |
let |
|
20304 | 367 |
val thy = Thm.theory_of_thm th; |
368 |
val subst = (Drule.fold_terms o Term.fold_aterms) |
|
369 |
(fn Free (x, T) => |
|
19777 | 370 |
let val x' = rename ren x |
20304 | 371 |
in if x = x' then I else insert (eq_fst (op =)) ((x, T), Free (x', T)) end |
372 |
| _ => I) th []; |
|
19777 | 373 |
in |
374 |
if null subst then th |
|
20304 | 375 |
else th |> hyps_rule (instantiate_frees thy subst) |
19777 | 376 |
end; |
377 |
||
21481 | 378 |
fun rename_morphism ren = Morphism.morphism |
21521 | 379 |
{name = I, var = rename_var ren, typ = I, term = rename_term ren, fact = map (rename_thm ren)}; |
19777 | 380 |
|
381 |
||
382 |
(* instantiate types *) |
|
383 |
||
384 |
fun instT_type env = |
|
385 |
if Symtab.is_empty env then I |
|
386 |
else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x)); |
|
387 |
||
388 |
fun instT_term env = |
|
389 |
if Symtab.is_empty env then I |
|
20548
8ef25fe585a8
renamed Term.map_term_types to Term.map_types (cf. Term.fold_types);
wenzelm
parents:
20304
diff
changeset
|
390 |
else Term.map_types (instT_type env); |
19777 | 391 |
|
20304 | 392 |
fun instT_subst env th = (Drule.fold_terms o Term.fold_types o Term.fold_atyps) |
393 |
(fn T as TFree (a, _) => |
|
394 |
let val T' = the_default T (Symtab.lookup env a) |
|
395 |
in if T = T' then I else insert (op =) (a, T') end |
|
396 |
| _ => I) th []; |
|
19777 | 397 |
|
398 |
fun instT_thm thy env th = |
|
399 |
if Symtab.is_empty env then th |
|
400 |
else |
|
401 |
let val subst = instT_subst env th |
|
402 |
in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end; |
|
403 |
||
21481 | 404 |
fun instT_morphism thy env = Morphism.morphism |
21521 | 405 |
{name = I, var = I, typ = instT_type env, term = instT_term env, fact = map (instT_thm thy env)}; |
19777 | 406 |
|
407 |
||
408 |
(* instantiate types and terms *) |
|
409 |
||
410 |
fun inst_term (envT, env) = |
|
411 |
if Symtab.is_empty env then instT_term envT |
|
412 |
else |
|
413 |
let |
|
414 |
val instT = instT_type envT; |
|
415 |
fun inst (Const (x, T)) = Const (x, instT T) |
|
416 |
| inst (Free (x, T)) = |
|
417 |
(case Symtab.lookup env x of |
|
418 |
NONE => Free (x, instT T) |
|
419 |
| SOME t => t) |
|
420 |
| inst (Var (xi, T)) = Var (xi, instT T) |
|
421 |
| inst (b as Bound _) = b |
|
422 |
| inst (Abs (x, T, t)) = Abs (x, instT T, inst t) |
|
423 |
| inst (t $ u) = inst t $ inst u; |
|
424 |
in Envir.beta_norm o inst end; |
|
425 |
||
426 |
fun inst_thm thy (envT, env) th = |
|
427 |
if Symtab.is_empty env then instT_thm thy envT th |
|
428 |
else |
|
429 |
let |
|
430 |
val substT = instT_subst envT th; |
|
20304 | 431 |
val subst = (Drule.fold_terms o Term.fold_aterms) |
432 |
(fn Free (x, T) => |
|
19777 | 433 |
let |
434 |
val T' = instT_type envT T; |
|
435 |
val t = Free (x, T'); |
|
436 |
val t' = the_default t (Symtab.lookup env x); |
|
20304 | 437 |
in if t aconv t' then I else insert (eq_fst (op =)) ((x, T'), t') end |
438 |
| _ => I) th []; |
|
19777 | 439 |
in |
440 |
if null substT andalso null subst then th |
|
441 |
else th |> hyps_rule |
|
442 |
(instantiate_tfrees thy substT #> |
|
443 |
instantiate_frees thy subst #> |
|
444 |
Drule.fconv_rule (Thm.beta_conversion true)) |
|
445 |
end; |
|
446 |
||
21481 | 447 |
fun inst_morphism thy envs = Morphism.morphism |
21521 | 448 |
{name = I, var = I, typ = instT_type (#1 envs), term = inst_term envs, |
449 |
fact = map (inst_thm thy envs)}; |
|
19777 | 450 |
|
451 |
||
452 |
(* satisfy hypotheses *) |
|
453 |
||
454 |
fun satisfy_thm witns thm = thm |> fold (fn hyp => |
|
455 |
(case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of |
|
456 |
NONE => I |
|
457 |
| SOME (Witness (_, th)) => Drule.implies_intr_protected [hyp] #> Goal.comp_hhf th)) |
|
458 |
(#hyps (Thm.crep_thm thm)); |
|
459 |
||
21497 | 460 |
fun satisfy_morphism witns = Morphism.thm_morphism (satisfy_thm witns); |
19843 | 461 |
|
21581 | 462 |
fun satisfy_facts witns = facts_map (morph_ctxt (satisfy_morphism witns)); |
20264 | 463 |
|
464 |
||
465 |
(* generalize type/term parameters *) |
|
466 |
||
467 |
val maxidx_atts = fold Args.maxidx_values; |
|
468 |
||
21581 | 469 |
fun generalize_facts inner outer facts = |
20264 | 470 |
let |
471 |
val thy = ProofContext.theory_of inner; |
|
472 |
val maxidx = |
|
473 |
fold (fn ((_, atts), bs) => maxidx_atts atts #> fold (maxidx_atts o #2) bs) facts ~1; |
|
21581 | 474 |
val exp_fact = map (Thm.adjust_maxidx_thm maxidx) #> Variable.export inner outer; |
21521 | 475 |
val exp_term = Drule.term_rule thy (singleton exp_fact); |
476 |
val exp_typ = Logic.type_map exp_term; |
|
477 |
val morphism = |
|
478 |
Morphism.morphism {name = I, var = I, typ = exp_typ, term = exp_term, fact = exp_fact}; |
|
21581 | 479 |
in facts_map (morph_ctxt morphism) facts end; |
20886
f26672c248ee
replaced generalize_facts by full export_(standard_)facts;
wenzelm
parents:
20548
diff
changeset
|
480 |
|
19267 | 481 |
end; |