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