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