author | ballarin |
Tue, 20 Jun 2006 15:53:44 +0200 | |
changeset 19931 | fb32b43e7f80 |
parent 19897 | fe661eb3b0e7 |
child 20007 | 8f9e6255108e |
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 | |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
21 |
Notes of ((string * Attrib.src list) * ('fact * Attrib.src list) list) list |
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 |
|
41 |
val prove_witness: theory -> term -> tactic -> witness |
|
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 |
19808 | 46 |
val refine_witness: Proof.state -> Proof.state Seq.seq |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
47 |
val rename: (string * (string * mixfix option)) list -> string -> string |
18669 | 48 |
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
|
49 |
val rename_term: (string * (string * mixfix option)) list -> term -> term |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
50 |
val rename_thm: (string * (string * mixfix option)) list -> thm -> thm |
19777 | 51 |
val rename_witness: (string * (string * mixfix option)) list -> witness -> witness |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
52 |
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
|
53 |
val instT_type: typ Symtab.table -> typ -> typ |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
54 |
val instT_term: typ Symtab.table -> term -> term |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
55 |
val instT_thm: theory -> typ Symtab.table -> thm -> thm |
19777 | 56 |
val instT_witness: theory -> typ Symtab.table -> witness -> witness |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
57 |
val instT_ctxt: theory -> typ Symtab.table -> context_i -> context_i |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
58 |
val inst_term: typ Symtab.table * term Symtab.table -> term -> term |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
59 |
val inst_thm: theory -> typ Symtab.table * term Symtab.table -> thm -> thm |
19777 | 60 |
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
|
61 |
val inst_ctxt: theory -> typ Symtab.table * term Symtab.table -> context_i -> context_i |
19777 | 62 |
val satisfy_thm: witness list -> thm -> thm |
63 |
val satisfy_witness: witness list -> witness -> witness |
|
19843 | 64 |
val satisfy_ctxt: witness list -> context_i -> context_i |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
65 |
end; |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
66 |
|
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
67 |
structure Element: ELEMENT = |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
68 |
struct |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
69 |
|
19259 | 70 |
|
19777 | 71 |
(** language elements **) |
72 |
||
73 |
(* statement *) |
|
19259 | 74 |
|
75 |
datatype ('typ, 'term) stmt = |
|
19585 | 76 |
Shows of ((string * Attrib.src list) * ('term * 'term list) list) list | |
19259 | 77 |
Obtains of (string * ((string * 'typ option) list * 'term list)) list; |
78 |
||
79 |
type statement = (string, string) stmt; |
|
80 |
type statement_i = (typ, term) stmt; |
|
81 |
||
82 |
||
19777 | 83 |
(* context *) |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
84 |
|
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
85 |
datatype ('typ, 'term, 'fact) ctxt = |
18669 | 86 |
Fixes of (string * 'typ option * mixfix) list | |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
87 |
Constrains of (string * 'typ) list | |
19585 | 88 |
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
|
89 |
Defines of ((string * Attrib.src list) * ('term * 'term list)) list | |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
90 |
Notes of ((string * Attrib.src list) * ('fact * Attrib.src list) list) list; |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
91 |
|
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
92 |
type context = (string, string, thmref) ctxt; |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
93 |
type context_i = (typ, term, thm list) ctxt; |
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 |
fun map_ctxt {name, var, typ, term, fact, attrib} = |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
96 |
fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) => |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
97 |
let val (x', mx') = var (x, mx) in (x', Option.map typ T, mx') end)) |
18669 | 98 |
| 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
|
99 |
| Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) => |
19585 | 100 |
((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
|
101 |
| Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) => |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
102 |
((name a, map attrib atts), (term t, map term ps)))) |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
103 |
| Notes facts => Notes (facts |> map (fn ((a, atts), bs) => |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
104 |
((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
|
105 |
|
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
106 |
fun map_ctxt_values typ term thm = map_ctxt |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
107 |
{name = I, var = I, typ = typ, term = term, fact = map thm, |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
108 |
attrib = Args.map_values I typ term thm}; |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
109 |
|
19808 | 110 |
|
111 |
(* logical content *) |
|
112 |
||
19777 | 113 |
fun params_of (Fixes fixes) = fixes |> map |
114 |
(fn (x, SOME T, _) => (x, T) |
|
115 |
| (x, _, _) => raise TERM ("Untyped context element parameter " ^ quote x, [])) |
|
116 |
| params_of _ = []; |
|
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
117 |
|
19777 | 118 |
fun prems_of (Assumes asms) = maps (map fst o snd) asms |
119 |
| prems_of (Defines defs) = map (fst o snd) defs |
|
120 |
| prems_of _ = []; |
|
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
121 |
|
19808 | 122 |
fun assume thy t = Goal.norm_hhf (Thm.assume (Thm.cterm_of thy t)); |
123 |
||
124 |
fun facts_of thy (Assumes asms) = map (apsnd (map (fn (t, _) => ([assume thy t], [])))) asms |
|
125 |
| facts_of thy (Defines defs) = map (apsnd (fn (t, _) => [([assume thy t], [])])) defs |
|
126 |
| facts_of _ (Notes facts) = facts |
|
127 |
| facts_of _ _ = []; |
|
128 |
||
18894 | 129 |
|
130 |
||
19259 | 131 |
(** pretty printing **) |
132 |
||
19267 | 133 |
fun pretty_items _ _ [] = [] |
134 |
| pretty_items keyword sep (x :: ys) = |
|
135 |
Pretty.block [Pretty.keyword keyword, Pretty.brk 1, x] :: |
|
136 |
map (fn y => Pretty.block [Pretty.str " ", Pretty.keyword sep, Pretty.brk 1, y]) ys; |
|
19259 | 137 |
|
138 |
fun pretty_name_atts ctxt (name, atts) sep = |
|
139 |
if name = "" andalso null atts then [] |
|
19731 | 140 |
else [Pretty.block |
141 |
(Pretty.breaks (Pretty.str name :: Args.pretty_attribs ctxt atts @ [Pretty.str sep]))]; |
|
19259 | 142 |
|
143 |
||
144 |
(* pretty_stmt *) |
|
145 |
||
146 |
fun pretty_stmt ctxt = |
|
147 |
let |
|
148 |
val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt; |
|
149 |
val prt_term = Pretty.quote o ProofContext.pretty_term ctxt; |
|
19267 | 150 |
val prt_terms = separate (Pretty.keyword "and") o map prt_term; |
19259 | 151 |
val prt_name_atts = pretty_name_atts ctxt; |
152 |
||
153 |
fun prt_show (a, ts) = |
|
19267 | 154 |
Pretty.block (Pretty.breaks (prt_name_atts a ":" @ prt_terms (map fst ts))); |
19259 | 155 |
|
156 |
fun prt_var (x, SOME T) = Pretty.block [Pretty.str (x ^ " ::"), Pretty.brk 1, prt_typ T] |
|
157 |
| prt_var (x, NONE) = Pretty.str x; |
|
19585 | 158 |
val prt_vars = separate (Pretty.keyword "and") o map prt_var; |
19259 | 159 |
|
19267 | 160 |
fun prt_obtain (_, ([], ts)) = Pretty.block (Pretty.breaks (prt_terms ts)) |
19259 | 161 |
| prt_obtain (_, (xs, ts)) = Pretty.block (Pretty.breaks |
19585 | 162 |
(prt_vars xs @ [Pretty.keyword "where"] @ prt_terms ts)); |
19259 | 163 |
in |
19267 | 164 |
fn Shows shows => pretty_items "shows" "and" (map prt_show shows) |
165 |
| Obtains obtains => pretty_items "obtains" "|" (map prt_obtain obtains) |
|
19259 | 166 |
end; |
167 |
||
18894 | 168 |
|
19259 | 169 |
(* pretty_ctxt *) |
170 |
||
171 |
fun pretty_ctxt ctxt = |
|
172 |
let |
|
173 |
val prt_typ = Pretty.quote o ProofContext.pretty_typ ctxt; |
|
174 |
val prt_term = Pretty.quote o ProofContext.pretty_term ctxt; |
|
175 |
val prt_thm = Pretty.backquote o ProofContext.pretty_thm ctxt; |
|
176 |
val prt_name_atts = pretty_name_atts ctxt; |
|
177 |
||
19267 | 178 |
fun prt_mixfix NoSyn = [] |
179 |
| prt_mixfix mx = [Pretty.brk 2, Syntax.pretty_mixfix mx]; |
|
180 |
||
19259 | 181 |
fun prt_fix (x, SOME T, mx) = Pretty.block (Pretty.str (x ^ " ::") :: Pretty.brk 1 :: |
182 |
prt_typ T :: Pretty.brk 1 :: prt_mixfix mx) |
|
183 |
| prt_fix (x, NONE, mx) = Pretty.block (Pretty.str x :: Pretty.brk 1 :: prt_mixfix mx); |
|
184 |
fun prt_constrain (x, T) = prt_fix (x, SOME T, NoSyn); |
|
18894 | 185 |
|
19259 | 186 |
fun prt_asm (a, ts) = |
187 |
Pretty.block (Pretty.breaks (prt_name_atts a ":" @ map (prt_term o fst) ts)); |
|
188 |
fun prt_def (a, (t, _)) = |
|
189 |
Pretty.block (Pretty.breaks (prt_name_atts a ":" @ [prt_term t])); |
|
190 |
||
191 |
fun prt_fact (ths, []) = map prt_thm ths |
|
192 |
| prt_fact (ths, atts) = Pretty.enclose "(" ")" |
|
193 |
(Pretty.breaks (map prt_thm ths)) :: Args.pretty_attribs ctxt atts; |
|
194 |
fun prt_note (a, ths) = |
|
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
195 |
Pretty.block (Pretty.breaks (flat (prt_name_atts a "=" :: map prt_fact ths))); |
19259 | 196 |
in |
19267 | 197 |
fn Fixes fixes => pretty_items "fixes" "and" (map prt_fix fixes) |
198 |
| Constrains xs => pretty_items "constrains" "and" (map prt_constrain xs) |
|
199 |
| Assumes asms => pretty_items "assumes" "and" (map prt_asm asms) |
|
200 |
| Defines defs => pretty_items "defines" "and" (map prt_def defs) |
|
201 |
| Notes facts => pretty_items "notes" "and" (map prt_note facts) |
|
19259 | 202 |
end; |
18894 | 203 |
|
19267 | 204 |
|
205 |
(* pretty_statement *) |
|
206 |
||
207 |
local |
|
208 |
||
209 |
fun thm_name kind th prts = |
|
210 |
let val head = |
|
211 |
(case #1 (Thm.get_name_tags th) of |
|
212 |
"" => Pretty.command kind |
|
213 |
| a => Pretty.block [Pretty.command kind, Pretty.brk 1, Pretty.str (Sign.base_name a ^ ":")]) |
|
214 |
in Pretty.block (Pretty.fbreaks (head :: prts)) end; |
|
215 |
||
216 |
fun obtain prop ctxt = |
|
217 |
let |
|
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19866
diff
changeset
|
218 |
val xs = Variable.rename_wrt ctxt [] (Logic.strip_params prop); |
19267 | 219 |
val args = rev (map Free xs); |
220 |
val As = Logic.strip_assums_hyp prop |> map (fn t => Term.subst_bounds (args, t)); |
|
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19866
diff
changeset
|
221 |
val ctxt' = ctxt |> fold Variable.declare_term args; |
19267 | 222 |
in (("", (map (apsnd SOME) xs, As)), ctxt') end; |
223 |
||
224 |
in |
|
225 |
||
226 |
fun pretty_statement ctxt kind raw_th = |
|
227 |
let |
|
228 |
val thy = ProofContext.theory_of ctxt; |
|
229 |
val th = Goal.norm_hhf raw_th; |
|
230 |
||
231 |
fun standard_thesis t = |
|
232 |
let |
|
233 |
val C = ObjectLogic.drop_judgment thy (Thm.concl_of th); |
|
19466 | 234 |
val C' = Var ((AutoBind.thesisN, Thm.maxidx_of th + 1), Term.fastype_of C); |
19267 | 235 |
in Term.subst_atomic [(C, C')] t end; |
236 |
||
237 |
val raw_prop = |
|
238 |
Thm.prop_of th |
|
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19866
diff
changeset
|
239 |
|> singleton (Variable.monomorphic ctxt) |
19267 | 240 |
|> K (ObjectLogic.is_elim th) ? standard_thesis |
241 |
|> Term.zero_var_indexes; |
|
242 |
||
243 |
val vars = Term.add_vars raw_prop []; |
|
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19866
diff
changeset
|
244 |
val frees = Variable.rename_wrt ctxt [raw_prop] (map (apfst fst) vars); |
19305 | 245 |
val fixes = rev (filter_out (fn (x, _) => x = AutoBind.thesisN) frees); |
19267 | 246 |
|
247 |
val prop = Term.instantiate ([], vars ~~ map Free frees) raw_prop; |
|
248 |
val (prems, concl) = Logic.strip_horn prop; |
|
249 |
val thesis = ObjectLogic.fixed_judgment thy AutoBind.thesisN; |
|
250 |
val (asms, cases) = take_suffix (fn prem => thesis aconv Logic.strip_assums_concl prem) prems; |
|
251 |
in |
|
252 |
pretty_ctxt ctxt (Fixes (map (fn (x, T) => (x, SOME T, NoSyn)) fixes)) @ |
|
19585 | 253 |
pretty_ctxt ctxt (Assumes (map (fn t => (("", []), [(t, [])])) asms)) @ |
19267 | 254 |
pretty_stmt ctxt |
19585 | 255 |
(if null cases then Shows [(("", []), [(concl, [])])] |
19897
fe661eb3b0e7
ProofContext: moved variable operations to struct Variable;
wenzelm
parents:
19866
diff
changeset
|
256 |
else Obtains (#1 (fold_map obtain cases (ctxt |> Variable.declare_term prop)))) |
19267 | 257 |
end |> thm_name kind raw_th; |
258 |
||
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
259 |
end; |
19267 | 260 |
|
19777 | 261 |
|
262 |
||
263 |
(** logical operations **) |
|
264 |
||
265 |
(* witnesses -- hypotheses as protected facts *) |
|
266 |
||
267 |
datatype witness = Witness of term * thm; |
|
268 |
||
269 |
fun map_witness f (Witness witn) = Witness (f witn); |
|
270 |
||
271 |
fun witness_prop (Witness (t, _)) = t; |
|
272 |
fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th); |
|
273 |
||
274 |
fun assume_witness thy t = |
|
275 |
Witness (t, Goal.protect (Thm.assume (Thm.cterm_of thy t))); |
|
276 |
||
277 |
fun prove_witness thy t tac = |
|
278 |
Witness (t, Goal.prove thy [] [] (Logic.protect t) (fn _ => |
|
279 |
Tactic.rtac Drule.protectI 1 THEN tac)); |
|
280 |
||
281 |
fun conclude_witness (Witness (_, th)) = Goal.norm_hhf (Goal.conclude th); |
|
282 |
||
283 |
val mark_witness = Logic.protect; |
|
284 |
||
285 |
fun make_witness t th = Witness (t, th); |
|
286 |
||
19931
fb32b43e7f80
Restructured locales with predicates: import is now an interpretation.
ballarin
parents:
19897
diff
changeset
|
287 |
fun dest_witness (Witness w) = w; |
fb32b43e7f80
Restructured locales with predicates: import is now an interpretation.
ballarin
parents:
19897
diff
changeset
|
288 |
|
19777 | 289 |
val refine_witness = |
290 |
Proof.refine (Method.Basic (K (Method.RAW_METHOD |
|
291 |
(K (ALLGOALS |
|
292 |
(PRECISE_CONJUNCTS ~1 (ALLGOALS |
|
19808 | 293 |
(PRECISE_CONJUNCTS ~1 (TRYALL (Tactic.rtac Drule.protectI)))))))))); |
19777 | 294 |
|
295 |
||
296 |
(* derived rules *) |
|
297 |
||
298 |
fun instantiate_tfrees thy subst = |
|
299 |
let |
|
300 |
val certT = Thm.ctyp_of thy; |
|
301 |
fun inst vs (a, T) = AList.lookup (op =) vs a |
|
302 |
|> Option.map (fn v => (certT (TVar v), certT T)); |
|
303 |
in |
|
304 |
Drule.tvars_intr_list (map fst subst) #-> |
|
305 |
(fn vs => Thm.instantiate (map_filter (inst vs) subst, [])) |
|
306 |
end; |
|
307 |
||
308 |
fun instantiate_frees thy subst = |
|
309 |
let val cert = Thm.cterm_of thy in |
|
310 |
Drule.forall_intr_list (map (cert o Free o fst) subst) #> |
|
311 |
Drule.forall_elim_list (map (cert o snd) subst) |
|
312 |
end; |
|
313 |
||
314 |
fun hyps_rule rule th = |
|
315 |
let |
|
19866 | 316 |
val cterm_rule = Drule.mk_term #> rule #> Drule.dest_term; |
19777 | 317 |
val {hyps, ...} = Thm.crep_thm th; |
318 |
in |
|
319 |
Drule.implies_elim_list |
|
320 |
(rule (Drule.implies_intr_list hyps th)) |
|
321 |
(map (Thm.assume o cterm_rule) hyps) |
|
322 |
end; |
|
323 |
||
324 |
||
325 |
(* rename *) |
|
326 |
||
327 |
fun rename ren x = |
|
328 |
(case AList.lookup (op =) ren (x: string) of |
|
329 |
NONE => x |
|
330 |
| SOME (x', _) => x'); |
|
331 |
||
332 |
fun rename_var ren (x, mx) = |
|
333 |
(case (AList.lookup (op =) ren (x: string), mx) of |
|
334 |
(NONE, _) => (x, mx) |
|
335 |
| (SOME (x', NONE), Structure) => (x', mx) |
|
336 |
| (SOME (x', SOME _), Structure) => |
|
337 |
error ("Attempt to change syntax of structure parameter " ^ quote x) |
|
338 |
| (SOME (x', NONE), _) => (x', NoSyn) |
|
339 |
| (SOME (x', SOME mx'), _) => (x', mx')); |
|
340 |
||
341 |
fun rename_term ren (Free (x, T)) = Free (rename ren x, T) |
|
342 |
| rename_term ren (t $ u) = rename_term ren t $ rename_term ren u |
|
343 |
| rename_term ren (Abs (x, T, t)) = Abs (x, T, rename_term ren t) |
|
344 |
| rename_term _ a = a; |
|
345 |
||
346 |
fun rename_thm ren th = |
|
347 |
let |
|
348 |
val subst = Drule.frees_of th |
|
349 |
|> map_filter (fn (x, T) => |
|
350 |
let val x' = rename ren x |
|
351 |
in if x = x' then NONE else SOME ((x, T), (Free (x', T))) end); |
|
352 |
in |
|
353 |
if null subst then th |
|
354 |
else th |> hyps_rule (instantiate_frees (Thm.theory_of_thm th) subst) |
|
355 |
end; |
|
356 |
||
357 |
fun rename_witness ren = |
|
358 |
map_witness (fn (t, th) => (rename_term ren t, rename_thm ren th)); |
|
359 |
||
360 |
fun rename_ctxt ren = |
|
361 |
map_ctxt_values I (rename_term ren) (rename_thm ren) |
|
362 |
#> map_ctxt {name = I, typ = I, term = I, fact = I, attrib = I, var = rename_var ren}; |
|
363 |
||
364 |
||
365 |
(* instantiate types *) |
|
366 |
||
367 |
fun instT_type env = |
|
368 |
if Symtab.is_empty env then I |
|
369 |
else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x)); |
|
370 |
||
371 |
fun instT_term env = |
|
372 |
if Symtab.is_empty env then I |
|
373 |
else Term.map_term_types (instT_type env); |
|
374 |
||
375 |
fun instT_subst env th = |
|
376 |
Drule.tfrees_of th |
|
377 |
|> map_filter (fn (a, S) => |
|
378 |
let |
|
379 |
val T = TFree (a, S); |
|
380 |
val T' = the_default T (Symtab.lookup env a); |
|
381 |
in if T = T' then NONE else SOME (a, T') end); |
|
382 |
||
383 |
fun instT_thm thy env th = |
|
384 |
if Symtab.is_empty env then th |
|
385 |
else |
|
386 |
let val subst = instT_subst env th |
|
387 |
in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end; |
|
388 |
||
389 |
fun instT_witness thy env = |
|
390 |
map_witness (fn (t, th) => (instT_term env t, instT_thm thy env th)); |
|
391 |
||
392 |
fun instT_ctxt thy env = |
|
393 |
map_ctxt_values (instT_type env) (instT_term env) (instT_thm thy env); |
|
394 |
||
395 |
||
396 |
(* instantiate types and terms *) |
|
397 |
||
398 |
fun inst_term (envT, env) = |
|
399 |
if Symtab.is_empty env then instT_term envT |
|
400 |
else |
|
401 |
let |
|
402 |
val instT = instT_type envT; |
|
403 |
fun inst (Const (x, T)) = Const (x, instT T) |
|
404 |
| inst (Free (x, T)) = |
|
405 |
(case Symtab.lookup env x of |
|
406 |
NONE => Free (x, instT T) |
|
407 |
| SOME t => t) |
|
408 |
| inst (Var (xi, T)) = Var (xi, instT T) |
|
409 |
| inst (b as Bound _) = b |
|
410 |
| inst (Abs (x, T, t)) = Abs (x, instT T, inst t) |
|
411 |
| inst (t $ u) = inst t $ inst u; |
|
412 |
in Envir.beta_norm o inst end; |
|
413 |
||
414 |
fun inst_thm thy (envT, env) th = |
|
415 |
if Symtab.is_empty env then instT_thm thy envT th |
|
416 |
else |
|
417 |
let |
|
418 |
val substT = instT_subst envT th; |
|
419 |
val subst = Drule.frees_of th |
|
420 |
|> map_filter (fn (x, T) => |
|
421 |
let |
|
422 |
val T' = instT_type envT T; |
|
423 |
val t = Free (x, T'); |
|
424 |
val t' = the_default t (Symtab.lookup env x); |
|
425 |
in if t aconv t' then NONE else SOME ((x, T'), t') end); |
|
426 |
in |
|
427 |
if null substT andalso null subst then th |
|
428 |
else th |> hyps_rule |
|
429 |
(instantiate_tfrees thy substT #> |
|
430 |
instantiate_frees thy subst #> |
|
431 |
Drule.fconv_rule (Thm.beta_conversion true)) |
|
432 |
end; |
|
433 |
||
434 |
fun inst_witness thy envs = |
|
435 |
map_witness (fn (t, th) => (inst_term envs t, inst_thm thy envs th)); |
|
436 |
||
437 |
fun inst_ctxt thy envs = |
|
438 |
map_ctxt_values (instT_type (#1 envs)) (inst_term envs) (inst_thm thy envs); |
|
439 |
||
440 |
||
441 |
(* satisfy hypotheses *) |
|
442 |
||
443 |
fun satisfy_thm witns thm = thm |> fold (fn hyp => |
|
444 |
(case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of |
|
445 |
NONE => I |
|
446 |
| SOME (Witness (_, th)) => Drule.implies_intr_protected [hyp] #> Goal.comp_hhf th)) |
|
447 |
(#hyps (Thm.crep_thm thm)); |
|
448 |
||
449 |
fun satisfy_witness witns = map_witness (apsnd (satisfy_thm witns)); |
|
450 |
||
19843 | 451 |
fun satisfy_ctxt witns = map_ctxt_values I I (satisfy_thm witns); |
452 |
||
19267 | 453 |
end; |