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