author | wenzelm |
Tue, 21 Jan 2025 19:26:39 +0100 | |
changeset 81946 | ee680c69de38 |
parent 81941 | cb8f396dd39f |
permissions | -rw-r--r-- |
28308 | 1 |
(* Title: HOL/Statespace/state_space.ML |
74588
3cc363e8bfb2
cleanup; add Apple reference
Norbert Schirmer <nschirmer@apple.com>
parents:
74586
diff
changeset
|
2 |
Author: Norbert Schirmer, TU Muenchen, 2007 |
3cc363e8bfb2
cleanup; add Apple reference
Norbert Schirmer <nschirmer@apple.com>
parents:
74586
diff
changeset
|
3 |
Author: Norbert Schirmer, Apple, 2021 |
25171 | 4 |
*) |
5 |
||
25408 | 6 |
signature STATE_SPACE = |
45362 | 7 |
sig |
8 |
val distinct_compsN : string |
|
9 |
val getN : string |
|
10 |
val putN : string |
|
11 |
val injectN : string |
|
12 |
val namespaceN : string |
|
13 |
val projectN : string |
|
14 |
val valuetypesN : string |
|
25408 | 15 |
|
45362 | 16 |
val namespace_definition : |
17 |
bstring -> |
|
18 |
typ -> |
|
46925 | 19 |
(xstring, string) Expression.expr * (binding * string option * mixfix) list -> |
45362 | 20 |
string list -> string list -> theory -> theory |
25408 | 21 |
|
45362 | 22 |
val define_statespace : |
23 |
string list -> |
|
24 |
string -> |
|
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
25 |
((string * bool) * (string list * bstring * (string * string) list)) list -> |
45362 | 26 |
(string * string) list -> theory -> theory |
27 |
val define_statespace_i : |
|
28 |
string option -> |
|
29 |
string list -> |
|
30 |
string -> |
|
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
31 |
((string * bool) * (typ list * bstring * (string * string) list)) list -> |
45362 | 32 |
(string * typ) list -> theory -> theory |
25408 | 33 |
|
45362 | 34 |
val statespace_decl : |
35 |
((string list * bstring) * |
|
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
36 |
(((string * bool) * (string list * xstring * (bstring * bstring) list)) list * |
45362 | 37 |
(bstring * string) list)) parser |
25408 | 38 |
|
45362 | 39 |
val neq_x_y : Proof.context -> term -> term -> thm option |
40 |
val distinctNameSolver : Simplifier.solver |
|
41 |
val distinctTree_tac : Proof.context -> int -> tactic |
|
42 |
val distinct_simproc : Simplifier.simproc |
|
25408 | 43 |
|
44 |
||
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
45 |
val is_statespace : Context.generic -> xstring -> bool |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
46 |
|
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
47 |
val get_comp' : Context.generic -> string -> (typ * string list) option |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
48 |
val get_comp : Context.generic -> string -> (typ * string) option (* legacy wrapper, eventually replace by primed version *) |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
49 |
val get_comps : Context.generic -> (typ * string list) Termtab.table |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
50 |
|
78029
f78cdc6fe971
more standard val silent = Attrib.setup_config_bool;
wenzelm
parents:
78028
diff
changeset
|
51 |
val silent : bool Config.T |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
52 |
|
45362 | 53 |
val gen_lookup_tr : Proof.context -> term -> string -> term |
54 |
val lookup_swap_tr : Proof.context -> term list -> term |
|
55 |
val lookup_tr : Proof.context -> term list -> term |
|
56 |
val lookup_tr' : Proof.context -> term list -> term |
|
25408 | 57 |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
58 |
val gen'_update_tr : |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
59 |
bool -> bool -> Proof.context -> string -> term -> term -> term |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
60 |
val gen_update_tr : (* legacy wrapper, eventually replace by primed version *) |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
61 |
bool -> Proof.context -> string -> term -> term -> term |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
62 |
|
45362 | 63 |
val update_tr : Proof.context -> term list -> term |
64 |
val update_tr' : Proof.context -> term list -> term |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
65 |
|
78030 | 66 |
val trace_data: Context.generic -> unit |
45362 | 67 |
end; |
25408 | 68 |
|
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
69 |
structure StateSpace : STATE_SPACE = |
25171 | 70 |
struct |
71 |
||
45362 | 72 |
(* Names *) |
25171 | 73 |
|
74 |
val distinct_compsN = "distinct_names" |
|
75 |
val namespaceN = "_namespace" |
|
76 |
val valuetypesN = "_valuetypes" |
|
77 |
val projectN = "project" |
|
78 |
val injectN = "inject" |
|
79 |
val getN = "get" |
|
80 |
val putN = "put" |
|
81 |
val project_injectL = "StateSpaceLocale.project_inject"; |
|
82 |
||
83 |
||
84 |
(* Library *) |
|
85 |
||
86 |
fun fold1 f xs = fold f (tl xs) (hd xs) |
|
87 |
fun fold1' f [] x = x |
|
88 |
| fold1' f xs _ = fold1 f xs |
|
26478 | 89 |
|
25171 | 90 |
fun sorted_subset eq [] ys = true |
91 |
| sorted_subset eq (x::xs) [] = false |
|
92 |
| sorted_subset eq (x::xs) (y::ys) = if eq (x,y) then sorted_subset eq xs ys |
|
93 |
else sorted_subset eq (x::xs) ys; |
|
94 |
||
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
95 |
fun comps_of_distinct_thm thm = Thm.prop_of thm |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
96 |
|> (fn (_$(_$t)) => DistinctTreeProver.dest_tree t) |> map (fst o dest_Free) |> sort_strings; |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
97 |
|
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
98 |
fun insert_tagged_distinct_thms tagged_thm tagged_thms = |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
99 |
let |
78028 | 100 |
fun ins t1 [] = [t1] |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
101 |
| ins (t1 as (names1, _)) ((t2 as (names2, _))::thms) = |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
102 |
if Ord_List.subset string_ord (names1, names2) then t2::thms |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
103 |
else if Ord_List.subset string_ord (names2, names1) then ins t1 thms |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
104 |
else t2 :: ins t1 thms |
78028 | 105 |
in |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
106 |
ins tagged_thm tagged_thms |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
107 |
end |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
108 |
|
78028 | 109 |
fun join_tagged_distinct_thms tagged_thms1 tagged_thms2 = |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
110 |
tagged_thms1 |> fold insert_tagged_distinct_thms tagged_thms2 |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
111 |
|
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
112 |
fun tag_distinct_thm thm = (comps_of_distinct_thm thm, thm) |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
113 |
val tag_distinct_thms = map tag_distinct_thm |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
114 |
|
78028 | 115 |
fun join_distinct_thms (thms1, thms2) = |
116 |
if pointer_eq (thms1, thms2) then thms1 |
|
117 |
else join_tagged_distinct_thms (tag_distinct_thms thms1) (tag_distinct_thms thms2) |> map snd |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
118 |
|
78028 | 119 |
fun insert_distinct_thm thm thms = join_distinct_thms (thms, [thm]) |
25171 | 120 |
|
121 |
||
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
122 |
fun join_declinfo_entry name (T1:typ, names1:string list) (T2, names2) = |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
123 |
let |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
124 |
fun typ_info T names = @{make_string} T ^ " " ^ Pretty.string_of (Pretty.str_list "(" ")" names); |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
125 |
in |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
126 |
if T1 = T2 then (T1, distinct (op =) (names1 @ names2)) |
78028 | 127 |
else error ("statespace component '" ^ name ^ "' disagrees on type:\n " ^ |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
128 |
typ_info T1 names1 ^ " vs. " ^ typ_info T2 names2 |
78028 | 129 |
) |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
130 |
end |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
131 |
fun guess_name (Free (x,_)) = x |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
132 |
| guess_name _ = "unknown" |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
133 |
|
78030 | 134 |
val join_declinfo = Termtab.join (fn t => uncurry (join_declinfo_entry (guess_name t))) |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
135 |
|
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
136 |
|
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
137 |
(* |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
138 |
A component might appear in *different* statespaces within the same context. However, it must |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
139 |
be mapped to the same type. Note that this information is currently only properly maintained |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
140 |
within contexts where all components are actually "fixes" and not arbitrary terms. Moreover, on |
78028 | 141 |
the theory level the info stays empty. |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
142 |
|
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
143 |
This means that on the theory level we do not make use of the syntax and the tree-based distinct simprocs. |
78028 | 144 |
N.B: It might still make sense (from a performance perspective) to overcome this limitation |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
145 |
and even use the simprocs when a statespace is interpreted for concrete names like HOL-strings. |
78028 | 146 |
Once the distinct-theorem is proven by the interpretation the simproc can use the positions in |
147 |
the tree to derive distinctness of two strings vs. HOL-string comparison. |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
148 |
*) |
26478 | 149 |
|
78030 | 150 |
type statespace_info = |
151 |
{args: (string * sort) list, (* type arguments *) |
|
152 |
parents: (typ list * string * string option list) list, |
|
153 |
(* type instantiation, state-space name, component renamings *) |
|
154 |
components: (string * typ) list, |
|
155 |
types: typ list (* range types of state space *) |
|
156 |
}; |
|
157 |
||
158 |
structure Data = Generic_Data |
|
33519 | 159 |
( |
78030 | 160 |
type T = |
161 |
(typ * string list) Termtab.table * (*declinfo: type, names of statespaces of component*) |
|
162 |
thm list Symtab.table * (*distinctthm: minimal list of maximal distinctness-assumptions for a component name*) |
|
163 |
statespace_info Symtab.table; |
|
164 |
val empty = (Termtab.empty, Symtab.empty, Symtab.empty); |
|
165 |
fun merge ((declinfo1, distinctthm1, statespaces1), (declinfo2, distinctthm2, statespaces2)) = |
|
166 |
(join_declinfo (declinfo1, declinfo2), |
|
167 |
Symtab.join (K join_distinct_thms) (distinctthm1, distinctthm2), |
|
168 |
Symtab.merge (K true) (statespaces1, statespaces2)); |
|
33519 | 169 |
); |
25171 | 170 |
|
78030 | 171 |
val get_declinfo = #1 o Data.get |
172 |
val get_distinctthm = #2 o Data.get |
|
173 |
val get_statespaces = #3 o Data.get |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
174 |
|
78030 | 175 |
val map_declinfo = Data.map o @{apply 3(1)} |
176 |
val map_distinctthm = Data.map o @{apply 3(2)} |
|
177 |
val map_statespaces = Data.map o @{apply 3(3)} |
|
25171 | 178 |
|
78030 | 179 |
fun trace_data context = |
180 |
tracing ("StateSpace.Data: " ^ @{make_string} |
|
181 |
{declinfo = get_declinfo context, |
|
182 |
distinctthm = get_distinctthm context, |
|
183 |
statespaces = get_statespaces context}) |
|
184 |
||
185 |
fun update_declinfo (n,v) = map_declinfo (fn declinfo => |
|
186 |
let val vs = apsnd single v |
|
187 |
in Termtab.map_default (n, vs) (join_declinfo_entry (guess_name n) vs) declinfo end); |
|
25171 | 188 |
|
46925 | 189 |
fun expression_no_pos (expr, fixes) : Expression.expression = |
190 |
(map (fn (name, inst) => ((name, Position.none), inst)) expr, fixes); |
|
191 |
||
25171 | 192 |
fun prove_interpretation_in ctxt_tac (name, expr) thy = |
193 |
thy |
|
67777
2d3c1091527b
Drop rewrite rule arguments of sublocale and interpretation implementations.
ballarin
parents:
67450
diff
changeset
|
194 |
|> Interpretation.global_sublocale_cmd (name, Position.none) (expression_no_pos expr) [] |
26478 | 195 |
|> Proof.global_terminal_proof |
49889
00ea087e83d8
more method position information, notably finished_pos after end of previous text;
wenzelm
parents:
49866
diff
changeset
|
196 |
((Method.Basic (fn ctxt => SIMPLE_METHOD (ctxt_tac ctxt)), Position.no_range), NONE) |
42361 | 197 |
|> Proof_Context.theory_of |
25171 | 198 |
|
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
199 |
fun add_locale name expr elems thy = |
45362 | 200 |
thy |
72536
589645894305
bundle mixins for locale and class specifications
haftmann
parents:
72186
diff
changeset
|
201 |
|> Expression.add_locale (Binding.name name) (Binding.name name) [] expr elems |
29362 | 202 |
|> snd |
33671 | 203 |
|> Local_Theory.exit; |
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
204 |
|
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
205 |
fun add_locale_cmd name expr elems thy = |
45362 | 206 |
thy |
72536
589645894305
bundle mixins for locale and class specifications
haftmann
parents:
72186
diff
changeset
|
207 |
|> Expression.add_locale_cmd (Binding.name name) Binding.empty [] (expression_no_pos expr) elems |
29362 | 208 |
|> snd |
33671 | 209 |
|> Local_Theory.exit; |
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
210 |
|
78030 | 211 |
fun is_statespace context name = |
212 |
Symtab.defined (get_statespaces context) (Locale.intern (Context.theory_of context) name) |
|
25171 | 213 |
|
78030 | 214 |
fun add_statespace name args parents components types = |
215 |
map_statespaces (Symtab.update_new (name, {args=args,parents=parents, components=components,types=types})) |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
216 |
|
78030 | 217 |
val get_statespace = Symtab.lookup o get_statespaces |
218 |
val the_statespace = the oo get_statespace |
|
25171 | 219 |
|
220 |
||
221 |
fun mk_free ctxt name = |
|
26478 | 222 |
if Variable.is_fixed ctxt name orelse Variable.is_declared ctxt name |
25171 | 223 |
then |
78028 | 224 |
let |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
225 |
val n' = Variable.intern_fixed ctxt name |> perhaps Long_Name.dest_hidden; |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
226 |
val free = Free (n', Proof_Context.infer_type ctxt (n', dummyT)) |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
227 |
in SOME (free) end |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
228 |
else (tracing ("variable not fixed or declared: " ^ name); NONE) |
26478 | 229 |
|
230 |
||
78040 | 231 |
fun get_dist_thm context name = |
232 |
Symtab.lookup_list (get_distinctthm context) name |
|
233 |
|> map (Thm.transfer'' context) |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
234 |
|
78028 | 235 |
fun get_dist_thm2 ctxt x y = |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
236 |
(let |
78028 | 237 |
val dist_thms = [x, y] |> map (#1 o dest_Free) |
78040 | 238 |
|> maps (get_dist_thm (Context.Proof ctxt)); |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
239 |
|
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
240 |
fun get_paths dist_thm = |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
241 |
let |
81946
ee680c69de38
misc tuning: prefer specific variants of Thm.dest_comb;
wenzelm
parents:
81941
diff
changeset
|
242 |
val ctree = Thm.cprop_of dist_thm |> Thm.dest_arg |> Thm.dest_arg; |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
243 |
val tree = Thm.term_of ctree; |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
244 |
val x_path = the (DistinctTreeProver.find_tree x tree); |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
245 |
val y_path = the (DistinctTreeProver.find_tree y tree); |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
246 |
in SOME (dist_thm, x_path, y_path) end |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
247 |
handle Option.Option => NONE |
78028 | 248 |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
249 |
val result = get_first get_paths dist_thms |
78028 | 250 |
in |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
251 |
result |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
252 |
end) |
78028 | 253 |
|
254 |
||
78030 | 255 |
fun get_comp' context name = |
256 |
mk_free (Context.proof_of context) name |
|
78028 | 257 |
|> Option.mapPartial (fn t => |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
258 |
let |
78030 | 259 |
val declinfo = get_declinfo context |
78028 | 260 |
in |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
261 |
case Termtab.lookup declinfo t of |
78028 | 262 |
NONE => (* during syntax phase, types of fixes might not yet be constrained completely *) |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
263 |
AList.lookup (fn (x, Free (n,_)) => n = x | _ => false) (Termtab.dest declinfo) name |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
264 |
| some => some |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
265 |
end) |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
266 |
|
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
267 |
(* legacy wrapper *) |
26478 | 268 |
fun get_comp ctxt name = |
78028 | 269 |
get_comp' ctxt name |> Option.map (apsnd (fn ns => if null ns then "" else hd ns)) |
25171 | 270 |
|
78030 | 271 |
val get_comps = get_declinfo |
272 |
||
25171 | 273 |
|
274 |
(*** Tactics ***) |
|
275 |
||
276 |
fun neq_x_y ctxt x y = |
|
277 |
(let |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
278 |
val (dist_thm, x_path, y_path) = the (get_dist_thm2 ctxt x y); |
60327 | 279 |
val thm = DistinctTreeProver.distinctTreeProver ctxt dist_thm x_path y_path; |
26478 | 280 |
in SOME thm |
45361 | 281 |
end handle Option.Option => NONE) |
25171 | 282 |
|
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
283 |
fun distinctTree_tac ctxt = SUBGOAL (fn (goal, i) => |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
284 |
(case goal of |
69597 | 285 |
Const (\<^const_name>\<open>Trueprop\<close>, _) $ |
286 |
(Const (\<^const_name>\<open>Not\<close>, _) $ |
|
287 |
(Const (\<^const_name>\<open>HOL.eq\<close>, _) $ (x as Free _) $ (y as Free _))) => |
|
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
288 |
(case neq_x_y ctxt x y of |
60754 | 289 |
SOME neq => resolve_tac ctxt [neq] i |
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
290 |
| NONE => no_tac) |
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
291 |
| _ => no_tac)); |
25171 | 292 |
|
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
50214
diff
changeset
|
293 |
val distinctNameSolver = mk_solver "distinctNameSolver" distinctTree_tac; |
25171 | 294 |
|
295 |
val distinct_simproc = |
|
78807
f6d2679ab6c1
more standard simproc_setup using ML antiquotation;
wenzelm
parents:
78095
diff
changeset
|
296 |
\<^simproc_setup>\<open>passive distinct_simproc ("x = y") = |
f6d2679ab6c1
more standard simproc_setup using ML antiquotation;
wenzelm
parents:
78095
diff
changeset
|
297 |
\<open>fn _ => fn ctxt => fn ct => |
61144 | 298 |
(case Thm.term_of ct of |
69597 | 299 |
Const (\<^const_name>\<open>HOL.eq\<close>,_) $ (x as Free _) $ (y as Free _) => |
61144 | 300 |
Option.map (fn neq => DistinctTreeProver.neq_to_eq_False OF [neq]) |
301 |
(neq_x_y ctxt x y) |
|
78807
f6d2679ab6c1
more standard simproc_setup using ML antiquotation;
wenzelm
parents:
78095
diff
changeset
|
302 |
| _ => NONE)\<close>\<close>; |
25171 | 303 |
|
26478 | 304 |
fun interprete_parent name dist_thm_name parent_expr thy = |
25171 | 305 |
let |
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
306 |
fun solve_tac ctxt = CSUBGOAL (fn (goal, i) => |
25171 | 307 |
let |
42361 | 308 |
val distinct_thm = Proof_Context.get_thm ctxt dist_thm_name; |
60327 | 309 |
val rule = DistinctTreeProver.distinct_implProver ctxt distinct_thm goal; |
60754 | 310 |
in resolve_tac ctxt [rule] i end); |
26478 | 311 |
|
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
312 |
fun tac ctxt = |
69017 | 313 |
Locale.intro_locales_tac {strict = true, eager = true} ctxt [] THEN ALLGOALS (solve_tac ctxt); |
26478 | 314 |
|
45362 | 315 |
in |
316 |
thy |> prove_interpretation_in tac (name, parent_expr) |
|
26478 | 317 |
end; |
25171 | 318 |
|
26478 | 319 |
fun namespace_definition name nameT parent_expr parent_comps new_comps thy = |
25171 | 320 |
let |
321 |
val all_comps = parent_comps @ new_comps; |
|
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
322 |
val vars = (map (fn n => (Binding.name n, NONE, NoSyn)) all_comps); |
25171 | 323 |
val dist_thm_name = distinct_compsN; |
324 |
||
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
325 |
val dist_thm_full_name = dist_thm_name; |
26478 | 326 |
|
38835
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
wenzelm
parents:
38715
diff
changeset
|
327 |
fun type_attr phi = Thm.declaration_attribute (fn thm => fn context => |
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
wenzelm
parents:
38715
diff
changeset
|
328 |
(case context of |
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
wenzelm
parents:
38715
diff
changeset
|
329 |
Context.Theory _ => context |
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
wenzelm
parents:
38715
diff
changeset
|
330 |
| Context.Proof ctxt => |
26478 | 331 |
let |
78030 | 332 |
val declinfo = get_declinfo context |
333 |
val tt = get_distinctthm context; |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
334 |
val all_names = comps_of_distinct_thm thm; |
78040 | 335 |
val thm0 = Thm.trim_context thm; |
336 |
fun upd name = Symtab.map_default (name, [thm0]) (insert_distinct_thm thm0) |
|
25171 | 337 |
|
338 |
val tt' = tt |> fold upd all_names; |
|
38835
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
wenzelm
parents:
38715
diff
changeset
|
339 |
val context' = |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
50214
diff
changeset
|
340 |
Context_Position.set_visible false ctxt |
80703 | 341 |
|> Simplifier.add_proc distinct_simproc |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
50214
diff
changeset
|
342 |
|> Context_Position.restore_visible ctxt |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
50214
diff
changeset
|
343 |
|> Context.Proof |
78030 | 344 |
|> map_declinfo (K declinfo) |
345 |
|> map_distinctthm (K tt'); |
|
38835
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
wenzelm
parents:
38715
diff
changeset
|
346 |
in context' end)); |
26478 | 347 |
|
78095 | 348 |
val attr = Attrib.internal \<^here> type_attr; |
25171 | 349 |
|
45362 | 350 |
val assume = |
351 |
((Binding.name dist_thm_name, [attr]), |
|
81941 | 352 |
[(HOLogic.mk_Trueprop |
69597 | 353 |
(Const (\<^const_name>\<open>all_distinct\<close>, Type (\<^type_name>\<open>tree\<close>, [nameT]) --> HOLogic.boolT) $ |
45362 | 354 |
DistinctTreeProver.mk_tree (fn n => Free (n, nameT)) nameT |
355 |
(sort fast_string_ord all_comps)), [])]); |
|
356 |
in |
|
357 |
thy |
|
358 |
|> add_locale name ([], vars) [Element.Assumes [assume]] |
|
359 |
|> Proof_Context.theory_of |
|
360 |
|> interprete_parent name dist_thm_full_name parent_expr |
|
25171 | 361 |
end; |
362 |
||
55972 | 363 |
fun encode_dot x = if x = #"." then #"_" else x; |
25171 | 364 |
|
365 |
fun encode_type (TFree (s, _)) = s |
|
366 |
| encode_type (TVar ((s,i),_)) = "?" ^ s ^ string_of_int i |
|
26478 | 367 |
| encode_type (Type (n,Ts)) = |
25171 | 368 |
let |
369 |
val Ts' = fold1' (fn x => fn y => x ^ "_" ^ y) (map encode_type Ts) ""; |
|
32651 | 370 |
val n' = String.map encode_dot n; |
25171 | 371 |
in if Ts'="" then n' else Ts' ^ "_" ^ n' end; |
372 |
||
373 |
fun project_name T = projectN ^"_"^encode_type T; |
|
374 |
fun inject_name T = injectN ^"_"^encode_type T; |
|
375 |
||
376 |
||
377 |
fun add_declaration name decl thy = |
|
378 |
thy |
|
72536
589645894305
bundle mixins for locale and class specifications
haftmann
parents:
72186
diff
changeset
|
379 |
|> Named_Target.init [] name |
78095 | 380 |
|> (fn lthy => |
381 |
Local_Theory.declaration {syntax = true, pervasive = false, pos = Position.thread_data ()} |
|
382 |
(decl lthy) lthy) |
|
33671 | 383 |
|> Local_Theory.exit_global; |
25171 | 384 |
|
385 |
fun parent_components thy (Ts, pname, renaming) = |
|
386 |
let |
|
387 |
fun rename [] xs = xs |
|
388 |
| rename (NONE::rs) (x::xs) = x::rename rs xs |
|
389 |
| rename (SOME r::rs) ((x,T)::xs) = (r,T)::rename rs xs; |
|
78030 | 390 |
val {args, parents, components, ...} = the_statespace (Context.Theory thy) pname; |
25171 | 391 |
val inst = map fst args ~~ Ts; |
392 |
val subst = Term.map_type_tfree (the o AList.lookup (op =) inst o fst); |
|
26478 | 393 |
val parent_comps = |
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
394 |
maps (fn (Ts',n,rs) => parent_components thy (map subst Ts', n, rs)) parents; |
25171 | 395 |
val all_comps = rename renaming (parent_comps @ map (apsnd subst) components); |
396 |
in all_comps end; |
|
397 |
||
398 |
fun statespace_definition state_type args name parents parent_comps components thy = |
|
26478 | 399 |
let |
28965 | 400 |
val full_name = Sign.full_bname thy name; |
25171 | 401 |
val all_comps = parent_comps @ components; |
402 |
||
403 |
val components' = map (fn (n,T) => (n,(T,full_name))) components; |
|
404 |
||
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
405 |
fun parent_expr (prefix, (_, n, rs)) = |
67450
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
406 |
(suffix namespaceN n, (prefix, (Expression.Positional rs,[]))); |
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
407 |
val parents_expr = map parent_expr parents; |
25171 | 408 |
fun distinct_types Ts = |
27276 | 409 |
let val tab = fold (fn T => fn tab => Typtab.update (T,()) tab) Ts Typtab.empty; |
410 |
in map fst (Typtab.dest tab) end; |
|
25171 | 411 |
|
412 |
val Ts = distinct_types (map snd all_comps); |
|
413 |
val arg_names = map fst args; |
|
43324
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
43278
diff
changeset
|
414 |
val valueN = singleton (Name.variant_list arg_names) "'value"; |
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
43278
diff
changeset
|
415 |
val nameN = singleton (Name.variant_list (valueN :: arg_names)) "'name"; |
25171 | 416 |
val valueT = TFree (valueN, Sign.defaultS thy); |
417 |
val nameT = TFree (nameN, Sign.defaultS thy); |
|
418 |
val stateT = nameT --> valueT; |
|
419 |
fun projectT T = valueT --> T; |
|
26478 | 420 |
fun injectT T = T --> valueT; |
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
421 |
val locinsts = map (fn T => (project_injectL, |
67450
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
422 |
((encode_type T,false),(Expression.Positional |
45362 | 423 |
[SOME (Free (project_name T,projectT T)), |
67450
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
424 |
SOME (Free ((inject_name T,injectT T)))],[])))) Ts; |
32952 | 425 |
val locs = maps (fn T => [(Binding.name (project_name T),NONE,NoSyn), |
426 |
(Binding.name (inject_name T),NONE,NoSyn)]) Ts; |
|
427 |
val constrains = maps (fn T => [(project_name T,projectT T),(inject_name T,injectT T)]) Ts; |
|
25171 | 428 |
|
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
429 |
fun interprete_parent_valuetypes (prefix, (Ts, pname, _)) thy = |
25171 | 430 |
let |
78030 | 431 |
val {args,types,...} = the_statespace (Context.Theory thy) pname; |
25171 | 432 |
val inst = map fst args ~~ Ts; |
433 |
val subst = Term.map_type_tfree (the o AList.lookup (op =) inst o fst); |
|
32952 | 434 |
val pars = maps ((fn T => [project_name T,inject_name T]) o subst) types; |
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
435 |
|
45362 | 436 |
val expr = ([(suffix valuetypesN name, |
67450
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
437 |
(prefix, (Expression.Positional (map SOME pars),[])))],[]); |
29291 | 438 |
in |
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
57181
diff
changeset
|
439 |
prove_interpretation_in (fn ctxt => ALLGOALS (solve_tac ctxt (Assumption.all_prems_of ctxt))) |
29291 | 440 |
(suffix valuetypesN name, expr) thy |
441 |
end; |
|
25171 | 442 |
|
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
443 |
fun interprete_parent (prefix, (_, pname, rs)) = |
25171 | 444 |
let |
67450
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
445 |
val expr = ([(pname, (prefix, (Expression.Positional rs,[])))],[]) |
26478 | 446 |
in prove_interpretation_in |
69017 | 447 |
(fn ctxt => Locale.intro_locales_tac {strict = true, eager = false} ctxt []) |
25171 | 448 |
(full_name, expr) end; |
449 |
||
450 |
fun declare_declinfo updates lthy phi ctxt = |
|
451 |
let |
|
26478 | 452 |
fun upd_prf ctxt = |
25171 | 453 |
let |
454 |
fun upd (n,v) = |
|
455 |
let |
|
42361 | 456 |
val nT = Proof_Context.infer_type (Local_Theory.target_of lthy) (n, dummyT) |
26478 | 457 |
in Context.proof_map |
458 |
(update_declinfo (Morphism.term phi (Free (n,nT)),v)) |
|
25171 | 459 |
end; |
78028 | 460 |
val ctxt' = ctxt |> fold upd updates |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
461 |
in ctxt' end; |
25171 | 462 |
|
463 |
in Context.mapping I upd_prf ctxt end; |
|
464 |
||
26478 | 465 |
fun string_of_typ T = |
80866
8c67b14fdd48
clarified internal tool output: prefer Pretty.pure_string_of over manipulation of print_mode;
wenzelm
parents:
80703
diff
changeset
|
466 |
Pretty.pure_string_of |
8c67b14fdd48
clarified internal tool output: prefer Pretty.pure_string_of over manipulation of print_mode;
wenzelm
parents:
80703
diff
changeset
|
467 |
(Syntax.pretty_typ (Config.put show_sorts true (Syntax.init_pretty_global thy)) T); |
25171 | 468 |
val fixestate = (case state_type of |
469 |
NONE => [] |
|
26478 | 470 |
| SOME s => |
471 |
let |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
32952
diff
changeset
|
472 |
val fx = Element.Fixes [(Binding.name s,SOME (string_of_typ stateT),NoSyn)]; |
26478 | 473 |
val cs = Element.Constrains |
25171 | 474 |
(map (fn (n,T) => (n,string_of_typ T)) |
475 |
((map (fn (n,_) => (n,nameT)) all_comps) @ |
|
476 |
constrains)) |
|
477 |
in [fx,cs] end |
|
478 |
) |
|
479 |
||
26478 | 480 |
|
481 |
in thy |
|
482 |
|> namespace_definition |
|
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
483 |
(suffix namespaceN name) nameT (parents_expr,[]) |
25171 | 484 |
(map fst parent_comps) (map fst components) |
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
485 |
|> Context.theory_map (add_statespace full_name args (map snd parents) components []) |
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
486 |
|> add_locale (suffix valuetypesN name) (locinsts,locs) [] |
45362 | 487 |
|> Proof_Context.theory_of |
26478 | 488 |
|> fold interprete_parent_valuetypes parents |
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
489 |
|> add_locale_cmd name |
67450
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
490 |
([(suffix namespaceN full_name ,(("",false),(Expression.Named [],[]))), |
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
491 |
(suffix valuetypesN full_name,(("",false),(Expression.Named [],[])))],[]) fixestate |
45362 | 492 |
|> Proof_Context.theory_of |
25171 | 493 |
|> fold interprete_parent parents |
38389
d7d915bae307
Named_Target.init: empty string represents theory target
haftmann
parents:
38350
diff
changeset
|
494 |
|> add_declaration full_name (declare_declinfo components') |
25171 | 495 |
end; |
496 |
||
497 |
||
498 |
(* prepare arguments *) |
|
499 |
||
36149 | 500 |
fun read_typ ctxt raw_T env = |
501 |
let |
|
502 |
val ctxt' = fold (Variable.declare_typ o TFree) env ctxt; |
|
503 |
val T = Syntax.read_typ ctxt' raw_T; |
|
45741 | 504 |
val env' = Term.add_tfreesT T env; |
36149 | 505 |
in (T, env') end; |
506 |
||
507 |
fun cert_typ ctxt raw_T env = |
|
508 |
let |
|
42361 | 509 |
val thy = Proof_Context.theory_of ctxt; |
36149 | 510 |
val T = Type.no_tvars (Sign.certify_typ thy raw_T) |
511 |
handle TYPE (msg, _, _) => error msg; |
|
45741 | 512 |
val env' = Term.add_tfreesT T env; |
36149 | 513 |
in (T, env') end; |
514 |
||
25171 | 515 |
fun gen_define_statespace prep_typ state_space args name parents comps thy = |
516 |
let (* - args distinct |
|
517 |
- only args may occur in comps and parent-instantiations |
|
26478 | 518 |
- number of insts must match parent args |
25171 | 519 |
- no duplicate renamings |
520 |
- renaming should occur in namespace |
|
521 |
*) |
|
26478 | 522 |
val _ = writeln ("Defining statespace " ^ quote name ^ " ..."); |
25171 | 523 |
|
42361 | 524 |
val ctxt = Proof_Context.init_global thy; |
27283 | 525 |
|
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
526 |
fun add_parent (prefix, (Ts, pname, rs)) env = |
25171 | 527 |
let |
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
528 |
val prefix' = |
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
529 |
(case prefix of |
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
530 |
("", mandatory) => (pname, mandatory) |
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
531 |
| _ => prefix); |
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
532 |
|
28965 | 533 |
val full_pname = Sign.full_bname thy pname; |
26478 | 534 |
val {args,components,...} = |
25171 | 535 |
(case get_statespace (Context.Theory thy) full_pname of |
536 |
SOME r => r |
|
78030 | 537 |
| NONE => error ("Undefined statespace " ^ quote pname)); |
25171 | 538 |
|
539 |
||
27283 | 540 |
val (Ts',env') = fold_map (prep_typ ctxt) Ts env |
26478 | 541 |
handle ERROR msg => cat_error msg |
36149 | 542 |
("The error(s) above occurred in parent statespace specification " |
25171 | 543 |
^ quote pname); |
544 |
val err_insts = if length args <> length Ts' then |
|
545 |
["number of type instantiation(s) does not match arguments of parent statespace " |
|
546 |
^ quote pname] |
|
547 |
else []; |
|
26478 | 548 |
|
25171 | 549 |
val rnames = map fst rs |
550 |
val err_dup_renamings = (case duplicates (op =) rnames of |
|
551 |
[] => [] |
|
552 |
| dups => ["Duplicate renaming(s) for " ^ commas dups]) |
|
553 |
||
554 |
val cnames = map fst components; |
|
36692
54b64d4ad524
farewell to old-style mem infixes -- type inference in situations with mem_int and mem_string should provide enough information to resolve the type of (op =)
haftmann
parents:
36610
diff
changeset
|
555 |
val err_rename_unknowns = (case subtract (op =) cnames rnames of |
25171 | 556 |
[] => [] |
557 |
| rs => ["Unknown components " ^ commas rs]); |
|
26478 | 558 |
|
25171 | 559 |
|
560 |
val rs' = map (AList.lookup (op =) rs o fst) components; |
|
561 |
val errs =err_insts @ err_dup_renamings @ err_rename_unknowns |
|
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
562 |
in |
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
563 |
if null errs then ((prefix', (Ts', full_pname, rs')), env') |
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
564 |
else error (cat_lines (errs @ ["in parent statespace " ^ quote pname])) |
25171 | 565 |
end; |
26478 | 566 |
|
25171 | 567 |
val (parents',env) = fold_map add_parent parents []; |
568 |
||
569 |
val err_dup_args = |
|
570 |
(case duplicates (op =) args of |
|
571 |
[] => [] |
|
572 |
| dups => ["Duplicate type argument(s) " ^ commas dups]); |
|
26478 | 573 |
|
25171 | 574 |
|
26478 | 575 |
val err_dup_components = |
25171 | 576 |
(case duplicates (op =) (map fst comps) of |
577 |
[] => [] |
|
578 |
| dups => ["Duplicate state-space components " ^ commas dups]); |
|
579 |
||
580 |
fun prep_comp (n,T) env = |
|
27283 | 581 |
let val (T', env') = prep_typ ctxt T env handle ERROR msg => |
36149 | 582 |
cat_error msg ("The error(s) above occurred in component " ^ quote n) |
25171 | 583 |
in ((n,T'), env') end; |
584 |
||
585 |
val (comps',env') = fold_map prep_comp comps env; |
|
586 |
||
587 |
val err_extra_frees = |
|
588 |
(case subtract (op =) args (map fst env') of |
|
589 |
[] => [] |
|
590 |
| extras => ["Extra free type variable(s) " ^ commas extras]); |
|
591 |
||
592 |
val defaultS = Sign.defaultS thy; |
|
593 |
val args' = map (fn x => (x, AList.lookup (op =) env x |> the_default defaultS)) args; |
|
594 |
||
595 |
||
596 |
fun fst_eq ((x:string,_),(y,_)) = x = y; |
|
26478 | 597 |
fun snd_eq ((_,t:typ),(_,u)) = t = u; |
25171 | 598 |
|
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
599 |
val raw_parent_comps = maps (parent_components thy o snd) parents'; |
26478 | 600 |
fun check_type (n,T) = |
25171 | 601 |
(case distinct (snd_eq) (filter (curry fst_eq (n,T)) raw_parent_comps) of |
602 |
[] => [] |
|
603 |
| [_] => [] |
|
45660 | 604 |
| rs => ["Different types for component " ^ quote n ^ ": " ^ |
32432
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32194
diff
changeset
|
605 |
commas (map (Syntax.string_of_typ ctxt o snd) rs)]) |
26478 | 606 |
|
32952 | 607 |
val err_dup_types = maps check_type (duplicates fst_eq raw_parent_comps) |
25171 | 608 |
|
609 |
val parent_comps = distinct (fst_eq) raw_parent_comps; |
|
610 |
val all_comps = parent_comps @ comps'; |
|
611 |
val err_comp_in_parent = (case duplicates (op =) (map fst all_comps) of |
|
612 |
[] => [] |
|
45660 | 613 |
| xs => ["Components already defined in parents: " ^ commas_quote xs]); |
25171 | 614 |
val errs = err_dup_args @ err_dup_components @ err_extra_frees @ |
615 |
err_dup_types @ err_comp_in_parent; |
|
26478 | 616 |
in if null errs |
25171 | 617 |
then thy |> statespace_definition state_space args' name parents' parent_comps comps' |
26478 | 618 |
else error (cat_lines errs) |
25171 | 619 |
end |
620 |
handle ERROR msg => cat_error msg ("Failed to define statespace " ^ quote name); |
|
26478 | 621 |
|
36149 | 622 |
val define_statespace = gen_define_statespace read_typ NONE; |
623 |
val define_statespace_i = gen_define_statespace cert_typ; |
|
26478 | 624 |
|
25171 | 625 |
|
45362 | 626 |
|
25171 | 627 |
(*** parse/print - translations ***) |
628 |
||
78029
f78cdc6fe971
more standard val silent = Attrib.setup_config_bool;
wenzelm
parents:
78028
diff
changeset
|
629 |
val silent = Attrib.setup_config_bool \<^binding>\<open>statespace_silent\<close> (K false); |
25171 | 630 |
|
26478 | 631 |
fun gen_lookup_tr ctxt s n = |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
632 |
(case get_comp' (Context.Proof ctxt) n of |
45362 | 633 |
SOME (T, _) => |
69597 | 634 |
Syntax.const \<^const_name>\<open>StateFun.lookup\<close> $ |
45362 | 635 |
Syntax.free (project_name T) $ Syntax.free n $ s |
636 |
| NONE => |
|
78029
f78cdc6fe971
more standard val silent = Attrib.setup_config_bool;
wenzelm
parents:
78028
diff
changeset
|
637 |
if Config.get ctxt silent |
69597 | 638 |
then Syntax.const \<^const_name>\<open>StateFun.lookup\<close> $ |
639 |
Syntax.const \<^const_syntax>\<open>undefined\<close> $ Syntax.free n $ s |
|
74588
3cc363e8bfb2
cleanup; add Apple reference
Norbert Schirmer <nschirmer@apple.com>
parents:
74586
diff
changeset
|
640 |
else raise TERM ("StateSpace.gen_lookup_tr: component " ^ quote n ^ " not defined", [])); |
25171 | 641 |
|
42052
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents:
41585
diff
changeset
|
642 |
fun lookup_tr ctxt [s, x] = |
42264 | 643 |
(case Term_Position.strip_positions x of |
42052
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents:
41585
diff
changeset
|
644 |
Free (n,_) => gen_lookup_tr ctxt s n |
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents:
41585
diff
changeset
|
645 |
| _ => raise Match); |
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents:
41585
diff
changeset
|
646 |
|
25171 | 647 |
fun lookup_swap_tr ctxt [Free (n,_),s] = gen_lookup_tr ctxt s n; |
648 |
||
45362 | 649 |
fun lookup_tr' ctxt [_ $ Free (prj, _), n as (_ $ Free (name, _)), s] = |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
650 |
(case get_comp' (Context.Proof ctxt) name of |
45362 | 651 |
SOME (T, _) => |
652 |
if prj = project_name T |
|
653 |
then Syntax.const "_statespace_lookup" $ s $ n |
|
654 |
else raise Match |
|
25171 | 655 |
| NONE => raise Match) |
55972 | 656 |
| lookup_tr' _ _ = raise Match; |
25171 | 657 |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
658 |
fun gen'_update_tr const_val id ctxt n v s = |
25171 | 659 |
let |
69597 | 660 |
fun pname T = if id then \<^const_name>\<open>Fun.id\<close> else project_name T; |
661 |
fun iname T = if id then \<^const_name>\<open>Fun.id\<close> else inject_name T; |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
662 |
val v = if const_val then (Syntax.const \<^const_name>\<open>K_statefun\<close> $ v) else v |
45362 | 663 |
in |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
664 |
(case get_comp' (Context.Proof ctxt) n of |
45362 | 665 |
SOME (T, _) => |
69597 | 666 |
Syntax.const \<^const_name>\<open>StateFun.update\<close> $ |
45362 | 667 |
Syntax.free (pname T) $ Syntax.free (iname T) $ |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
668 |
Syntax.free n $ v $ s |
45362 | 669 |
| NONE => |
78029
f78cdc6fe971
more standard val silent = Attrib.setup_config_bool;
wenzelm
parents:
78028
diff
changeset
|
670 |
if Config.get ctxt silent then |
69597 | 671 |
Syntax.const \<^const_name>\<open>StateFun.update\<close> $ |
672 |
Syntax.const \<^const_syntax>\<open>undefined\<close> $ Syntax.const \<^const_syntax>\<open>undefined\<close> $ |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
673 |
Syntax.free n $ v $ s |
45362 | 674 |
else raise TERM ("StateSpace.gen_update_tr: component " ^ n ^ " not defined", [])) |
25171 | 675 |
end; |
676 |
||
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
677 |
val gen_update_tr = gen'_update_tr true |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
678 |
|
42052
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents:
41585
diff
changeset
|
679 |
fun update_tr ctxt [s, x, v] = |
42264 | 680 |
(case Term_Position.strip_positions x of |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
681 |
Free (n, _) => gen'_update_tr true false ctxt n v s |
42052
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents:
41585
diff
changeset
|
682 |
| _ => raise Match); |
25171 | 683 |
|
45362 | 684 |
fun update_tr' ctxt |
685 |
[_ $ Free (prj, _), _ $ Free (inj, _), n as (_ $ Free (name, _)), (Const (k, _) $ v), s] = |
|
69597 | 686 |
if Long_Name.base_name k = Long_Name.base_name \<^const_name>\<open>K_statefun\<close> then |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
687 |
(case get_comp' (Context.Proof ctxt) name of |
45362 | 688 |
SOME (T, _) => |
689 |
if inj = inject_name T andalso prj = project_name T then |
|
690 |
Syntax.const "_statespace_update" $ s $ n $ v |
|
691 |
else raise Match |
|
692 |
| NONE => raise Match) |
|
25171 | 693 |
else raise Match |
694 |
| update_tr' _ _ = raise Match; |
|
695 |
||
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
696 |
|
25171 | 697 |
|
698 |
||
699 |
(*** outer syntax *) |
|
700 |
||
45362 | 701 |
local |
702 |
||
25171 | 703 |
val type_insts = |
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36958
diff
changeset
|
704 |
Parse.typ >> single || |
69597 | 705 |
\<^keyword>\<open>(\<close> |-- Parse.!!! (Parse.list1 Parse.typ --| \<^keyword>\<open>)\<close>) |
25171 | 706 |
|
69597 | 707 |
val comp = Parse.name -- (\<^keyword>\<open>::\<close> |-- Parse.!!! Parse.typ); |
25171 | 708 |
fun plus1_unless test scan = |
69597 | 709 |
scan ::: Scan.repeat (\<^keyword>\<open>+\<close> |-- Scan.unless test (Parse.!!! scan)); |
25171 | 710 |
|
69597 | 711 |
val mapsto = \<^keyword>\<open>=\<close>; |
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36958
diff
changeset
|
712 |
val rename = Parse.name -- (mapsto |-- Parse.name); |
69597 | 713 |
val renames = Scan.optional (\<^keyword>\<open>[\<close> |-- Parse.!!! (Parse.list1 rename --| \<^keyword>\<open>]\<close>)) []; |
25171 | 714 |
|
45362 | 715 |
val parent = |
61606
6d5213bd9709
uniform mandatory qualifier for all locale expressions, including 'statespace' parent;
wenzelm
parents:
61144
diff
changeset
|
716 |
Parse_Spec.locale_prefix -- |
62969 | 717 |
((type_insts -- Parse.name) || (Parse.name >> pair [])) -- renames |
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
718 |
>> (fn ((prefix, (insts, name)), renames) => (prefix, (insts, name, renames))); |
25171 | 719 |
|
45362 | 720 |
in |
25171 | 721 |
|
722 |
val statespace_decl = |
|
45362 | 723 |
Parse.type_args -- Parse.name -- |
69597 | 724 |
(\<^keyword>\<open>=\<close> |-- |
45362 | 725 |
((Scan.repeat1 comp >> pair []) || |
726 |
(plus1_unless comp parent -- |
|
69597 | 727 |
Scan.optional (\<^keyword>\<open>+\<close> |-- Parse.!!! (Scan.repeat1 comp)) []))); |
45362 | 728 |
val _ = |
69597 | 729 |
Outer_Syntax.command \<^command_keyword>\<open>statespace\<close> "define state-space as locale context" |
45362 | 730 |
(statespace_decl >> (fn ((args, name), (parents, comps)) => |
731 |
Toplevel.theory (define_statespace args name parents comps))); |
|
25171 | 732 |
|
45362 | 733 |
end; |
25171 | 734 |
|
45362 | 735 |
end; |