src/HOL/Statespace/state_space.ML
author wenzelm
Sat, 29 Mar 2008 22:55:49 +0100
changeset 26496 49ae9456eba9
parent 26478 9d1029ce0e13
child 27276 ea82bd1e3c20
permissions -rw-r--r--
purely functional setup of claset/simpset/clasimpset;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
     1
(*  Title:      state_space.ML
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
     2
    ID:         $Id$
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
     3
    Author:     Norbert Schirmer, TU Muenchen
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
     4
*)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
     5
25408
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
     6
signature STATE_SPACE =
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
     7
  sig
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
     8
    val KN : string
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
     9
    val distinct_compsN : string
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    10
    val getN : string
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    11
    val putN : string
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    12
    val injectN : string
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    13
    val namespaceN : string
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    14
    val projectN : string
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    15
    val valuetypesN : string
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    16
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    17
    val namespace_definition :
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    18
       bstring ->
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    19
       Term.typ ->
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    20
       Locale.expr ->
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    21
       string list -> string list -> Context.theory -> Context.theory
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    22
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    23
    val define_statespace :
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    24
       string list ->
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    25
       string ->
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    26
       (string list * bstring * (string * string) list) list ->
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    27
       (string * string) list -> Context.theory -> Context.theory
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    28
    val define_statespace_i :
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    29
       string option ->
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    30
       string list ->
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    31
       string ->
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    32
       (Term.typ list * bstring * (string * string) list) list ->
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    33
       (string * Term.typ) list -> Context.theory -> Context.theory
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    34
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    35
    val statespace_decl :
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
    36
       OuterParse.token list ->
25408
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    37
       ((string list * bstring) *
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    38
         ((string list * xstring * (bstring * bstring) list) list *
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    39
          (bstring * string) list)) * OuterParse.token list
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    40
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    41
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    42
    val neq_x_y : Context.proof -> Term.term -> Term.term -> Thm.thm option
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    43
    val distinctNameSolver : MetaSimplifier.solver
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    44
    val distinctTree_tac :
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    45
       Context.proof -> Term.term * int -> Tactical.tactic
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    46
    val distinct_simproc : MetaSimplifier.simproc
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    47
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    48
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    49
    val get_comp : Context.generic -> string -> (Term.typ * string) Option.option
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    50
    val get_silent : Context.generic -> bool
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    51
    val set_silent : bool -> Context.generic -> Context.generic
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    52
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    53
    val read_typ :
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    54
       Context.theory ->
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    55
       string ->
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    56
       (string * Term.sort) list -> Term.typ * (string * Term.sort) list
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    57
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    58
    val gen_lookup_tr : Context.proof -> Term.term -> string -> Term.term
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    59
    val lookup_swap_tr : Context.proof -> Term.term list -> Term.term
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    60
    val lookup_tr : Context.proof -> Term.term list -> Term.term
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    61
    val lookup_tr' : Context.proof -> Term.term list -> Term.term
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    62
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    63
    val gen_update_tr :
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    64
       bool -> Context.proof -> string -> Term.term -> Term.term -> Term.term
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    65
    val update_tr : Context.proof -> Term.term list -> Term.term
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    66
    val update_tr' : Context.proof -> Term.term list -> Term.term
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    67
  end;
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    68
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    69
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    70
structure StateSpace: STATE_SPACE =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    71
struct
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    72
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    73
(* Theorems *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    74
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    75
(* Names *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    76
val distinct_compsN = "distinct_names"
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    77
val namespaceN = "_namespace"
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    78
val valuetypesN = "_valuetypes"
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    79
val projectN = "project"
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    80
val injectN = "inject"
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    81
val getN = "get"
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    82
val putN = "put"
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    83
val project_injectL = "StateSpaceLocale.project_inject";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    84
val KN = "StateFun.K_statefun"
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    85
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    86
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    87
(* Library *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    88
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    89
fun fold1 f xs = fold f (tl xs) (hd xs)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    90
fun fold1' f [] x = x
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    91
  | fold1' f xs _ = fold1 f xs
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
    92
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
    93
fun sublist_idx eq xs ys =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    94
  let
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
    95
    fun sublist n xs ys =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    96
         if is_prefix eq xs ys then SOME n
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    97
         else (case ys of [] => NONE
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    98
               | (y::ys') => sublist (n+1) xs ys')
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    99
  in sublist 0 xs ys end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   100
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   101
fun is_sublist eq xs ys = is_some (sublist_idx eq xs ys);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   102
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   103
fun sorted_subset eq [] ys = true
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   104
  | sorted_subset eq (x::xs) [] = false
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   105
  | 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
   106
                                       else sorted_subset eq (x::xs) ys;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   107
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   108
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   109
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   110
type namespace_info =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   111
 {declinfo: (typ*string) Termtab.table, (* type, name of statespace *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   112
  distinctthm: thm Symtab.table,
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   113
  silent: bool
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   114
 };
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   115
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   116
structure NameSpaceArgs =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   117
struct
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   118
  type T = namespace_info;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   119
  val empty = {declinfo = Termtab.empty, distinctthm = Symtab.empty, silent = false};
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   120
  val extend = I;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   121
  fun merge pp ({declinfo=declinfo1, distinctthm=distinctthm1, silent=silent1},
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   122
                {declinfo=declinfo2, distinctthm=distinctthm2, silent=silent2}) =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   123
                {declinfo = Termtab.merge (K true) (declinfo1, declinfo2),
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   124
                 distinctthm = Symtab.merge (K true) (distinctthm1, distinctthm2),
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   125
                 silent = silent1 andalso silent2}
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   126
end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   127
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   128
structure NameSpaceData = GenericDataFun(NameSpaceArgs);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   129
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   130
fun make_namespace_data declinfo distinctthm silent =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   131
     {declinfo=declinfo,distinctthm=distinctthm,silent=silent};
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   132
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   133
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   134
fun delete_declinfo n ctxt =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   135
  let val {declinfo,distinctthm,silent} = 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 (Termtab.delete_safe n 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
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   141
fun update_declinfo (n,v) ctxt =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   142
  let val {declinfo,distinctthm,silent} = NameSpaceData.get ctxt;
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   143
  in NameSpaceData.put
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   144
      (make_namespace_data (Termtab.update (n,v) declinfo) distinctthm silent) ctxt
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   145
  end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   146
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   147
fun set_silent silent ctxt =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   148
  let val {declinfo,distinctthm,...} = NameSpaceData.get ctxt;
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   149
  in NameSpaceData.put
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   150
      (make_namespace_data declinfo distinctthm silent) ctxt
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   151
  end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   152
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   153
val get_silent = #silent o NameSpaceData.get;
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   154
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   155
fun prove_interpretation_in ctxt_tac (name, expr) thy =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   156
   thy
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   157
   |> Locale.interpretation_in_locale I (name, expr)
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   158
   |> Proof.global_terminal_proof
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   159
         (Method.Basic (fn ctxt => Method.SIMPLE_METHOD (ctxt_tac ctxt),Position.none), NONE)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   160
   |> ProofContext.theory_of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   161
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   162
type statespace_info =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   163
 {args: (string * sort) list, (* type arguments *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   164
  parents: (typ list * string * string option list) list,
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   165
             (* type instantiation, state-space name, component renamings *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   166
  components: (string * typ) list,
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   167
  types: typ list (* range types of state space *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   168
 };
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   169
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   170
structure StateSpaceArgs =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   171
struct
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   172
  val name = "HOL/StateSpace";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   173
  type T = statespace_info Symtab.table;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   174
  val empty = Symtab.empty;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   175
  val extend = I;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   176
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   177
  fun merge pp (nt1,nt2) = Symtab.merge (K true) (nt1, nt2);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   178
end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   179
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   180
structure StateSpaceData = GenericDataFun(StateSpaceArgs);
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
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   201
    in SOME (Free (n',ProofContext.infer_type ctxt n')) end
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"
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   234
     (fn ss => case #context (#1 (rep_ss ss)) of
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 =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   239
  Simplifier.simproc HOL.thy "StateSpace.distinct_simproc" ["x = y"]
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   240
    (fn thy => fn ss => (fn (Const ("op =",_)$(x as Free _)$(y as Free _)) =>
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   241
        (case #context (#1 (rep_ss 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
fun read_typ thy s =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   248
  Sign.read_typ thy s;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   249
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   250
local
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   251
  val ss = HOL_basic_ss
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   252
in
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   253
fun interprete_parent name dist_thm_name parent_expr thy =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   254
  let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   255
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   256
    fun solve_tac ctxt (_,i) st =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   257
      let
26343
0dd2eab7b296 simplified get_thm(s): back to plain name argument;
wenzelm
parents: 26336
diff changeset
   258
        val distinct_thm = ProofContext.get_thm ctxt dist_thm_name;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   259
        val goal = List.nth (cprems_of st,i-1);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   260
        val rule = DistinctTreeProver.distinct_implProver distinct_thm goal;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   261
      in EVERY [rtac rule i] st
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   262
      end
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   263
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   264
    fun tac ctxt = EVERY [Locale.intro_locales_tac true ctxt [],
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   265
                          ALLGOALS (SUBGOAL (solve_tac ctxt))]
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   266
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   267
  in thy
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   268
     |> prove_interpretation_in tac (name,parent_expr)
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   269
  end;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   270
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   271
end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   272
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   273
fun namespace_definition name nameT parent_expr parent_comps new_comps thy =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   274
  let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   275
    val all_comps = parent_comps @ new_comps;
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   276
    val vars = Locale.Merge
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   277
                (map (fn n => Locale.Rename (Locale.Locale (Locale.intern thy "var")
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   278
                                            ,[SOME (n,NONE)])) all_comps);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   279
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   280
    val full_name = Sign.full_name thy name;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   281
    val dist_thm_name = distinct_compsN;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   282
    val dist_thm_full_name =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   283
        let val prefix = fold1' (fn name => fn prfx => prfx ^ "_" ^ name) all_comps "";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   284
        in if prefix = "" then dist_thm_name else prefix ^ "." ^ dist_thm_name end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   285
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   286
    fun comps_of_thm thm = prop_of thm
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   287
             |> (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
   288
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   289
    fun type_attr phi (ctxt,thm) =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   290
      (case ctxt of Context.Theory _ => (ctxt,thm)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   291
       | _ =>
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   292
        let
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   293
          val {declinfo,distinctthm=tt,silent} = (NameSpaceData.get ctxt);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   294
	  val all_names = comps_of_thm thm;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   295
          fun upd name tt =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   296
               (case (Symtab.lookup tt name) of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   297
                 SOME dthm => if sorted_subset (op =) (comps_of_thm dthm) all_names
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   298
                              then Symtab.update (name,thm) tt else tt
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   299
                | NONE => Symtab.update (name,thm) tt)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   300
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   301
          val tt' = tt |> fold upd all_names;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   302
          val activate_simproc =
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   303
              Output.no_warnings
26496
49ae9456eba9 purely functional setup of claset/simpset/clasimpset;
wenzelm
parents: 26478
diff changeset
   304
               (Simplifier.map_ss (fn ss => ss addsimprocs [distinct_simproc]));
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   305
          val ctxt' =
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   306
              ctxt
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   307
              |> NameSpaceData.put {declinfo=declinfo,distinctthm=tt',silent=silent}
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   308
              |> activate_simproc
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   309
        in (ctxt',thm)
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   310
        end)
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   311
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   312
    val attr = Attrib.internal type_attr;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   313
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   314
    val assumes = Element.Assumes [((dist_thm_name,[attr]),
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   315
                    [(HOLogic.Trueprop $
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   316
                      (Const ("DistinctTreeProver.all_distinct",
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   317
                                 Type ("DistinctTreeProver.tree",[nameT]) --> HOLogic.boolT) $
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   318
                      DistinctTreeProver.mk_tree (fn n => Free (n,nameT)) nameT
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   319
                                (sort fast_string_ord all_comps)),
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   320
                      ([]))])];
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   321
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   322
  in thy
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   323
     |> Locale.add_locale_i (SOME "") name vars [assumes]
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   324
     ||> ProofContext.theory_of
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   325
     ||> interprete_parent name dist_thm_full_name parent_expr
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   326
     |> #2
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   327
  end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   328
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   329
structure Typetab = TableFun(type key=typ val ord = Term.typ_ord);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   330
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   331
fun encode_dot x = if x= #"." then #"_" else x;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   332
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   333
fun encode_type (TFree (s, _)) = s
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   334
  | encode_type (TVar ((s,i),_)) = "?" ^ s ^ string_of_int i
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   335
  | encode_type (Type (n,Ts)) =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   336
      let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   337
        val Ts' = fold1' (fn x => fn y => x ^ "_" ^ y) (map encode_type Ts) "";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   338
        val n' = String.map encode_dot n;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   339
      in if Ts'="" then n' else Ts' ^ "_" ^ n' end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   340
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   341
fun project_name T = projectN ^"_"^encode_type T;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   342
fun inject_name T = injectN ^"_"^encode_type T;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   343
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   344
fun project_free T pT V = Free (project_name T, V --> pT);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   345
fun inject_free T pT V = Free (inject_name T, pT --> V);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   346
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   347
fun get_name n = getN ^ "_" ^ n;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   348
fun put_name n = putN ^ "_" ^ n;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   349
fun get_const n T nT V = Free (get_name n, (nT --> V) --> T);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   350
fun put_const n T nT V = Free (put_name n, T --> (nT --> V) --> (nT --> V));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   351
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   352
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
   353
fun update_const T nT V =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   354
 Const ("StateFun.update",
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   355
          (V --> T) --> (T --> V) --> nT --> (T --> T) --> (nT --> V) --> (nT --> V));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   356
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   357
fun K_const T = Const ("StateFun.K_statefun",T --> T --> T);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   358
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   359
val no_syn = #3 (Syntax.no_syn ((),()));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   360
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   361
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   362
fun add_declaration name decl thy =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   363
  thy
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   364
  |> TheoryTarget.init name
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   365
  |> (fn lthy => LocalTheory.declaration (decl lthy) lthy)
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   366
  |> LocalTheory.exit
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   367
  |> ProofContext.theory_of;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   368
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   369
fun parent_components thy (Ts, pname, renaming) =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   370
  let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   371
    val ctxt = Context.Theory thy;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   372
    fun rename [] xs = xs
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   373
      | rename (NONE::rs)  (x::xs) = x::rename rs xs
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   374
      | 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
   375
    val {args,parents,components,...} =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   376
	      the (Symtab.lookup (StateSpaceData.get ctxt) pname);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   377
    val inst = map fst args ~~ Ts;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   378
    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
   379
    val parent_comps =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   380
     List.concat (map (fn (Ts',n,rs) => parent_components thy (map subst Ts',n,rs)) parents);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   381
    val all_comps = rename renaming (parent_comps @ map (apsnd subst) components);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   382
  in all_comps end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   383
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   384
fun take_upto i xs = List.take(xs,i) handle Subscript => xs;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   385
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   386
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
   387
  let
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   388
    val full_name = Sign.full_name thy name;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   389
    val all_comps = parent_comps @ components;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   390
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   391
    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
   392
    val all_comps' = map (fn (n,T) => (n,(T,full_name))) all_comps;
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   393
    fun parent_expr (_,n,rs) = Locale.Rename
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   394
                                (Locale.Locale (suffix namespaceN n),
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   395
                                 map (Option.map (fn s => (s,NONE))) rs);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   396
    val parents_expr = Locale.Merge (fold (fn p => fn es => parent_expr p::es) parents []);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   397
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   398
    fun distinct_types Ts =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   399
      let val tab = fold (fn T => fn tab => Typetab.update (T,()) tab) Ts Typetab.empty;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   400
      in map fst (Typetab.dest tab) end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   401
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   402
    val Ts = distinct_types (map snd all_comps);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   403
    val arg_names = map fst args;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   404
    val valueN = Name.variant arg_names "'value";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   405
    val nameN = Name.variant (valueN::arg_names) "'name";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   406
    val valueT = TFree (valueN, Sign.defaultS thy);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   407
    val nameT = TFree (nameN, Sign.defaultS thy);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   408
    val stateT = nameT --> valueT;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   409
    fun projectT T = valueT --> T;
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   410
    fun injectT T = T --> valueT;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   411
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   412
    val locs = map (fn T => Locale.Rename (Locale.Locale project_injectL,
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   413
                             [SOME (project_name T,NONE),
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   414
                              SOME (inject_name T ,NONE)])) Ts;
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   415
    val constrains = List.concat
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   416
         (map (fn T => [(project_name T,projectT T),(inject_name T,injectT T)]) Ts);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   417
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   418
    fun interprete_parent_valuetypes (Ts, pname, _) =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   419
      let
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   420
        val {args,types,...} =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   421
             the (Symtab.lookup (StateSpaceData.get (Context.Theory thy)) pname);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   422
        val inst = map fst args ~~ Ts;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   423
        val subst = Term.map_type_tfree (the o AList.lookup (op =) inst o fst);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   424
        val pars = List.concat (map ((fn T => [project_name T,inject_name T]) o subst) types);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   425
        val expr = Locale.Rename (Locale.Locale (suffix valuetypesN name),
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   426
                                  map (fn n => SOME (n,NONE)) pars);
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   427
      in prove_interpretation_in (K all_tac)
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   428
           (suffix valuetypesN name, expr) end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   429
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   430
    fun interprete_parent (_, pname, rs) =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   431
      let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   432
        val expr = Locale.Rename (Locale.Locale pname, map (Option.map (fn n => (n,NONE))) rs)
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   433
      in prove_interpretation_in
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   434
           (fn ctxt => Locale.intro_locales_tac false ctxt [])
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   435
           (full_name, expr) end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   436
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   437
    fun declare_declinfo updates lthy phi ctxt =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   438
      let
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   439
        fun upd_prf ctxt =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   440
          let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   441
            fun upd (n,v) =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   442
              let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   443
                val nT = ProofContext.infer_type (LocalTheory.target_of lthy) n
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   444
              in Context.proof_map
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   445
                  (update_declinfo (Morphism.term phi (Free (n,nT)),v))
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   446
              end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   447
          in ctxt |> fold upd updates end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   448
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   449
      in Context.mapping I upd_prf ctxt end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   450
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   451
   fun string_of_typ T =
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   452
      setmp show_sorts true
25696
c2058af6d9bc PrintMode.setmp (avoid direct access to print_mode ref);
wenzelm
parents: 25408
diff changeset
   453
       (PrintMode.setmp [] (Syntax.string_of_typ (ProofContext.init thy))) T;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   454
   val fixestate = (case state_type of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   455
         NONE => []
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   456
       | SOME s =>
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   457
          let
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   458
	    val fx = Element.Fixes [(s,SOME (string_of_typ stateT),NoSyn)];
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   459
            val cs = Element.Constrains
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   460
                       (map (fn (n,T) =>  (n,string_of_typ T))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   461
                         ((map (fn (n,_) => (n,nameT)) all_comps) @
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   462
                          constrains))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   463
          in [fx,cs] end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   464
       )
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   465
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   466
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   467
  in thy
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   468
     |> namespace_definition
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   469
           (suffix namespaceN name) nameT parents_expr
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   470
           (map fst parent_comps) (map fst components)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   471
     |> Context.theory_map (add_statespace full_name args parents components [])
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   472
     |> Locale.add_locale_i (SOME "") (suffix valuetypesN name) (Locale.Merge locs)
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   473
            [Element.Constrains constrains]
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   474
     |> ProofContext.theory_of o #2
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   475
     |> fold interprete_parent_valuetypes parents
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   476
     |> Locale.add_locale (SOME "") name
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   477
              (Locale.Merge [Locale.Locale (suffix namespaceN full_name)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   478
                            ,Locale.Locale (suffix valuetypesN full_name)]) fixestate
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   479
     |> ProofContext.theory_of o #2
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   480
     |> fold interprete_parent parents
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   481
     |> add_declaration (SOME full_name) (declare_declinfo components')
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   482
  end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   483
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   484
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   485
(* prepare arguments *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   486
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   487
fun read_raw_parent sg s =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   488
  (case Sign.read_typ_abbrev sg s handle TYPE (msg, _, _) => error msg of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   489
    Type (name, Ts) => (Ts, name)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   490
  | _ => error ("Bad parent statespace specification: " ^ quote s));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   491
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   492
fun read_typ sg s env  =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   493
  let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   494
    fun def_sort (x, ~1) = AList.lookup (op =) env x
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   495
      | def_sort _ = NONE;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   496
    val T = Type.no_tvars (Sign.read_def_typ (sg, def_sort) s) handle TYPE (msg, _, _) => error msg;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   497
  in (T, Term.add_typ_tfrees (T, env)) end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   498
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   499
fun cert_typ sg raw_T env =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   500
  let val T = Type.no_tvars (Sign.certify_typ sg raw_T) handle TYPE (msg, _, _) => error msg
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   501
  in (T, Term.add_typ_tfrees (T, env)) end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   502
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   503
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   504
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   505
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   506
fun gen_define_statespace prep_typ state_space args name parents comps thy =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   507
  let (* - args distinct
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   508
         - only args may occur in comps and parent-instantiations
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   509
         - number of insts must match parent args
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   510
         - no duplicate renamings
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   511
         - renaming should occur in namespace
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   512
      *)
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   513
    val _ = writeln ("Defining statespace " ^ quote name ^ " ...");
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   514
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   515
    fun add_parent (Ts,pname,rs) env =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   516
      let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   517
        val full_pname = Sign.full_name thy pname;
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   518
        val {args,components,...} =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   519
              (case get_statespace (Context.Theory thy) full_pname of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   520
                SOME r => r
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   521
               | NONE => error ("Undefined statespace " ^ quote pname));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   522
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   523
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   524
        val (Ts',env') = fold_map (prep_typ thy) Ts env
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   525
            handle ERROR msg => cat_error msg
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   526
                    ("The error(s) above occured in parent statespace specification "
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   527
                    ^ quote pname);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   528
        val err_insts = if length args <> length Ts' then
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   529
            ["number of type instantiation(s) does not match arguments of parent statespace "
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   530
              ^ quote pname]
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   531
            else [];
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   532
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   533
        val rnames = map fst rs
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   534
        val err_dup_renamings = (case duplicates (op =) rnames of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   535
             [] => []
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   536
            | dups => ["Duplicate renaming(s) for " ^ commas dups])
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   537
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   538
        val cnames = map fst components;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   539
        val err_rename_unknowns = (case (filter (fn n => not (n mem cnames))) rnames of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   540
              [] => []
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   541
             | rs => ["Unknown components " ^ commas rs]);
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   542
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   543
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   544
        val rs' = map (AList.lookup (op =) rs o fst) components;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   545
        val errs =err_insts @ err_dup_renamings @ err_rename_unknowns
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   546
      in if null errs then ((Ts',full_pname,rs'),env')
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   547
         else error (cat_lines (errs @ ["in parent statespace " ^ quote pname]))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   548
      end;
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   549
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   550
    val (parents',env) = fold_map add_parent parents [];
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   551
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   552
    val err_dup_args =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   553
         (case duplicates (op =) args of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   554
            [] => []
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   555
          | dups => ["Duplicate type argument(s) " ^ commas dups]);
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   556
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   557
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   558
    val err_dup_components =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   559
         (case duplicates (op =) (map fst comps) of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   560
           [] => []
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   561
          | dups => ["Duplicate state-space components " ^ commas dups]);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   562
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   563
    fun prep_comp (n,T) env =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   564
      let val (T', env') = prep_typ thy T env handle ERROR msg =>
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   565
       cat_error msg ("The error(s) above occured in component " ^ quote n)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   566
      in ((n,T'), env') end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   567
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   568
    val (comps',env') = fold_map prep_comp comps env;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   569
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   570
    val err_extra_frees =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   571
      (case subtract (op =) args (map fst env') of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   572
        [] => []
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   573
      | extras => ["Extra free type variable(s) " ^ commas extras]);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   574
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   575
    val defaultS = Sign.defaultS thy;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   576
    val args' = map (fn x => (x, AList.lookup (op =) env x |> the_default defaultS)) args;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   577
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   578
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   579
    fun fst_eq ((x:string,_),(y,_)) = x = y;
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   580
    fun snd_eq ((_,t:typ),(_,u)) = t = u;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   581
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   582
    val raw_parent_comps = (List.concat (map (parent_components thy) parents'));
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   583
    fun check_type (n,T) =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   584
          (case distinct (snd_eq) (filter (curry fst_eq (n,T)) raw_parent_comps) of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   585
             []  => []
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   586
           | [_] => []
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   587
           | rs  => ["Different types for component " ^ n ^": " ^ commas
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   588
                       (map (Pretty.string_of o Display.pretty_ctyp o ctyp_of thy o snd) rs)])
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   589
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   590
    val err_dup_types = List.concat (map check_type (duplicates fst_eq raw_parent_comps))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   591
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   592
    val parent_comps = distinct (fst_eq) raw_parent_comps;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   593
    val all_comps = parent_comps @ comps';
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   594
    val err_comp_in_parent = (case duplicates (op =) (map fst all_comps) of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   595
               [] => []
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   596
             | xs => ["Components already defined in parents: " ^ commas xs]);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   597
    val errs = err_dup_args @ err_dup_components @ err_extra_frees @
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   598
               err_dup_types @ err_comp_in_parent;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   599
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   600
  in if null errs
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   601
     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
   602
     else error (cat_lines errs)
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   603
  end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   604
  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
   605
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   606
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   607
val define_statespace = gen_define_statespace read_typ NONE;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   608
val define_statespace_i = gen_define_statespace cert_typ;
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   609
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   610
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   611
(*** parse/print - translations ***)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   612
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   613
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   614
local
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   615
fun map_get_comp f ctxt (Free (name,_)) =
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   616
      (case (get_comp ctxt name) of
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   617
        SOME (T,_) => f T T dummyT
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   618
      | NONE => (Syntax.free "arbitrary"(*; error "context not ready"*)))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   619
  | map_get_comp _ _ _ = Syntax.free "arbitrary";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   620
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   621
val get_comp_projection = map_get_comp project_free;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   622
val get_comp_injection  = map_get_comp inject_free;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   623
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   624
fun name_of (Free (n,_)) = n;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   625
in
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   626
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   627
fun gen_lookup_tr ctxt s n =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   628
      (case get_comp (Context.Proof ctxt) n of
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   629
        SOME (T,_) =>
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   630
           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
   631
       | NONE =>
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   632
           if get_silent (Context.Proof ctxt)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   633
	   then Syntax.const "StateFun.lookup"$Syntax.const "arbitrary"$Syntax.free n$s
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   634
           else raise TERM ("StateSpace.gen_lookup_tr: component " ^ n ^ " not defined",[]));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   635
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   636
fun lookup_tr ctxt [s,Free (n,_)] = gen_lookup_tr ctxt s n;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   637
fun lookup_swap_tr ctxt [Free (n,_),s] = gen_lookup_tr ctxt s n;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   638
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   639
fun lookup_tr' ctxt [_$Free (prj,_),n as (_$Free (name,_)),s] =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   640
     ( case get_comp (Context.Proof ctxt) name of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   641
         SOME (T,_) =>  if prj=project_name T then
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   642
                           Syntax.const "_statespace_lookup" $ s $ n
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   643
                        else raise Match
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   644
      | NONE => raise Match)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   645
  | lookup_tr' _ ts = raise Match;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   646
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   647
fun gen_update_tr id ctxt n v s =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   648
  let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   649
    fun pname T = if id then "Fun.id" else project_name T
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   650
    fun iname T = if id then "Fun.id" else inject_name T
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   651
   in
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   652
     (case get_comp (Context.Proof ctxt) n of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   653
       SOME (T,_) => Syntax.const "StateFun.update"$
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   654
                   Syntax.free (pname T)$Syntax.free (iname T)$
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   655
                   Syntax.free n$(Syntax.const KN $ v)$s
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   656
      | NONE =>
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   657
         if get_silent (Context.Proof ctxt)
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   658
         then Syntax.const "StateFun.update"$
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   659
                   Syntax.const "arbitrary"$Syntax.const "arbitrary"$
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   660
                   Syntax.free n$(Syntax.const KN $ v)$s
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   661
         else raise TERM ("StateSpace.gen_update_tr: component " ^ n ^ " not defined",[]))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   662
   end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   663
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   664
fun update_tr ctxt [s,Free (n,_),v] = gen_update_tr false ctxt n v s;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   665
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   666
fun update_tr' ctxt [_$Free (prj,_),_$Free (inj,_),n as (_$Free (name,_)),(Const (k,_)$v),s] =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   667
     if NameSpace.base k = NameSpace.base KN then
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   668
        (case get_comp (Context.Proof ctxt) name of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   669
          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
   670
                           Syntax.const "_statespace_update" $ s $ n $ v
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   671
                        else raise Match
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   672
         | NONE => raise Match)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   673
     else raise Match
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   674
  | update_tr' _ _ = raise Match;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   675
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   676
end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   677
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   678
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   679
(*** outer syntax *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   680
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   681
local structure P = OuterParse and K = OuterKeyword in
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   682
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   683
val type_insts =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   684
  P.typ >> single ||
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   685
  P.$$$ "(" |-- P.!!! (P.list1 P.typ --| P.$$$ ")")
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   686
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   687
val comp = P.name -- (P.$$$ "::" |-- P.!!! P.typ);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   688
fun plus1_unless test scan =
25999
f8bcd311d501 added ::: / @@@ scanner combinators;
wenzelm
parents: 25696
diff changeset
   689
  scan ::: Scan.repeat (P.$$$ "+" |-- Scan.unless test (P.!!! scan));
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   690
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   691
val mapsto = P.$$$ "=";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   692
val rename = P.name -- (mapsto |-- P.name);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   693
val renames =  Scan.optional (P.$$$ "[" |-- P.!!! (P.list1 rename --| P.$$$ "]")) [];
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   694
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   695
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   696
val parent = ((type_insts -- P.xname) || (P.xname >> pair [])) -- renames
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   697
             >> (fn ((insts,name),renames) => (insts,name,renames))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   698
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   699
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   700
val statespace_decl =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   701
   P.type_args -- P.name --
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   702
    (P.$$$ "=" |--
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   703
     ((Scan.repeat1 comp >> pair []) ||
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   704
      (plus1_unless comp parent --
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   705
        Scan.optional (P.$$$ "+" |-- P.!!! (Scan.repeat1 comp)) [])))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   706
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   707
val statespace_command =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   708
  OuterSyntax.command "statespace" "define state space" K.thy_decl
26478
9d1029ce0e13 eliminated quiete_mode ref (not really needed);
wenzelm
parents: 26343
diff changeset
   709
  (statespace_decl >> (fn ((args,name),(parents,comps)) =>
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   710
                           Toplevel.theory (define_statespace args name parents comps)))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   711
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   712
end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   713
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   714
end;