author | blanchet |
Wed, 04 Mar 2009 11:05:29 +0100 | |
changeset 30242 | aea5d7fa7ef5 |
parent 30240 | 5b25fee0362c |
parent 30223 | 24d975352879 |
child 30280 | eb98b49ef835 |
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 |
Author: Makarius |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
3 |
|
19777 | 4 |
Explicit data structures for some Isar language elements, with derived |
5 |
logical operations. |
|
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
6 |
*) |
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 |
signature ELEMENT = |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
9 |
sig |
19259 | 10 |
datatype ('typ, 'term) stmt = |
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
11 |
Shows of (Attrib.binding * ('term * 'term list) list) list | |
29578 | 12 |
Obtains of (binding * ((binding * 'typ option) list * 'term list)) list |
26336
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents:
25739
diff
changeset
|
13 |
type statement = (string, string) stmt |
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents:
25739
diff
changeset
|
14 |
type statement_i = (typ, term) stmt |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
15 |
datatype ('typ, 'term, 'fact) ctxt = |
29578 | 16 |
Fixes of (binding * 'typ option * mixfix) list | |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
17 |
Constrains of (string * 'typ) list | |
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
18 |
Assumes of (Attrib.binding * ('term * 'term list) list) list | |
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
19 |
Defines of (Attrib.binding * ('term * 'term list)) list | |
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
20 |
Notes of string * (Attrib.binding * ('fact * Attrib.src list) list) list |
26336
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents:
25739
diff
changeset
|
21 |
type context = (string, string, Facts.ref) ctxt |
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents:
25739
diff
changeset
|
22 |
type context_i = (typ, term, thm list) ctxt |
21581 | 23 |
val facts_map: (('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt) -> |
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
24 |
(Attrib.binding * ('fact * Attrib.src list) list) list -> |
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
25 |
(Attrib.binding * ('c * Attrib.src list) list) list |
29603 | 26 |
val map_ctxt: {binding: binding -> binding, typ: 'typ -> 'a, term: 'term -> 'b, |
27 |
pattern: 'term -> 'b, fact: 'fact -> 'c, attrib: Attrib.src -> Attrib.src} -> |
|
28 |
('typ, 'term, 'fact) ctxt -> ('a, 'b, 'c) ctxt |
|
21528 | 29 |
val map_ctxt_attrib: (Attrib.src -> Attrib.src) -> |
30 |
('typ, 'term, 'fact) ctxt -> ('typ, 'term, 'fact) ctxt |
|
21481 | 31 |
val morph_ctxt: morphism -> context_i -> context_i |
19777 | 32 |
val pretty_stmt: Proof.context -> statement_i -> Pretty.T list |
33 |
val pretty_ctxt: Proof.context -> context_i -> Pretty.T list |
|
34 |
val pretty_statement: Proof.context -> string -> thm -> Pretty.T |
|
35 |
type witness |
|
29578 | 36 |
val prove_witness: Proof.context -> term -> tactic -> witness |
37 |
val witness_proof: (witness list list -> Proof.context -> Proof.context) -> |
|
38 |
term list list -> Proof.context -> Proof.state |
|
39 |
val witness_proof_eqs: (witness list list -> thm list -> Proof.context -> Proof.context) -> |
|
40 |
term list list -> term list -> Proof.context -> Proof.state |
|
41 |
val witness_local_proof: (witness list list -> Proof.state -> Proof.state) -> |
|
42 |
string -> term list list -> Proof.context -> bool -> Proof.state -> Proof.state |
|
21481 | 43 |
val morph_witness: morphism -> witness -> witness |
19777 | 44 |
val conclude_witness: witness -> thm |
22658
263d42253f53
Experimental interpretation code for definitions.
ballarin
parents:
22568
diff
changeset
|
45 |
val pretty_witness: Proof.context -> witness -> Pretty.T |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
46 |
val instT_type: typ Symtab.table -> typ -> typ |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
47 |
val instT_term: typ Symtab.table -> term -> term |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
48 |
val instT_thm: theory -> typ Symtab.table -> thm -> thm |
21481 | 49 |
val instT_morphism: theory -> typ Symtab.table -> morphism |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
50 |
val inst_term: typ Symtab.table * term Symtab.table -> term -> term |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
51 |
val inst_thm: theory -> typ Symtab.table * term Symtab.table -> thm -> thm |
21481 | 52 |
val inst_morphism: theory -> typ Symtab.table * term Symtab.table -> morphism |
19777 | 53 |
val satisfy_thm: witness list -> thm -> thm |
21481 | 54 |
val satisfy_morphism: witness list -> morphism |
20264 | 55 |
val satisfy_facts: witness list -> |
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
56 |
(Attrib.binding * (thm list * Attrib.src list) list) list -> |
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
57 |
(Attrib.binding * (thm list * Attrib.src list) list) list |
21581 | 58 |
val generalize_facts: Proof.context -> Proof.context -> |
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
59 |
(Attrib.binding * (thm list * Attrib.src list) list) list -> |
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
60 |
(Attrib.binding * (thm list * Attrib.src list) list) list |
29525 | 61 |
val eq_morphism: theory -> thm list -> morphism |
29218 | 62 |
val transfer_morphism: theory -> morphism |
29217
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29215
diff
changeset
|
63 |
val activate: (typ, term, Facts.ref) ctxt list -> Proof.context -> context_i list * Proof.context |
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29215
diff
changeset
|
64 |
val activate_i: context_i list -> Proof.context -> context_i list * Proof.context |
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 |
|
19777 | 70 |
(** language elements **) |
71 |
||
72 |
(* statement *) |
|
19259 | 73 |
|
74 |
datatype ('typ, 'term) stmt = |
|
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
75 |
Shows of (Attrib.binding * ('term * 'term list) list) list | |
29578 | 76 |
Obtains of (binding * ((binding * 'typ option) list * 'term list)) list; |
19259 | 77 |
|
78 |
type statement = (string, string) stmt; |
|
79 |
type statement_i = (typ, term) stmt; |
|
80 |
||
81 |
||
19777 | 82 |
(* context *) |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
83 |
|
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
84 |
datatype ('typ, 'term, 'fact) ctxt = |
29578 | 85 |
Fixes of (binding * 'typ option * mixfix) list | |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
86 |
Constrains of (string * 'typ) list | |
28084
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
87 |
Assumes of (Attrib.binding * ('term * 'term list) list) list | |
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
88 |
Defines of (Attrib.binding * ('term * 'term list)) list | |
a05ca48ef263
type Attrib.binding abbreviates Name.binding without attributes;
wenzelm
parents:
28079
diff
changeset
|
89 |
Notes of string * (Attrib.binding * ('fact * Attrib.src list) list) list; |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
90 |
|
26336
a0e2b706ce73
renamed datatype thmref to Facts.ref, tuned interfaces;
wenzelm
parents:
25739
diff
changeset
|
91 |
type context = (string, string, Facts.ref) ctxt; |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
92 |
type context_i = (typ, term, thm list) ctxt; |
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
93 |
|
21581 | 94 |
fun facts_map f facts = Notes ("", facts) |> f |> (fn Notes (_, facts') => facts'); |
95 |
||
29603 | 96 |
fun map_ctxt {binding, typ, term, pattern, fact, attrib} = |
97 |
fn Fixes fixes => Fixes (fixes |> map (fn (x, T, mx) => (binding x, Option.map typ T, mx))) |
|
28079
955c42c8a5e4
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27865
diff
changeset
|
98 |
| Constrains xs => Constrains (xs |> map (fn (x, T) => |
30223
24d975352879
renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
wenzelm
parents:
30219
diff
changeset
|
99 |
(Binding.name_of (binding (Binding.name x)), typ T))) |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
100 |
| Assumes asms => Assumes (asms |> map (fn ((a, atts), propps) => |
29603 | 101 |
((binding a, map attrib atts), propps |> map (fn (t, ps) => (term t, map pattern ps))))) |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
102 |
| Defines defs => Defines (defs |> map (fn ((a, atts), (t, ps)) => |
29603 | 103 |
((binding a, map attrib atts), (term t, map pattern ps)))) |
21440 | 104 |
| Notes (kind, facts) => Notes (kind, facts |> map (fn ((a, atts), bs) => |
28965 | 105 |
((binding a, map attrib atts), bs |> map (fn (ths, btts) => (fact ths, map attrib btts))))); |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
106 |
|
21528 | 107 |
fun map_ctxt_attrib attrib = |
29603 | 108 |
map_ctxt {binding = I, typ = I, term = I, pattern = I, fact = I, attrib = attrib}; |
21528 | 109 |
|
21481 | 110 |
fun morph_ctxt phi = map_ctxt |
28965 | 111 |
{binding = Morphism.binding phi, |
21481 | 112 |
typ = Morphism.typ phi, |
113 |
term = Morphism.term phi, |
|
29603 | 114 |
pattern = Morphism.term phi, |
21521 | 115 |
fact = Morphism.fact phi, |
21481 | 116 |
attrib = Args.morph_values phi}; |
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
117 |
|
19808 | 118 |
|
18894 | 119 |
|
19259 | 120 |
(** pretty printing **) |
121 |
||
19267 | 122 |
fun pretty_items _ _ [] = [] |
123 |
| pretty_items keyword sep (x :: ys) = |
|
124 |
Pretty.block [Pretty.keyword keyword, Pretty.brk 1, x] :: |
|
125 |
map (fn y => Pretty.block [Pretty.str " ", Pretty.keyword sep, Pretty.brk 1, y]) ys; |
|
19259 | 126 |
|
28862 | 127 |
fun pretty_name_atts ctxt (b, atts) sep = |
30219 | 128 |
if Binding.is_empty b andalso null atts then [] |
129 |
else [Pretty.block (Pretty.breaks |
|
130 |
(Pretty.str (Binding.str_of b) :: Attrib.pretty_attribs ctxt atts @ [Pretty.str sep]))]; |
|
19259 | 131 |
|
132 |
||
133 |
(* pretty_stmt *) |
|
134 |
||
135 |
fun pretty_stmt ctxt = |
|
136 |
let |
|
24920 | 137 |
val prt_typ = Pretty.quote o Syntax.pretty_typ ctxt; |
138 |
val prt_term = Pretty.quote o Syntax.pretty_term ctxt; |
|
19267 | 139 |
val prt_terms = separate (Pretty.keyword "and") o map prt_term; |
19259 | 140 |
val prt_name_atts = pretty_name_atts ctxt; |
141 |
||
142 |
fun prt_show (a, ts) = |
|
19267 | 143 |
Pretty.block (Pretty.breaks (prt_name_atts a ":" @ prt_terms (map fst ts))); |
19259 | 144 |
|
28079
955c42c8a5e4
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27865
diff
changeset
|
145 |
fun prt_var (x, SOME T) = Pretty.block |
30223
24d975352879
renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
wenzelm
parents:
30219
diff
changeset
|
146 |
[Pretty.str (Binding.name_of x ^ " ::"), Pretty.brk 1, prt_typ T] |
24d975352879
renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
wenzelm
parents:
30219
diff
changeset
|
147 |
| prt_var (x, NONE) = Pretty.str (Binding.name_of x); |
26721 | 148 |
val prt_vars = separate (Pretty.keyword "and") o map prt_var; |
19259 | 149 |
|
19267 | 150 |
fun prt_obtain (_, ([], ts)) = Pretty.block (Pretty.breaks (prt_terms ts)) |
19259 | 151 |
| prt_obtain (_, (xs, ts)) = Pretty.block (Pretty.breaks |
19585 | 152 |
(prt_vars xs @ [Pretty.keyword "where"] @ prt_terms ts)); |
19259 | 153 |
in |
19267 | 154 |
fn Shows shows => pretty_items "shows" "and" (map prt_show shows) |
155 |
| Obtains obtains => pretty_items "obtains" "|" (map prt_obtain obtains) |
|
19259 | 156 |
end; |
157 |
||
18894 | 158 |
|
19259 | 159 |
(* pretty_ctxt *) |
160 |
||
161 |
fun pretty_ctxt ctxt = |
|
162 |
let |
|
24920 | 163 |
val prt_typ = Pretty.quote o Syntax.pretty_typ ctxt; |
164 |
val prt_term = Pretty.quote o Syntax.pretty_term ctxt; |
|
19259 | 165 |
val prt_thm = Pretty.backquote o ProofContext.pretty_thm ctxt; |
166 |
val prt_name_atts = pretty_name_atts ctxt; |
|
167 |
||
19267 | 168 |
fun prt_mixfix NoSyn = [] |
169 |
| prt_mixfix mx = [Pretty.brk 2, Syntax.pretty_mixfix mx]; |
|
170 |
||
30223
24d975352879
renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
wenzelm
parents:
30219
diff
changeset
|
171 |
fun prt_fix (x, SOME T, mx) = Pretty.block (Pretty.str (Binding.name_of x ^ " ::") :: |
28079
955c42c8a5e4
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27865
diff
changeset
|
172 |
Pretty.brk 1 :: prt_typ T :: Pretty.brk 1 :: prt_mixfix mx) |
30223
24d975352879
renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
wenzelm
parents:
30219
diff
changeset
|
173 |
| prt_fix (x, NONE, mx) = Pretty.block (Pretty.str (Binding.name_of x) :: |
28079
955c42c8a5e4
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27865
diff
changeset
|
174 |
Pretty.brk 1 :: prt_mixfix mx); |
28965 | 175 |
fun prt_constrain (x, T) = prt_fix (Binding.name x, SOME T, NoSyn); |
18894 | 176 |
|
19259 | 177 |
fun prt_asm (a, ts) = |
178 |
Pretty.block (Pretty.breaks (prt_name_atts a ":" @ map (prt_term o fst) ts)); |
|
179 |
fun prt_def (a, (t, _)) = |
|
180 |
Pretty.block (Pretty.breaks (prt_name_atts a ":" @ [prt_term t])); |
|
181 |
||
182 |
fun prt_fact (ths, []) = map prt_thm ths |
|
183 |
| prt_fact (ths, atts) = Pretty.enclose "(" ")" |
|
21032 | 184 |
(Pretty.breaks (map prt_thm ths)) :: Attrib.pretty_attribs ctxt atts; |
19259 | 185 |
fun prt_note (a, ths) = |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19466
diff
changeset
|
186 |
Pretty.block (Pretty.breaks (flat (prt_name_atts a "=" :: map prt_fact ths))); |
19259 | 187 |
in |
19267 | 188 |
fn Fixes fixes => pretty_items "fixes" "and" (map prt_fix fixes) |
189 |
| Constrains xs => pretty_items "constrains" "and" (map prt_constrain xs) |
|
190 |
| Assumes asms => pretty_items "assumes" "and" (map prt_asm asms) |
|
191 |
| Defines defs => pretty_items "defines" "and" (map prt_def defs) |
|
21440 | 192 |
| Notes ("", facts) => pretty_items "notes" "and" (map prt_note facts) |
193 |
| Notes (kind, facts) => pretty_items ("notes " ^ kind) "and" (map prt_note facts) |
|
19259 | 194 |
end; |
18894 | 195 |
|
19267 | 196 |
|
197 |
(* pretty_statement *) |
|
198 |
||
199 |
local |
|
200 |
||
201 |
fun thm_name kind th prts = |
|
202 |
let val head = |
|
27865
27a8ad9612a3
moved basic thm operations from structure PureThy to Thm (cf. more_thm.ML);
wenzelm
parents:
26721
diff
changeset
|
203 |
if Thm.has_name_hint th then |
21965
7120ef5bc378
pretty_statement: more careful handling of name_hint;
wenzelm
parents:
21646
diff
changeset
|
204 |
Pretty.block [Pretty.command kind, |
27865
27a8ad9612a3
moved basic thm operations from structure PureThy to Thm (cf. more_thm.ML);
wenzelm
parents:
26721
diff
changeset
|
205 |
Pretty.brk 1, Pretty.str (Sign.base_name (Thm.get_name_hint th) ^ ":")] |
21965
7120ef5bc378
pretty_statement: more careful handling of name_hint;
wenzelm
parents:
21646
diff
changeset
|
206 |
else Pretty.command kind |
19267 | 207 |
in Pretty.block (Pretty.fbreaks (head :: prts)) end; |
208 |
||
28965 | 209 |
fun fix (x, T) = (Binding.name x, SOME T); |
28079
955c42c8a5e4
explicit type Name.binding for higher-specification elements;
wenzelm
parents:
27865
diff
changeset
|
210 |
|
19267 | 211 |
fun obtain prop ctxt = |
212 |
let |
|
20150 | 213 |
val ((xs, prop'), ctxt') = Variable.focus prop ctxt; |
214 |
val As = Logic.strip_imp_prems (Thm.term_of prop'); |
|
28965 | 215 |
in ((Binding.empty, (map (fix o Term.dest_Free o Thm.term_of) xs, As)), ctxt') end; |
19267 | 216 |
|
217 |
in |
|
218 |
||
219 |
fun pretty_statement ctxt kind raw_th = |
|
220 |
let |
|
221 |
val thy = ProofContext.theory_of ctxt; |
|
20150 | 222 |
val cert = Thm.cterm_of thy; |
223 |
||
21605 | 224 |
val th = MetaSimplifier.norm_hhf raw_th; |
20150 | 225 |
val is_elim = ObjectLogic.is_elim th; |
19267 | 226 |
|
26716
8690e75e1395
print_statement: reset body mode, i.e. invent global frees (no need for revert_skolem);
wenzelm
parents:
26628
diff
changeset
|
227 |
val ((_, [th']), ctxt') = Variable.import_thms true [th] (Variable.set_body false ctxt); |
20150 | 228 |
val prop = Thm.prop_of th'; |
229 |
val (prems, concl) = Logic.strip_horn prop; |
|
230 |
val concl_term = ObjectLogic.drop_judgment thy concl; |
|
19267 | 231 |
|
20150 | 232 |
val fixes = fold_aterms (fn v as Free (x, T) => |
233 |
if Variable.newly_fixed ctxt' ctxt x andalso not (v aconv concl_term) |
|
26716
8690e75e1395
print_statement: reset body mode, i.e. invent global frees (no need for revert_skolem);
wenzelm
parents:
26628
diff
changeset
|
234 |
then insert (op =) (x, T) else I | _ => I) prop [] |> rev; |
20150 | 235 |
val (assumes, cases) = take_suffix (fn prem => |
236 |
is_elim andalso concl aconv Logic.strip_assums_concl prem) prems; |
|
19267 | 237 |
in |
28965 | 238 |
pretty_ctxt ctxt' (Fixes (map (fn (x, T) => (Binding.name x, SOME T, NoSyn)) fixes)) @ |
239 |
pretty_ctxt ctxt' (Assumes (map (fn t => (Attrib.empty_binding, [(t, [])])) assumes)) @ |
|
240 |
(if null cases then pretty_stmt ctxt' (Shows [(Attrib.empty_binding, [(concl, [])])]) |
|
26716
8690e75e1395
print_statement: reset body mode, i.e. invent global frees (no need for revert_skolem);
wenzelm
parents:
26628
diff
changeset
|
241 |
else |
8690e75e1395
print_statement: reset body mode, i.e. invent global frees (no need for revert_skolem);
wenzelm
parents:
26628
diff
changeset
|
242 |
let val (clauses, ctxt'') = fold_map (obtain o cert) cases ctxt' |
8690e75e1395
print_statement: reset body mode, i.e. invent global frees (no need for revert_skolem);
wenzelm
parents:
26628
diff
changeset
|
243 |
in pretty_stmt ctxt'' (Obtains clauses) end) |
19267 | 244 |
end |> thm_name kind raw_th; |
245 |
||
18140
691c64d615a5
Explicit data structures for some Isar language elements.
wenzelm
parents:
diff
changeset
|
246 |
end; |
19267 | 247 |
|
19777 | 248 |
|
249 |
||
250 |
(** logical operations **) |
|
251 |
||
252 |
(* witnesses -- hypotheses as protected facts *) |
|
253 |
||
254 |
datatype witness = Witness of term * thm; |
|
255 |
||
29578 | 256 |
val mark_witness = Logic.protect; |
257 |
fun witness_prop (Witness (t, _)) = t; |
|
258 |
fun witness_hyps (Witness (_, th)) = #hyps (Thm.rep_thm th); |
|
19777 | 259 |
fun map_witness f (Witness witn) = Witness (f witn); |
260 |
||
21481 | 261 |
fun morph_witness phi = map_witness (fn (t, th) => (Morphism.term phi t, Morphism.thm phi th)); |
262 |
||
20058 | 263 |
fun prove_witness ctxt t tac = |
29578 | 264 |
Witness (t, Thm.close_derivation (Goal.prove ctxt [] [] (mark_witness t) (fn _ => |
25202
3a539d9995fb
proven witness: proper Goal.close_result save huge amounts of resources when using proof terms;
wenzelm
parents:
24920
diff
changeset
|
265 |
Tactic.rtac Drule.protectI 1 THEN tac))); |
19777 | 266 |
|
29603 | 267 |
|
29578 | 268 |
local |
269 |
||
270 |
val refine_witness = |
|
271 |
Proof.refine (Method.Basic (K (Method.RAW_METHOD |
|
272 |
(K (ALLGOALS |
|
273 |
(CONJUNCTS (ALLGOALS |
|
274 |
(CONJUNCTS (TRYALL (Tactic.rtac Drule.protectI)))))))), Position.none)); |
|
25624 | 275 |
|
29578 | 276 |
fun gen_witness_proof proof after_qed wit_propss eq_props = |
277 |
let |
|
278 |
val propss = (map o map) (fn prop => (mark_witness prop, [])) wit_propss |
|
279 |
@ [map (rpair []) eq_props]; |
|
280 |
fun after_qed' thmss = |
|
29603 | 281 |
let val (wits, eqs) = split_last ((map o map) Thm.close_derivation thmss); |
29578 | 282 |
in after_qed ((map2 o map2) (curry Witness) wit_propss wits) eqs end; |
283 |
in proof after_qed' propss #> refine_witness #> Seq.hd end; |
|
284 |
||
285 |
in |
|
286 |
||
287 |
fun witness_proof after_qed wit_propss = |
|
288 |
gen_witness_proof (Proof.theorem_i NONE) (fn wits => fn _ => after_qed wits) |
|
289 |
wit_propss []; |
|
290 |
||
291 |
val witness_proof_eqs = gen_witness_proof (Proof.theorem_i NONE); |
|
292 |
||
293 |
fun witness_local_proof after_qed cmd wit_propss goal_ctxt int = |
|
294 |
gen_witness_proof (fn after_qed' => fn propss => |
|
295 |
Proof.map_context (K goal_ctxt) |
|
296 |
#> Proof.local_goal (ProofDisplay.print_results int) (K I) ProofContext.bind_propp_i |
|
30211 | 297 |
cmd NONE after_qed' (map (pair Thm.empty_binding) propss)) |
29578 | 298 |
(fn wits => fn _ => after_qed wits) wit_propss []; |
299 |
||
29603 | 300 |
end; |
301 |
||
19777 | 302 |
|
25302 | 303 |
fun compose_witness (Witness (_, th)) r = |
304 |
let |
|
305 |
val th' = Goal.conclude th; |
|
306 |
val A = Thm.cprem_of r 1; |
|
25739 | 307 |
in |
308 |
Thm.implies_elim |
|
309 |
(Conv.gconv_rule Drule.beta_eta_conversion 1 r) |
|
310 |
(Conv.fconv_rule Drule.beta_eta_conversion |
|
311 |
(Thm.instantiate (Thm.match (Thm.cprop_of th', A)) th')) |
|
312 |
end; |
|
25302 | 313 |
|
29578 | 314 |
fun conclude_witness (Witness (_, th)) = |
315 |
Thm.close_derivation (MetaSimplifier.norm_hhf_protect (Goal.conclude th)); |
|
19777 | 316 |
|
22658
263d42253f53
Experimental interpretation code for definitions.
ballarin
parents:
22568
diff
changeset
|
317 |
fun pretty_witness ctxt witn = |
24920 | 318 |
let val prt_term = Pretty.quote o Syntax.pretty_term ctxt in |
22658
263d42253f53
Experimental interpretation code for definitions.
ballarin
parents:
22568
diff
changeset
|
319 |
Pretty.block (prt_term (witness_prop witn) :: |
263d42253f53
Experimental interpretation code for definitions.
ballarin
parents:
22568
diff
changeset
|
320 |
(if ! show_hyps then [Pretty.brk 2, Pretty.list "[" "]" |
263d42253f53
Experimental interpretation code for definitions.
ballarin
parents:
22568
diff
changeset
|
321 |
(map prt_term (witness_hyps witn))] else [])) |
263d42253f53
Experimental interpretation code for definitions.
ballarin
parents:
22568
diff
changeset
|
322 |
end; |
263d42253f53
Experimental interpretation code for definitions.
ballarin
parents:
22568
diff
changeset
|
323 |
|
19777 | 324 |
|
325 |
(* derived rules *) |
|
326 |
||
20007 | 327 |
fun instantiate_tfrees thy subst th = |
19777 | 328 |
let |
329 |
val certT = Thm.ctyp_of thy; |
|
20007 | 330 |
val idx = Thm.maxidx_of th + 1; |
331 |
fun cert_inst (a, (S, T)) = (certT (TVar ((a, idx), S)), certT T); |
|
332 |
||
333 |
fun add_inst (a, S) insts = |
|
334 |
if AList.defined (op =) insts a then insts |
|
335 |
else (case AList.lookup (op =) subst a of NONE => insts | SOME T => (a, (S, T)) :: insts); |
|
336 |
val insts = |
|
337 |
Term.fold_types (Term.fold_atyps (fn TFree v => add_inst v | _ => I)) |
|
338 |
(Thm.full_prop_of th) []; |
|
19777 | 339 |
in |
20007 | 340 |
th |
341 |
|> Thm.generalize (map fst insts, []) idx |
|
342 |
|> Thm.instantiate (map cert_inst insts, []) |
|
19777 | 343 |
end; |
344 |
||
345 |
fun instantiate_frees thy subst = |
|
346 |
let val cert = Thm.cterm_of thy in |
|
347 |
Drule.forall_intr_list (map (cert o Free o fst) subst) #> |
|
348 |
Drule.forall_elim_list (map (cert o snd) subst) |
|
349 |
end; |
|
350 |
||
351 |
fun hyps_rule rule th = |
|
21521 | 352 |
let val {hyps, ...} = Thm.crep_thm th in |
19777 | 353 |
Drule.implies_elim_list |
354 |
(rule (Drule.implies_intr_list hyps th)) |
|
21521 | 355 |
(map (Thm.assume o Drule.cterm_rule rule) hyps) |
19777 | 356 |
end; |
357 |
||
358 |
||
359 |
(* instantiate types *) |
|
360 |
||
361 |
fun instT_type env = |
|
362 |
if Symtab.is_empty env then I |
|
363 |
else Term.map_type_tfree (fn (x, S) => the_default (TFree (x, S)) (Symtab.lookup env x)); |
|
364 |
||
365 |
fun instT_term env = |
|
366 |
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
|
367 |
else Term.map_types (instT_type env); |
19777 | 368 |
|
22691 | 369 |
fun instT_subst env th = (Thm.fold_terms o Term.fold_types o Term.fold_atyps) |
20304 | 370 |
(fn T as TFree (a, _) => |
371 |
let val T' = the_default T (Symtab.lookup env a) |
|
372 |
in if T = T' then I else insert (op =) (a, T') end |
|
373 |
| _ => I) th []; |
|
19777 | 374 |
|
375 |
fun instT_thm thy env th = |
|
376 |
if Symtab.is_empty env then th |
|
377 |
else |
|
378 |
let val subst = instT_subst env th |
|
379 |
in if null subst then th else th |> hyps_rule (instantiate_tfrees thy subst) end; |
|
380 |
||
22672
777af26d5713
inst(T)_morphism: avoid reference to static theory value;
wenzelm
parents:
22658
diff
changeset
|
381 |
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
|
382 |
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
|
383 |
Morphism.morphism |
29603 | 384 |
{binding = I, |
22672
777af26d5713
inst(T)_morphism: avoid reference to static theory value;
wenzelm
parents:
22658
diff
changeset
|
385 |
typ = instT_type env, |
777af26d5713
inst(T)_morphism: avoid reference to static theory value;
wenzelm
parents:
22658
diff
changeset
|
386 |
term = instT_term env, |
777af26d5713
inst(T)_morphism: avoid reference to static theory value;
wenzelm
parents:
22658
diff
changeset
|
387 |
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
|
388 |
end; |
19777 | 389 |
|
390 |
||
391 |
(* instantiate types and terms *) |
|
392 |
||
393 |
fun inst_term (envT, env) = |
|
394 |
if Symtab.is_empty env then instT_term envT |
|
395 |
else |
|
396 |
let |
|
397 |
val instT = instT_type envT; |
|
398 |
fun inst (Const (x, T)) = Const (x, instT T) |
|
399 |
| inst (Free (x, T)) = |
|
400 |
(case Symtab.lookup env x of |
|
401 |
NONE => Free (x, instT T) |
|
402 |
| SOME t => t) |
|
403 |
| inst (Var (xi, T)) = Var (xi, instT T) |
|
404 |
| inst (b as Bound _) = b |
|
405 |
| inst (Abs (x, T, t)) = Abs (x, instT T, inst t) |
|
406 |
| inst (t $ u) = inst t $ inst u; |
|
407 |
in Envir.beta_norm o inst end; |
|
408 |
||
409 |
fun inst_thm thy (envT, env) th = |
|
410 |
if Symtab.is_empty env then instT_thm thy envT th |
|
411 |
else |
|
412 |
let |
|
413 |
val substT = instT_subst envT th; |
|
22691 | 414 |
val subst = (Thm.fold_terms o Term.fold_aterms) |
20304 | 415 |
(fn Free (x, T) => |
19777 | 416 |
let |
417 |
val T' = instT_type envT T; |
|
418 |
val t = Free (x, T'); |
|
419 |
val t' = the_default t (Symtab.lookup env x); |
|
20304 | 420 |
in if t aconv t' then I else insert (eq_fst (op =)) ((x, T'), t') end |
421 |
| _ => I) th []; |
|
19777 | 422 |
in |
423 |
if null substT andalso null subst then th |
|
424 |
else th |> hyps_rule |
|
425 |
(instantiate_tfrees thy substT #> |
|
426 |
instantiate_frees thy subst #> |
|
22900 | 427 |
Conv.fconv_rule (Thm.beta_conversion true)) |
19777 | 428 |
end; |
429 |
||
22672
777af26d5713
inst(T)_morphism: avoid reference to static theory value;
wenzelm
parents:
22658
diff
changeset
|
430 |
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
|
431 |
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
|
432 |
Morphism.morphism |
29603 | 433 |
{binding = I, |
22672
777af26d5713
inst(T)_morphism: avoid reference to static theory value;
wenzelm
parents:
22658
diff
changeset
|
434 |
typ = instT_type (#1 envs), |
777af26d5713
inst(T)_morphism: avoid reference to static theory value;
wenzelm
parents:
22658
diff
changeset
|
435 |
term = inst_term envs, |
777af26d5713
inst(T)_morphism: avoid reference to static theory value;
wenzelm
parents:
22658
diff
changeset
|
436 |
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
|
437 |
end; |
19777 | 438 |
|
439 |
||
440 |
(* satisfy hypotheses *) |
|
441 |
||
442 |
fun satisfy_thm witns thm = thm |> fold (fn hyp => |
|
443 |
(case find_first (fn Witness (t, _) => Thm.term_of hyp aconv t) witns of |
|
444 |
NONE => I |
|
25302 | 445 |
| SOME w => Thm.implies_intr hyp #> compose_witness w)) (#hyps (Thm.crep_thm thm)); |
19777 | 446 |
|
29603 | 447 |
val satisfy_morphism = Morphism.thm_morphism o satisfy_thm; |
448 |
val satisfy_facts = facts_map o morph_ctxt o satisfy_morphism; |
|
20264 | 449 |
|
450 |
||
29525 | 451 |
(* rewriting with equalities *) |
452 |
||
453 |
fun eq_morphism thy thms = Morphism.morphism |
|
29603 | 454 |
{binding = I, |
455 |
typ = I, |
|
29525 | 456 |
term = MetaSimplifier.rewrite_term thy thms [], |
457 |
fact = map (MetaSimplifier.rewrite_rule thms)}; |
|
458 |
||
459 |
||
20264 | 460 |
(* generalize type/term parameters *) |
461 |
||
462 |
val maxidx_atts = fold Args.maxidx_values; |
|
463 |
||
21581 | 464 |
fun generalize_facts inner outer facts = |
20264 | 465 |
let |
466 |
val thy = ProofContext.theory_of inner; |
|
467 |
val maxidx = |
|
468 |
fold (fn ((_, atts), bs) => maxidx_atts atts #> fold (maxidx_atts o #2) bs) facts ~1; |
|
21581 | 469 |
val exp_fact = map (Thm.adjust_maxidx_thm maxidx) #> Variable.export inner outer; |
21521 | 470 |
val exp_term = Drule.term_rule thy (singleton exp_fact); |
471 |
val exp_typ = Logic.type_map exp_term; |
|
29603 | 472 |
val morphism = Morphism.morphism {binding = I, typ = exp_typ, term = exp_term, fact = exp_fact}; |
21581 | 473 |
in facts_map (morph_ctxt morphism) facts end; |
20886
f26672c248ee
replaced generalize_facts by full export_(standard_)facts;
wenzelm
parents:
20548
diff
changeset
|
474 |
|
28832 | 475 |
|
29218 | 476 |
(* transfer to theory using closure *) |
477 |
||
478 |
fun transfer_morphism thy = |
|
29603 | 479 |
let val thy_ref = Theory.check_thy thy |
480 |
in Morphism.thm_morphism (fn th => transfer (Theory.deref thy_ref) th) end; |
|
481 |
||
29218 | 482 |
|
483 |
||
28832 | 484 |
(** activate in context, return elements and facts **) |
485 |
||
486 |
local |
|
487 |
||
488 |
fun activate_elem (Fixes fixes) ctxt = |
|
29217
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29215
diff
changeset
|
489 |
ctxt |> ProofContext.add_fixes_i fixes |> snd |
28832 | 490 |
| activate_elem (Constrains _) ctxt = |
29217
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29215
diff
changeset
|
491 |
ctxt |
28832 | 492 |
| activate_elem (Assumes asms) ctxt = |
493 |
let |
|
494 |
val asms' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) asms; |
|
495 |
val ts = maps (map #1 o #2) asms'; |
|
496 |
val (_, ctxt') = |
|
497 |
ctxt |> fold Variable.auto_fixes ts |
|
28888 | 498 |
|> ProofContext.add_assms_i Assumption.presume_export asms'; |
29217
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29215
diff
changeset
|
499 |
in ctxt' end |
28832 | 500 |
| activate_elem (Defines defs) ctxt = |
501 |
let |
|
502 |
val defs' = Attrib.map_specs (Attrib.attribute_i (ProofContext.theory_of ctxt)) defs; |
|
503 |
val asms = defs' |> map (fn ((name, atts), (t, ps)) => |
|
504 |
let val ((c, _), t') = LocalDefs.cert_def ctxt t |
|
30223
24d975352879
renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
wenzelm
parents:
30219
diff
changeset
|
505 |
in (t', ((Binding.map_name (Thm.def_name_optional c) name, atts), [(t', ps)])) end); |
28832 | 506 |
val (_, ctxt') = |
507 |
ctxt |> fold (Variable.auto_fixes o #1) asms |
|
508 |
|> ProofContext.add_assms_i LocalDefs.def_export (map #2 asms); |
|
29217
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29215
diff
changeset
|
509 |
in ctxt' end |
28832 | 510 |
| activate_elem (Notes (kind, facts)) ctxt = |
511 |
let |
|
512 |
val facts' = Attrib.map_facts (Attrib.attribute_i (ProofContext.theory_of ctxt)) facts; |
|
513 |
val (res, ctxt') = ctxt |> ProofContext.note_thmss_i kind facts'; |
|
29217
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29215
diff
changeset
|
514 |
in ctxt' end; |
28832 | 515 |
|
516 |
fun gen_activate prep_facts raw_elems ctxt = |
|
517 |
let |
|
29217
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29215
diff
changeset
|
518 |
fun activate elem ctxt = |
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29215
diff
changeset
|
519 |
let val elem' = (map_ctxt_attrib Args.assignable o prep_facts ctxt) elem |
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29215
diff
changeset
|
520 |
in (elem', activate_elem elem' ctxt) end |
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29215
diff
changeset
|
521 |
val (elems, ctxt') = fold_map activate raw_elems (ProofContext.qualified_names ctxt); |
a1c992fb3184
Finer-grained activation so that facts from earlier elements are available.
ballarin
parents:
29215
diff
changeset
|
522 |
in (elems |> map (map_ctxt_attrib Args.closure), ProofContext.restore_naming ctxt ctxt') end; |
28832 | 523 |
|
524 |
fun check_name name = |
|
525 |
if NameSpace.is_qualified name then error ("Illegal qualified name: " ^ quote name) |
|
526 |
else name; |
|
527 |
||
29603 | 528 |
fun prep_facts prep_name get intern ctxt = |
529 |
map_ctxt |
|
30223
24d975352879
renamed Binding.name_pos to Binding.make, renamed Binding.base_name to Binding.name_of, renamed Binding.map_base to Binding.map_name, added mandatory flag to Binding.qualify;
wenzelm
parents:
30219
diff
changeset
|
530 |
{binding = Binding.map_name prep_name, |
29603 | 531 |
typ = I, |
532 |
term = I, |
|
533 |
pattern = I, |
|
534 |
fact = get ctxt, |
|
535 |
attrib = intern (ProofContext.theory_of ctxt)}; |
|
28832 | 536 |
|
537 |
in |
|
538 |
||
539 |
fun activate x = gen_activate (prep_facts check_name ProofContext.get_fact Attrib.intern_src) x; |
|
540 |
fun activate_i x = gen_activate (K I) x; |
|
541 |
||
19267 | 542 |
end; |
28832 | 543 |
|
544 |
end; |