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