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