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