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