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