src/HOL/Statespace/state_fun.ML
author paulson
Thu, 26 Mar 2009 14:10:48 +0000
changeset 30730 4d3565f2cb0e
parent 30528 7173bf123335
child 31723 f5cafe803b55
permissions -rw-r--r--
New theorems mostly concerning infinite series.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28308
d4396a28fb29 fixed headers
haftmann
parents: 27330
diff changeset
     1
(*  Title:      HOL/Statespace/state_fun.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_FUN =
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
     6
sig
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
     7
  val lookupN : string
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
     8
  val updateN : string
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
     9
29302
eb782d1dc07c normalized some ML type/val aliases;
wenzelm
parents: 29064
diff changeset
    10
  val mk_constr : theory -> typ -> term
eb782d1dc07c normalized some ML type/val aliases;
wenzelm
parents: 29064
diff changeset
    11
  val mk_destr : theory -> typ -> term
25408
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    12
29302
eb782d1dc07c normalized some ML type/val aliases;
wenzelm
parents: 29064
diff changeset
    13
  val lookup_simproc : simproc
eb782d1dc07c normalized some ML type/val aliases;
wenzelm
parents: 29064
diff changeset
    14
  val update_simproc : simproc
eb782d1dc07c normalized some ML type/val aliases;
wenzelm
parents: 29064
diff changeset
    15
  val ex_lookup_eq_simproc : simproc
eb782d1dc07c normalized some ML type/val aliases;
wenzelm
parents: 29064
diff changeset
    16
  val ex_lookup_ss : simpset
eb782d1dc07c normalized some ML type/val aliases;
wenzelm
parents: 29064
diff changeset
    17
  val lazy_conj_simproc : simproc
eb782d1dc07c normalized some ML type/val aliases;
wenzelm
parents: 29064
diff changeset
    18
  val string_eq_simp_tac : int -> tactic
25408
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    19
29302
eb782d1dc07c normalized some ML type/val aliases;
wenzelm
parents: 29064
diff changeset
    20
  val setup : theory -> theory
25408
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    21
end;
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    22
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    23
structure StateFun: STATE_FUN = 
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    24
struct
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    25
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    26
val lookupN = "StateFun.lookup";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    27
val updateN = "StateFun.update";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    28
25408
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    29
val sel_name = HOLogic.dest_string;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    30
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    31
fun mk_name i t =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    32
  (case try sel_name t of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    33
     SOME name => name
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    34
   | NONE => (case t of 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    35
               Free (x,_) => x
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    36
              |Const (x,_) => x
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    37
              |_ => "x"^string_of_int i))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    38
               
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    39
local
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    40
val conj1_False = thm "conj1_False";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    41
val conj2_False = thm "conj2_False";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    42
val conj_True = thm "conj_True";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    43
val conj_cong = thm "conj_cong";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    44
fun isFalse (Const ("False",_)) = true
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    45
  | isFalse _ = false;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    46
fun isTrue (Const ("True",_)) = true
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    47
  | isTrue _ = false;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    48
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    49
in
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    50
val lazy_conj_simproc =
29064
70a61d58460e more antiquotations;
wenzelm
parents: 28308
diff changeset
    51
  Simplifier.simproc @{theory HOL} "lazy_conj_simp" ["P & Q"]
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    52
    (fn thy => fn ss => fn t =>
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    53
      (case t of (Const ("op &",_)$P$Q) => 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    54
         let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    55
            val P_P' = Simplifier.rewrite ss (cterm_of thy P);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    56
            val P' = P_P' |> prop_of |> Logic.dest_equals |> #2 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    57
         in if isFalse P'
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    58
            then SOME (conj1_False OF [P_P'])
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    59
            else 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    60
              let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    61
                val Q_Q' = Simplifier.rewrite ss (cterm_of thy Q);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    62
                val Q' = Q_Q' |> prop_of |> Logic.dest_equals |> #2 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    63
              in if isFalse Q'
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    64
                 then SOME (conj2_False OF [Q_Q'])
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    65
                 else if isTrue P' andalso isTrue Q'
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    66
                      then SOME (conj_True OF [P_P', Q_Q'])
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    67
                      else if P aconv P' andalso Q aconv Q' then NONE
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    68
                           else SOME (conj_cong OF [P_P', Q_Q'])
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    69
              end 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    70
         end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    71
        
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    72
      | _ => NONE));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    73
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    74
val string_eq_simp_tac =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    75
     simp_tac (HOL_basic_ss 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    76
                 addsimps (thms "list.inject"@thms "char.inject"@simp_thms)
27099
2a91d9575935 DatatypePackage.distinct_simproc;
wenzelm
parents: 26496
diff changeset
    77
                 addsimprocs [DatatypePackage.distinct_simproc,lazy_conj_simproc]
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    78
                 addcongs [thm "block_conj_cong"])
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    79
end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    80
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    81
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    82
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    83
local
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    84
val rules = 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    85
 [thm "StateFun.lookup_update_id_same",
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    86
  thm "StateFun.id_id_cancel",
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    87
  thm "StateFun.lookup_update_same",thm "StateFun.lookup_update_other"
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    88
  ]
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    89
in
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    90
val lookup_ss = (HOL_basic_ss 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    91
                 addsimps (thms "list.inject"@thms "char.inject"@simp_thms@rules)
27099
2a91d9575935 DatatypePackage.distinct_simproc;
wenzelm
parents: 26496
diff changeset
    92
                 addsimprocs [DatatypePackage.distinct_simproc,lazy_conj_simproc]
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    93
                 addcongs [thm "block_conj_cong"]
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    94
                 addSolver StateSpace.distinctNameSolver) 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    95
end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    96
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    97
val ex_lookup_ss = HOL_ss addsimps [thm "StateFun.ex_id"];
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    98
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    99
structure StateFunArgs =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   100
struct
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   101
  type T = (simpset * simpset * bool); 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   102
           (* lookup simpset, ex_lookup simpset, are simprocs installed *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   103
  val empty = (empty_ss, empty_ss, false);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   104
  val extend = I;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   105
  fun merge pp ((ss1,ex_ss1,b1),(ss2,ex_ss2,b2)) = 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   106
               (merge_ss (ss1,ss2)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   107
               ,merge_ss (ex_ss1,ex_ss2)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   108
               ,b1 orelse b2);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   109
end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   110
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   111
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   112
structure StateFunData = GenericDataFun(StateFunArgs);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   113
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   114
val init_state_fun_data =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   115
  Context.theory_map (StateFunData.put (lookup_ss,ex_lookup_ss,false));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   116
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   117
val lookup_simproc =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   118
  Simplifier.simproc (the_context ()) "lookup_simp" ["lookup d n (update d' c m v s)"]
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   119
    (fn thy => fn ss => fn t =>
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   120
      (case t of (Const ("StateFun.lookup",lT)$destr$n$
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   121
                   (s as Const ("StateFun.update",uT)$_$_$_$_$_)) =>
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   122
        (let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   123
          val (_::_::_::_::sT::_) = binder_types uT;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   124
          val mi = maxidx_of_term t;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   125
          fun mk_upds (Const ("StateFun.update",uT)$d'$c$m$v$s) =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   126
               let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   127
                 val (_::_::_::fT::_::_) = binder_types uT;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   128
                 val vT = domain_type fT;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   129
                 val (s',cnt) = mk_upds s;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   130
                 val (v',cnt') = 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   131
                      (case v of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   132
                        Const ("StateFun.K_statefun",KT)$v''
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   133
                         => (case v'' of 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   134
                             (Const ("StateFun.lookup",_)$(d as (Const ("Fun.id",_)))$n'$_)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   135
                              => if d aconv c andalso n aconv m andalso m aconv n' 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   136
                                 then (v,cnt) (* Keep value so that 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   137
                                                 lookup_update_id_same can fire *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   138
                                 else (Const ("StateFun.K_statefun",KT)$Var (("v",cnt),vT),
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   139
                                       cnt+1)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   140
                              | _ => (Const ("StateFun.K_statefun",KT)$Var (("v",cnt),vT),
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   141
                                       cnt+1))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   142
                       | _ => (v,cnt));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   143
               in (Const ("StateFun.update",uT)$d'$c$m$v'$s',cnt')
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   144
               end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   145
            | mk_upds s = (Var (("s",mi+1),sT),mi+2);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   146
          
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   147
          val ct = cterm_of thy 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   148
                    (Const ("StateFun.lookup",lT)$destr$n$(fst (mk_upds s)));
30289
b28caca9157f removed spurious occurrences of old rep_ss;
wenzelm
parents: 30280
diff changeset
   149
          val ctxt = Simplifier.the_context ss;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   150
          val basic_ss = #1 (StateFunData.get (Context.Proof ctxt));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   151
          val ss' = Simplifier.context 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   152
                     (Config.map MetaSimplifier.simp_depth_limit (K 100) ctxt) basic_ss;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   153
          val thm = Simplifier.rewrite ss' ct;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   154
        in if (op aconv) (Logic.dest_equals (prop_of thm))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   155
           then NONE
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   156
           else SOME thm
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   157
        end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   158
        handle Option => NONE)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   159
         
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   160
      | _ => NONE ));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   161
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   162
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   163
fun foldl1 f (x::xs) = foldl f x xs;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   164
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   165
local
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   166
val update_apply = thm "StateFun.update_apply";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   167
val meta_ext = thm "StateFun.meta_ext";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   168
val o_apply = thm "Fun.o_apply";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   169
val ss' = (HOL_ss addsimps (update_apply::o_apply::thms "list.inject"@thms "char.inject")
27099
2a91d9575935 DatatypePackage.distinct_simproc;
wenzelm
parents: 26496
diff changeset
   170
                 addsimprocs [DatatypePackage.distinct_simproc,lazy_conj_simproc,StateSpace.distinct_simproc]
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   171
                 addcongs [thm "block_conj_cong"]);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   172
in
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   173
val update_simproc =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   174
  Simplifier.simproc (the_context ()) "update_simp" ["update d c n v s"]
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   175
    (fn thy => fn ss => fn t =>
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   176
      (case t of ((upd as Const ("StateFun.update", uT)) $ d $ c $ n $ v $ s) =>
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   177
         let 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   178
            
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   179
             val (_::_::_::_::sT::_) = binder_types uT;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   180
                (*"('v => 'a1) => ('a2 => 'v) => 'n => ('a1 => 'a2) => ('n => 'v) => ('n => 'v)"*)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   181
             fun init_seed s = (Bound 0,Bound 0, [("s",sT)],[], false);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   182
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   183
             fun mk_comp f fT g gT =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   184
               let val T = (domain_type fT --> range_type gT) 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   185
               in (Const ("Fun.comp",gT --> fT --> T)$g$f,T) end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   186
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   187
             fun mk_comps fs = 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   188
                   foldl1 (fn ((f,fT),(g,gT)) => mk_comp f fT g gT) fs;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   189
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   190
             fun append n c cT f fT d dT comps =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   191
               (case AList.lookup (op aconv) comps n of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   192
                  SOME gTs => AList.update (op aconv) 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   193
                                    (n,[(c,cT),(f,fT),(d,dT)]@gTs) comps
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   194
                | NONE => AList.update (op aconv) (n,[(c,cT),(f,fT),(d,dT)]) comps)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   195
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   196
             fun split_list (x::xs) = let val (xs',y) = split_last xs in (x,xs',y) end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   197
               | split_list _ = error "StateFun.split_list";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   198
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   199
             fun merge_upds n comps =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   200
               let val ((c,cT),fs,(d,dT)) = split_list (the (AList.lookup (op aconv) comps n))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   201
               in ((c,cT),fst (mk_comps fs),(d,dT)) end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   202
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   203
             (* mk_updterm returns 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   204
              *  - (orig-term-skeleton,simplified-term-skeleton, vars, b)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   205
              *     where boolean b tells if a simplification has occured.
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   206
                    "orig-term-skeleton = simplified-term-skeleton" is
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   207
              *     the desired simplification rule.
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   208
              * The algorithm first walks down the updates to the seed-state while
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   209
              * memorising the updates in the already-table. While walking up the
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   210
              * updates again, the optimised term is constructed.
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   211
              *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   212
             fun mk_updterm already
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   213
                 (t as ((upd as Const ("StateFun.update", uT)) $ d $ c $ n $ v $ s)) =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   214
                      let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   215
                         fun rest already = mk_updterm already;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   216
                         val (dT::cT::nT::vT::sT::_) = binder_types uT;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   217
                          (*"('v => 'a1) => ('a2 => 'v) => 'n => ('a1 => 'a2) => 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   218
                                ('n => 'v) => ('n => 'v)"*)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   219
                      in if member (op aconv) already n
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   220
                         then (case rest already s of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   221
                                 (trm,trm',vars,comps,_) =>
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   222
                                   let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   223
                                     val i = length vars;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   224
                                     val kv = (mk_name i n,vT);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   225
                                     val kb = Bound i;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   226
                                     val comps' = append n c cT kb vT d dT comps;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   227
                                   in (upd$d$c$n$kb$trm, trm', kv::vars,comps',true) end)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   228
                         else
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   229
                          (case rest (n::already) s of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   230
                             (trm,trm',vars,comps,b) =>
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   231
                                let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   232
                                   val i = length vars;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   233
                                   val kv = (mk_name i n,vT);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   234
                                   val kb = Bound i;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   235
                                   val comps' = append n c cT kb vT d dT comps;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   236
                                   val ((c',c'T),f',(d',d'T)) = merge_upds n comps';
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   237
                                   val vT' = range_type d'T --> domain_type c'T;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   238
                                   val upd' = Const ("StateFun.update",d'T --> c'T --> nT --> vT' --> sT --> sT);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   239
                                in (upd$d$c$n$kb$trm, upd'$d'$c'$n$f'$trm', kv::vars,comps',b) 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   240
                                end)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   241
                      end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   242
               | mk_updterm _ t = init_seed t;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   243
30289
b28caca9157f removed spurious occurrences of old rep_ss;
wenzelm
parents: 30280
diff changeset
   244
	     val ctxt = Simplifier.the_context ss |>
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   245
                        Config.map MetaSimplifier.simp_depth_limit (K 100);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   246
             val ss1 = Simplifier.context ctxt ss';
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   247
             val ss2 = Simplifier.context ctxt 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   248
                         (#1 (StateFunData.get (Context.Proof ctxt)));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   249
         in (case mk_updterm [] t of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   250
               (trm,trm',vars,_,true)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   251
                => let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   252
                     val eq1 = Goal.prove ctxt [] [] 
27330
1af2598b5f7d Logic.all/mk_equals/mk_implies;
wenzelm
parents: 27099
diff changeset
   253
                                      (list_all (vars, Logic.mk_equals (trm, trm')))
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   254
                                      (fn _ => rtac meta_ext 1 THEN 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   255
                                               simp_tac ss1 1);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   256
                     val eq2 = Simplifier.asm_full_rewrite ss2 (Thm.dest_equals_rhs (cprop_of eq1));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   257
                   in SOME (transitive eq1 eq2) end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   258
             | _ => NONE)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   259
         end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   260
       | _ => NONE));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   261
end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   262
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   263
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   264
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   265
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   266
local
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   267
val swap_ex_eq = thm "StateFun.swap_ex_eq";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   268
fun is_selector thy T sel =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   269
     let 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   270
       val (flds,more) = RecordPackage.get_recT_fields thy T 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   271
     in member (fn (s,(n,_)) => n=s) (more::flds) sel
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   272
     end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   273
in
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   274
val ex_lookup_eq_simproc =
29064
70a61d58460e more antiquotations;
wenzelm
parents: 28308
diff changeset
   275
  Simplifier.simproc @{theory HOL} "ex_lookup_eq_simproc" ["Ex t"]
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   276
    (fn thy => fn ss => fn t =>
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   277
       let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   278
         val ctxt = Simplifier.the_context ss |>
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   279
                    Config.map MetaSimplifier.simp_depth_limit (K 100)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   280
         val ex_lookup_ss = #2 (StateFunData.get (Context.Proof ctxt));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   281
         val ss' = (Simplifier.context ctxt ex_lookup_ss);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   282
         fun prove prop =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   283
           Goal.prove_global thy [] [] prop 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   284
             (fn _ => record_split_simp_tac [] (K ~1) 1 THEN
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   285
                      simp_tac ss' 1);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   286
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   287
         fun mkeq (swap,Teq,lT,lo,d,n,x,s) i =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   288
               let val (_::nT::_) = binder_types lT;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   289
                         (*  ('v => 'a) => 'n => ('n => 'v) => 'a *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   290
                   val x' = if not (loose_bvar1 (x,0))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   291
			    then Bound 1
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   292
                            else raise TERM ("",[x]);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   293
                   val n' = if not (loose_bvar1 (n,0))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   294
			    then Bound 2
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   295
                            else raise TERM ("",[n]);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   296
                   val sel' = lo $ d $ n' $ s;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   297
                  in (Const ("op =",Teq)$sel'$x',hd (binder_types Teq),nT,swap) end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   298
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   299
         fun dest_state (s as Bound 0) = s
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   300
           | dest_state (s as (Const (sel,sT)$Bound 0)) =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   301
               if is_selector thy (domain_type sT) sel
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   302
               then s
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   303
               else raise TERM ("StateFun.ex_lookup_eq_simproc: not a record slector",[s])
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   304
           | dest_state s = 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   305
                    raise TERM ("StateFun.ex_lookup_eq_simproc: not a record slector",[s]);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   306
  
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   307
         fun dest_sel_eq (Const ("op =",Teq)$
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   308
                           ((lo as (Const ("StateFun.lookup",lT)))$d$n$s)$X) =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   309
                           (false,Teq,lT,lo,d,n,X,dest_state s)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   310
           | dest_sel_eq (Const ("op =",Teq)$X$
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   311
                            ((lo as (Const ("StateFun.lookup",lT)))$d$n$s)) =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   312
                           (true,Teq,lT,lo,d,n,X,dest_state s)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   313
           | dest_sel_eq _ = raise TERM ("",[]);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   314
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   315
       in
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   316
         (case t of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   317
           (Const ("Ex",Tex)$Abs(s,T,t)) =>
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   318
             (let val (eq,eT,nT,swap) = mkeq (dest_sel_eq t) 0;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   319
                  val prop = list_all ([("n",nT),("x",eT)],
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   320
                              Logic.mk_equals (Const ("Ex",Tex)$Abs(s,T,eq),
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   321
                                               HOLogic.true_const));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   322
                  val thm = standard (prove prop);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   323
                  val thm' = if swap then swap_ex_eq OF [thm] else thm
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   324
             in SOME thm' end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   325
             handle TERM _ => NONE)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   326
          | _ => NONE)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   327
        end handle Option => NONE) 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   328
end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   329
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   330
val val_sfx = "V";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   331
val val_prfx = "StateFun."
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   332
fun deco base_prfx s = val_prfx ^ (base_prfx ^ suffix val_sfx s);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   333
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   334
fun mkUpper str = 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   335
  (case String.explode str of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   336
    [] => ""
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   337
   | c::cs => String.implode (Char.toUpper c::cs ))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   338
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30289
diff changeset
   339
fun mkName (Type (T,args)) = concat (map mkName args) ^ mkUpper (Long_Name.base_name T)
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30289
diff changeset
   340
  | mkName (TFree (x,_)) = mkUpper (Long_Name.base_name x)
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30289
diff changeset
   341
  | mkName (TVar ((x,_),_)) = mkUpper (Long_Name.base_name x);
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   342
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   343
fun is_datatype thy n = is_some (Symtab.lookup (DatatypePackage.get_datatypes thy) n);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   344
30280
eb98b49ef835 renamed NameSpace.base to NameSpace.base_name;
wenzelm
parents: 29302
diff changeset
   345
fun mk_map "List.list" = Syntax.const "List.map"
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30289
diff changeset
   346
  | mk_map n = Syntax.const ("StateFun.map_" ^ Long_Name.base_name n);
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   347
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   348
fun gen_constr_destr comp prfx thy (Type (T,[])) = 
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30289
diff changeset
   349
      Syntax.const (deco prfx (mkUpper (Long_Name.base_name T)))
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   350
  | gen_constr_destr comp prfx thy (T as Type ("fun",_)) =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   351
     let val (argTs,rangeT) = strip_type T;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   352
     in comp 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   353
          (Syntax.const (deco prfx (concat (map mkName argTs) ^ "Fun")))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   354
          (fold (fn x => fn y => x$y)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   355
               (replicate (length argTs) (Syntax.const "StateFun.map_fun"))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   356
               (gen_constr_destr comp prfx thy rangeT))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   357
     end
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   358
  | gen_constr_destr comp prfx thy (T' as Type (T,argTs)) = 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   359
     if is_datatype thy T
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   360
     then (* datatype args are recursively embedded into val *)
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   361
         (case argTs of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   362
           [argT] => comp 
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30289
diff changeset
   363
                     ((Syntax.const (deco prfx (mkUpper (Long_Name.base_name T)))))
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   364
                     ((mk_map T $ gen_constr_destr comp prfx thy argT))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   365
          | _ => raise (TYPE ("StateFun.gen_constr_destr",[T'],[])))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   366
     else (* type args are not recursively embedded into val *)
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30289
diff changeset
   367
           Syntax.const (deco prfx (concat (map mkName argTs) ^ mkUpper (Long_Name.base_name T)))
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   368
  | gen_constr_destr thy _ _ T = raise (TYPE ("StateFun.gen_constr_destr",[T],[]));
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   369
                   
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   370
val mk_constr = gen_constr_destr (fn a => fn b => Syntax.const "Fun.comp" $ a $ b) ""
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   371
val mk_destr =  gen_constr_destr (fn a => fn b => Syntax.const "Fun.comp" $ b $ a) "the_"
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   372
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   373
  
30528
7173bf123335 simplified attribute setup;
wenzelm
parents: 30364
diff changeset
   374
val statefun_simp_attr = Thm.declaration_attribute (fn thm => fn ctxt =>
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   375
  let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   376
     val (lookup_ss,ex_lookup_ss,simprocs_active) = StateFunData.get ctxt;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   377
     val (lookup_ss', ex_lookup_ss') = 
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   378
	   (case (concl_of thm) of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   379
            (_$((Const ("Ex",_)$_))) => (lookup_ss, ex_lookup_ss addsimps [thm])
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   380
            | _ => (lookup_ss addsimps [thm], ex_lookup_ss))
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   381
     fun activate_simprocs ctxt =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   382
          if simprocs_active then ctxt
26496
49ae9456eba9 purely functional setup of claset/simpset/clasimpset;
wenzelm
parents: 25408
diff changeset
   383
          else Simplifier.map_ss (fn ss => ss addsimprocs [lookup_simproc,update_simproc]) ctxt
30528
7173bf123335 simplified attribute setup;
wenzelm
parents: 30364
diff changeset
   384
  in
7173bf123335 simplified attribute setup;
wenzelm
parents: 30364
diff changeset
   385
    ctxt 
7173bf123335 simplified attribute setup;
wenzelm
parents: 30364
diff changeset
   386
    |> activate_simprocs
7173bf123335 simplified attribute setup;
wenzelm
parents: 30364
diff changeset
   387
    |> (StateFunData.put (lookup_ss',ex_lookup_ss',true))
7173bf123335 simplified attribute setup;
wenzelm
parents: 30364
diff changeset
   388
  end);
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   389
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   390
val setup = 
30528
7173bf123335 simplified attribute setup;
wenzelm
parents: 30364
diff changeset
   391
  init_state_fun_data #>
7173bf123335 simplified attribute setup;
wenzelm
parents: 30364
diff changeset
   392
  Attrib.setup @{binding statefun_simp} (Scan.succeed statefun_simp_attr)
7173bf123335 simplified attribute setup;
wenzelm
parents: 30364
diff changeset
   393
    "simplification in statespaces"
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   394
end