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