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