haftmann@28308
|
1 |
(* Title: HOL/Statespace/state_space.ML
|
schirmer@25171
|
2 |
Author: Norbert Schirmer, TU Muenchen
|
schirmer@25171
|
3 |
*)
|
schirmer@25171
|
4 |
|
schirmer@25408
|
5 |
signature STATE_SPACE =
|
schirmer@25408
|
6 |
sig
|
schirmer@25408
|
7 |
val KN : string
|
schirmer@25408
|
8 |
val distinct_compsN : string
|
schirmer@25408
|
9 |
val getN : string
|
schirmer@25408
|
10 |
val putN : string
|
schirmer@25408
|
11 |
val injectN : string
|
schirmer@25408
|
12 |
val namespaceN : string
|
schirmer@25408
|
13 |
val projectN : string
|
schirmer@25408
|
14 |
val valuetypesN : string
|
schirmer@25408
|
15 |
|
schirmer@25408
|
16 |
val namespace_definition :
|
schirmer@25408
|
17 |
bstring ->
|
wenzelm@27276
|
18 |
typ ->
|
norbert@29247
|
19 |
Expression.expression ->
|
wenzelm@27276
|
20 |
string list -> string list -> theory -> theory
|
schirmer@25408
|
21 |
|
schirmer@25408
|
22 |
val define_statespace :
|
schirmer@25408
|
23 |
string list ->
|
schirmer@25408
|
24 |
string ->
|
schirmer@25408
|
25 |
(string list * bstring * (string * string) list) list ->
|
wenzelm@27276
|
26 |
(string * string) list -> theory -> theory
|
schirmer@25408
|
27 |
val define_statespace_i :
|
schirmer@25408
|
28 |
string option ->
|
schirmer@25408
|
29 |
string list ->
|
schirmer@25408
|
30 |
string ->
|
wenzelm@27276
|
31 |
(typ list * bstring * (string * string) list) list ->
|
wenzelm@27276
|
32 |
(string * typ) list -> theory -> theory
|
schirmer@25408
|
33 |
|
schirmer@25408
|
34 |
val statespace_decl :
|
wenzelm@26478
|
35 |
OuterParse.token list ->
|
schirmer@25408
|
36 |
((string list * bstring) *
|
schirmer@25408
|
37 |
((string list * xstring * (bstring * bstring) list) list *
|
schirmer@25408
|
38 |
(bstring * string) list)) * OuterParse.token list
|
schirmer@25408
|
39 |
|
schirmer@25408
|
40 |
|
wenzelm@27276
|
41 |
val neq_x_y : Proof.context -> term -> term -> thm option
|
wenzelm@27283
|
42 |
val distinctNameSolver : Simplifier.solver
|
schirmer@25408
|
43 |
val distinctTree_tac :
|
wenzelm@27276
|
44 |
Proof.context -> term * int -> Tactical.tactic
|
wenzelm@27283
|
45 |
val distinct_simproc : Simplifier.simproc
|
schirmer@25408
|
46 |
|
schirmer@25408
|
47 |
|
wenzelm@27276
|
48 |
val get_comp : Context.generic -> string -> (typ * string) Option.option
|
schirmer@25408
|
49 |
val get_silent : Context.generic -> bool
|
schirmer@25408
|
50 |
val set_silent : bool -> Context.generic -> Context.generic
|
schirmer@25408
|
51 |
|
wenzelm@27276
|
52 |
val gen_lookup_tr : Proof.context -> term -> string -> term
|
wenzelm@27276
|
53 |
val lookup_swap_tr : Proof.context -> term list -> term
|
wenzelm@27276
|
54 |
val lookup_tr : Proof.context -> term list -> term
|
wenzelm@27276
|
55 |
val lookup_tr' : Proof.context -> term list -> term
|
schirmer@25408
|
56 |
|
schirmer@25408
|
57 |
val gen_update_tr :
|
wenzelm@27276
|
58 |
bool -> Proof.context -> string -> term -> term -> term
|
wenzelm@27276
|
59 |
val update_tr : Proof.context -> term list -> term
|
wenzelm@27276
|
60 |
val update_tr' : Proof.context -> term list -> term
|
schirmer@25408
|
61 |
end;
|
schirmer@25408
|
62 |
|
norbert@29247
|
63 |
structure StateSpace : STATE_SPACE =
|
schirmer@25171
|
64 |
struct
|
schirmer@25171
|
65 |
|
schirmer@25171
|
66 |
(* Theorems *)
|
schirmer@25171
|
67 |
|
schirmer@25171
|
68 |
(* Names *)
|
schirmer@25171
|
69 |
val distinct_compsN = "distinct_names"
|
schirmer@25171
|
70 |
val namespaceN = "_namespace"
|
schirmer@25171
|
71 |
val valuetypesN = "_valuetypes"
|
schirmer@25171
|
72 |
val projectN = "project"
|
schirmer@25171
|
73 |
val injectN = "inject"
|
schirmer@25171
|
74 |
val getN = "get"
|
schirmer@25171
|
75 |
val putN = "put"
|
schirmer@25171
|
76 |
val project_injectL = "StateSpaceLocale.project_inject";
|
schirmer@25171
|
77 |
val KN = "StateFun.K_statefun"
|
schirmer@25171
|
78 |
|
schirmer@25171
|
79 |
|
schirmer@25171
|
80 |
(* Library *)
|
schirmer@25171
|
81 |
|
schirmer@25171
|
82 |
fun fold1 f xs = fold f (tl xs) (hd xs)
|
schirmer@25171
|
83 |
fun fold1' f [] x = x
|
schirmer@25171
|
84 |
| fold1' f xs _ = fold1 f xs
|
wenzelm@26478
|
85 |
|
wenzelm@26478
|
86 |
fun sublist_idx eq xs ys =
|
schirmer@25171
|
87 |
let
|
wenzelm@26478
|
88 |
fun sublist n xs ys =
|
schirmer@25171
|
89 |
if is_prefix eq xs ys then SOME n
|
schirmer@25171
|
90 |
else (case ys of [] => NONE
|
schirmer@25171
|
91 |
| (y::ys') => sublist (n+1) xs ys')
|
schirmer@25171
|
92 |
in sublist 0 xs ys end;
|
schirmer@25171
|
93 |
|
schirmer@25171
|
94 |
fun is_sublist eq xs ys = is_some (sublist_idx eq xs ys);
|
schirmer@25171
|
95 |
|
schirmer@25171
|
96 |
fun sorted_subset eq [] ys = true
|
schirmer@25171
|
97 |
| sorted_subset eq (x::xs) [] = false
|
schirmer@25171
|
98 |
| sorted_subset eq (x::xs) (y::ys) = if eq (x,y) then sorted_subset eq xs ys
|
schirmer@25171
|
99 |
else sorted_subset eq (x::xs) ys;
|
schirmer@25171
|
100 |
|
schirmer@25171
|
101 |
|
schirmer@25171
|
102 |
|
schirmer@25171
|
103 |
type namespace_info =
|
schirmer@25171
|
104 |
{declinfo: (typ*string) Termtab.table, (* type, name of statespace *)
|
schirmer@25171
|
105 |
distinctthm: thm Symtab.table,
|
schirmer@25171
|
106 |
silent: bool
|
schirmer@25171
|
107 |
};
|
wenzelm@26478
|
108 |
|
schirmer@25171
|
109 |
structure NameSpaceArgs =
|
schirmer@25171
|
110 |
struct
|
wenzelm@26478
|
111 |
type T = namespace_info;
|
schirmer@25171
|
112 |
val empty = {declinfo = Termtab.empty, distinctthm = Symtab.empty, silent = false};
|
schirmer@25171
|
113 |
val extend = I;
|
schirmer@25171
|
114 |
fun merge pp ({declinfo=declinfo1, distinctthm=distinctthm1, silent=silent1},
|
schirmer@25171
|
115 |
{declinfo=declinfo2, distinctthm=distinctthm2, silent=silent2}) =
|
schirmer@25171
|
116 |
{declinfo = Termtab.merge (K true) (declinfo1, declinfo2),
|
schirmer@25171
|
117 |
distinctthm = Symtab.merge (K true) (distinctthm1, distinctthm2),
|
schirmer@25171
|
118 |
silent = silent1 andalso silent2}
|
schirmer@25171
|
119 |
end;
|
schirmer@25171
|
120 |
|
schirmer@25171
|
121 |
structure NameSpaceData = GenericDataFun(NameSpaceArgs);
|
schirmer@25171
|
122 |
|
schirmer@25171
|
123 |
fun make_namespace_data declinfo distinctthm silent =
|
schirmer@25171
|
124 |
{declinfo=declinfo,distinctthm=distinctthm,silent=silent};
|
schirmer@25171
|
125 |
|
schirmer@25171
|
126 |
|
schirmer@25171
|
127 |
fun delete_declinfo n ctxt =
|
schirmer@25171
|
128 |
let val {declinfo,distinctthm,silent} = NameSpaceData.get ctxt;
|
wenzelm@26478
|
129 |
in NameSpaceData.put
|
schirmer@25171
|
130 |
(make_namespace_data (Termtab.delete_safe n declinfo) distinctthm silent) ctxt
|
schirmer@25171
|
131 |
end;
|
schirmer@25171
|
132 |
|
schirmer@25171
|
133 |
|
schirmer@25171
|
134 |
fun update_declinfo (n,v) ctxt =
|
schirmer@25171
|
135 |
let val {declinfo,distinctthm,silent} = NameSpaceData.get ctxt;
|
wenzelm@26478
|
136 |
in NameSpaceData.put
|
schirmer@25171
|
137 |
(make_namespace_data (Termtab.update (n,v) declinfo) distinctthm silent) ctxt
|
schirmer@25171
|
138 |
end;
|
schirmer@25171
|
139 |
|
schirmer@25171
|
140 |
fun set_silent silent ctxt =
|
schirmer@25171
|
141 |
let val {declinfo,distinctthm,...} = NameSpaceData.get ctxt;
|
wenzelm@26478
|
142 |
in NameSpaceData.put
|
schirmer@25171
|
143 |
(make_namespace_data declinfo distinctthm silent) ctxt
|
schirmer@25171
|
144 |
end;
|
schirmer@25171
|
145 |
|
schirmer@25171
|
146 |
val get_silent = #silent o NameSpaceData.get;
|
wenzelm@26478
|
147 |
|
schirmer@25171
|
148 |
fun prove_interpretation_in ctxt_tac (name, expr) thy =
|
schirmer@25171
|
149 |
thy
|
norbert@29247
|
150 |
|> Expression.sublocale_cmd name expr
|
wenzelm@26478
|
151 |
|> Proof.global_terminal_proof
|
wenzelm@32194
|
152 |
(Method.Basic (fn ctxt => SIMPLE_METHOD (ctxt_tac ctxt)), NONE)
|
schirmer@25171
|
153 |
|> ProofContext.theory_of
|
schirmer@25171
|
154 |
|
norbert@29247
|
155 |
fun add_locale name expr elems thy =
|
norbert@29247
|
156 |
thy
|
wenzelm@30352
|
157 |
|> Expression.add_locale (Binding.name name) (Binding.name name) expr elems
|
haftmann@29362
|
158 |
|> snd
|
norbert@29247
|
159 |
|> LocalTheory.exit;
|
norbert@29247
|
160 |
|
norbert@29247
|
161 |
fun add_locale_cmd name expr elems thy =
|
norbert@29247
|
162 |
thy
|
wenzelm@30352
|
163 |
|> Expression.add_locale_cmd (Binding.name name) Binding.empty expr elems
|
haftmann@29362
|
164 |
|> snd
|
norbert@29247
|
165 |
|> LocalTheory.exit;
|
norbert@29247
|
166 |
|
schirmer@25171
|
167 |
type statespace_info =
|
schirmer@25171
|
168 |
{args: (string * sort) list, (* type arguments *)
|
schirmer@25171
|
169 |
parents: (typ list * string * string option list) list,
|
schirmer@25171
|
170 |
(* type instantiation, state-space name, component renamings *)
|
schirmer@25171
|
171 |
components: (string * typ) list,
|
schirmer@25171
|
172 |
types: typ list (* range types of state space *)
|
schirmer@25171
|
173 |
};
|
schirmer@25171
|
174 |
|
schirmer@25171
|
175 |
structure StateSpaceArgs =
|
schirmer@25171
|
176 |
struct
|
schirmer@25171
|
177 |
val name = "HOL/StateSpace";
|
schirmer@25171
|
178 |
type T = statespace_info Symtab.table;
|
schirmer@25171
|
179 |
val empty = Symtab.empty;
|
schirmer@25171
|
180 |
val extend = I;
|
schirmer@25171
|
181 |
|
schirmer@25171
|
182 |
fun merge pp (nt1,nt2) = Symtab.merge (K true) (nt1, nt2);
|
schirmer@25171
|
183 |
end;
|
schirmer@25171
|
184 |
|
schirmer@25171
|
185 |
structure StateSpaceData = GenericDataFun(StateSpaceArgs);
|
schirmer@25171
|
186 |
|
schirmer@25171
|
187 |
fun add_statespace name args parents components types ctxt =
|
wenzelm@26478
|
188 |
StateSpaceData.put
|
schirmer@25171
|
189 |
(Symtab.update_new (name, {args=args,parents=parents,
|
schirmer@25171
|
190 |
components=components,types=types}) (StateSpaceData.get ctxt))
|
wenzelm@26478
|
191 |
ctxt;
|
schirmer@25171
|
192 |
|
schirmer@25171
|
193 |
fun get_statespace ctxt name =
|
schirmer@25171
|
194 |
Symtab.lookup (StateSpaceData.get ctxt) name;
|
schirmer@25171
|
195 |
|
schirmer@25171
|
196 |
|
wenzelm@26478
|
197 |
fun lookupI eq xs n =
|
schirmer@25171
|
198 |
(case AList.lookup eq xs n of
|
schirmer@25171
|
199 |
SOME v => v
|
schirmer@25171
|
200 |
| NONE => n);
|
wenzelm@26478
|
201 |
|
schirmer@25171
|
202 |
fun mk_free ctxt name =
|
wenzelm@26478
|
203 |
if Variable.is_fixed ctxt name orelse Variable.is_declared ctxt name
|
schirmer@25171
|
204 |
then
|
schirmer@25171
|
205 |
let val n' = lookupI (op =) (Variable.fixes_of ctxt) name
|
schirmer@25171
|
206 |
in SOME (Free (n',ProofContext.infer_type ctxt n')) end
|
schirmer@25171
|
207 |
else NONE
|
wenzelm@26478
|
208 |
|
wenzelm@26478
|
209 |
|
schirmer@25171
|
210 |
fun get_dist_thm ctxt name = Symtab.lookup (#distinctthm (NameSpaceData.get ctxt)) name;
|
wenzelm@26478
|
211 |
fun get_comp ctxt name =
|
wenzelm@26478
|
212 |
Option.mapPartial
|
wenzelm@26478
|
213 |
(Termtab.lookup (#declinfo (NameSpaceData.get ctxt)))
|
schirmer@25171
|
214 |
(mk_free (Context.proof_of ctxt) name);
|
schirmer@25171
|
215 |
|
schirmer@25171
|
216 |
|
schirmer@25171
|
217 |
(*** Tactics ***)
|
schirmer@25171
|
218 |
|
schirmer@25171
|
219 |
fun neq_x_y ctxt x y =
|
schirmer@25171
|
220 |
(let
|
wenzelm@26478
|
221 |
val dist_thm = the (get_dist_thm (Context.Proof ctxt) (#1 (dest_Free x)));
|
schirmer@25171
|
222 |
val ctree = cprop_of dist_thm |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2;
|
schirmer@25171
|
223 |
val tree = term_of ctree;
|
schirmer@25171
|
224 |
val x_path = the (DistinctTreeProver.find_tree x tree);
|
schirmer@25171
|
225 |
val y_path = the (DistinctTreeProver.find_tree y tree);
|
schirmer@25171
|
226 |
val thm = DistinctTreeProver.distinctTreeProver dist_thm x_path y_path;
|
wenzelm@26478
|
227 |
in SOME thm
|
schirmer@25171
|
228 |
end handle Option => NONE)
|
schirmer@25171
|
229 |
|
wenzelm@26478
|
230 |
fun distinctTree_tac ctxt
|
wenzelm@26478
|
231 |
(Const ("Trueprop",_) $
|
schirmer@25171
|
232 |
(Const ("Not", _) $ (Const ("op =", _) $ (x as Free _)$ (y as Free _))), i) =
|
schirmer@25171
|
233 |
(case (neq_x_y ctxt x y) of
|
schirmer@25171
|
234 |
SOME neq => rtac neq i
|
schirmer@25171
|
235 |
| NONE => no_tac)
|
schirmer@25171
|
236 |
| distinctTree_tac _ _ = no_tac;
|
schirmer@25171
|
237 |
|
schirmer@25171
|
238 |
val distinctNameSolver = mk_solver' "distinctNameSolver"
|
wenzelm@30289
|
239 |
(fn ss => case try Simplifier.the_context ss of
|
schirmer@25171
|
240 |
SOME ctxt => SUBGOAL (distinctTree_tac ctxt)
|
schirmer@25171
|
241 |
| NONE => fn i => no_tac)
|
schirmer@25171
|
242 |
|
schirmer@25171
|
243 |
val distinct_simproc =
|
wenzelm@29064
|
244 |
Simplifier.simproc @{theory HOL} "StateSpace.distinct_simproc" ["x = y"]
|
schirmer@25171
|
245 |
(fn thy => fn ss => (fn (Const ("op =",_)$(x as Free _)$(y as Free _)) =>
|
wenzelm@30289
|
246 |
(case try Simplifier.the_context ss of
|
wenzelm@26478
|
247 |
SOME ctxt => Option.map (fn neq => DistinctTreeProver.neq_to_eq_False OF [neq])
|
wenzelm@26478
|
248 |
(neq_x_y ctxt x y)
|
schirmer@25171
|
249 |
| NONE => NONE)
|
schirmer@25171
|
250 |
| _ => NONE))
|
schirmer@25171
|
251 |
|
schirmer@25171
|
252 |
local
|
wenzelm@26478
|
253 |
val ss = HOL_basic_ss
|
wenzelm@26478
|
254 |
in
|
wenzelm@26478
|
255 |
fun interprete_parent name dist_thm_name parent_expr thy =
|
schirmer@25171
|
256 |
let
|
schirmer@25171
|
257 |
|
schirmer@25171
|
258 |
fun solve_tac ctxt (_,i) st =
|
schirmer@25171
|
259 |
let
|
wenzelm@26343
|
260 |
val distinct_thm = ProofContext.get_thm ctxt dist_thm_name;
|
schirmer@25171
|
261 |
val goal = List.nth (cprems_of st,i-1);
|
schirmer@25171
|
262 |
val rule = DistinctTreeProver.distinct_implProver distinct_thm goal;
|
schirmer@25171
|
263 |
in EVERY [rtac rule i] st
|
schirmer@25171
|
264 |
end
|
wenzelm@26478
|
265 |
|
haftmann@29360
|
266 |
fun tac ctxt = EVERY [Locale.intro_locales_tac true ctxt [],
|
wenzelm@26478
|
267 |
ALLGOALS (SUBGOAL (solve_tac ctxt))]
|
wenzelm@26478
|
268 |
|
wenzelm@26478
|
269 |
in thy
|
schirmer@25171
|
270 |
|> prove_interpretation_in tac (name,parent_expr)
|
wenzelm@26478
|
271 |
end;
|
schirmer@25171
|
272 |
|
schirmer@25171
|
273 |
end;
|
schirmer@25171
|
274 |
|
wenzelm@26478
|
275 |
fun namespace_definition name nameT parent_expr parent_comps new_comps thy =
|
schirmer@25171
|
276 |
let
|
schirmer@25171
|
277 |
val all_comps = parent_comps @ new_comps;
|
norbert@29247
|
278 |
val vars = (map (fn n => (Binding.name n, NONE, NoSyn)) all_comps);
|
haftmann@28965
|
279 |
val full_name = Sign.full_bname thy name;
|
schirmer@25171
|
280 |
val dist_thm_name = distinct_compsN;
|
schirmer@25171
|
281 |
|
norbert@29247
|
282 |
val dist_thm_full_name = dist_thm_name;
|
wenzelm@26478
|
283 |
fun comps_of_thm thm = prop_of thm
|
schirmer@25171
|
284 |
|> (fn (_$(_$t)) => DistinctTreeProver.dest_tree t) |> map (fst o dest_Free);
|
wenzelm@26478
|
285 |
|
schirmer@25171
|
286 |
fun type_attr phi (ctxt,thm) =
|
schirmer@25171
|
287 |
(case ctxt of Context.Theory _ => (ctxt,thm)
|
schirmer@25171
|
288 |
| _ =>
|
wenzelm@26478
|
289 |
let
|
schirmer@25171
|
290 |
val {declinfo,distinctthm=tt,silent} = (NameSpaceData.get ctxt);
|
schirmer@25171
|
291 |
val all_names = comps_of_thm thm;
|
schirmer@25171
|
292 |
fun upd name tt =
|
schirmer@25171
|
293 |
(case (Symtab.lookup tt name) of
|
schirmer@25171
|
294 |
SOME dthm => if sorted_subset (op =) (comps_of_thm dthm) all_names
|
schirmer@25171
|
295 |
then Symtab.update (name,thm) tt else tt
|
schirmer@25171
|
296 |
| NONE => Symtab.update (name,thm) tt)
|
schirmer@25171
|
297 |
|
schirmer@25171
|
298 |
val tt' = tt |> fold upd all_names;
|
schirmer@25171
|
299 |
val activate_simproc =
|
wenzelm@26478
|
300 |
Output.no_warnings
|
wenzelm@26496
|
301 |
(Simplifier.map_ss (fn ss => ss addsimprocs [distinct_simproc]));
|
schirmer@25171
|
302 |
val ctxt' =
|
wenzelm@26478
|
303 |
ctxt
|
schirmer@25171
|
304 |
|> NameSpaceData.put {declinfo=declinfo,distinctthm=tt',silent=silent}
|
schirmer@25171
|
305 |
|> activate_simproc
|
wenzelm@26478
|
306 |
in (ctxt',thm)
|
schirmer@25171
|
307 |
end)
|
wenzelm@26478
|
308 |
|
schirmer@25171
|
309 |
val attr = Attrib.internal type_attr;
|
schirmer@25171
|
310 |
|
haftmann@28965
|
311 |
val assumes = Element.Assumes [((Binding.name dist_thm_name,[attr]),
|
schirmer@25171
|
312 |
[(HOLogic.Trueprop $
|
schirmer@25171
|
313 |
(Const ("DistinctTreeProver.all_distinct",
|
schirmer@25171
|
314 |
Type ("DistinctTreeProver.tree",[nameT]) --> HOLogic.boolT) $
|
wenzelm@26478
|
315 |
DistinctTreeProver.mk_tree (fn n => Free (n,nameT)) nameT
|
schirmer@25171
|
316 |
(sort fast_string_ord all_comps)),
|
schirmer@25171
|
317 |
([]))])];
|
wenzelm@26478
|
318 |
in thy
|
norbert@29247
|
319 |
|> add_locale name ([],vars) [assumes]
|
norbert@29247
|
320 |
|> ProofContext.theory_of
|
norbert@29247
|
321 |
|> interprete_parent name dist_thm_full_name parent_expr
|
schirmer@25171
|
322 |
end;
|
schirmer@25171
|
323 |
|
hoelzl@32650
|
324 |
fun encode_dot x =
|
hoelzl@32650
|
325 |
if x= #"." then #"_" else x;
|
schirmer@25171
|
326 |
|
schirmer@25171
|
327 |
fun encode_type (TFree (s, _)) = s
|
schirmer@25171
|
328 |
| encode_type (TVar ((s,i),_)) = "?" ^ s ^ string_of_int i
|
wenzelm@26478
|
329 |
| encode_type (Type (n,Ts)) =
|
schirmer@25171
|
330 |
let
|
schirmer@25171
|
331 |
val Ts' = fold1' (fn x => fn y => x ^ "_" ^ y) (map encode_type Ts) "";
|
hoelzl@32650
|
332 |
val n' = if n = "*" then "Prod" else
|
hoelzl@32650
|
333 |
if n = "+" then "Sum" else
|
hoelzl@32650
|
334 |
String.map encode_dot n;
|
schirmer@25171
|
335 |
in if Ts'="" then n' else Ts' ^ "_" ^ n' end;
|
schirmer@25171
|
336 |
|
schirmer@25171
|
337 |
fun project_name T = projectN ^"_"^encode_type T;
|
schirmer@25171
|
338 |
fun inject_name T = injectN ^"_"^encode_type T;
|
schirmer@25171
|
339 |
|
schirmer@25171
|
340 |
fun project_free T pT V = Free (project_name T, V --> pT);
|
schirmer@25171
|
341 |
fun inject_free T pT V = Free (inject_name T, pT --> V);
|
schirmer@25171
|
342 |
|
schirmer@25171
|
343 |
fun get_name n = getN ^ "_" ^ n;
|
schirmer@25171
|
344 |
fun put_name n = putN ^ "_" ^ n;
|
schirmer@25171
|
345 |
fun get_const n T nT V = Free (get_name n, (nT --> V) --> T);
|
schirmer@25171
|
346 |
fun put_const n T nT V = Free (put_name n, T --> (nT --> V) --> (nT --> V));
|
schirmer@25171
|
347 |
|
schirmer@25171
|
348 |
fun lookup_const T nT V = Const ("StateFun.lookup",(V --> T) --> nT --> (nT --> V) --> T);
|
wenzelm@26478
|
349 |
fun update_const T nT V =
|
schirmer@25171
|
350 |
Const ("StateFun.update",
|
schirmer@25171
|
351 |
(V --> T) --> (T --> V) --> nT --> (T --> T) --> (nT --> V) --> (nT --> V));
|
schirmer@25171
|
352 |
|
schirmer@25171
|
353 |
fun K_const T = Const ("StateFun.K_statefun",T --> T --> T);
|
schirmer@25171
|
354 |
|
schirmer@25171
|
355 |
val no_syn = #3 (Syntax.no_syn ((),()));
|
schirmer@25171
|
356 |
|
schirmer@25171
|
357 |
|
schirmer@25171
|
358 |
fun add_declaration name decl thy =
|
schirmer@25171
|
359 |
thy
|
schirmer@25171
|
360 |
|> TheoryTarget.init name
|
schirmer@25171
|
361 |
|> (fn lthy => LocalTheory.declaration (decl lthy) lthy)
|
wenzelm@28394
|
362 |
|> LocalTheory.exit_global;
|
schirmer@25171
|
363 |
|
schirmer@25171
|
364 |
fun parent_components thy (Ts, pname, renaming) =
|
schirmer@25171
|
365 |
let
|
schirmer@25171
|
366 |
val ctxt = Context.Theory thy;
|
schirmer@25171
|
367 |
fun rename [] xs = xs
|
schirmer@25171
|
368 |
| rename (NONE::rs) (x::xs) = x::rename rs xs
|
schirmer@25171
|
369 |
| rename (SOME r::rs) ((x,T)::xs) = (r,T)::rename rs xs;
|
wenzelm@26478
|
370 |
val {args,parents,components,...} =
|
schirmer@25171
|
371 |
the (Symtab.lookup (StateSpaceData.get ctxt) pname);
|
schirmer@25171
|
372 |
val inst = map fst args ~~ Ts;
|
schirmer@25171
|
373 |
val subst = Term.map_type_tfree (the o AList.lookup (op =) inst o fst);
|
wenzelm@26478
|
374 |
val parent_comps =
|
schirmer@25171
|
375 |
List.concat (map (fn (Ts',n,rs) => parent_components thy (map subst Ts',n,rs)) parents);
|
schirmer@25171
|
376 |
val all_comps = rename renaming (parent_comps @ map (apsnd subst) components);
|
schirmer@25171
|
377 |
in all_comps end;
|
schirmer@25171
|
378 |
|
schirmer@25171
|
379 |
fun take_upto i xs = List.take(xs,i) handle Subscript => xs;
|
schirmer@25171
|
380 |
|
schirmer@25171
|
381 |
fun statespace_definition state_type args name parents parent_comps components thy =
|
wenzelm@26478
|
382 |
let
|
haftmann@28965
|
383 |
val full_name = Sign.full_bname thy name;
|
schirmer@25171
|
384 |
val all_comps = parent_comps @ components;
|
schirmer@25171
|
385 |
|
schirmer@25171
|
386 |
val components' = map (fn (n,T) => (n,(T,full_name))) components;
|
wenzelm@26478
|
387 |
val all_comps' = map (fn (n,T) => (n,(T,full_name))) all_comps;
|
schirmer@25171
|
388 |
|
norbert@29247
|
389 |
fun parent_expr (_,n,rs) = (suffix namespaceN n,((n,false),Expression.Positional rs));
|
norbert@29247
|
390 |
(* FIXME: a more specific renaming-prefix (including parameter names) may be nicer *)
|
norbert@29247
|
391 |
val parents_expr = map parent_expr parents;
|
schirmer@25171
|
392 |
fun distinct_types Ts =
|
wenzelm@27276
|
393 |
let val tab = fold (fn T => fn tab => Typtab.update (T,()) tab) Ts Typtab.empty;
|
wenzelm@27276
|
394 |
in map fst (Typtab.dest tab) end;
|
schirmer@25171
|
395 |
|
schirmer@25171
|
396 |
val Ts = distinct_types (map snd all_comps);
|
schirmer@25171
|
397 |
val arg_names = map fst args;
|
schirmer@25171
|
398 |
val valueN = Name.variant arg_names "'value";
|
schirmer@25171
|
399 |
val nameN = Name.variant (valueN::arg_names) "'name";
|
schirmer@25171
|
400 |
val valueT = TFree (valueN, Sign.defaultS thy);
|
schirmer@25171
|
401 |
val nameT = TFree (nameN, Sign.defaultS thy);
|
schirmer@25171
|
402 |
val stateT = nameT --> valueT;
|
schirmer@25171
|
403 |
fun projectT T = valueT --> T;
|
wenzelm@26478
|
404 |
fun injectT T = T --> valueT;
|
norbert@29247
|
405 |
val locinsts = map (fn T => (project_injectL,
|
norbert@29247
|
406 |
(("",false),Expression.Positional
|
norbert@29247
|
407 |
[SOME (Free (project_name T,projectT T)),
|
norbert@29247
|
408 |
SOME (Free ((inject_name T,injectT T)))]))) Ts;
|
norbert@29247
|
409 |
val locs = flat (map (fn T => [(Binding.name (project_name T),NONE,NoSyn),
|
norbert@29247
|
410 |
(Binding.name (inject_name T),NONE,NoSyn)]) Ts);
|
wenzelm@26478
|
411 |
val constrains = List.concat
|
schirmer@25171
|
412 |
(map (fn T => [(project_name T,projectT T),(inject_name T,injectT T)]) Ts);
|
schirmer@25171
|
413 |
|
norbert@29247
|
414 |
fun interprete_parent_valuetypes (Ts, pname, _) thy =
|
schirmer@25171
|
415 |
let
|
wenzelm@26478
|
416 |
val {args,types,...} =
|
schirmer@25171
|
417 |
the (Symtab.lookup (StateSpaceData.get (Context.Theory thy)) pname);
|
schirmer@25171
|
418 |
val inst = map fst args ~~ Ts;
|
schirmer@25171
|
419 |
val subst = Term.map_type_tfree (the o AList.lookup (op =) inst o fst);
|
schirmer@25171
|
420 |
val pars = List.concat (map ((fn T => [project_name T,inject_name T]) o subst) types);
|
norbert@29247
|
421 |
|
norbert@29247
|
422 |
val expr = ([(suffix valuetypesN name,
|
norbert@29247
|
423 |
(("",false),Expression.Positional (map SOME pars)))],[]);
|
wenzelm@29291
|
424 |
in
|
wenzelm@30479
|
425 |
prove_interpretation_in (ALLGOALS o solve_tac o Assumption.all_prems_of)
|
wenzelm@29291
|
426 |
(suffix valuetypesN name, expr) thy
|
wenzelm@29291
|
427 |
end;
|
schirmer@25171
|
428 |
|
schirmer@25171
|
429 |
fun interprete_parent (_, pname, rs) =
|
schirmer@25171
|
430 |
let
|
norbert@29247
|
431 |
val expr = ([(pname, (("",false), Expression.Positional rs))],[])
|
wenzelm@26478
|
432 |
in prove_interpretation_in
|
haftmann@29360
|
433 |
(fn ctxt => Locale.intro_locales_tac false ctxt [])
|
schirmer@25171
|
434 |
(full_name, expr) end;
|
schirmer@25171
|
435 |
|
schirmer@25171
|
436 |
fun declare_declinfo updates lthy phi ctxt =
|
schirmer@25171
|
437 |
let
|
wenzelm@26478
|
438 |
fun upd_prf ctxt =
|
schirmer@25171
|
439 |
let
|
schirmer@25171
|
440 |
fun upd (n,v) =
|
schirmer@25171
|
441 |
let
|
schirmer@25171
|
442 |
val nT = ProofContext.infer_type (LocalTheory.target_of lthy) n
|
wenzelm@26478
|
443 |
in Context.proof_map
|
wenzelm@26478
|
444 |
(update_declinfo (Morphism.term phi (Free (n,nT)),v))
|
schirmer@25171
|
445 |
end;
|
schirmer@25171
|
446 |
in ctxt |> fold upd updates end;
|
schirmer@25171
|
447 |
|
schirmer@25171
|
448 |
in Context.mapping I upd_prf ctxt end;
|
schirmer@25171
|
449 |
|
wenzelm@26478
|
450 |
fun string_of_typ T =
|
wenzelm@26478
|
451 |
setmp show_sorts true
|
wenzelm@25696
|
452 |
(PrintMode.setmp [] (Syntax.string_of_typ (ProofContext.init thy))) T;
|
schirmer@25171
|
453 |
val fixestate = (case state_type of
|
schirmer@25171
|
454 |
NONE => []
|
wenzelm@26478
|
455 |
| SOME s =>
|
wenzelm@26478
|
456 |
let
|
haftmann@28965
|
457 |
val fx = Element.Fixes [(Binding.name s,SOME (string_of_typ stateT),NoSyn)];
|
wenzelm@26478
|
458 |
val cs = Element.Constrains
|
schirmer@25171
|
459 |
(map (fn (n,T) => (n,string_of_typ T))
|
schirmer@25171
|
460 |
((map (fn (n,_) => (n,nameT)) all_comps) @
|
schirmer@25171
|
461 |
constrains))
|
schirmer@25171
|
462 |
in [fx,cs] end
|
schirmer@25171
|
463 |
)
|
schirmer@25171
|
464 |
|
wenzelm@26478
|
465 |
|
wenzelm@26478
|
466 |
in thy
|
wenzelm@26478
|
467 |
|> namespace_definition
|
norbert@29247
|
468 |
(suffix namespaceN name) nameT (parents_expr,[])
|
schirmer@25171
|
469 |
(map fst parent_comps) (map fst components)
|
schirmer@25171
|
470 |
|> Context.theory_map (add_statespace full_name args parents components [])
|
norbert@29247
|
471 |
|> add_locale (suffix valuetypesN name) (locinsts,locs) []
|
norbert@29247
|
472 |
|> ProofContext.theory_of
|
wenzelm@26478
|
473 |
|> fold interprete_parent_valuetypes parents
|
norbert@29247
|
474 |
|> add_locale_cmd name
|
norbert@29247
|
475 |
([(suffix namespaceN full_name ,(("",false),Expression.Named [])),
|
norbert@29247
|
476 |
(suffix valuetypesN full_name,(("",false),Expression.Named []))],[]) fixestate
|
norbert@29247
|
477 |
|> ProofContext.theory_of
|
schirmer@25171
|
478 |
|> fold interprete_parent parents
|
schirmer@25171
|
479 |
|> add_declaration (SOME full_name) (declare_declinfo components')
|
schirmer@25171
|
480 |
end;
|
schirmer@25171
|
481 |
|
schirmer@25171
|
482 |
|
schirmer@25171
|
483 |
(* prepare arguments *)
|
schirmer@25171
|
484 |
|
wenzelm@27283
|
485 |
fun read_raw_parent ctxt raw_T =
|
wenzelm@27283
|
486 |
(case ProofContext.read_typ_abbrev ctxt raw_T of
|
schirmer@25171
|
487 |
Type (name, Ts) => (Ts, name)
|
wenzelm@27283
|
488 |
| T => error ("Bad parent statespace specification: " ^ Syntax.string_of_typ ctxt T));
|
schirmer@25171
|
489 |
|
schirmer@25171
|
490 |
fun gen_define_statespace prep_typ state_space args name parents comps thy =
|
schirmer@25171
|
491 |
let (* - args distinct
|
schirmer@25171
|
492 |
- only args may occur in comps and parent-instantiations
|
wenzelm@26478
|
493 |
- number of insts must match parent args
|
schirmer@25171
|
494 |
- no duplicate renamings
|
schirmer@25171
|
495 |
- renaming should occur in namespace
|
schirmer@25171
|
496 |
*)
|
wenzelm@26478
|
497 |
val _ = writeln ("Defining statespace " ^ quote name ^ " ...");
|
schirmer@25171
|
498 |
|
wenzelm@27283
|
499 |
val ctxt = ProofContext.init thy;
|
wenzelm@27283
|
500 |
|
schirmer@25171
|
501 |
fun add_parent (Ts,pname,rs) env =
|
schirmer@25171
|
502 |
let
|
haftmann@28965
|
503 |
val full_pname = Sign.full_bname thy pname;
|
wenzelm@26478
|
504 |
val {args,components,...} =
|
schirmer@25171
|
505 |
(case get_statespace (Context.Theory thy) full_pname of
|
schirmer@25171
|
506 |
SOME r => r
|
schirmer@25171
|
507 |
| NONE => error ("Undefined statespace " ^ quote pname));
|
schirmer@25171
|
508 |
|
schirmer@25171
|
509 |
|
wenzelm@27283
|
510 |
val (Ts',env') = fold_map (prep_typ ctxt) Ts env
|
wenzelm@26478
|
511 |
handle ERROR msg => cat_error msg
|
wenzelm@26478
|
512 |
("The error(s) above occured in parent statespace specification "
|
schirmer@25171
|
513 |
^ quote pname);
|
schirmer@25171
|
514 |
val err_insts = if length args <> length Ts' then
|
schirmer@25171
|
515 |
["number of type instantiation(s) does not match arguments of parent statespace "
|
schirmer@25171
|
516 |
^ quote pname]
|
schirmer@25171
|
517 |
else [];
|
wenzelm@26478
|
518 |
|
schirmer@25171
|
519 |
val rnames = map fst rs
|
schirmer@25171
|
520 |
val err_dup_renamings = (case duplicates (op =) rnames of
|
schirmer@25171
|
521 |
[] => []
|
schirmer@25171
|
522 |
| dups => ["Duplicate renaming(s) for " ^ commas dups])
|
schirmer@25171
|
523 |
|
schirmer@25171
|
524 |
val cnames = map fst components;
|
schirmer@25171
|
525 |
val err_rename_unknowns = (case (filter (fn n => not (n mem cnames))) rnames of
|
schirmer@25171
|
526 |
[] => []
|
schirmer@25171
|
527 |
| rs => ["Unknown components " ^ commas rs]);
|
wenzelm@26478
|
528 |
|
schirmer@25171
|
529 |
|
schirmer@25171
|
530 |
val rs' = map (AList.lookup (op =) rs o fst) components;
|
schirmer@25171
|
531 |
val errs =err_insts @ err_dup_renamings @ err_rename_unknowns
|
schirmer@25171
|
532 |
in if null errs then ((Ts',full_pname,rs'),env')
|
schirmer@25171
|
533 |
else error (cat_lines (errs @ ["in parent statespace " ^ quote pname]))
|
schirmer@25171
|
534 |
end;
|
wenzelm@26478
|
535 |
|
schirmer@25171
|
536 |
val (parents',env) = fold_map add_parent parents [];
|
schirmer@25171
|
537 |
|
schirmer@25171
|
538 |
val err_dup_args =
|
schirmer@25171
|
539 |
(case duplicates (op =) args of
|
schirmer@25171
|
540 |
[] => []
|
schirmer@25171
|
541 |
| dups => ["Duplicate type argument(s) " ^ commas dups]);
|
schirmer@25171
|
542 |
|
wenzelm@26478
|
543 |
|
wenzelm@26478
|
544 |
val err_dup_components =
|
schirmer@25171
|
545 |
(case duplicates (op =) (map fst comps) of
|
schirmer@25171
|
546 |
[] => []
|
schirmer@25171
|
547 |
| dups => ["Duplicate state-space components " ^ commas dups]);
|
schirmer@25171
|
548 |
|
schirmer@25171
|
549 |
fun prep_comp (n,T) env =
|
wenzelm@27283
|
550 |
let val (T', env') = prep_typ ctxt T env handle ERROR msg =>
|
schirmer@25171
|
551 |
cat_error msg ("The error(s) above occured in component " ^ quote n)
|
schirmer@25171
|
552 |
in ((n,T'), env') end;
|
schirmer@25171
|
553 |
|
schirmer@25171
|
554 |
val (comps',env') = fold_map prep_comp comps env;
|
schirmer@25171
|
555 |
|
schirmer@25171
|
556 |
val err_extra_frees =
|
schirmer@25171
|
557 |
(case subtract (op =) args (map fst env') of
|
schirmer@25171
|
558 |
[] => []
|
schirmer@25171
|
559 |
| extras => ["Extra free type variable(s) " ^ commas extras]);
|
schirmer@25171
|
560 |
|
schirmer@25171
|
561 |
val defaultS = Sign.defaultS thy;
|
schirmer@25171
|
562 |
val args' = map (fn x => (x, AList.lookup (op =) env x |> the_default defaultS)) args;
|
schirmer@25171
|
563 |
|
schirmer@25171
|
564 |
|
schirmer@25171
|
565 |
fun fst_eq ((x:string,_),(y,_)) = x = y;
|
wenzelm@26478
|
566 |
fun snd_eq ((_,t:typ),(_,u)) = t = u;
|
schirmer@25171
|
567 |
|
schirmer@25171
|
568 |
val raw_parent_comps = (List.concat (map (parent_components thy) parents'));
|
wenzelm@26478
|
569 |
fun check_type (n,T) =
|
schirmer@25171
|
570 |
(case distinct (snd_eq) (filter (curry fst_eq (n,T)) raw_parent_comps) of
|
schirmer@25171
|
571 |
[] => []
|
schirmer@25171
|
572 |
| [_] => []
|
wenzelm@32445
|
573 |
| rs => ["Different types for component " ^ n ^": " ^
|
wenzelm@32445
|
574 |
commas (map (Syntax.string_of_typ ctxt o snd) rs)])
|
wenzelm@26478
|
575 |
|
schirmer@25171
|
576 |
val err_dup_types = List.concat (map check_type (duplicates fst_eq raw_parent_comps))
|
schirmer@25171
|
577 |
|
schirmer@25171
|
578 |
val parent_comps = distinct (fst_eq) raw_parent_comps;
|
schirmer@25171
|
579 |
val all_comps = parent_comps @ comps';
|
schirmer@25171
|
580 |
val err_comp_in_parent = (case duplicates (op =) (map fst all_comps) of
|
schirmer@25171
|
581 |
[] => []
|
schirmer@25171
|
582 |
| xs => ["Components already defined in parents: " ^ commas xs]);
|
schirmer@25171
|
583 |
val errs = err_dup_args @ err_dup_components @ err_extra_frees @
|
schirmer@25171
|
584 |
err_dup_types @ err_comp_in_parent;
|
wenzelm@26478
|
585 |
in if null errs
|
schirmer@25171
|
586 |
then thy |> statespace_definition state_space args' name parents' parent_comps comps'
|
wenzelm@26478
|
587 |
else error (cat_lines errs)
|
schirmer@25171
|
588 |
end
|
schirmer@25171
|
589 |
handle ERROR msg => cat_error msg ("Failed to define statespace " ^ quote name);
|
wenzelm@26478
|
590 |
|
haftmann@31723
|
591 |
val define_statespace = gen_define_statespace Record.read_typ NONE;
|
haftmann@31723
|
592 |
val define_statespace_i = gen_define_statespace Record.cert_typ;
|
wenzelm@26478
|
593 |
|
schirmer@25171
|
594 |
|
schirmer@25171
|
595 |
(*** parse/print - translations ***)
|
schirmer@25171
|
596 |
|
schirmer@25171
|
597 |
|
schirmer@25171
|
598 |
local
|
wenzelm@26478
|
599 |
fun map_get_comp f ctxt (Free (name,_)) =
|
wenzelm@26478
|
600 |
(case (get_comp ctxt name) of
|
wenzelm@26478
|
601 |
SOME (T,_) => f T T dummyT
|
schirmer@25171
|
602 |
| NONE => (Syntax.free "arbitrary"(*; error "context not ready"*)))
|
schirmer@25171
|
603 |
| map_get_comp _ _ _ = Syntax.free "arbitrary";
|
schirmer@25171
|
604 |
|
schirmer@25171
|
605 |
val get_comp_projection = map_get_comp project_free;
|
schirmer@25171
|
606 |
val get_comp_injection = map_get_comp inject_free;
|
schirmer@25171
|
607 |
|
schirmer@25171
|
608 |
fun name_of (Free (n,_)) = n;
|
schirmer@25171
|
609 |
in
|
schirmer@25171
|
610 |
|
wenzelm@26478
|
611 |
fun gen_lookup_tr ctxt s n =
|
schirmer@25171
|
612 |
(case get_comp (Context.Proof ctxt) n of
|
wenzelm@26478
|
613 |
SOME (T,_) =>
|
schirmer@25171
|
614 |
Syntax.const "StateFun.lookup"$Syntax.free (project_name T)$Syntax.free n$s
|
wenzelm@26478
|
615 |
| NONE =>
|
schirmer@25171
|
616 |
if get_silent (Context.Proof ctxt)
|
haftmann@30249
|
617 |
then Syntax.const "StateFun.lookup" $ Syntax.const "undefined" $ Syntax.free n $ s
|
schirmer@25171
|
618 |
else raise TERM ("StateSpace.gen_lookup_tr: component " ^ n ^ " not defined",[]));
|
schirmer@25171
|
619 |
|
schirmer@25171
|
620 |
fun lookup_tr ctxt [s,Free (n,_)] = gen_lookup_tr ctxt s n;
|
schirmer@25171
|
621 |
fun lookup_swap_tr ctxt [Free (n,_),s] = gen_lookup_tr ctxt s n;
|
schirmer@25171
|
622 |
|
schirmer@25171
|
623 |
fun lookup_tr' ctxt [_$Free (prj,_),n as (_$Free (name,_)),s] =
|
schirmer@25171
|
624 |
( case get_comp (Context.Proof ctxt) name of
|
schirmer@25171
|
625 |
SOME (T,_) => if prj=project_name T then
|
wenzelm@26478
|
626 |
Syntax.const "_statespace_lookup" $ s $ n
|
schirmer@25171
|
627 |
else raise Match
|
schirmer@25171
|
628 |
| NONE => raise Match)
|
schirmer@25171
|
629 |
| lookup_tr' _ ts = raise Match;
|
schirmer@25171
|
630 |
|
wenzelm@26478
|
631 |
fun gen_update_tr id ctxt n v s =
|
schirmer@25171
|
632 |
let
|
schirmer@25171
|
633 |
fun pname T = if id then "Fun.id" else project_name T
|
schirmer@25171
|
634 |
fun iname T = if id then "Fun.id" else inject_name T
|
schirmer@25171
|
635 |
in
|
schirmer@25171
|
636 |
(case get_comp (Context.Proof ctxt) n of
|
schirmer@25171
|
637 |
SOME (T,_) => Syntax.const "StateFun.update"$
|
schirmer@25171
|
638 |
Syntax.free (pname T)$Syntax.free (iname T)$
|
schirmer@25171
|
639 |
Syntax.free n$(Syntax.const KN $ v)$s
|
wenzelm@26478
|
640 |
| NONE =>
|
wenzelm@26478
|
641 |
if get_silent (Context.Proof ctxt)
|
schirmer@25171
|
642 |
then Syntax.const "StateFun.update"$
|
haftmann@30249
|
643 |
Syntax.const "undefined" $ Syntax.const "undefined" $
|
haftmann@30249
|
644 |
Syntax.free n $ (Syntax.const KN $ v) $ s
|
schirmer@25171
|
645 |
else raise TERM ("StateSpace.gen_update_tr: component " ^ n ^ " not defined",[]))
|
schirmer@25171
|
646 |
end;
|
schirmer@25171
|
647 |
|
schirmer@25171
|
648 |
fun update_tr ctxt [s,Free (n,_),v] = gen_update_tr false ctxt n v s;
|
schirmer@25171
|
649 |
|
schirmer@25171
|
650 |
fun update_tr' ctxt [_$Free (prj,_),_$Free (inj,_),n as (_$Free (name,_)),(Const (k,_)$v),s] =
|
wenzelm@30364
|
651 |
if Long_Name.base_name k = Long_Name.base_name KN then
|
schirmer@25171
|
652 |
(case get_comp (Context.Proof ctxt) name of
|
schirmer@25171
|
653 |
SOME (T,_) => if inj=inject_name T andalso prj=project_name T then
|
wenzelm@26478
|
654 |
Syntax.const "_statespace_update" $ s $ n $ v
|
schirmer@25171
|
655 |
else raise Match
|
schirmer@25171
|
656 |
| NONE => raise Match)
|
schirmer@25171
|
657 |
else raise Match
|
schirmer@25171
|
658 |
| update_tr' _ _ = raise Match;
|
schirmer@25171
|
659 |
|
schirmer@25171
|
660 |
end;
|
schirmer@25171
|
661 |
|
schirmer@25171
|
662 |
|
schirmer@25171
|
663 |
(*** outer syntax *)
|
schirmer@25171
|
664 |
|
schirmer@25171
|
665 |
local structure P = OuterParse and K = OuterKeyword in
|
schirmer@25171
|
666 |
|
schirmer@25171
|
667 |
val type_insts =
|
schirmer@25171
|
668 |
P.typ >> single ||
|
wenzelm@26478
|
669 |
P.$$$ "(" |-- P.!!! (P.list1 P.typ --| P.$$$ ")")
|
schirmer@25171
|
670 |
|
schirmer@25171
|
671 |
val comp = P.name -- (P.$$$ "::" |-- P.!!! P.typ);
|
schirmer@25171
|
672 |
fun plus1_unless test scan =
|
wenzelm@25999
|
673 |
scan ::: Scan.repeat (P.$$$ "+" |-- Scan.unless test (P.!!! scan));
|
schirmer@25171
|
674 |
|
schirmer@25171
|
675 |
val mapsto = P.$$$ "=";
|
schirmer@25171
|
676 |
val rename = P.name -- (mapsto |-- P.name);
|
schirmer@25171
|
677 |
val renames = Scan.optional (P.$$$ "[" |-- P.!!! (P.list1 rename --| P.$$$ "]")) [];
|
schirmer@25171
|
678 |
|
schirmer@25171
|
679 |
|
schirmer@25171
|
680 |
val parent = ((type_insts -- P.xname) || (P.xname >> pair [])) -- renames
|
schirmer@25171
|
681 |
>> (fn ((insts,name),renames) => (insts,name,renames))
|
schirmer@25171
|
682 |
|
schirmer@25171
|
683 |
|
schirmer@25171
|
684 |
val statespace_decl =
|
schirmer@25171
|
685 |
P.type_args -- P.name --
|
wenzelm@26478
|
686 |
(P.$$$ "=" |--
|
schirmer@25171
|
687 |
((Scan.repeat1 comp >> pair []) ||
|
schirmer@25171
|
688 |
(plus1_unless comp parent --
|
schirmer@25171
|
689 |
Scan.optional (P.$$$ "+" |-- P.!!! (Scan.repeat1 comp)) [])))
|
schirmer@25171
|
690 |
|
schirmer@25171
|
691 |
val statespace_command =
|
schirmer@25171
|
692 |
OuterSyntax.command "statespace" "define state space" K.thy_decl
|
wenzelm@26478
|
693 |
(statespace_decl >> (fn ((args,name),(parents,comps)) =>
|
schirmer@25171
|
694 |
Toplevel.theory (define_statespace args name parents comps)))
|
schirmer@25171
|
695 |
|
schirmer@25171
|
696 |
end;
|
schirmer@25171
|
697 |
|
hoelzl@32650
|
698 |
end;
|