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