| author | wenzelm | 
| Tue, 07 Jun 2022 19:15:08 +0200 | |
| changeset 75536 | 7cdeed5dc96d | 
| parent 74588 | 3cc363e8bfb2 | 
| child 78028 | 0ee49c509fea | 
| permissions | -rw-r--r-- | 
| 28308 | 1 | (* Title: HOL/Statespace/state_space.ML | 
| 74588 
3cc363e8bfb2
cleanup; add Apple reference
 Norbert Schirmer <nschirmer@apple.com> parents: 
74586diff
changeset | 2 | Author: Norbert Schirmer, TU Muenchen, 2007 | 
| 
3cc363e8bfb2
cleanup; add Apple reference
 Norbert Schirmer <nschirmer@apple.com> parents: 
74586diff
changeset | 3 | Author: Norbert Schirmer, Apple, 2021 | 
| 25171 | 4 | *) | 
| 5 | ||
| 25408 | 6 | signature STATE_SPACE = | 
| 45362 | 7 | sig | 
| 8 | val distinct_compsN : string | |
| 9 | val getN : string | |
| 10 | val putN : string | |
| 11 | val injectN : string | |
| 12 | val namespaceN : string | |
| 13 | val projectN : string | |
| 14 | val valuetypesN : string | |
| 25408 | 15 | |
| 45362 | 16 | val namespace_definition : | 
| 17 | bstring -> | |
| 18 | typ -> | |
| 46925 | 19 | (xstring, string) Expression.expr * (binding * string option * mixfix) list -> | 
| 45362 | 20 | string list -> string list -> theory -> theory | 
| 25408 | 21 | |
| 45362 | 22 | val define_statespace : | 
| 23 | string list -> | |
| 24 | string -> | |
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 25 | ((string * bool) * (string list * bstring * (string * string) list)) list -> | 
| 45362 | 26 | (string * string) list -> theory -> theory | 
| 27 | val define_statespace_i : | |
| 28 | string option -> | |
| 29 | string list -> | |
| 30 | string -> | |
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 31 | ((string * bool) * (typ list * bstring * (string * string) list)) list -> | 
| 45362 | 32 | (string * typ) list -> theory -> theory | 
| 25408 | 33 | |
| 45362 | 34 | val statespace_decl : | 
| 35 | ((string list * bstring) * | |
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 36 | (((string * bool) * (string list * xstring * (bstring * bstring) list)) list * | 
| 45362 | 37 | (bstring * string) list)) parser | 
| 25408 | 38 | |
| 39 | ||
| 45362 | 40 | val neq_x_y : Proof.context -> term -> term -> thm option | 
| 41 | val distinctNameSolver : Simplifier.solver | |
| 42 | val distinctTree_tac : Proof.context -> int -> tactic | |
| 43 | val distinct_simproc : Simplifier.simproc | |
| 25408 | 44 | |
| 45 | ||
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 46 | val is_statespace : Context.generic -> xstring -> bool | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 47 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 48 | val get_comp' : Context.generic -> string -> (typ * string list) option | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 49 | val get_comp : Context.generic -> string -> (typ * string) option (* legacy wrapper, eventually replace by primed version *) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 50 | val get_comps : Context.generic -> (typ * string list) Termtab.table | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 51 | |
| 45362 | 52 | val get_silent : Context.generic -> bool | 
| 53 | val set_silent : bool -> Context.generic -> Context.generic | |
| 25408 | 54 | |
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 55 | |
| 45362 | 56 | val gen_lookup_tr : Proof.context -> term -> string -> term | 
| 57 | val lookup_swap_tr : Proof.context -> term list -> term | |
| 58 | val lookup_tr : Proof.context -> term list -> term | |
| 59 | val lookup_tr' : Proof.context -> term list -> term | |
| 25408 | 60 | |
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 61 | val gen'_update_tr : | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 62 | bool -> bool -> Proof.context -> string -> term -> term -> term | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 63 | val gen_update_tr : (* legacy wrapper, eventually replace by primed version *) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 64 | bool -> Proof.context -> string -> term -> term -> term | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 65 | |
| 45362 | 66 | val update_tr : Proof.context -> term list -> term | 
| 67 | val update_tr' : Proof.context -> term list -> term | |
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 68 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 69 | val trace_name_space_data: Context.generic -> unit | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 70 | val trace_state_space_data: Context.generic -> unit | 
| 45362 | 71 | end; | 
| 25408 | 72 | |
| 29247 
95d3a82857e5
adapted statespace module to new locales;
 Norbert Schirmer <norbert.schirmer@web.de> parents: 
29064diff
changeset | 73 | structure StateSpace : STATE_SPACE = | 
| 25171 | 74 | struct | 
| 75 | ||
| 45362 | 76 | (* Names *) | 
| 25171 | 77 | |
| 78 | val distinct_compsN = "distinct_names" | |
| 79 | val namespaceN = "_namespace" | |
| 80 | val valuetypesN = "_valuetypes" | |
| 81 | val projectN = "project" | |
| 82 | val injectN = "inject" | |
| 83 | val getN = "get" | |
| 84 | val putN = "put" | |
| 85 | val project_injectL = "StateSpaceLocale.project_inject"; | |
| 86 | ||
| 87 | ||
| 88 | (* Library *) | |
| 89 | ||
| 90 | fun fold1 f xs = fold f (tl xs) (hd xs) | |
| 91 | fun fold1' f [] x = x | |
| 92 | | fold1' f xs _ = fold1 f xs | |
| 26478 | 93 | |
| 25171 | 94 | fun sorted_subset eq [] ys = true | 
| 95 | | sorted_subset eq (x::xs) [] = false | |
| 96 | | sorted_subset eq (x::xs) (y::ys) = if eq (x,y) then sorted_subset eq xs ys | |
| 97 | else sorted_subset eq (x::xs) ys; | |
| 98 | ||
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 99 | fun comps_of_distinct_thm thm = Thm.prop_of thm | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 100 | |> (fn (_$(_$t)) => DistinctTreeProver.dest_tree t) |> map (fst o dest_Free) |> sort_strings; | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 101 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 102 | fun insert_tagged_distinct_thms tagged_thm tagged_thms = | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 103 | let | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 104 | fun ins t1 [] = [t1] | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 105 | | ins (t1 as (names1, _)) ((t2 as (names2, _))::thms) = | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 106 | if Ord_List.subset string_ord (names1, names2) then t2::thms | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 107 | else if Ord_List.subset string_ord (names2, names1) then ins t1 thms | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 108 | else t2 :: ins t1 thms | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 109 | in | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 110 | ins tagged_thm tagged_thms | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 111 | end | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 112 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 113 | fun join_tagged_distinct_thms tagged_thms1 tagged_thms2 = | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 114 | tagged_thms1 |> fold insert_tagged_distinct_thms tagged_thms2 | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 115 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 116 | fun tag_distinct_thm thm = (comps_of_distinct_thm thm, thm) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 117 | val tag_distinct_thms = map tag_distinct_thm | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 118 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 119 | fun join_distinct_thms thms1 thms2 = | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 120 | if pointer_eq (thms1, thms2) then thms1 | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 121 | else join_tagged_distinct_thms (tag_distinct_thms thms1) (tag_distinct_thms thms2) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 122 | |> map snd | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 123 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 124 | fun insert_distinct_thm thm thms = join_distinct_thms thms [thm] | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 125 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 126 | val join_distinctthm_tab = Symtab.join (fn name => fn (thms1, thms2) => join_distinct_thms thms1 thms2) | 
| 25171 | 127 | |
| 128 | ||
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 129 | fun join_declinfo_entry name (T1:typ, names1:string list) (T2, names2) = | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 130 | let | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 131 |     fun typ_info T names = @{make_string} T ^ " "  ^ Pretty.string_of (Pretty.str_list "(" ")" names);
 | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 132 | in | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 133 | if T1 = T2 then (T1, distinct (op =) (names1 @ names2)) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 134 |     else error ("statespace component '" ^ name ^ "' disagrees on type:\n " ^ 
 | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 135 | typ_info T1 names1 ^ " vs. " ^ typ_info T2 names2 | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 136 | ) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 137 | end | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 138 | fun guess_name (Free (x,_)) = x | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 139 | | guess_name _ = "unknown" | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 140 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 141 | val join_declinfo = Termtab.join (fn trm => uncurry (join_declinfo_entry (guess_name trm))) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 142 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 143 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 144 | (* | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 145 | A component might appear in *different* statespaces within the same context. However, it must | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 146 | be mapped to the same type. Note that this information is currently only properly maintained | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 147 | within contexts where all components are actually "fixes" and not arbitrary terms. Moreover, on | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 148 | the theory level the info stays empty. | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 149 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 150 | This means that on the theory level we do not make use of the syntax and the tree-based distinct simprocs. | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 151 | N.B: It might still make sense (from a performance perspective) to overcome this limitation | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 152 | and even use the simprocs when a statespace is interpreted for concrete names like HOL-strings. | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 153 | Once the distinct-theorem is proven by the interpretation the simproc can use the positions in | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 154 | the tree to derive distinctness of two strings vs. HOL-string comparison. | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 155 | *) | 
| 25171 | 156 | type namespace_info = | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 157 |  {declinfo: (typ * string list) Termtab.table, (* type, names of statespaces of component  *)
 | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 158 | distinctthm: thm list Symtab.table, (* minimal list of maximal distinctness-assumptions for a component name *) | 
| 25171 | 159 | silent: bool | 
| 160 | }; | |
| 26478 | 161 | |
| 33519 | 162 | structure NameSpaceData = Generic_Data | 
| 163 | ( | |
| 26478 | 164 | type T = namespace_info; | 
| 25171 | 165 |   val empty = {declinfo = Termtab.empty, distinctthm = Symtab.empty, silent = false};
 | 
| 33519 | 166 | fun merge | 
| 167 |     ({declinfo=declinfo1, distinctthm=distinctthm1, silent=silent1},
 | |
| 168 |       {declinfo=declinfo2, distinctthm=distinctthm2, silent=silent2}) : T =
 | |
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 169 |     {declinfo = join_declinfo (declinfo1, declinfo2),
 | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 170 | distinctthm = join_distinctthm_tab (distinctthm1, distinctthm2), | 
| 41472 
f6ab14e61604
misc tuning and comments based on review of Theory_Data, Proof_Data, Generic_Data usage;
 wenzelm parents: 
41270diff
changeset | 171 | silent = silent1 andalso silent2 (* FIXME odd merge *)} | 
| 33519 | 172 | ); | 
| 25171 | 173 | |
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 174 | fun trace_name_space_data context = | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 175 |   tracing ("NameSpaceData: " ^ @{make_string} (NameSpaceData.get context))
 | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 176 | |
| 25171 | 177 | fun make_namespace_data declinfo distinctthm silent = | 
| 178 |      {declinfo=declinfo,distinctthm=distinctthm,silent=silent};
 | |
| 179 | ||
| 180 | ||
| 181 | fun update_declinfo (n,v) ctxt = | |
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 182 | let | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 183 |     val {declinfo,distinctthm,silent} = NameSpaceData.get ctxt;
 | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 184 | val v = apsnd single v | 
| 26478 | 185 | in NameSpaceData.put | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 186 | (make_namespace_data (Termtab.map_default (n,v) (join_declinfo_entry (guess_name n) v) declinfo) distinctthm silent) ctxt | 
| 25171 | 187 | end; | 
| 188 | ||
| 189 | fun set_silent silent ctxt = | |
| 190 |   let val {declinfo,distinctthm,...} = NameSpaceData.get ctxt;
 | |
| 26478 | 191 | in NameSpaceData.put | 
| 25171 | 192 | (make_namespace_data declinfo distinctthm silent) ctxt | 
| 193 | end; | |
| 194 | ||
| 195 | val get_silent = #silent o NameSpaceData.get; | |
| 26478 | 196 | |
| 46925 | 197 | fun expression_no_pos (expr, fixes) : Expression.expression = | 
| 198 | (map (fn (name, inst) => ((name, Position.none), inst)) expr, fixes); | |
| 199 | ||
| 25171 | 200 | fun prove_interpretation_in ctxt_tac (name, expr) thy = | 
| 201 | thy | |
| 67777 
2d3c1091527b
Drop rewrite rule arguments of sublocale and interpretation implementations.
 ballarin parents: 
67450diff
changeset | 202 | |> Interpretation.global_sublocale_cmd (name, Position.none) (expression_no_pos expr) [] | 
| 26478 | 203 | |> Proof.global_terminal_proof | 
| 49889 
00ea087e83d8
more method position information, notably finished_pos after end of previous text;
 wenzelm parents: 
49866diff
changeset | 204 | ((Method.Basic (fn ctxt => SIMPLE_METHOD (ctxt_tac ctxt)), Position.no_range), NONE) | 
| 42361 | 205 | |> Proof_Context.theory_of | 
| 25171 | 206 | |
| 29247 
95d3a82857e5
adapted statespace module to new locales;
 Norbert Schirmer <norbert.schirmer@web.de> parents: 
29064diff
changeset | 207 | fun add_locale name expr elems thy = | 
| 45362 | 208 | thy | 
| 72536 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72186diff
changeset | 209 | |> Expression.add_locale (Binding.name name) (Binding.name name) [] expr elems | 
| 29362 | 210 | |> snd | 
| 33671 | 211 | |> Local_Theory.exit; | 
| 29247 
95d3a82857e5
adapted statespace module to new locales;
 Norbert Schirmer <norbert.schirmer@web.de> parents: 
29064diff
changeset | 212 | |
| 
95d3a82857e5
adapted statespace module to new locales;
 Norbert Schirmer <norbert.schirmer@web.de> parents: 
29064diff
changeset | 213 | fun add_locale_cmd name expr elems thy = | 
| 45362 | 214 | thy | 
| 72536 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72186diff
changeset | 215 | |> Expression.add_locale_cmd (Binding.name name) Binding.empty [] (expression_no_pos expr) elems | 
| 29362 | 216 | |> snd | 
| 33671 | 217 | |> Local_Theory.exit; | 
| 29247 
95d3a82857e5
adapted statespace module to new locales;
 Norbert Schirmer <norbert.schirmer@web.de> parents: 
29064diff
changeset | 218 | |
| 25171 | 219 | type statespace_info = | 
| 220 |  {args: (string * sort) list, (* type arguments *)
 | |
| 221 | parents: (typ list * string * string option list) list, | |
| 222 | (* type instantiation, state-space name, component renamings *) | |
| 223 | components: (string * typ) list, | |
| 224 | types: typ list (* range types of state space *) | |
| 225 | }; | |
| 226 | ||
| 33519 | 227 | structure StateSpaceData = Generic_Data | 
| 228 | ( | |
| 25171 | 229 | type T = statespace_info Symtab.table; | 
| 230 | val empty = Symtab.empty; | |
| 33519 | 231 | fun merge data : T = Symtab.merge (K true) data; | 
| 232 | ); | |
| 25171 | 233 | |
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 234 | fun is_statespace context name = | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 235 | Symtab.defined (StateSpaceData.get context) (Locale.intern (Context.theory_of context) name) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 236 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 237 | fun trace_state_space_data context = | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 238 |   tracing ("StateSpaceData: " ^ @{make_string} (StateSpaceData.get context))
 | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 239 | |
| 25171 | 240 | fun add_statespace name args parents components types ctxt = | 
| 26478 | 241 | StateSpaceData.put | 
| 25171 | 242 |       (Symtab.update_new (name, {args=args,parents=parents,
 | 
| 243 | components=components,types=types}) (StateSpaceData.get ctxt)) | |
| 26478 | 244 | ctxt; | 
| 25171 | 245 | |
| 246 | fun get_statespace ctxt name = | |
| 247 | Symtab.lookup (StateSpaceData.get ctxt) name; | |
| 248 | ||
| 249 | ||
| 250 | fun mk_free ctxt name = | |
| 26478 | 251 | if Variable.is_fixed ctxt name orelse Variable.is_declared ctxt name | 
| 25171 | 252 | then | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 253 | let | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 254 | val n' = Variable.intern_fixed ctxt name |> perhaps Long_Name.dest_hidden; | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 255 | val free = Free (n', Proof_Context.infer_type ctxt (n', dummyT)) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 256 | in SOME (free) end | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 257 |   else (tracing ("variable not fixed or declared: " ^ name); NONE)
 | 
| 26478 | 258 | |
| 259 | ||
| 25171 | 260 | fun get_dist_thm ctxt name = Symtab.lookup (#distinctthm (NameSpaceData.get ctxt)) name; | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 261 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 262 | fun get_dist_thm2 ctxt x y = | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 263 | (let | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 264 | val dist_thms = [x, y] |> map (#1 o dest_Free) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 265 | |> map (these o get_dist_thm (Context.Proof ctxt)) |> flat; | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 266 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 267 | fun get_paths dist_thm = | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 268 | let | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 269 | val ctree = Thm.cprop_of dist_thm |> Thm.dest_comb |> #2 |> Thm.dest_comb |> #2; | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 270 | val tree = Thm.term_of ctree; | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 271 | val x_path = the (DistinctTreeProver.find_tree x tree); | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 272 | val y_path = the (DistinctTreeProver.find_tree y tree); | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 273 | in SOME (dist_thm, x_path, y_path) end | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 274 | handle Option.Option => NONE | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 275 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 276 | val result = get_first get_paths dist_thms | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 277 | in | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 278 | result | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 279 | end) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 280 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 281 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 282 | fun get_comp' ctxt name = | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 283 | mk_free (Context.proof_of ctxt) name | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 284 | |> Option.mapPartial (fn t => | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 285 | let | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 286 | val declinfo = #declinfo (NameSpaceData.get ctxt) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 287 | in | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 288 | case Termtab.lookup declinfo t of | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 289 | NONE => (* during syntax phase, types of fixes might not yet be constrained completely *) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 290 | AList.lookup (fn (x, Free (n,_)) => n = x | _ => false) (Termtab.dest declinfo) name | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 291 | | some => some | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 292 | end) | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 293 | |
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 294 | (* legacy wrapper *) | 
| 26478 | 295 | fun get_comp ctxt name = | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 296 | get_comp' ctxt name |> Option.map (apsnd (fn ns => if null ns then "" else hd ns)) | 
| 25171 | 297 | |
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 298 | fun get_comps ctxt = #declinfo (NameSpaceData.get ctxt) | 
| 25171 | 299 | |
| 300 | (*** Tactics ***) | |
| 301 | ||
| 302 | fun neq_x_y ctxt x y = | |
| 303 | (let | |
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 304 | val (dist_thm, x_path, y_path) = the (get_dist_thm2 ctxt x y); | 
| 60327 | 305 | val thm = DistinctTreeProver.distinctTreeProver ctxt dist_thm x_path y_path; | 
| 26478 | 306 | in SOME thm | 
| 45361 | 307 | end handle Option.Option => NONE) | 
| 25171 | 308 | |
| 42368 
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
 wenzelm parents: 
42364diff
changeset | 309 | 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 | 310 | (case goal of | 
| 69597 | 311 | Const (\<^const_name>\<open>Trueprop\<close>, _) $ | 
| 312 | (Const (\<^const_name>\<open>Not\<close>, _) $ | |
| 313 | (Const (\<^const_name>\<open>HOL.eq\<close>, _) $ (x as Free _) $ (y as Free _))) => | |
| 42368 
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
 wenzelm parents: 
42364diff
changeset | 314 | (case neq_x_y ctxt x y of | 
| 60754 | 315 | SOME neq => resolve_tac ctxt [neq] i | 
| 42368 
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
 wenzelm parents: 
42364diff
changeset | 316 | | 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 | 317 | | _ => no_tac)); | 
| 25171 | 318 | |
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50214diff
changeset | 319 | val distinctNameSolver = mk_solver "distinctNameSolver" distinctTree_tac; | 
| 25171 | 320 | |
| 321 | val distinct_simproc = | |
| 69597 | 322 | Simplifier.make_simproc \<^context> "StateSpace.distinct_simproc" | 
| 323 |    {lhss = [\<^term>\<open>x = y\<close>],
 | |
| 61144 | 324 | proc = fn _ => fn ctxt => fn ct => | 
| 325 | (case Thm.term_of ct of | |
| 69597 | 326 | Const (\<^const_name>\<open>HOL.eq\<close>,_) $ (x as Free _) $ (y as Free _) => | 
| 61144 | 327 | Option.map (fn neq => DistinctTreeProver.neq_to_eq_False OF [neq]) | 
| 328 | (neq_x_y ctxt x y) | |
| 62913 | 329 | | _ => NONE)}; | 
| 25171 | 330 | |
| 26478 | 331 | fun interprete_parent name dist_thm_name parent_expr thy = | 
| 25171 | 332 | let | 
| 42368 
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
 wenzelm parents: 
42364diff
changeset | 333 | fun solve_tac ctxt = CSUBGOAL (fn (goal, i) => | 
| 25171 | 334 | let | 
| 42361 | 335 | val distinct_thm = Proof_Context.get_thm ctxt dist_thm_name; | 
| 60327 | 336 | val rule = DistinctTreeProver.distinct_implProver ctxt distinct_thm goal; | 
| 60754 | 337 | in resolve_tac ctxt [rule] i end); | 
| 26478 | 338 | |
| 42368 
3b8498ac2314
proper subgoal addressing via SUBGOAL/CSUBGOAL -- assuming these tactics did not handle Subscript in any special way;
 wenzelm parents: 
42364diff
changeset | 339 | fun tac ctxt = | 
| 69017 | 340 |       Locale.intro_locales_tac {strict = true, eager = true} ctxt [] THEN ALLGOALS (solve_tac ctxt);
 | 
| 26478 | 341 | |
| 45362 | 342 | in | 
| 343 | thy |> prove_interpretation_in tac (name, parent_expr) | |
| 26478 | 344 | end; | 
| 25171 | 345 | |
| 26478 | 346 | fun namespace_definition name nameT parent_expr parent_comps new_comps thy = | 
| 25171 | 347 | let | 
| 348 | val all_comps = parent_comps @ new_comps; | |
| 29247 
95d3a82857e5
adapted statespace module to new locales;
 Norbert Schirmer <norbert.schirmer@web.de> parents: 
29064diff
changeset | 349 | val vars = (map (fn n => (Binding.name n, NONE, NoSyn)) all_comps); | 
| 25171 | 350 | val dist_thm_name = distinct_compsN; | 
| 351 | ||
| 29247 
95d3a82857e5
adapted statespace module to new locales;
 Norbert Schirmer <norbert.schirmer@web.de> parents: 
29064diff
changeset | 352 | val dist_thm_full_name = dist_thm_name; | 
| 26478 | 353 | |
| 38835 
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
 wenzelm parents: 
38715diff
changeset | 354 | 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 | 355 | (case context of | 
| 
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
 wenzelm parents: 
38715diff
changeset | 356 | Context.Theory _ => context | 
| 
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
 wenzelm parents: 
38715diff
changeset | 357 | | Context.Proof ctxt => | 
| 26478 | 358 | let | 
| 38835 
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
 wenzelm parents: 
38715diff
changeset | 359 |           val {declinfo,distinctthm=tt,silent} = NameSpaceData.get context;
 | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 360 | val all_names = comps_of_distinct_thm thm; | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 361 | fun upd name = Symtab.map_default (name, [thm]) (fn thms => insert_distinct_thm thm thms) | 
| 25171 | 362 | |
| 363 | val tt' = tt |> fold upd all_names; | |
| 38835 
088502dfd89f
eliminated broken Output.no_warnings_CRITICAL -- context visibility does the job;
 wenzelm parents: 
38715diff
changeset | 364 | val context' = | 
| 51717 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50214diff
changeset | 365 | Context_Position.set_visible false ctxt | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50214diff
changeset | 366 | addsimprocs [distinct_simproc] | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50214diff
changeset | 367 | |> Context_Position.restore_visible ctxt | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50214diff
changeset | 368 | |> Context.Proof | 
| 
9e7d1c139569
simplifier uses proper Proof.context instead of historic type simpset;
 wenzelm parents: 
50214diff
changeset | 369 |               |> 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 | 370 | in context' end)); | 
| 26478 | 371 | |
| 25171 | 372 | val attr = Attrib.internal type_attr; | 
| 373 | ||
| 45362 | 374 | val assume = | 
| 375 | ((Binding.name dist_thm_name, [attr]), | |
| 376 | [(HOLogic.Trueprop $ | |
| 69597 | 377 | (Const (\<^const_name>\<open>all_distinct\<close>, Type (\<^type_name>\<open>tree\<close>, [nameT]) --> HOLogic.boolT) $ | 
| 45362 | 378 | DistinctTreeProver.mk_tree (fn n => Free (n, nameT)) nameT | 
| 379 | (sort fast_string_ord all_comps)), [])]); | |
| 380 | in | |
| 381 | thy | |
| 382 | |> add_locale name ([], vars) [Element.Assumes [assume]] | |
| 383 | |> Proof_Context.theory_of | |
| 384 | |> interprete_parent name dist_thm_full_name parent_expr | |
| 25171 | 385 | end; | 
| 386 | ||
| 55972 | 387 | fun encode_dot x = if x = #"." then #"_" else x; | 
| 25171 | 388 | |
| 389 | fun encode_type (TFree (s, _)) = s | |
| 390 | | encode_type (TVar ((s,i),_)) = "?" ^ s ^ string_of_int i | |
| 26478 | 391 | | encode_type (Type (n,Ts)) = | 
| 25171 | 392 | let | 
| 393 | val Ts' = fold1' (fn x => fn y => x ^ "_" ^ y) (map encode_type Ts) ""; | |
| 32651 | 394 | val n' = String.map encode_dot n; | 
| 25171 | 395 | in if Ts'="" then n' else Ts' ^ "_" ^ n' end; | 
| 396 | ||
| 397 | fun project_name T = projectN ^"_"^encode_type T; | |
| 398 | fun inject_name T = injectN ^"_"^encode_type T; | |
| 399 | ||
| 400 | ||
| 401 | fun add_declaration name decl thy = | |
| 402 | thy | |
| 72536 
589645894305
bundle mixins for locale and class specifications
 haftmann parents: 
72186diff
changeset | 403 | |> Named_Target.init [] name | 
| 72186 | 404 |   |> (fn lthy => Local_Theory.declaration {syntax = true, pervasive = false} (decl lthy) lthy)
 | 
| 33671 | 405 | |> Local_Theory.exit_global; | 
| 25171 | 406 | |
| 407 | fun parent_components thy (Ts, pname, renaming) = | |
| 408 | let | |
| 409 | val ctxt = Context.Theory thy; | |
| 410 | fun rename [] xs = xs | |
| 411 | | rename (NONE::rs) (x::xs) = x::rename rs xs | |
| 412 | | rename (SOME r::rs) ((x,T)::xs) = (r,T)::rename rs xs; | |
| 45362 | 413 |     val {args, parents, components, ...} = the (Symtab.lookup (StateSpaceData.get ctxt) pname);
 | 
| 25171 | 414 | val inst = map fst args ~~ Ts; | 
| 415 | val subst = Term.map_type_tfree (the o AList.lookup (op =) inst o fst); | |
| 26478 | 416 | val parent_comps = | 
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 417 | maps (fn (Ts',n,rs) => parent_components thy (map subst Ts', n, rs)) parents; | 
| 25171 | 418 | val all_comps = rename renaming (parent_comps @ map (apsnd subst) components); | 
| 419 | in all_comps end; | |
| 420 | ||
| 421 | fun statespace_definition state_type args name parents parent_comps components thy = | |
| 26478 | 422 | let | 
| 28965 | 423 | val full_name = Sign.full_bname thy name; | 
| 25171 | 424 | val all_comps = parent_comps @ components; | 
| 425 | ||
| 426 | val components' = map (fn (n,T) => (n,(T,full_name))) components; | |
| 427 | ||
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 428 | fun parent_expr (prefix, (_, n, rs)) = | 
| 67450 
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
 ballarin parents: 
66334diff
changeset | 429 | (suffix namespaceN n, (prefix, (Expression.Positional rs,[]))); | 
| 29247 
95d3a82857e5
adapted statespace module to new locales;
 Norbert Schirmer <norbert.schirmer@web.de> parents: 
29064diff
changeset | 430 | val parents_expr = map parent_expr parents; | 
| 25171 | 431 | fun distinct_types Ts = | 
| 27276 | 432 | let val tab = fold (fn T => fn tab => Typtab.update (T,()) tab) Ts Typtab.empty; | 
| 433 | in map fst (Typtab.dest tab) end; | |
| 25171 | 434 | |
| 435 | val Ts = distinct_types (map snd all_comps); | |
| 436 | val arg_names = map fst args; | |
| 43324 
2b47822868e4
discontinued Name.variant to emphasize that this is old-style / indirect;
 wenzelm parents: 
43278diff
changeset | 437 | 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 | 438 | val nameN = singleton (Name.variant_list (valueN :: arg_names)) "'name"; | 
| 25171 | 439 | val valueT = TFree (valueN, Sign.defaultS thy); | 
| 440 | val nameT = TFree (nameN, Sign.defaultS thy); | |
| 441 | val stateT = nameT --> valueT; | |
| 442 | fun projectT T = valueT --> T; | |
| 26478 | 443 | fun injectT T = T --> valueT; | 
| 29247 
95d3a82857e5
adapted statespace module to new locales;
 Norbert Schirmer <norbert.schirmer@web.de> parents: 
29064diff
changeset | 444 | val locinsts = map (fn T => (project_injectL, | 
| 67450 
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
 ballarin parents: 
66334diff
changeset | 445 | ((encode_type T,false),(Expression.Positional | 
| 45362 | 446 | [SOME (Free (project_name T,projectT T)), | 
| 67450 
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
 ballarin parents: 
66334diff
changeset | 447 | SOME (Free ((inject_name T,injectT T)))],[])))) Ts; | 
| 32952 | 448 | val locs = maps (fn T => [(Binding.name (project_name T),NONE,NoSyn), | 
| 449 | (Binding.name (inject_name T),NONE,NoSyn)]) Ts; | |
| 450 | val constrains = maps (fn T => [(project_name T,projectT T),(inject_name T,injectT T)]) Ts; | |
| 25171 | 451 | |
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 452 | fun interprete_parent_valuetypes (prefix, (Ts, pname, _)) thy = | 
| 25171 | 453 | let | 
| 26478 | 454 |         val {args,types,...} =
 | 
| 25171 | 455 | the (Symtab.lookup (StateSpaceData.get (Context.Theory thy)) pname); | 
| 456 | val inst = map fst args ~~ Ts; | |
| 457 | val subst = Term.map_type_tfree (the o AList.lookup (op =) inst o fst); | |
| 32952 | 458 | 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 | 459 | |
| 45362 | 460 | val expr = ([(suffix valuetypesN name, | 
| 67450 
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
 ballarin parents: 
66334diff
changeset | 461 | (prefix, (Expression.Positional (map SOME pars),[])))],[]); | 
| 29291 | 462 | in | 
| 59498 
50b60f501b05
proper context for resolve_tac, eresolve_tac, dresolve_tac, forward_tac etc.;
 wenzelm parents: 
57181diff
changeset | 463 | prove_interpretation_in (fn ctxt => ALLGOALS (solve_tac ctxt (Assumption.all_prems_of ctxt))) | 
| 29291 | 464 | (suffix valuetypesN name, expr) thy | 
| 465 | end; | |
| 25171 | 466 | |
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 467 | fun interprete_parent (prefix, (_, pname, rs)) = | 
| 25171 | 468 | let | 
| 67450 
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
 ballarin parents: 
66334diff
changeset | 469 | val expr = ([(pname, (prefix, (Expression.Positional rs,[])))],[]) | 
| 26478 | 470 | in prove_interpretation_in | 
| 69017 | 471 |            (fn ctxt => Locale.intro_locales_tac {strict = true, eager = false} ctxt [])
 | 
| 25171 | 472 | (full_name, expr) end; | 
| 473 | ||
| 474 | fun declare_declinfo updates lthy phi ctxt = | |
| 475 | let | |
| 26478 | 476 | fun upd_prf ctxt = | 
| 25171 | 477 | let | 
| 478 | fun upd (n,v) = | |
| 479 | let | |
| 42361 | 480 | val nT = Proof_Context.infer_type (Local_Theory.target_of lthy) (n, dummyT) | 
| 26478 | 481 | in Context.proof_map | 
| 482 | (update_declinfo (Morphism.term phi (Free (n,nT)),v)) | |
| 25171 | 483 | end; | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 484 | val ctxt' = ctxt |> fold upd updates | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 485 | in ctxt' end; | 
| 25171 | 486 | |
| 487 | in Context.mapping I upd_prf ctxt end; | |
| 488 | ||
| 26478 | 489 | fun string_of_typ T = | 
| 39134 
917b4b6ba3d2
turned show_sorts/show_types into proper configuration options;
 wenzelm parents: 
38864diff
changeset | 490 | Print_Mode.setmp [] | 
| 
917b4b6ba3d2
turned show_sorts/show_types into proper configuration options;
 wenzelm parents: 
38864diff
changeset | 491 | (Syntax.string_of_typ (Config.put show_sorts true (Syntax.init_pretty_global thy))) T; | 
| 25171 | 492 | val fixestate = (case state_type of | 
| 493 | NONE => [] | |
| 26478 | 494 | | SOME s => | 
| 495 | let | |
| 32960 
69916a850301
eliminated hard tabulators, guessing at each author's individual tab-width;
 wenzelm parents: 
32952diff
changeset | 496 | val fx = Element.Fixes [(Binding.name s,SOME (string_of_typ stateT),NoSyn)]; | 
| 26478 | 497 | val cs = Element.Constrains | 
| 25171 | 498 | (map (fn (n,T) => (n,string_of_typ T)) | 
| 499 | ((map (fn (n,_) => (n,nameT)) all_comps) @ | |
| 500 | constrains)) | |
| 501 | in [fx,cs] end | |
| 502 | ) | |
| 503 | ||
| 26478 | 504 | |
| 505 | in thy | |
| 506 | |> namespace_definition | |
| 29247 
95d3a82857e5
adapted statespace module to new locales;
 Norbert Schirmer <norbert.schirmer@web.de> parents: 
29064diff
changeset | 507 | (suffix namespaceN name) nameT (parents_expr,[]) | 
| 25171 | 508 | (map fst parent_comps) (map fst components) | 
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 509 | |> Context.theory_map (add_statespace full_name args (map snd parents) components []) | 
| 29247 
95d3a82857e5
adapted statespace module to new locales;
 Norbert Schirmer <norbert.schirmer@web.de> parents: 
29064diff
changeset | 510 | |> add_locale (suffix valuetypesN name) (locinsts,locs) [] | 
| 45362 | 511 | |> Proof_Context.theory_of | 
| 26478 | 512 | |> fold interprete_parent_valuetypes parents | 
| 29247 
95d3a82857e5
adapted statespace module to new locales;
 Norbert Schirmer <norbert.schirmer@web.de> parents: 
29064diff
changeset | 513 | |> add_locale_cmd name | 
| 67450 
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
 ballarin parents: 
66334diff
changeset | 514 |               ([(suffix namespaceN full_name ,(("",false),(Expression.Named [],[]))),
 | 
| 
b0ae74b86ef3
Experimental support for rewrite morphisms in locale instances.
 ballarin parents: 
66334diff
changeset | 515 |                 (suffix valuetypesN full_name,(("",false),(Expression.Named [],[])))],[]) fixestate
 | 
| 45362 | 516 | |> Proof_Context.theory_of | 
| 25171 | 517 | |> fold interprete_parent parents | 
| 38389 
d7d915bae307
Named_Target.init: empty string represents theory target
 haftmann parents: 
38350diff
changeset | 518 | |> add_declaration full_name (declare_declinfo components') | 
| 25171 | 519 | end; | 
| 520 | ||
| 521 | ||
| 522 | (* prepare arguments *) | |
| 523 | ||
| 36149 | 524 | fun read_typ ctxt raw_T env = | 
| 525 | let | |
| 526 | val ctxt' = fold (Variable.declare_typ o TFree) env ctxt; | |
| 527 | val T = Syntax.read_typ ctxt' raw_T; | |
| 45741 | 528 | val env' = Term.add_tfreesT T env; | 
| 36149 | 529 | in (T, env') end; | 
| 530 | ||
| 531 | fun cert_typ ctxt raw_T env = | |
| 532 | let | |
| 42361 | 533 | val thy = Proof_Context.theory_of ctxt; | 
| 36149 | 534 | val T = Type.no_tvars (Sign.certify_typ thy raw_T) | 
| 535 | handle TYPE (msg, _, _) => error msg; | |
| 45741 | 536 | val env' = Term.add_tfreesT T env; | 
| 36149 | 537 | in (T, env') end; | 
| 538 | ||
| 25171 | 539 | fun gen_define_statespace prep_typ state_space args name parents comps thy = | 
| 540 | let (* - args distinct | |
| 541 | - only args may occur in comps and parent-instantiations | |
| 26478 | 542 | - number of insts must match parent args | 
| 25171 | 543 | - no duplicate renamings | 
| 544 | - renaming should occur in namespace | |
| 545 | *) | |
| 26478 | 546 |     val _ = writeln ("Defining statespace " ^ quote name ^ " ...");
 | 
| 25171 | 547 | |
| 42361 | 548 | val ctxt = Proof_Context.init_global thy; | 
| 27283 | 549 | |
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 550 | fun add_parent (prefix, (Ts, pname, rs)) env = | 
| 25171 | 551 | let | 
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 552 | val prefix' = | 
| 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 553 | (case prefix of | 
| 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 554 |             ("", mandatory) => (pname, mandatory)
 | 
| 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 555 | | _ => prefix); | 
| 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 556 | |
| 28965 | 557 | val full_pname = Sign.full_bname thy pname; | 
| 26478 | 558 |         val {args,components,...} =
 | 
| 25171 | 559 | (case get_statespace (Context.Theory thy) full_pname of | 
| 560 | SOME r => r | |
| 561 |                | NONE => error ("Undefined statespace " ^ quote pname));
 | |
| 562 | ||
| 563 | ||
| 27283 | 564 | val (Ts',env') = fold_map (prep_typ ctxt) Ts env | 
| 26478 | 565 | handle ERROR msg => cat_error msg | 
| 36149 | 566 |                     ("The error(s) above occurred in parent statespace specification "
 | 
| 25171 | 567 | ^ quote pname); | 
| 568 | val err_insts = if length args <> length Ts' then | |
| 569 | ["number of type instantiation(s) does not match arguments of parent statespace " | |
| 570 | ^ quote pname] | |
| 571 | else []; | |
| 26478 | 572 | |
| 25171 | 573 | val rnames = map fst rs | 
| 574 | val err_dup_renamings = (case duplicates (op =) rnames of | |
| 575 | [] => [] | |
| 576 | | dups => ["Duplicate renaming(s) for " ^ commas dups]) | |
| 577 | ||
| 578 | 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 | 579 | val err_rename_unknowns = (case subtract (op =) cnames rnames of | 
| 25171 | 580 | [] => [] | 
| 581 | | rs => ["Unknown components " ^ commas rs]); | |
| 26478 | 582 | |
| 25171 | 583 | |
| 584 | val rs' = map (AList.lookup (op =) rs o fst) components; | |
| 585 | val errs =err_insts @ err_dup_renamings @ err_rename_unknowns | |
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 586 | in | 
| 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 587 | if null errs then ((prefix', (Ts', full_pname, rs')), env') | 
| 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 588 | else error (cat_lines (errs @ ["in parent statespace " ^ quote pname])) | 
| 25171 | 589 | end; | 
| 26478 | 590 | |
| 25171 | 591 | val (parents',env) = fold_map add_parent parents []; | 
| 592 | ||
| 593 | val err_dup_args = | |
| 594 | (case duplicates (op =) args of | |
| 595 | [] => [] | |
| 596 | | dups => ["Duplicate type argument(s) " ^ commas dups]); | |
| 26478 | 597 | |
| 25171 | 598 | |
| 26478 | 599 | val err_dup_components = | 
| 25171 | 600 | (case duplicates (op =) (map fst comps) of | 
| 601 | [] => [] | |
| 602 | | dups => ["Duplicate state-space components " ^ commas dups]); | |
| 603 | ||
| 604 | fun prep_comp (n,T) env = | |
| 27283 | 605 | let val (T', env') = prep_typ ctxt T env handle ERROR msg => | 
| 36149 | 606 |        cat_error msg ("The error(s) above occurred in component " ^ quote n)
 | 
| 25171 | 607 | in ((n,T'), env') end; | 
| 608 | ||
| 609 | val (comps',env') = fold_map prep_comp comps env; | |
| 610 | ||
| 611 | val err_extra_frees = | |
| 612 | (case subtract (op =) args (map fst env') of | |
| 613 | [] => [] | |
| 614 | | extras => ["Extra free type variable(s) " ^ commas extras]); | |
| 615 | ||
| 616 | val defaultS = Sign.defaultS thy; | |
| 617 | val args' = map (fn x => (x, AList.lookup (op =) env x |> the_default defaultS)) args; | |
| 618 | ||
| 619 | ||
| 620 | fun fst_eq ((x:string,_),(y,_)) = x = y; | |
| 26478 | 621 | fun snd_eq ((_,t:typ),(_,u)) = t = u; | 
| 25171 | 622 | |
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 623 | val raw_parent_comps = maps (parent_components thy o snd) parents'; | 
| 26478 | 624 | fun check_type (n,T) = | 
| 25171 | 625 | (case distinct (snd_eq) (filter (curry fst_eq (n,T)) raw_parent_comps) of | 
| 626 | [] => [] | |
| 627 | | [_] => [] | |
| 45660 | 628 | | rs => ["Different types for component " ^ quote n ^ ": " ^ | 
| 32432 
64f30bdd3ba1
modernized messages -- eliminated ctyp/cterm operations;
 wenzelm parents: 
32194diff
changeset | 629 | commas (map (Syntax.string_of_typ ctxt o snd) rs)]) | 
| 26478 | 630 | |
| 32952 | 631 | val err_dup_types = maps check_type (duplicates fst_eq raw_parent_comps) | 
| 25171 | 632 | |
| 633 | val parent_comps = distinct (fst_eq) raw_parent_comps; | |
| 634 | val all_comps = parent_comps @ comps'; | |
| 635 | val err_comp_in_parent = (case duplicates (op =) (map fst all_comps) of | |
| 636 | [] => [] | |
| 45660 | 637 | | xs => ["Components already defined in parents: " ^ commas_quote xs]); | 
| 25171 | 638 | val errs = err_dup_args @ err_dup_components @ err_extra_frees @ | 
| 639 | err_dup_types @ err_comp_in_parent; | |
| 26478 | 640 | in if null errs | 
| 25171 | 641 | then thy |> statespace_definition state_space args' name parents' parent_comps comps' | 
| 26478 | 642 | else error (cat_lines errs) | 
| 25171 | 643 | end | 
| 644 |   handle ERROR msg => cat_error msg ("Failed to define statespace " ^ quote name);
 | |
| 26478 | 645 | |
| 36149 | 646 | val define_statespace = gen_define_statespace read_typ NONE; | 
| 647 | val define_statespace_i = gen_define_statespace cert_typ; | |
| 26478 | 648 | |
| 25171 | 649 | |
| 45362 | 650 | |
| 25171 | 651 | (*** parse/print - translations ***) | 
| 652 | ||
| 653 | ||
| 26478 | 654 | fun gen_lookup_tr ctxt s n = | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 655 | (case get_comp' (Context.Proof ctxt) n of | 
| 45362 | 656 | SOME (T, _) => | 
| 69597 | 657 | Syntax.const \<^const_name>\<open>StateFun.lookup\<close> $ | 
| 45362 | 658 | Syntax.free (project_name T) $ Syntax.free n $ s | 
| 659 | | NONE => | |
| 660 | if get_silent (Context.Proof ctxt) | |
| 69597 | 661 | then Syntax.const \<^const_name>\<open>StateFun.lookup\<close> $ | 
| 662 | Syntax.const \<^const_syntax>\<open>undefined\<close> $ Syntax.free n $ s | |
| 74588 
3cc363e8bfb2
cleanup; add Apple reference
 Norbert Schirmer <nschirmer@apple.com> parents: 
74586diff
changeset | 663 |       else raise TERM ("StateSpace.gen_lookup_tr: component " ^ quote n ^ " not defined", []));
 | 
| 25171 | 664 | |
| 42052 
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
 wenzelm parents: 
41585diff
changeset | 665 | fun lookup_tr ctxt [s, x] = | 
| 42264 | 666 | (case Term_Position.strip_positions x of | 
| 42052 
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
 wenzelm parents: 
41585diff
changeset | 667 | Free (n,_) => gen_lookup_tr ctxt s n | 
| 
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
 wenzelm parents: 
41585diff
changeset | 668 | | _ => raise Match); | 
| 
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
 wenzelm parents: 
41585diff
changeset | 669 | |
| 25171 | 670 | fun lookup_swap_tr ctxt [Free (n,_),s] = gen_lookup_tr ctxt s n; | 
| 671 | ||
| 45362 | 672 | fun lookup_tr' ctxt [_ $ Free (prj, _), n as (_ $ Free (name, _)), s] = | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 673 | (case get_comp' (Context.Proof ctxt) name of | 
| 45362 | 674 | SOME (T, _) => | 
| 675 | if prj = project_name T | |
| 676 | then Syntax.const "_statespace_lookup" $ s $ n | |
| 677 | else raise Match | |
| 25171 | 678 | | NONE => raise Match) | 
| 55972 | 679 | | lookup_tr' _ _ = raise Match; | 
| 25171 | 680 | |
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 681 | fun gen'_update_tr const_val id ctxt n v s = | 
| 25171 | 682 | let | 
| 69597 | 683 | fun pname T = if id then \<^const_name>\<open>Fun.id\<close> else project_name T; | 
| 684 | fun iname T = if id then \<^const_name>\<open>Fun.id\<close> else inject_name T; | |
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 685 | val v = if const_val then (Syntax.const \<^const_name>\<open>K_statefun\<close> $ v) else v | 
| 45362 | 686 | in | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 687 | (case get_comp' (Context.Proof ctxt) n of | 
| 45362 | 688 | SOME (T, _) => | 
| 69597 | 689 | Syntax.const \<^const_name>\<open>StateFun.update\<close> $ | 
| 45362 | 690 | Syntax.free (pname T) $ Syntax.free (iname T) $ | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 691 | Syntax.free n $ v $ s | 
| 45362 | 692 | | NONE => | 
| 693 | if get_silent (Context.Proof ctxt) then | |
| 69597 | 694 | Syntax.const \<^const_name>\<open>StateFun.update\<close> $ | 
| 695 | Syntax.const \<^const_syntax>\<open>undefined\<close> $ Syntax.const \<^const_syntax>\<open>undefined\<close> $ | |
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 696 | Syntax.free n $ v $ s | 
| 45362 | 697 |        else raise TERM ("StateSpace.gen_update_tr: component " ^ n ^ " not defined", []))
 | 
| 25171 | 698 | end; | 
| 699 | ||
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 700 | val gen_update_tr = gen'_update_tr true | 
| 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 701 | |
| 42052 
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
 wenzelm parents: 
41585diff
changeset | 702 | fun update_tr ctxt [s, x, v] = | 
| 42264 | 703 | (case Term_Position.strip_positions x of | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 704 | Free (n, _) => gen'_update_tr true false ctxt n v s | 
| 42052 
34f1d2d81284
statespace syntax: strip positions -- type constraints are unexpected here;
 wenzelm parents: 
41585diff
changeset | 705 | | _ => raise Match); | 
| 25171 | 706 | |
| 45362 | 707 | fun update_tr' ctxt | 
| 708 | [_ $ Free (prj, _), _ $ Free (inj, _), n as (_ $ Free (name, _)), (Const (k, _) $ v), s] = | |
| 69597 | 709 | if Long_Name.base_name k = Long_Name.base_name \<^const_name>\<open>K_statefun\<close> then | 
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 710 | (case get_comp' (Context.Proof ctxt) name of | 
| 45362 | 711 | SOME (T, _) => | 
| 712 | if inj = inject_name T andalso prj = project_name T then | |
| 713 | Syntax.const "_statespace_update" $ s $ n $ v | |
| 714 | else raise Match | |
| 715 | | NONE => raise Match) | |
| 25171 | 716 | else raise Match | 
| 717 | | update_tr' _ _ = raise Match; | |
| 718 | ||
| 74586 
5ac762b53119
generalized component lookup for syntax and distinctness proofs. added some tracing.
 Norbert Schirmer <nschirmer@apple.com> parents: 
74561diff
changeset | 719 | |
| 25171 | 720 | |
| 721 | ||
| 722 | (*** outer syntax *) | |
| 723 | ||
| 45362 | 724 | local | 
| 725 | ||
| 25171 | 726 | val type_insts = | 
| 36960 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 wenzelm parents: 
36958diff
changeset | 727 | Parse.typ >> single || | 
| 69597 | 728 | \<^keyword>\<open>(\<close> |-- Parse.!!! (Parse.list1 Parse.typ --| \<^keyword>\<open>)\<close>) | 
| 25171 | 729 | |
| 69597 | 730 | val comp = Parse.name -- (\<^keyword>\<open>::\<close> |-- Parse.!!! Parse.typ); | 
| 25171 | 731 | fun plus1_unless test scan = | 
| 69597 | 732 | scan ::: Scan.repeat (\<^keyword>\<open>+\<close> |-- Scan.unless test (Parse.!!! scan)); | 
| 25171 | 733 | |
| 69597 | 734 | val mapsto = \<^keyword>\<open>=\<close>; | 
| 36960 
01594f816e3a
prefer structure Keyword, Parse, Parse_Spec, Outer_Syntax;
 wenzelm parents: 
36958diff
changeset | 735 | val rename = Parse.name -- (mapsto |-- Parse.name); | 
| 69597 | 736 | val renames = Scan.optional (\<^keyword>\<open>[\<close> |-- Parse.!!! (Parse.list1 rename --| \<^keyword>\<open>]\<close>)) []; | 
| 25171 | 737 | |
| 45362 | 738 | val parent = | 
| 61606 
6d5213bd9709
uniform mandatory qualifier for all locale expressions, including 'statespace' parent;
 wenzelm parents: 
61144diff
changeset | 739 | Parse_Spec.locale_prefix -- | 
| 62969 | 740 | ((type_insts -- Parse.name) || (Parse.name >> pair [])) -- renames | 
| 49754 
acafcac41690
more explicit namespace prefix for 'statespace' -- duplicate facts;
 wenzelm parents: 
48741diff
changeset | 741 | >> (fn ((prefix, (insts, name)), renames) => (prefix, (insts, name, renames))); | 
| 25171 | 742 | |
| 45362 | 743 | in | 
| 25171 | 744 | |
| 745 | val statespace_decl = | |
| 45362 | 746 | Parse.type_args -- Parse.name -- | 
| 69597 | 747 | (\<^keyword>\<open>=\<close> |-- | 
| 45362 | 748 | ((Scan.repeat1 comp >> pair []) || | 
| 749 | (plus1_unless comp parent -- | |
| 69597 | 750 | Scan.optional (\<^keyword>\<open>+\<close> |-- Parse.!!! (Scan.repeat1 comp)) []))); | 
| 45362 | 751 | val _ = | 
| 69597 | 752 | Outer_Syntax.command \<^command_keyword>\<open>statespace\<close> "define state-space as locale context" | 
| 45362 | 753 | (statespace_decl >> (fn ((args, name), (parents, comps)) => | 
| 754 | Toplevel.theory (define_statespace args name parents comps))); | |
| 25171 | 755 | |
| 45362 | 756 | end; | 
| 25171 | 757 | |
| 45362 | 758 | end; |