| author | nipkow | 
| Thu, 11 Dec 2008 08:56:02 +0100 | |
| changeset 29108 | 12ca66b887a0 | 
| parent 28965 | 1de908189869 | 
| child 29064 | 70a61d58460e | 
| 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: 
26336diff
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 | ||
| 28965 | 271 | val full_name = Sign.full_bname thy name; | 
| 25171 | 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: 
26478diff
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 | ||
| 28965 | 305 | val assumes = Element.Assumes [((Binding.name 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 | 
| 28820 | 314 | |> Locale.add_locale "" 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) | |
| 28394 | 355 | |> LocalTheory.exit_global; | 
| 25171 | 356 | |
| 357 | fun parent_components thy (Ts, pname, renaming) = | |
| 358 | let | |
| 359 | val ctxt = Context.Theory thy; | |
| 360 | fun rename [] xs = xs | |
| 361 | | rename (NONE::rs) (x::xs) = x::rename rs xs | |
| 362 | | rename (SOME r::rs) ((x,T)::xs) = (r,T)::rename rs xs; | |
| 26478 | 363 |     val {args,parents,components,...} =
 | 
| 25171 | 364 | the (Symtab.lookup (StateSpaceData.get ctxt) pname); | 
| 365 | val inst = map fst args ~~ Ts; | |
| 366 | val subst = Term.map_type_tfree (the o AList.lookup (op =) inst o fst); | |
| 26478 | 367 | val parent_comps = | 
| 25171 | 368 | List.concat (map (fn (Ts',n,rs) => parent_components thy (map subst Ts',n,rs)) parents); | 
| 369 | val all_comps = rename renaming (parent_comps @ map (apsnd subst) components); | |
| 370 | in all_comps end; | |
| 371 | ||
| 372 | fun take_upto i xs = List.take(xs,i) handle Subscript => xs; | |
| 373 | ||
| 374 | fun statespace_definition state_type args name parents parent_comps components thy = | |
| 26478 | 375 | let | 
| 28965 | 376 | val full_name = Sign.full_bname thy name; | 
| 25171 | 377 | val all_comps = parent_comps @ components; | 
| 378 | ||
| 379 | val components' = map (fn (n,T) => (n,(T,full_name))) components; | |
| 26478 | 380 | val all_comps' = map (fn (n,T) => (n,(T,full_name))) all_comps; | 
| 381 | fun parent_expr (_,n,rs) = Locale.Rename | |
| 25171 | 382 | (Locale.Locale (suffix namespaceN n), | 
| 383 | map (Option.map (fn s => (s,NONE))) rs); | |
| 384 | val parents_expr = Locale.Merge (fold (fn p => fn es => parent_expr p::es) parents []); | |
| 385 | ||
| 386 | fun distinct_types Ts = | |
| 27276 | 387 | let val tab = fold (fn T => fn tab => Typtab.update (T,()) tab) Ts Typtab.empty; | 
| 388 | in map fst (Typtab.dest tab) end; | |
| 25171 | 389 | |
| 390 | val Ts = distinct_types (map snd all_comps); | |
| 391 | val arg_names = map fst args; | |
| 392 | val valueN = Name.variant arg_names "'value"; | |
| 393 | val nameN = Name.variant (valueN::arg_names) "'name"; | |
| 394 | val valueT = TFree (valueN, Sign.defaultS thy); | |
| 395 | val nameT = TFree (nameN, Sign.defaultS thy); | |
| 396 | val stateT = nameT --> valueT; | |
| 397 | fun projectT T = valueT --> T; | |
| 26478 | 398 | fun injectT T = T --> valueT; | 
| 25171 | 399 | |
| 400 | val locs = map (fn T => Locale.Rename (Locale.Locale project_injectL, | |
| 401 | [SOME (project_name T,NONE), | |
| 402 | SOME (inject_name T ,NONE)])) Ts; | |
| 26478 | 403 | val constrains = List.concat | 
| 25171 | 404 | (map (fn T => [(project_name T,projectT T),(inject_name T,injectT T)]) Ts); | 
| 405 | ||
| 26478 | 406 | fun interprete_parent_valuetypes (Ts, pname, _) = | 
| 25171 | 407 | let | 
| 26478 | 408 |         val {args,types,...} =
 | 
| 25171 | 409 | the (Symtab.lookup (StateSpaceData.get (Context.Theory thy)) pname); | 
| 410 | val inst = map fst args ~~ Ts; | |
| 411 | val subst = Term.map_type_tfree (the o AList.lookup (op =) inst o fst); | |
| 412 | val pars = List.concat (map ((fn T => [project_name T,inject_name T]) o subst) types); | |
| 413 | val expr = Locale.Rename (Locale.Locale (suffix valuetypesN name), | |
| 414 | map (fn n => SOME (n,NONE)) pars); | |
| 26478 | 415 | in prove_interpretation_in (K all_tac) | 
| 25171 | 416 | (suffix valuetypesN name, expr) end; | 
| 417 | ||
| 418 | fun interprete_parent (_, pname, rs) = | |
| 419 | let | |
| 420 | val expr = Locale.Rename (Locale.Locale pname, map (Option.map (fn n => (n,NONE))) rs) | |
| 26478 | 421 | in prove_interpretation_in | 
| 422 | (fn ctxt => Locale.intro_locales_tac false ctxt []) | |
| 25171 | 423 | (full_name, expr) end; | 
| 424 | ||
| 425 | fun declare_declinfo updates lthy phi ctxt = | |
| 426 | let | |
| 26478 | 427 | fun upd_prf ctxt = | 
| 25171 | 428 | let | 
| 429 | fun upd (n,v) = | |
| 430 | let | |
| 431 | val nT = ProofContext.infer_type (LocalTheory.target_of lthy) n | |
| 26478 | 432 | in Context.proof_map | 
| 433 | (update_declinfo (Morphism.term phi (Free (n,nT)),v)) | |
| 25171 | 434 | end; | 
| 435 | in ctxt |> fold upd updates end; | |
| 436 | ||
| 437 | in Context.mapping I upd_prf ctxt end; | |
| 438 | ||
| 26478 | 439 | fun string_of_typ T = | 
| 440 | setmp show_sorts true | |
| 25696 
c2058af6d9bc
PrintMode.setmp (avoid direct access to print_mode ref);
 wenzelm parents: 
25408diff
changeset | 441 | (PrintMode.setmp [] (Syntax.string_of_typ (ProofContext.init thy))) T; | 
| 25171 | 442 | val fixestate = (case state_type of | 
| 443 | NONE => [] | |
| 26478 | 444 | | SOME s => | 
| 445 | let | |
| 28965 | 446 | val fx = Element.Fixes [(Binding.name s,SOME (string_of_typ stateT),NoSyn)]; | 
| 26478 | 447 | val cs = Element.Constrains | 
| 25171 | 448 | (map (fn (n,T) => (n,string_of_typ T)) | 
| 449 | ((map (fn (n,_) => (n,nameT)) all_comps) @ | |
| 450 | constrains)) | |
| 451 | in [fx,cs] end | |
| 452 | ) | |
| 453 | ||
| 26478 | 454 | |
| 455 | in thy | |
| 456 | |> namespace_definition | |
| 457 | (suffix namespaceN name) nameT parents_expr | |
| 25171 | 458 | (map fst parent_comps) (map fst components) | 
| 459 | |> Context.theory_map (add_statespace full_name args parents components []) | |
| 28820 | 460 | |> Locale.add_locale "" (suffix valuetypesN name) (Locale.Merge locs) | 
| 25171 | 461 | [Element.Constrains constrains] | 
| 462 | |> ProofContext.theory_of o #2 | |
| 26478 | 463 | |> fold interprete_parent_valuetypes parents | 
| 28820 | 464 | |> Locale.add_locale_cmd name | 
| 25171 | 465 | (Locale.Merge [Locale.Locale (suffix namespaceN full_name) | 
| 466 | ,Locale.Locale (suffix valuetypesN full_name)]) fixestate | |
| 467 | |> ProofContext.theory_of o #2 | |
| 468 | |> fold interprete_parent parents | |
| 469 | |> add_declaration (SOME full_name) (declare_declinfo components') | |
| 470 | end; | |
| 471 | ||
| 472 | ||
| 473 | (* prepare arguments *) | |
| 474 | ||
| 27283 | 475 | fun read_raw_parent ctxt raw_T = | 
| 476 | (case ProofContext.read_typ_abbrev ctxt raw_T of | |
| 25171 | 477 | Type (name, Ts) => (Ts, name) | 
| 27283 | 478 |   | T => error ("Bad parent statespace specification: " ^ Syntax.string_of_typ ctxt T));
 | 
| 25171 | 479 | |
| 480 | fun gen_define_statespace prep_typ state_space args name parents comps thy = | |
| 481 | let (* - args distinct | |
| 482 | - only args may occur in comps and parent-instantiations | |
| 26478 | 483 | - number of insts must match parent args | 
| 25171 | 484 | - no duplicate renamings | 
| 485 | - renaming should occur in namespace | |
| 486 | *) | |
| 26478 | 487 |     val _ = writeln ("Defining statespace " ^ quote name ^ " ...");
 | 
| 25171 | 488 | |
| 27283 | 489 | val ctxt = ProofContext.init thy; | 
| 490 | ||
| 25171 | 491 | fun add_parent (Ts,pname,rs) env = | 
| 492 | let | |
| 28965 | 493 | val full_pname = Sign.full_bname thy pname; | 
| 26478 | 494 |         val {args,components,...} =
 | 
| 25171 | 495 | (case get_statespace (Context.Theory thy) full_pname of | 
| 496 | SOME r => r | |
| 497 |                | NONE => error ("Undefined statespace " ^ quote pname));
 | |
| 498 | ||
| 499 | ||
| 27283 | 500 | val (Ts',env') = fold_map (prep_typ ctxt) Ts env | 
| 26478 | 501 | handle ERROR msg => cat_error msg | 
| 502 |                     ("The error(s) above occured in parent statespace specification "
 | |
| 25171 | 503 | ^ quote pname); | 
| 504 | val err_insts = if length args <> length Ts' then | |
| 505 | ["number of type instantiation(s) does not match arguments of parent statespace " | |
| 506 | ^ quote pname] | |
| 507 | else []; | |
| 26478 | 508 | |
| 25171 | 509 | val rnames = map fst rs | 
| 510 | val err_dup_renamings = (case duplicates (op =) rnames of | |
| 511 | [] => [] | |
| 512 | | dups => ["Duplicate renaming(s) for " ^ commas dups]) | |
| 513 | ||
| 514 | val cnames = map fst components; | |
| 515 | val err_rename_unknowns = (case (filter (fn n => not (n mem cnames))) rnames of | |
| 516 | [] => [] | |
| 517 | | rs => ["Unknown components " ^ commas rs]); | |
| 26478 | 518 | |
| 25171 | 519 | |
| 520 | val rs' = map (AList.lookup (op =) rs o fst) components; | |
| 521 | val errs =err_insts @ err_dup_renamings @ err_rename_unknowns | |
| 522 | in if null errs then ((Ts',full_pname,rs'),env') | |
| 523 | else error (cat_lines (errs @ ["in parent statespace " ^ quote pname])) | |
| 524 | end; | |
| 26478 | 525 | |
| 25171 | 526 | val (parents',env) = fold_map add_parent parents []; | 
| 527 | ||
| 528 | val err_dup_args = | |
| 529 | (case duplicates (op =) args of | |
| 530 | [] => [] | |
| 531 | | dups => ["Duplicate type argument(s) " ^ commas dups]); | |
| 26478 | 532 | |
| 25171 | 533 | |
| 26478 | 534 | val err_dup_components = | 
| 25171 | 535 | (case duplicates (op =) (map fst comps) of | 
| 536 | [] => [] | |
| 537 | | dups => ["Duplicate state-space components " ^ commas dups]); | |
| 538 | ||
| 539 | fun prep_comp (n,T) env = | |
| 27283 | 540 | let val (T', env') = prep_typ ctxt T env handle ERROR msg => | 
| 25171 | 541 |        cat_error msg ("The error(s) above occured in component " ^ quote n)
 | 
| 542 | in ((n,T'), env') end; | |
| 543 | ||
| 544 | val (comps',env') = fold_map prep_comp comps env; | |
| 545 | ||
| 546 | val err_extra_frees = | |
| 547 | (case subtract (op =) args (map fst env') of | |
| 548 | [] => [] | |
| 549 | | extras => ["Extra free type variable(s) " ^ commas extras]); | |
| 550 | ||
| 551 | val defaultS = Sign.defaultS thy; | |
| 552 | val args' = map (fn x => (x, AList.lookup (op =) env x |> the_default defaultS)) args; | |
| 553 | ||
| 554 | ||
| 555 | fun fst_eq ((x:string,_),(y,_)) = x = y; | |
| 26478 | 556 | fun snd_eq ((_,t:typ),(_,u)) = t = u; | 
| 25171 | 557 | |
| 558 | val raw_parent_comps = (List.concat (map (parent_components thy) parents')); | |
| 26478 | 559 | fun check_type (n,T) = | 
| 25171 | 560 | (case distinct (snd_eq) (filter (curry fst_eq (n,T)) raw_parent_comps) of | 
| 561 | [] => [] | |
| 562 | | [_] => [] | |
| 26478 | 563 | | rs => ["Different types for component " ^ n ^": " ^ commas | 
| 25171 | 564 | (map (Pretty.string_of o Display.pretty_ctyp o ctyp_of thy o snd) rs)]) | 
| 26478 | 565 | |
| 25171 | 566 | val err_dup_types = List.concat (map check_type (duplicates fst_eq raw_parent_comps)) | 
| 567 | ||
| 568 | val parent_comps = distinct (fst_eq) raw_parent_comps; | |
| 569 | val all_comps = parent_comps @ comps'; | |
| 570 | val err_comp_in_parent = (case duplicates (op =) (map fst all_comps) of | |
| 571 | [] => [] | |
| 572 | | xs => ["Components already defined in parents: " ^ commas xs]); | |
| 573 | val errs = err_dup_args @ err_dup_components @ err_extra_frees @ | |
| 574 | err_dup_types @ err_comp_in_parent; | |
| 575 | ||
| 26478 | 576 | in if null errs | 
| 25171 | 577 | then thy |> statespace_definition state_space args' name parents' parent_comps comps' | 
| 26478 | 578 | else error (cat_lines errs) | 
| 25171 | 579 | end | 
| 580 |   handle ERROR msg => cat_error msg ("Failed to define statespace " ^ quote name);
 | |
| 26478 | 581 | |
| 27283 | 582 | val define_statespace = gen_define_statespace RecordPackage.read_typ NONE; | 
| 583 | val define_statespace_i = gen_define_statespace RecordPackage.cert_typ; | |
| 26478 | 584 | |
| 25171 | 585 | |
| 586 | (*** parse/print - translations ***) | |
| 587 | ||
| 588 | ||
| 589 | local | |
| 26478 | 590 | fun map_get_comp f ctxt (Free (name,_)) = | 
| 591 | (case (get_comp ctxt name) of | |
| 592 | SOME (T,_) => f T T dummyT | |
| 25171 | 593 | | NONE => (Syntax.free "arbitrary"(*; error "context not ready"*))) | 
| 594 | | map_get_comp _ _ _ = Syntax.free "arbitrary"; | |
| 595 | ||
| 596 | val get_comp_projection = map_get_comp project_free; | |
| 597 | val get_comp_injection = map_get_comp inject_free; | |
| 598 | ||
| 599 | fun name_of (Free (n,_)) = n; | |
| 600 | in | |
| 601 | ||
| 26478 | 602 | fun gen_lookup_tr ctxt s n = | 
| 25171 | 603 | (case get_comp (Context.Proof ctxt) n of | 
| 26478 | 604 | SOME (T,_) => | 
| 25171 | 605 | Syntax.const "StateFun.lookup"$Syntax.free (project_name T)$Syntax.free n$s | 
| 26478 | 606 | | NONE => | 
| 25171 | 607 | if get_silent (Context.Proof ctxt) | 
| 608 | then Syntax.const "StateFun.lookup"$Syntax.const "arbitrary"$Syntax.free n$s | |
| 609 |            else raise TERM ("StateSpace.gen_lookup_tr: component " ^ n ^ " not defined",[]));
 | |
| 610 | ||
| 611 | fun lookup_tr ctxt [s,Free (n,_)] = gen_lookup_tr ctxt s n; | |
| 612 | fun lookup_swap_tr ctxt [Free (n,_),s] = gen_lookup_tr ctxt s n; | |
| 613 | ||
| 614 | fun lookup_tr' ctxt [_$Free (prj,_),n as (_$Free (name,_)),s] = | |
| 615 | ( case get_comp (Context.Proof ctxt) name of | |
| 616 | SOME (T,_) => if prj=project_name T then | |
| 26478 | 617 | Syntax.const "_statespace_lookup" $ s $ n | 
| 25171 | 618 | else raise Match | 
| 619 | | NONE => raise Match) | |
| 620 | | lookup_tr' _ ts = raise Match; | |
| 621 | ||
| 26478 | 622 | fun gen_update_tr id ctxt n v s = | 
| 25171 | 623 | let | 
| 624 | fun pname T = if id then "Fun.id" else project_name T | |
| 625 | fun iname T = if id then "Fun.id" else inject_name T | |
| 626 | in | |
| 627 | (case get_comp (Context.Proof ctxt) n of | |
| 628 | SOME (T,_) => Syntax.const "StateFun.update"$ | |
| 629 | Syntax.free (pname T)$Syntax.free (iname T)$ | |
| 630 | Syntax.free n$(Syntax.const KN $ v)$s | |
| 26478 | 631 | | NONE => | 
| 632 | if get_silent (Context.Proof ctxt) | |
| 25171 | 633 | then Syntax.const "StateFun.update"$ | 
| 634 | Syntax.const "arbitrary"$Syntax.const "arbitrary"$ | |
| 635 | Syntax.free n$(Syntax.const KN $ v)$s | |
| 636 |          else raise TERM ("StateSpace.gen_update_tr: component " ^ n ^ " not defined",[]))
 | |
| 637 | end; | |
| 638 | ||
| 639 | fun update_tr ctxt [s,Free (n,_),v] = gen_update_tr false ctxt n v s; | |
| 640 | ||
| 641 | fun update_tr' ctxt [_$Free (prj,_),_$Free (inj,_),n as (_$Free (name,_)),(Const (k,_)$v),s] = | |
| 642 | if NameSpace.base k = NameSpace.base KN then | |
| 643 | (case get_comp (Context.Proof ctxt) name of | |
| 644 | SOME (T,_) => if inj=inject_name T andalso prj=project_name T then | |
| 26478 | 645 | Syntax.const "_statespace_update" $ s $ n $ v | 
| 25171 | 646 | else raise Match | 
| 647 | | NONE => raise Match) | |
| 648 | else raise Match | |
| 649 | | update_tr' _ _ = raise Match; | |
| 650 | ||
| 651 | end; | |
| 652 | ||
| 653 | ||
| 654 | (*** outer syntax *) | |
| 655 | ||
| 656 | local structure P = OuterParse and K = OuterKeyword in | |
| 657 | ||
| 658 | val type_insts = | |
| 659 | P.typ >> single || | |
| 26478 | 660 |   P.$$$ "(" |-- P.!!! (P.list1 P.typ --| P.$$$ ")")
 | 
| 25171 | 661 | |
| 662 | val comp = P.name -- (P.$$$ "::" |-- P.!!! P.typ); | |
| 663 | fun plus1_unless test scan = | |
| 25999 | 664 | scan ::: Scan.repeat (P.$$$ "+" |-- Scan.unless test (P.!!! scan)); | 
| 25171 | 665 | |
| 666 | val mapsto = P.$$$ "="; | |
| 667 | val rename = P.name -- (mapsto |-- P.name); | |
| 668 | val renames = Scan.optional (P.$$$ "[" |-- P.!!! (P.list1 rename --| P.$$$ "]")) []; | |
| 669 | ||
| 670 | ||
| 671 | val parent = ((type_insts -- P.xname) || (P.xname >> pair [])) -- renames | |
| 672 | >> (fn ((insts,name),renames) => (insts,name,renames)) | |
| 673 | ||
| 674 | ||
| 675 | val statespace_decl = | |
| 676 | P.type_args -- P.name -- | |
| 26478 | 677 | (P.$$$ "=" |-- | 
| 25171 | 678 | ((Scan.repeat1 comp >> pair []) || | 
| 679 | (plus1_unless comp parent -- | |
| 680 | Scan.optional (P.$$$ "+" |-- P.!!! (Scan.repeat1 comp)) []))) | |
| 681 | ||
| 682 | val statespace_command = | |
| 683 | OuterSyntax.command "statespace" "define state space" K.thy_decl | |
| 26478 | 684 | (statespace_decl >> (fn ((args,name),(parents,comps)) => | 
| 25171 | 685 | Toplevel.theory (define_statespace args name parents comps))) | 
| 686 | ||
| 687 | end; | |
| 688 | ||
| 689 | end; |