author | wenzelm |
Tue, 23 May 2023 18:46:15 +0200 | |
changeset 78095 | bc42c074e58f |
parent 78040 | 6200555461c6 |
child 78807 | f6d2679ab6c1 |
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 |
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
242 |
val ctree = Thm.cprop_of dist_thm |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2; |
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 = |
|
69597 | 296 |
Simplifier.make_simproc \<^context> "StateSpace.distinct_simproc" |
297 |
{lhss = [\<^term>\<open>x = y\<close>], |
|
61144 | 298 |
proc = fn _ => fn ctxt => fn ct => |
299 |
(case Thm.term_of ct of |
|
69597 | 300 |
Const (\<^const_name>\<open>HOL.eq\<close>,_) $ (x as Free _) $ (y as Free _) => |
61144 | 301 |
Option.map (fn neq => DistinctTreeProver.neq_to_eq_False OF [neq]) |
302 |
(neq_x_y ctxt x y) |
|
62913 | 303 |
| _ => NONE)}; |
25171 | 304 |
|
26478 | 305 |
fun interprete_parent name dist_thm_name parent_expr thy = |
25171 | 306 |
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
|
307 |
fun solve_tac ctxt = CSUBGOAL (fn (goal, i) => |
25171 | 308 |
let |
42361 | 309 |
val distinct_thm = Proof_Context.get_thm ctxt dist_thm_name; |
60327 | 310 |
val rule = DistinctTreeProver.distinct_implProver ctxt distinct_thm goal; |
60754 | 311 |
in resolve_tac ctxt [rule] i end); |
26478 | 312 |
|
42368
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
wenzelm
parents:
42364
diff
changeset
|
313 |
fun tac ctxt = |
69017 | 314 |
Locale.intro_locales_tac {strict = true, eager = true} ctxt [] THEN ALLGOALS (solve_tac ctxt); |
26478 | 315 |
|
45362 | 316 |
in |
317 |
thy |> prove_interpretation_in tac (name, parent_expr) |
|
26478 | 318 |
end; |
25171 | 319 |
|
26478 | 320 |
fun namespace_definition name nameT parent_expr parent_comps new_comps thy = |
25171 | 321 |
let |
322 |
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
|
323 |
val vars = (map (fn n => (Binding.name n, NONE, NoSyn)) all_comps); |
25171 | 324 |
val dist_thm_name = distinct_compsN; |
325 |
||
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
326 |
val dist_thm_full_name = dist_thm_name; |
26478 | 327 |
|
38835
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
wenzelm
parents:
38715
diff
changeset
|
328 |
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
|
329 |
(case context of |
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
wenzelm
parents:
38715
diff
changeset
|
330 |
Context.Theory _ => context |
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
wenzelm
parents:
38715
diff
changeset
|
331 |
| Context.Proof ctxt => |
26478 | 332 |
let |
78030 | 333 |
val declinfo = get_declinfo context |
334 |
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
|
335 |
val all_names = comps_of_distinct_thm thm; |
78040 | 336 |
val thm0 = Thm.trim_context thm; |
337 |
fun upd name = Symtab.map_default (name, [thm0]) (insert_distinct_thm thm0) |
|
25171 | 338 |
|
339 |
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
|
340 |
val context' = |
51717
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
50214
diff
changeset
|
341 |
Context_Position.set_visible false ctxt |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
50214
diff
changeset
|
342 |
addsimprocs [distinct_simproc] |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
50214
diff
changeset
|
343 |
|> Context_Position.restore_visible ctxt |
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents:
50214
diff
changeset
|
344 |
|> Context.Proof |
78030 | 345 |
|> map_declinfo (K declinfo) |
346 |
|> map_distinctthm (K tt'); |
|
38835
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
wenzelm
parents:
38715
diff
changeset
|
347 |
in context' end)); |
26478 | 348 |
|
78095 | 349 |
val attr = Attrib.internal \<^here> type_attr; |
25171 | 350 |
|
45362 | 351 |
val assume = |
352 |
((Binding.name dist_thm_name, [attr]), |
|
353 |
[(HOLogic.Trueprop $ |
|
69597 | 354 |
(Const (\<^const_name>\<open>all_distinct\<close>, Type (\<^type_name>\<open>tree\<close>, [nameT]) --> HOLogic.boolT) $ |
45362 | 355 |
DistinctTreeProver.mk_tree (fn n => Free (n, nameT)) nameT |
356 |
(sort fast_string_ord all_comps)), [])]); |
|
357 |
in |
|
358 |
thy |
|
359 |
|> add_locale name ([], vars) [Element.Assumes [assume]] |
|
360 |
|> Proof_Context.theory_of |
|
361 |
|> interprete_parent name dist_thm_full_name parent_expr |
|
25171 | 362 |
end; |
363 |
||
55972 | 364 |
fun encode_dot x = if x = #"." then #"_" else x; |
25171 | 365 |
|
366 |
fun encode_type (TFree (s, _)) = s |
|
367 |
| encode_type (TVar ((s,i),_)) = "?" ^ s ^ string_of_int i |
|
26478 | 368 |
| encode_type (Type (n,Ts)) = |
25171 | 369 |
let |
370 |
val Ts' = fold1' (fn x => fn y => x ^ "_" ^ y) (map encode_type Ts) ""; |
|
32651 | 371 |
val n' = String.map encode_dot n; |
25171 | 372 |
in if Ts'="" then n' else Ts' ^ "_" ^ n' end; |
373 |
||
374 |
fun project_name T = projectN ^"_"^encode_type T; |
|
375 |
fun inject_name T = injectN ^"_"^encode_type T; |
|
376 |
||
377 |
||
378 |
fun add_declaration name decl thy = |
|
379 |
thy |
|
72536
589645894305
bundle mixins for locale and class specifications
haftmann
parents:
72186
diff
changeset
|
380 |
|> Named_Target.init [] name |
78095 | 381 |
|> (fn lthy => |
382 |
Local_Theory.declaration {syntax = true, pervasive = false, pos = Position.thread_data ()} |
|
383 |
(decl lthy) lthy) |
|
33671 | 384 |
|> Local_Theory.exit_global; |
25171 | 385 |
|
386 |
fun parent_components thy (Ts, pname, renaming) = |
|
387 |
let |
|
388 |
fun rename [] xs = xs |
|
389 |
| rename (NONE::rs) (x::xs) = x::rename rs xs |
|
390 |
| rename (SOME r::rs) ((x,T)::xs) = (r,T)::rename rs xs; |
|
78030 | 391 |
val {args, parents, components, ...} = the_statespace (Context.Theory thy) pname; |
25171 | 392 |
val inst = map fst args ~~ Ts; |
393 |
val subst = Term.map_type_tfree (the o AList.lookup (op =) inst o fst); |
|
26478 | 394 |
val parent_comps = |
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
395 |
maps (fn (Ts',n,rs) => parent_components thy (map subst Ts', n, rs)) parents; |
25171 | 396 |
val all_comps = rename renaming (parent_comps @ map (apsnd subst) components); |
397 |
in all_comps end; |
|
398 |
||
399 |
fun statespace_definition state_type args name parents parent_comps components thy = |
|
26478 | 400 |
let |
28965 | 401 |
val full_name = Sign.full_bname thy name; |
25171 | 402 |
val all_comps = parent_comps @ components; |
403 |
||
404 |
val components' = map (fn (n,T) => (n,(T,full_name))) components; |
|
405 |
||
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
406 |
fun parent_expr (prefix, (_, n, rs)) = |
67450
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
407 |
(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
|
408 |
val parents_expr = map parent_expr parents; |
25171 | 409 |
fun distinct_types Ts = |
27276 | 410 |
let val tab = fold (fn T => fn tab => Typtab.update (T,()) tab) Ts Typtab.empty; |
411 |
in map fst (Typtab.dest tab) end; |
|
25171 | 412 |
|
413 |
val Ts = distinct_types (map snd all_comps); |
|
414 |
val arg_names = map fst args; |
|
43324
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
wenzelm
parents:
43278
diff
changeset
|
415 |
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
|
416 |
val nameN = singleton (Name.variant_list (valueN :: arg_names)) "'name"; |
25171 | 417 |
val valueT = TFree (valueN, Sign.defaultS thy); |
418 |
val nameT = TFree (nameN, Sign.defaultS thy); |
|
419 |
val stateT = nameT --> valueT; |
|
420 |
fun projectT T = valueT --> T; |
|
26478 | 421 |
fun injectT T = T --> valueT; |
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
422 |
val locinsts = map (fn T => (project_injectL, |
67450
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
423 |
((encode_type T,false),(Expression.Positional |
45362 | 424 |
[SOME (Free (project_name T,projectT T)), |
67450
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
425 |
SOME (Free ((inject_name T,injectT T)))],[])))) Ts; |
32952 | 426 |
val locs = maps (fn T => [(Binding.name (project_name T),NONE,NoSyn), |
427 |
(Binding.name (inject_name T),NONE,NoSyn)]) Ts; |
|
428 |
val constrains = maps (fn T => [(project_name T,projectT T),(inject_name T,injectT T)]) Ts; |
|
25171 | 429 |
|
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
430 |
fun interprete_parent_valuetypes (prefix, (Ts, pname, _)) thy = |
25171 | 431 |
let |
78030 | 432 |
val {args,types,...} = the_statespace (Context.Theory thy) pname; |
25171 | 433 |
val inst = map fst args ~~ Ts; |
434 |
val subst = Term.map_type_tfree (the o AList.lookup (op =) inst o fst); |
|
32952 | 435 |
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
|
436 |
|
45362 | 437 |
val expr = ([(suffix valuetypesN name, |
67450
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
438 |
(prefix, (Expression.Positional (map SOME pars),[])))],[]); |
29291 | 439 |
in |
59498
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
wenzelm
parents:
57181
diff
changeset
|
440 |
prove_interpretation_in (fn ctxt => ALLGOALS (solve_tac ctxt (Assumption.all_prems_of ctxt))) |
29291 | 441 |
(suffix valuetypesN name, expr) thy |
442 |
end; |
|
25171 | 443 |
|
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
444 |
fun interprete_parent (prefix, (_, pname, rs)) = |
25171 | 445 |
let |
67450
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
446 |
val expr = ([(pname, (prefix, (Expression.Positional rs,[])))],[]) |
26478 | 447 |
in prove_interpretation_in |
69017 | 448 |
(fn ctxt => Locale.intro_locales_tac {strict = true, eager = false} ctxt []) |
25171 | 449 |
(full_name, expr) end; |
450 |
||
451 |
fun declare_declinfo updates lthy phi ctxt = |
|
452 |
let |
|
26478 | 453 |
fun upd_prf ctxt = |
25171 | 454 |
let |
455 |
fun upd (n,v) = |
|
456 |
let |
|
42361 | 457 |
val nT = Proof_Context.infer_type (Local_Theory.target_of lthy) (n, dummyT) |
26478 | 458 |
in Context.proof_map |
459 |
(update_declinfo (Morphism.term phi (Free (n,nT)),v)) |
|
25171 | 460 |
end; |
78028 | 461 |
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
|
462 |
in ctxt' end; |
25171 | 463 |
|
464 |
in Context.mapping I upd_prf ctxt end; |
|
465 |
||
26478 | 466 |
fun string_of_typ T = |
39134
917b4b6ba3d2
turned show_sorts/show_types into proper configuration options;
wenzelm
parents:
38864
diff
changeset
|
467 |
Print_Mode.setmp [] |
917b4b6ba3d2
turned show_sorts/show_types into proper configuration options;
wenzelm
parents:
38864
diff
changeset
|
468 |
(Syntax.string_of_typ (Config.put show_sorts true (Syntax.init_pretty_global thy))) T; |
25171 | 469 |
val fixestate = (case state_type of |
470 |
NONE => [] |
|
26478 | 471 |
| SOME s => |
472 |
let |
|
32960
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
wenzelm
parents:
32952
diff
changeset
|
473 |
val fx = Element.Fixes [(Binding.name s,SOME (string_of_typ stateT),NoSyn)]; |
26478 | 474 |
val cs = Element.Constrains |
25171 | 475 |
(map (fn (n,T) => (n,string_of_typ T)) |
476 |
((map (fn (n,_) => (n,nameT)) all_comps) @ |
|
477 |
constrains)) |
|
478 |
in [fx,cs] end |
|
479 |
) |
|
480 |
||
26478 | 481 |
|
482 |
in thy |
|
483 |
|> namespace_definition |
|
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
484 |
(suffix namespaceN name) nameT (parents_expr,[]) |
25171 | 485 |
(map fst parent_comps) (map fst components) |
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
486 |
|> 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
|
487 |
|> add_locale (suffix valuetypesN name) (locinsts,locs) [] |
45362 | 488 |
|> Proof_Context.theory_of |
26478 | 489 |
|> fold interprete_parent_valuetypes parents |
29247
95d3a82857e5
adapted statespace module to new locales;
Norbert Schirmer <norbert.schirmer@web.de>
parents:
29064
diff
changeset
|
490 |
|> add_locale_cmd name |
67450
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
491 |
([(suffix namespaceN full_name ,(("",false),(Expression.Named [],[]))), |
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
ballarin
parents:
66334
diff
changeset
|
492 |
(suffix valuetypesN full_name,(("",false),(Expression.Named [],[])))],[]) fixestate |
45362 | 493 |
|> Proof_Context.theory_of |
25171 | 494 |
|> fold interprete_parent parents |
38389
d7d915bae307
Named_Target.init: empty string represents theory target
haftmann
parents:
38350
diff
changeset
|
495 |
|> add_declaration full_name (declare_declinfo components') |
25171 | 496 |
end; |
497 |
||
498 |
||
499 |
(* prepare arguments *) |
|
500 |
||
36149 | 501 |
fun read_typ ctxt raw_T env = |
502 |
let |
|
503 |
val ctxt' = fold (Variable.declare_typ o TFree) env ctxt; |
|
504 |
val T = Syntax.read_typ ctxt' raw_T; |
|
45741 | 505 |
val env' = Term.add_tfreesT T env; |
36149 | 506 |
in (T, env') end; |
507 |
||
508 |
fun cert_typ ctxt raw_T env = |
|
509 |
let |
|
42361 | 510 |
val thy = Proof_Context.theory_of ctxt; |
36149 | 511 |
val T = Type.no_tvars (Sign.certify_typ thy raw_T) |
512 |
handle TYPE (msg, _, _) => error msg; |
|
45741 | 513 |
val env' = Term.add_tfreesT T env; |
36149 | 514 |
in (T, env') end; |
515 |
||
25171 | 516 |
fun gen_define_statespace prep_typ state_space args name parents comps thy = |
517 |
let (* - args distinct |
|
518 |
- only args may occur in comps and parent-instantiations |
|
26478 | 519 |
- number of insts must match parent args |
25171 | 520 |
- no duplicate renamings |
521 |
- renaming should occur in namespace |
|
522 |
*) |
|
26478 | 523 |
val _ = writeln ("Defining statespace " ^ quote name ^ " ..."); |
25171 | 524 |
|
42361 | 525 |
val ctxt = Proof_Context.init_global thy; |
27283 | 526 |
|
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
527 |
fun add_parent (prefix, (Ts, pname, rs)) env = |
25171 | 528 |
let |
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
529 |
val prefix' = |
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
530 |
(case prefix of |
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
531 |
("", mandatory) => (pname, mandatory) |
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
532 |
| _ => prefix); |
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
533 |
|
28965 | 534 |
val full_pname = Sign.full_bname thy pname; |
26478 | 535 |
val {args,components,...} = |
25171 | 536 |
(case get_statespace (Context.Theory thy) full_pname of |
537 |
SOME r => r |
|
78030 | 538 |
| NONE => error ("Undefined statespace " ^ quote pname)); |
25171 | 539 |
|
540 |
||
27283 | 541 |
val (Ts',env') = fold_map (prep_typ ctxt) Ts env |
26478 | 542 |
handle ERROR msg => cat_error msg |
36149 | 543 |
("The error(s) above occurred in parent statespace specification " |
25171 | 544 |
^ quote pname); |
545 |
val err_insts = if length args <> length Ts' then |
|
546 |
["number of type instantiation(s) does not match arguments of parent statespace " |
|
547 |
^ quote pname] |
|
548 |
else []; |
|
26478 | 549 |
|
25171 | 550 |
val rnames = map fst rs |
551 |
val err_dup_renamings = (case duplicates (op =) rnames of |
|
552 |
[] => [] |
|
553 |
| dups => ["Duplicate renaming(s) for " ^ commas dups]) |
|
554 |
||
555 |
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
|
556 |
val err_rename_unknowns = (case subtract (op =) cnames rnames of |
25171 | 557 |
[] => [] |
558 |
| rs => ["Unknown components " ^ commas rs]); |
|
26478 | 559 |
|
25171 | 560 |
|
561 |
val rs' = map (AList.lookup (op =) rs o fst) components; |
|
562 |
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
|
563 |
in |
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
564 |
if null errs then ((prefix', (Ts', full_pname, rs')), env') |
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
565 |
else error (cat_lines (errs @ ["in parent statespace " ^ quote pname])) |
25171 | 566 |
end; |
26478 | 567 |
|
25171 | 568 |
val (parents',env) = fold_map add_parent parents []; |
569 |
||
570 |
val err_dup_args = |
|
571 |
(case duplicates (op =) args of |
|
572 |
[] => [] |
|
573 |
| dups => ["Duplicate type argument(s) " ^ commas dups]); |
|
26478 | 574 |
|
25171 | 575 |
|
26478 | 576 |
val err_dup_components = |
25171 | 577 |
(case duplicates (op =) (map fst comps) of |
578 |
[] => [] |
|
579 |
| dups => ["Duplicate state-space components " ^ commas dups]); |
|
580 |
||
581 |
fun prep_comp (n,T) env = |
|
27283 | 582 |
let val (T', env') = prep_typ ctxt T env handle ERROR msg => |
36149 | 583 |
cat_error msg ("The error(s) above occurred in component " ^ quote n) |
25171 | 584 |
in ((n,T'), env') end; |
585 |
||
586 |
val (comps',env') = fold_map prep_comp comps env; |
|
587 |
||
588 |
val err_extra_frees = |
|
589 |
(case subtract (op =) args (map fst env') of |
|
590 |
[] => [] |
|
591 |
| extras => ["Extra free type variable(s) " ^ commas extras]); |
|
592 |
||
593 |
val defaultS = Sign.defaultS thy; |
|
594 |
val args' = map (fn x => (x, AList.lookup (op =) env x |> the_default defaultS)) args; |
|
595 |
||
596 |
||
597 |
fun fst_eq ((x:string,_),(y,_)) = x = y; |
|
26478 | 598 |
fun snd_eq ((_,t:typ),(_,u)) = t = u; |
25171 | 599 |
|
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
600 |
val raw_parent_comps = maps (parent_components thy o snd) parents'; |
26478 | 601 |
fun check_type (n,T) = |
25171 | 602 |
(case distinct (snd_eq) (filter (curry fst_eq (n,T)) raw_parent_comps) of |
603 |
[] => [] |
|
604 |
| [_] => [] |
|
45660 | 605 |
| rs => ["Different types for component " ^ quote n ^ ": " ^ |
32432
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents:
32194
diff
changeset
|
606 |
commas (map (Syntax.string_of_typ ctxt o snd) rs)]) |
26478 | 607 |
|
32952 | 608 |
val err_dup_types = maps check_type (duplicates fst_eq raw_parent_comps) |
25171 | 609 |
|
610 |
val parent_comps = distinct (fst_eq) raw_parent_comps; |
|
611 |
val all_comps = parent_comps @ comps'; |
|
612 |
val err_comp_in_parent = (case duplicates (op =) (map fst all_comps) of |
|
613 |
[] => [] |
|
45660 | 614 |
| xs => ["Components already defined in parents: " ^ commas_quote xs]); |
25171 | 615 |
val errs = err_dup_args @ err_dup_components @ err_extra_frees @ |
616 |
err_dup_types @ err_comp_in_parent; |
|
26478 | 617 |
in if null errs |
25171 | 618 |
then thy |> statespace_definition state_space args' name parents' parent_comps comps' |
26478 | 619 |
else error (cat_lines errs) |
25171 | 620 |
end |
621 |
handle ERROR msg => cat_error msg ("Failed to define statespace " ^ quote name); |
|
26478 | 622 |
|
36149 | 623 |
val define_statespace = gen_define_statespace read_typ NONE; |
624 |
val define_statespace_i = gen_define_statespace cert_typ; |
|
26478 | 625 |
|
25171 | 626 |
|
45362 | 627 |
|
25171 | 628 |
(*** parse/print - translations ***) |
629 |
||
78029
f78cdc6fe971
more standard val silent = Attrib.setup_config_bool;
wenzelm
parents:
78028
diff
changeset
|
630 |
val silent = Attrib.setup_config_bool \<^binding>\<open>statespace_silent\<close> (K false); |
25171 | 631 |
|
26478 | 632 |
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
|
633 |
(case get_comp' (Context.Proof ctxt) n of |
45362 | 634 |
SOME (T, _) => |
69597 | 635 |
Syntax.const \<^const_name>\<open>StateFun.lookup\<close> $ |
45362 | 636 |
Syntax.free (project_name T) $ Syntax.free n $ s |
637 |
| NONE => |
|
78029
f78cdc6fe971
more standard val silent = Attrib.setup_config_bool;
wenzelm
parents:
78028
diff
changeset
|
638 |
if Config.get ctxt silent |
69597 | 639 |
then Syntax.const \<^const_name>\<open>StateFun.lookup\<close> $ |
640 |
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
|
641 |
else raise TERM ("StateSpace.gen_lookup_tr: component " ^ quote n ^ " not defined", [])); |
25171 | 642 |
|
42052
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents:
41585
diff
changeset
|
643 |
fun lookup_tr ctxt [s, x] = |
42264 | 644 |
(case Term_Position.strip_positions x of |
42052
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents:
41585
diff
changeset
|
645 |
Free (n,_) => gen_lookup_tr ctxt s n |
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents:
41585
diff
changeset
|
646 |
| _ => raise Match); |
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents:
41585
diff
changeset
|
647 |
|
25171 | 648 |
fun lookup_swap_tr ctxt [Free (n,_),s] = gen_lookup_tr ctxt s n; |
649 |
||
45362 | 650 |
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
|
651 |
(case get_comp' (Context.Proof ctxt) name of |
45362 | 652 |
SOME (T, _) => |
653 |
if prj = project_name T |
|
654 |
then Syntax.const "_statespace_lookup" $ s $ n |
|
655 |
else raise Match |
|
25171 | 656 |
| NONE => raise Match) |
55972 | 657 |
| lookup_tr' _ _ = raise Match; |
25171 | 658 |
|
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
659 |
fun gen'_update_tr const_val id ctxt n v s = |
25171 | 660 |
let |
69597 | 661 |
fun pname T = if id then \<^const_name>\<open>Fun.id\<close> else project_name T; |
662 |
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
|
663 |
val v = if const_val then (Syntax.const \<^const_name>\<open>K_statefun\<close> $ v) else v |
45362 | 664 |
in |
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
665 |
(case get_comp' (Context.Proof ctxt) n of |
45362 | 666 |
SOME (T, _) => |
69597 | 667 |
Syntax.const \<^const_name>\<open>StateFun.update\<close> $ |
45362 | 668 |
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
|
669 |
Syntax.free n $ v $ s |
45362 | 670 |
| NONE => |
78029
f78cdc6fe971
more standard val silent = Attrib.setup_config_bool;
wenzelm
parents:
78028
diff
changeset
|
671 |
if Config.get ctxt silent then |
69597 | 672 |
Syntax.const \<^const_name>\<open>StateFun.update\<close> $ |
673 |
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
|
674 |
Syntax.free n $ v $ s |
45362 | 675 |
else raise TERM ("StateSpace.gen_update_tr: component " ^ n ^ " not defined", [])) |
25171 | 676 |
end; |
677 |
||
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
678 |
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
|
679 |
|
42052
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
wenzelm
parents:
41585
diff
changeset
|
680 |
fun update_tr ctxt [s, x, v] = |
42264 | 681 |
(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
|
682 |
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
|
683 |
| _ => raise Match); |
25171 | 684 |
|
45362 | 685 |
fun update_tr' ctxt |
686 |
[_ $ Free (prj, _), _ $ Free (inj, _), n as (_ $ Free (name, _)), (Const (k, _) $ v), s] = |
|
69597 | 687 |
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
|
688 |
(case get_comp' (Context.Proof ctxt) name of |
45362 | 689 |
SOME (T, _) => |
690 |
if inj = inject_name T andalso prj = project_name T then |
|
691 |
Syntax.const "_statespace_update" $ s $ n $ v |
|
692 |
else raise Match |
|
693 |
| NONE => raise Match) |
|
25171 | 694 |
else raise Match |
695 |
| update_tr' _ _ = raise Match; |
|
696 |
||
74586
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
Norbert Schirmer <nschirmer@apple.com>
parents:
74561
diff
changeset
|
697 |
|
25171 | 698 |
|
699 |
||
700 |
(*** outer syntax *) |
|
701 |
||
45362 | 702 |
local |
703 |
||
25171 | 704 |
val type_insts = |
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36958
diff
changeset
|
705 |
Parse.typ >> single || |
69597 | 706 |
\<^keyword>\<open>(\<close> |-- Parse.!!! (Parse.list1 Parse.typ --| \<^keyword>\<open>)\<close>) |
25171 | 707 |
|
69597 | 708 |
val comp = Parse.name -- (\<^keyword>\<open>::\<close> |-- Parse.!!! Parse.typ); |
25171 | 709 |
fun plus1_unless test scan = |
69597 | 710 |
scan ::: Scan.repeat (\<^keyword>\<open>+\<close> |-- Scan.unless test (Parse.!!! scan)); |
25171 | 711 |
|
69597 | 712 |
val mapsto = \<^keyword>\<open>=\<close>; |
36960
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
wenzelm
parents:
36958
diff
changeset
|
713 |
val rename = Parse.name -- (mapsto |-- Parse.name); |
69597 | 714 |
val renames = Scan.optional (\<^keyword>\<open>[\<close> |-- Parse.!!! (Parse.list1 rename --| \<^keyword>\<open>]\<close>)) []; |
25171 | 715 |
|
45362 | 716 |
val parent = |
61606
6d5213bd9709
uniform mandatory qualifier for all locale expressions, including 'statespace' parent;
wenzelm
parents:
61144
diff
changeset
|
717 |
Parse_Spec.locale_prefix -- |
62969 | 718 |
((type_insts -- Parse.name) || (Parse.name >> pair [])) -- renames |
49754
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
wenzelm
parents:
48741
diff
changeset
|
719 |
>> (fn ((prefix, (insts, name)), renames) => (prefix, (insts, name, renames))); |
25171 | 720 |
|
45362 | 721 |
in |
25171 | 722 |
|
723 |
val statespace_decl = |
|
45362 | 724 |
Parse.type_args -- Parse.name -- |
69597 | 725 |
(\<^keyword>\<open>=\<close> |-- |
45362 | 726 |
((Scan.repeat1 comp >> pair []) || |
727 |
(plus1_unless comp parent -- |
|
69597 | 728 |
Scan.optional (\<^keyword>\<open>+\<close> |-- Parse.!!! (Scan.repeat1 comp)) []))); |
45362 | 729 |
val _ = |
69597 | 730 |
Outer_Syntax.command \<^command_keyword>\<open>statespace\<close> "define state-space as locale context" |
45362 | 731 |
(statespace_decl >> (fn ((args, name), (parents, comps)) => |
732 |
Toplevel.theory (define_statespace args name parents comps))); |
|
25171 | 733 |
|
45362 | 734 |
end; |
25171 | 735 |
|
45362 | 736 |
end; |