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