src/HOL/Statespace/state_fun.ML
author haftmann
Sat, 12 Mar 2016 22:04:52 +0100
changeset 62597 b3f2b8c906a6
parent 61144 5e94dfead1c2
child 62913 13252110a6fe
permissions -rw-r--r--
model characters directly as range 0..255 * * * operate on syntax terms rather than asts
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
end;
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    20
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    21
structure StateFun: STATE_FUN =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    22
struct
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    23
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    24
val lookupN = @{const_name StateFun.lookup};
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    25
val updateN = @{const_name StateFun.update};
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    26
25408
156f6f7082b8 added signatures;
schirmer
parents: 25171
diff changeset
    27
val sel_name = HOLogic.dest_string;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    28
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    29
fun mk_name i t =
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    30
  (case try sel_name t of
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    31
    SOME name => name
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    32
  | NONE =>
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    33
      (case t of
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    34
        Free (x, _) => x
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    35
      | Const (x, _) => x
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    36
      | _ => "x" ^ string_of_int i));
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    37
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    38
local
32921
0d88ad6fcf02 dropped Datatype.distinct_simproc; tuned
haftmann
parents: 32010
diff changeset
    39
39159
0dec18004e75 more antiquotations;
wenzelm
parents: 38864
diff changeset
    40
val conj1_False = @{thm conj1_False};
0dec18004e75 more antiquotations;
wenzelm
parents: 38864
diff changeset
    41
val conj2_False = @{thm conj2_False};
0dec18004e75 more antiquotations;
wenzelm
parents: 38864
diff changeset
    42
val conj_True = @{thm conj_True};
0dec18004e75 more antiquotations;
wenzelm
parents: 38864
diff changeset
    43
val conj_cong = @{thm conj_cong};
32921
0d88ad6fcf02 dropped Datatype.distinct_simproc; tuned
haftmann
parents: 32010
diff changeset
    44
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    45
fun isFalse (Const (@{const_name False}, _)) = true
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    46
  | isFalse _ = false;
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    47
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
    48
fun isTrue (Const (@{const_name True}, _)) = true
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    49
  | isTrue _ = false;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    50
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    51
in
32921
0d88ad6fcf02 dropped Datatype.distinct_simproc; tuned
haftmann
parents: 32010
diff changeset
    52
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    53
val lazy_conj_simproc =
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    54
  Simplifier.make_simproc @{context} "lazy_conj_simp"
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    55
   {lhss = [@{term "P & Q"}],
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    56
    proc = fn _ => fn ctxt => fn ct =>
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    57
      (case Thm.term_of ct of
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    58
        Const (@{const_name HOL.conj},_) $ P $ Q =>
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    59
          let
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    60
            val P_P' = Simplifier.rewrite ctxt (Thm.cterm_of ctxt P);
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    61
            val P' = P_P' |> Thm.prop_of |> Logic.dest_equals |> #2;
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    62
          in
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    63
            if isFalse P' then SOME (conj1_False OF [P_P'])
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    64
            else
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    65
              let
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    66
                val Q_Q' = Simplifier.rewrite ctxt (Thm.cterm_of ctxt Q);
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    67
                val Q' = Q_Q' |> Thm.prop_of |> Logic.dest_equals |> #2;
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    68
              in
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    69
                if isFalse Q' then SOME (conj2_False OF [Q_Q'])
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    70
                else if isTrue P' andalso isTrue Q' then SOME (conj_True OF [P_P', Q_Q'])
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    71
                else if P aconv P' andalso Q aconv Q' then NONE
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    72
                else SOME (conj_cong OF [P_P', Q_Q'])
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    73
              end
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    74
           end
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    75
      | _ => NONE),
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
    76
    identifier = []};
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    77
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
    78
fun string_eq_simp_tac ctxt =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
    79
  simp_tac (put_simpset HOL_basic_ss ctxt
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 61144
diff changeset
    80
    addsimps @{thms list.inject list.distinct Char_eq_Char_iff
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 61144
diff changeset
    81
      cut_eq_simps simp_thms}
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
    82
    addsimprocs [lazy_conj_simproc]
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
    83
    |> Simplifier.add_cong @{thm block_conj_cong});
32921
0d88ad6fcf02 dropped Datatype.distinct_simproc; tuned
haftmann
parents: 32010
diff changeset
    84
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    85
end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    86
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
    87
val lookup_ss =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
    88
  simpset_of (put_simpset HOL_basic_ss @{context}
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 61144
diff changeset
    89
    addsimps (@{thms list.inject} @ @{thms Char_eq_Char_iff}
58156
e333bd3b4d3d removed vacuous theorem references
blanchet
parents: 58149
diff changeset
    90
      @ @{thms list.distinct} @ @{thms simp_thms}
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
    91
      @ [@{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
    92
        @{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
    93
    addsimprocs [lazy_conj_simproc]
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
    94
    addSolver StateSpace.distinctNameSolver
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
    95
    |> fold Simplifier.add_cong @{thms block_conj_cong});
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    96
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
    97
val ex_lookup_ss =
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
    98
  simpset_of (put_simpset HOL_ss @{context} addsimps @{thms StateFun.ex_id});
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
    99
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33003
diff changeset
   100
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   101
structure Data = Generic_Data
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33003
diff changeset
   102
(
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   103
  type T = simpset * simpset * bool;  (*lookup simpset, ex_lookup simpset, are simprocs installed*)
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   104
  val empty = (empty_ss, empty_ss, false);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   105
  val extend = I;
33519
e31a85f92ce9 adapted Generic_Data, Proof_Data;
wenzelm
parents: 33003
diff changeset
   106
  fun merge ((ss1, ex_ss1, b1), (ss2, ex_ss2, b2)) =
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   107
    (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
   108
);
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   109
58825
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   110
val _ = Theory.setup (Context.theory_map (Data.put (lookup_ss, ex_lookup_ss, false)));
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   111
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   112
val lookup_simproc =
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   113
  Simplifier.make_simproc @{context} "lookup_simp"
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   114
   {lhss = [@{term "lookup d n (update d' c m v s)"}],
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   115
    proc = fn _ => fn ctxt => fn ct =>
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   116
      (case Thm.term_of ct of (Const (@{const_name StateFun.lookup}, lT) $ destr $ n $
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   117
                   (s as Const (@{const_name StateFun.update}, uT) $ _ $ _ $ _ $ _ $ _)) =>
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   118
        (let
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   119
          val (_::_::_::_::sT::_) = binder_types uT;
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   120
          val mi = Term.maxidx_of_term (Thm.term_of ct);
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   121
          fun mk_upds (Const (@{const_name StateFun.update}, uT) $ d' $ c $ m $ v $ s) =
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   122
                let
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   123
                  val (_ :: _ :: _ :: fT :: _ :: _) = binder_types uT;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   124
                  val vT = domain_type fT;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   125
                  val (s', cnt) = mk_upds s;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   126
                  val (v', cnt') =
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   127
                    (case v of
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   128
                      Const (@{const_name K_statefun}, KT) $ v'' =>
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 StateFun.lookup}, _) $
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   131
                            (d as (Const (@{const_name Fun.id}, _))) $ n' $ _) =>
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   132
                              if d aconv c andalso n aconv m andalso m aconv n'
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   133
                              then (v,cnt) (* Keep value so that
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   134
                                              lookup_update_id_same can fire *)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   135
                              else
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   136
                                (Const (@{const_name StateFun.K_statefun}, KT) $
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   137
                                  Var (("v", cnt), vT), cnt + 1)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   138
                        | _ =>
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   139
                          (Const (@{const_name StateFun.K_statefun}, KT) $
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   140
                            Var (("v", cnt), vT), cnt + 1))
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   141
                     | _ => (v, cnt));
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   142
                in (Const (@{const_name StateFun.update}, uT) $ d' $ c $ m $ v' $ s', cnt') end
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   143
            | mk_upds s = (Var (("s", mi + 1), sT), mi + 2);
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   144
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   145
          val ct =
59643
f3be9235503d clarified context;
wenzelm
parents: 59621
diff changeset
   146
            Thm.cterm_of ctxt
f3be9235503d clarified context;
wenzelm
parents: 59621
diff changeset
   147
              (Const (@{const_name StateFun.lookup}, lT) $ destr $ n $ fst (mk_upds s));
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   148
          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
   149
          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
   150
          val thm = Simplifier.rewrite ctxt' ct;
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   151
        in
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58825
diff changeset
   152
          if (op aconv) (Logic.dest_equals (Thm.prop_of thm))
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   153
          then NONE
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   154
          else SOME thm
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   155
        end
45361
wenzelm
parents: 42083
diff changeset
   156
        handle Option.Option => NONE)
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   157
      | _ => NONE),
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   158
  identifier = []};
25171
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
62597
b3f2b8c906a6 model characters directly as range 0..255
haftmann
parents: 61144
diff changeset
   166
    (@{thm StateFun.update_apply} :: @{thm Fun.o_apply} :: @{thms list.inject} @ @{thms Char_eq_Char_iff}
58156
e333bd3b4d3d removed vacuous theorem references
blanchet
parents: 58149
diff changeset
   167
      @ @{thms list.distinct})
51717
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 =
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   174
  Simplifier.make_simproc @{context} "update_simp"
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   175
   {lhss = [@{term "update d c n v s"}],
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   176
    proc = fn _ => fn ctxt => fn ct =>
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   177
      (case Thm.term_of ct of
55972
51b342baecda removed dead code;
wenzelm
parents: 55465
diff changeset
   178
        Const (@{const_name StateFun.update}, uT) $ _ $ _ $ _ $ _ $ _ =>
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   179
          let
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   180
            val (_ :: _ :: _ :: _ :: sT :: _) = binder_types uT;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   181
              (*"('v => 'a1) => ('a2 => 'v) => 'n => ('a1 => 'a2) => ('n => 'v) => ('n => 'v)"*)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   182
            fun init_seed s = (Bound 0, Bound 0, [("s", sT)], [], false);
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   183
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   184
            fun mk_comp f fT g gT =
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   185
              let val T = domain_type fT --> range_type gT
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   186
              in (Const (@{const_name Fun.comp}, gT --> fT --> T) $ g $ f, T) end;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   187
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   188
            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
   189
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   190
            fun append n c cT f fT d dT comps =
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   191
              (case AList.lookup (op aconv) comps n of
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   192
                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
   193
              | NONE => AList.update (op aconv) (n, [(c, cT), (f, fT), (d, dT)]) comps);
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   194
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   195
            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
   196
              | split_list _ = error "StateFun.split_list";
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   197
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   198
            fun merge_upds n comps =
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   199
              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
   200
              in ((c, cT), fst (mk_comps fs), (d, dT)) end;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   201
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   202
               (* mk_updterm returns
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   203
                *  - (orig-term-skeleton,simplified-term-skeleton, vars, b)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   204
                *     where boolean b tells if a simplification has occurred.
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   205
                      "orig-term-skeleton = simplified-term-skeleton" is
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   206
                *     the desired simplification rule.
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   207
                * The algorithm first walks down the updates to the seed-state while
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   208
                * memorising the updates in the already-table. While walking up the
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   209
                * updates again, the optimised term is constructed.
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   210
                *)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   211
            fun mk_updterm already
55972
51b342baecda removed dead code;
wenzelm
parents: 55465
diff changeset
   212
                ((upd as Const (@{const_name StateFun.update}, uT)) $ d $ c $ n $ v $ s) =
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   213
                  let
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   214
                    fun rest already = mk_updterm already;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   215
                    val (dT :: cT :: nT :: vT :: sT :: _) = binder_types uT;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   216
                      (*"('v => 'a1) => ('a2 => 'v) => 'n => ('a1 => 'a2) =>
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   217
                            ('n => 'v) => ('n => 'v)"*)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   218
                  in
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   219
                    if member (op aconv) already n then
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   220
                      (case rest already s of
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   221
                        (trm, trm', vars, comps, _) =>
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   222
                          let
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   223
                            val i = length vars;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   224
                            val kv = (mk_name i n, vT);
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   225
                            val kb = Bound i;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   226
                            val comps' = append n c cT kb vT d dT comps;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   227
                          in (upd $ d $ c $ n $ kb $ trm, trm', kv :: vars, comps',true) end)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   228
                    else
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   229
                      (case rest (n :: already) s of
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   230
                        (trm, trm', vars, comps, b) =>
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   231
                          let
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   232
                            val i = length vars;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   233
                            val kv = (mk_name i n, vT);
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   234
                            val kb = Bound i;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   235
                            val comps' = append n c cT kb vT d dT comps;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   236
                            val ((c', c'T), f', (d', d'T)) = merge_upds n comps';
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   237
                            val vT' = range_type d'T --> domain_type c'T;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   238
                            val upd' =
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   239
                              Const (@{const_name StateFun.update},
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   240
                                d'T --> c'T --> nT --> vT' --> sT --> sT);
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   241
                          in
55972
51b342baecda removed dead code;
wenzelm
parents: 55465
diff changeset
   242
                            (upd $ d $ c $ n $ kb $ trm, upd' $ d' $ c' $ n $ f' $ trm', kv :: vars,
51b342baecda removed dead code;
wenzelm
parents: 55465
diff changeset
   243
                              comps', b)
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   244
                          end)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   245
                  end
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   246
              | mk_updterm _ t = init_seed t;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   247
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
   248
            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
   249
            val ctxt1 = put_simpset ss' ctxt0;
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
   250
            val ctxt2 = put_simpset (#1 (Data.get (Context.Proof ctxt0))) ctxt0;
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   251
          in
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   252
            (case mk_updterm [] (Thm.term_of ct) of
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   253
              (trm, trm', vars, _, true) =>
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   254
                let
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   255
                  val eq1 =
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
   256
                    Goal.prove ctxt0 [] []
46218
ecf6375e2abb renamed Term.list_all to Logic.list_all, in accordance to HOLogic.list_all;
wenzelm
parents: 45740
diff changeset
   257
                      (Logic.list_all (vars, Logic.mk_equals (trm, trm')))
60754
02924903a6fd prefer tactics with explicit context;
wenzelm
parents: 59643
diff changeset
   258
                      (fn _ => resolve_tac ctxt0 [meta_ext] 1 THEN simp_tac ctxt1 1);
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58825
diff changeset
   259
                  val eq2 = Simplifier.asm_full_rewrite ctxt2 (Thm.dest_equals_rhs (Thm.cprop_of eq1));
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   260
                in SOME (Thm.transitive eq1 eq2) end
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   261
            | _ => NONE)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   262
          end
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   263
      | _ => NONE),
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   264
  identifier = []};
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   265
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   266
end;
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   267
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   268
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   269
local
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   270
39159
0dec18004e75 more antiquotations;
wenzelm
parents: 38864
diff changeset
   271
val swap_ex_eq = @{thm StateFun.swap_ex_eq};
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   272
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   273
fun is_selector thy T sel =
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   274
  let val (flds, more) = Record.get_recT_fields thy T
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   275
  in member (fn (s, (n, _)) => n = s) (more :: flds) sel end;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   276
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   277
in
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   278
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   279
val ex_lookup_eq_simproc =
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   280
  Simplifier.make_simproc @{context} "ex_lookup_eq_simproc"
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   281
   {lhss = [@{term "Ex t"}],
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   282
    proc = fn _ => fn ctxt => fn ct =>
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   283
      let
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
   284
        val thy = Proof_Context.theory_of ctxt;
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   285
        val t = Thm.term_of ct;
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
   286
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   287
        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
   288
        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
   289
        fun prove prop =
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   290
          Goal.prove_global thy [] [] prop
51717
9e7d1c139569 simplifier uses proper Proof.context instead of historic type simpset;
wenzelm
parents: 46218
diff changeset
   291
            (fn _ => Record.split_simp_tac ctxt [] (K ~1) 1 THEN simp_tac ctxt' 1);
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   292
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   293
        fun mkeq (swap, Teq, lT, lo, d, n, x, s) i =
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   294
          let
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   295
            val (_ :: nT :: _) = binder_types lT;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   296
            (*  ('v => 'a) => 'n => ('n => 'v) => 'a *)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   297
            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
   298
            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
   299
            val sel' = lo $ d $ n' $ s;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   300
          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
   301
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   302
        fun dest_state (s as Bound 0) = s
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   303
          | dest_state (s as (Const (sel, sT) $ Bound 0)) =
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   304
              if is_selector thy (domain_type sT) sel then s
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   305
              else raise TERM ("StateFun.ex_lookup_eq_simproc: not a record slector", [s])
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   306
          | dest_state s = raise TERM ("StateFun.ex_lookup_eq_simproc: not a record slector", [s]);
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   307
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   308
        fun dest_sel_eq
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   309
              (Const (@{const_name HOL.eq}, Teq) $
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   310
                ((lo as (Const (@{const_name StateFun.lookup}, lT))) $ d $ n $ s) $ X) =
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   311
              (false, Teq, lT, lo, d, n, X, dest_state s)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   312
          | dest_sel_eq
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   313
              (Const (@{const_name HOL.eq}, Teq) $ X $
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   314
                ((lo as (Const (@{const_name StateFun.lookup}, lT))) $ d $ n $ s)) =
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   315
              (true, Teq, lT, lo, d, n, X, dest_state s)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   316
          | dest_sel_eq _ = raise TERM ("", []);
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   317
      in
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   318
        (case t of
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   319
          Const (@{const_name Ex}, Tex) $ Abs (s, T, t) =>
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   320
            (let
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   321
              val (eq, eT, nT, swap) = mkeq (dest_sel_eq t) 0;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   322
              val prop =
46218
ecf6375e2abb renamed Term.list_all to Logic.list_all, in accordance to HOLogic.list_all;
wenzelm
parents: 45740
diff changeset
   323
                Logic.list_all ([("n", nT), ("x", eT)],
45740
132a3e1c0fe5 more antiquotations;
wenzelm
parents: 45661
diff changeset
   324
                  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
   325
              val thm = Drule.export_without_context (prove prop);
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   326
              val thm' = if swap then swap_ex_eq OF [thm] else thm
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   327
            in SOME thm' end handle TERM _ => NONE)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   328
        | _ => NONE)
61144
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   329
      end handle Option.Option => NONE,
5e94dfead1c2 simplified simproc programming interfaces;
wenzelm
parents: 60754
diff changeset
   330
  identifier = []};
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   331
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   332
end;
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   333
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   334
val val_sfx = "V";
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   335
val val_prfx = "StateFun."
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   336
fun deco base_prfx s = val_prfx ^ (base_prfx ^ suffix val_sfx s);
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   337
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   338
fun mkUpper str =
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   339
  (case String.explode str of
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   340
    [] => ""
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   341
  | c::cs => String.implode (Char.toUpper c :: cs));
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   342
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32921
diff changeset
   343
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
   344
  | 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
   345
  | mkName (TVar ((x,_),_)) = mkUpper (Long_Name.base_name x);
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   346
58354
04ac60da613e support (finite values of) codatatypes in Quickcheck
blanchet
parents: 58156
diff changeset
   347
fun is_datatype thy = is_some o BNF_LFP_Compat.get_info thy [BNF_LFP_Compat.Keep_Nesting];
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   348
55465
0d31c0546286 merged 'List.map' and 'List.list.map'
blanchet
parents: 51717
diff changeset
   349
fun mk_map @{type_name List.list} = Syntax.const @{const_name List.map}
30364
577edc39b501 moved basic algebra of long names from structure NameSpace to Long_Name;
wenzelm
parents: 30289
diff changeset
   350
  | mk_map n = Syntax.const ("StateFun.map_" ^ Long_Name.base_name n);
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   351
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   352
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
   353
      Syntax.const (deco prfx (mkUpper (Long_Name.base_name T)))
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   354
  | gen_constr_destr comp prfx thy (T as Type ("fun",_)) =
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   355
      let val (argTs, rangeT) = strip_type T;
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   356
      in
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   357
        comp
32952
aeb1e44fbc19 replaced String.concat by implode;
wenzelm
parents: 32921
diff changeset
   358
          (Syntax.const (deco prfx (implode (map mkName argTs) ^ "Fun")))
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   359
          (fold (fn x => fn y => x $ y)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   360
            (replicate (length argTs) (Syntax.const "StateFun.map_fun"))
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   361
            (gen_constr_destr comp prfx thy rangeT))
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   362
      end
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   363
  | gen_constr_destr comp prfx thy (T' as Type (T, argTs)) =
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   364
      if is_datatype thy T
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   365
      then (* datatype args are recursively embedded into val *)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   366
        (case argTs of
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   367
          [argT] =>
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   368
            comp
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   369
              ((Syntax.const (deco prfx (mkUpper (Long_Name.base_name T)))))
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   370
              ((mk_map T $ gen_constr_destr comp prfx thy argT))
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   371
        | _ => raise (TYPE ("StateFun.gen_constr_destr", [T'], [])))
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   372
      else (* type args are not recursively embedded into val *)
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   373
        Syntax.const (deco prfx (implode (map mkName argTs) ^ mkUpper (Long_Name.base_name T)))
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   374
  | gen_constr_destr thy _ _ T = raise (TYPE ("StateFun.gen_constr_destr", [T], []));
25171
4a9c25bffc9b added Statespace library
schirmer
parents:
diff changeset
   375
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   376
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
   377
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
   378
58825
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   379
val _ =
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   380
  Theory.setup
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   381
    (Attrib.setup @{binding statefun_simp}
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   382
      (Scan.succeed (Thm.declaration_attribute (fn thm => fn context =>
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   383
        let
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   384
          val ctxt = Context.proof_of context;
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   385
          val (lookup_ss, ex_lookup_ss, simprocs_active) = Data.get context;
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   386
          val (lookup_ss', ex_lookup_ss') =
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 58825
diff changeset
   387
            (case Thm.concl_of thm of
58825
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   388
              (_ $ ((Const (@{const_name Ex}, _) $ _))) =>
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   389
                (lookup_ss, simpset_map ctxt (Simplifier.add_simp thm) ex_lookup_ss)
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   390
            | _ =>
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   391
                (simpset_map ctxt (Simplifier.add_simp thm) lookup_ss, ex_lookup_ss));
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   392
          val activate_simprocs =
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   393
            if simprocs_active then I
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   394
            else Simplifier.map_ss (fn ctxt => ctxt addsimprocs [lookup_simproc, update_simproc]);
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   395
        in
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   396
          context
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   397
          |> activate_simprocs
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   398
          |> Data.put (lookup_ss', ex_lookup_ss', true)
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   399
        end)))
2065f49da190 modernized setup;
wenzelm
parents: 58354
diff changeset
   400
      "simplification in statespaces");
45363
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   401
208634369af2 misc tuning and modernization;
wenzelm
parents: 45361
diff changeset
   402
end;