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