src/HOL/Tools/Datatype/datatype_realizer.ML
author wenzelm
Sat Jan 14 20:05:58 2012 +0100 (2012-01-14)
changeset 46218 ecf6375e2abb
parent 46215 0da9433f959e
child 46219 426ed18eba43
permissions -rw-r--r--
renamed Term.list_all to Logic.list_all, in accordance to HOLogic.list_all;
haftmann@33968
     1
(*  Title:      HOL/Tools/Datatype/datatype_realizer.ML
berghofe@13467
     2
    Author:     Stefan Berghofer, TU Muenchen
berghofe@13467
     3
haftmann@33968
     4
Program extraction from proofs involving datatypes:
haftmann@33968
     5
realizers for induction and case analysis.
berghofe@13467
     6
*)
berghofe@13467
     7
berghofe@13467
     8
signature DATATYPE_REALIZER =
berghofe@13467
     9
sig
haftmann@31737
    10
  val add_dt_realizers: Datatype.config -> string list -> theory -> theory
haftmann@24699
    11
  val setup: theory -> theory
berghofe@13467
    12
end;
berghofe@13467
    13
haftmann@33968
    14
structure Datatype_Realizer : DATATYPE_REALIZER =
berghofe@13467
    15
struct
berghofe@13467
    16
wenzelm@35364
    17
fun subsets i j =
wenzelm@35364
    18
  if i <= j then
wenzelm@35364
    19
    let val is = subsets (i+1) j
wenzelm@35364
    20
    in map (fn ks => i::ks) is @ is end
wenzelm@35364
    21
  else [[]];
berghofe@13467
    22
wenzelm@40844
    23
fun is_unit t = body_type (fastype_of t) = HOLogic.unitT;
berghofe@13467
    24
berghofe@13725
    25
fun tname_of (Type (s, _)) = s
berghofe@13725
    26
  | tname_of _ = "";
berghofe@13725
    27
wenzelm@45896
    28
fun make_ind ({descr, rec_names, rec_rewrites, induct, ...} : Datatype.info) is thy =
berghofe@13467
    29
  let
wenzelm@45822
    30
    val recTs = Datatype_Aux.get_rec_types descr;
wenzelm@45700
    31
    val pnames =
wenzelm@45700
    32
      if length descr = 1 then ["P"]
berghofe@13467
    33
      else map (fn i => "P" ^ string_of_int i) (1 upto length descr);
berghofe@13467
    34
berghofe@13467
    35
    val rec_result_Ts = map (fn ((i, _), P) =>
wenzelm@45700
    36
        if member (op =) is i then TFree ("'" ^ P, HOLogic.typeS) else HOLogic.unitT)
wenzelm@45700
    37
      (descr ~~ pnames);
berghofe@13467
    38
berghofe@13467
    39
    fun make_pred i T U r x =
haftmann@36692
    40
      if member (op =) is i then
wenzelm@42364
    41
        Free (nth pnames i, T --> U --> HOLogic.boolT) $ r $ x
wenzelm@42364
    42
      else Free (nth pnames i, U --> HOLogic.boolT) $ x;
berghofe@13467
    43
berghofe@13467
    44
    fun mk_all i s T t =
wenzelm@46215
    45
      if member (op =) is i then Logic.all (Free (s, T)) t else t;
berghofe@13467
    46
haftmann@31458
    47
    val (prems, rec_fns) = split_list (flat (fst (fold_map
haftmann@31458
    48
      (fn ((i, (_, _, constrs)), T) => fold_map (fn (cname, cargs) => fn j =>
berghofe@13467
    49
        let
wenzelm@45822
    50
          val Ts = map (Datatype_Aux.typ_of_dtyp descr) cargs;
haftmann@33968
    51
          val tnames = Name.variant_list pnames (Datatype_Prop.make_tnames Ts);
wenzelm@41423
    52
          val recs = filter (Datatype_Aux.is_rec_type o fst o fst) (cargs ~~ tnames ~~ Ts);
berghofe@13467
    53
          val frees = tnames ~~ Ts;
berghofe@13467
    54
wenzelm@38977
    55
          fun mk_prems vs [] =
berghofe@13467
    56
                let
haftmann@31458
    57
                  val rT = nth (rec_result_Ts) i;
berghofe@13467
    58
                  val vs' = filter_out is_unit vs;
wenzelm@41423
    59
                  val f = Datatype_Aux.mk_Free "f" (map fastype_of vs' ---> rT) j;
wenzelm@45700
    60
                  val f' =
wenzelm@45700
    61
                    Envir.eta_contract (fold_rev (absfree o dest_Free) vs
wenzelm@45700
    62
                      (if member (op =) is i then list_comb (f, vs') else HOLogic.unit));
wenzelm@45700
    63
                in
wenzelm@45700
    64
                  (HOLogic.mk_Trueprop (make_pred i rT T (list_comb (f, vs'))
wenzelm@45700
    65
                    (list_comb (Const (cname, Ts ---> T), map Free frees))), f')
berghofe@13467
    66
                end
wenzelm@38977
    67
            | mk_prems vs (((dt, s), T) :: ds) =
berghofe@13467
    68
                let
wenzelm@41423
    69
                  val k = Datatype_Aux.body_index dt;
berghofe@13641
    70
                  val (Us, U) = strip_type T;
berghofe@13641
    71
                  val i = length Us;
haftmann@31458
    72
                  val rT = nth (rec_result_Ts) k;
berghofe@13641
    73
                  val r = Free ("r" ^ s, Us ---> rT);
wenzelm@45700
    74
                  val (p, f) = mk_prems (vs @ [r]) ds;
wenzelm@45700
    75
                in
wenzelm@45700
    76
                  (mk_all k ("r" ^ s) (Us ---> rT) (Logic.mk_implies
wenzelm@46218
    77
                    (Logic.list_all (map (pair "x") Us, HOLogic.mk_Trueprop
wenzelm@45700
    78
                      (make_pred k rT U (Datatype_Aux.app_bnds r i)
wenzelm@45700
    79
                        (Datatype_Aux.app_bnds (Free (s, T)) i))), p)), f)
wenzelm@45700
    80
                end;
wenzelm@46215
    81
        in (apfst (fold_rev (Logic.all o Free) frees) (mk_prems (map Free frees) recs), j + 1) end)
haftmann@31458
    82
          constrs) (descr ~~ recTs) 1)));
wenzelm@38977
    83
berghofe@13467
    84
    fun mk_proj j [] t = t
wenzelm@45700
    85
      | mk_proj j (i :: is) t =
wenzelm@45700
    86
          if null is then t
wenzelm@45700
    87
          else if (j: int) = i then HOLogic.mk_fst t
berghofe@13467
    88
          else mk_proj j is (HOLogic.mk_snd t);
berghofe@13467
    89
haftmann@33968
    90
    val tnames = Datatype_Prop.make_tnames recTs;
berghofe@13467
    91
    val fTs = map fastype_of rec_fns;
berghofe@13467
    92
    val ps = map (fn ((((i, _), T), U), s) => Abs ("x", T, make_pred i U T
berghofe@13467
    93
      (list_comb (Const (s, fTs ---> T --> U), rec_fns) $ Bound 0) (Bound 0)))
berghofe@13467
    94
        (descr ~~ recTs ~~ rec_result_Ts ~~ rec_names);
wenzelm@45700
    95
    val r =
wenzelm@45700
    96
      if null is then Extraction.nullt
wenzelm@45700
    97
      else
wenzelm@45700
    98
        foldr1 HOLogic.mk_prod (map_filter (fn (((((i, _), T), U), s), tname) =>
wenzelm@45700
    99
          if member (op =) is i then SOME
wenzelm@45700
   100
            (list_comb (Const (s, fTs ---> T --> U), rec_fns) $ Free (tname, T))
wenzelm@45700
   101
          else NONE) (descr ~~ recTs ~~ rec_result_Ts ~~ rec_names ~~ tnames));
wenzelm@45700
   102
    val concl =
wenzelm@45700
   103
      HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop @{const_name HOL.conj})
wenzelm@45700
   104
        (map (fn ((((i, _), T), U), tname) =>
wenzelm@45700
   105
          make_pred i U T (mk_proj i is r) (Free (tname, T)))
wenzelm@45700
   106
            (descr ~~ recTs ~~ rec_result_Ts ~~ tnames)));
wenzelm@22578
   107
    val cert = cterm_of thy;
berghofe@13467
   108
    val inst = map (pairself cert) (map head_of (HOLogic.dest_conj
haftmann@32712
   109
      (HOLogic.dest_Trueprop (concl_of induct))) ~~ ps);
berghofe@13467
   110
wenzelm@45700
   111
    val thm =
wenzelm@45700
   112
      Goal.prove_internal (map cert prems) (cert concl)
wenzelm@45700
   113
        (fn prems =>
wenzelm@45700
   114
           EVERY [
wenzelm@45700
   115
            rewrite_goals_tac (map mk_meta_eq [@{thm fst_conv}, @{thm snd_conv}]),
wenzelm@45700
   116
            rtac (cterm_instantiate inst induct) 1,
wenzelm@45700
   117
            ALLGOALS Object_Logic.atomize_prems_tac,
wenzelm@45700
   118
            rewrite_goals_tac (@{thm o_def} :: map mk_meta_eq rec_rewrites),
wenzelm@45700
   119
            REPEAT ((resolve_tac prems THEN_ALL_NEW (fn i =>
wenzelm@45700
   120
              REPEAT (etac allE i) THEN atac i)) 1)])
wenzelm@38977
   121
      |> Drule.export_without_context;
berghofe@13467
   122
wenzelm@36744
   123
    val ind_name = Thm.derivation_name induct;
wenzelm@42364
   124
    val vs = map (nth pnames) is;
haftmann@18358
   125
    val (thm', thy') = thy
wenzelm@30435
   126
      |> Sign.root_path
wenzelm@39557
   127
      |> Global_Theory.store_thm
wenzelm@30435
   128
        (Binding.qualified_name (space_implode "_" (ind_name :: vs @ ["correctness"])), thm)
wenzelm@24712
   129
      ||> Sign.restore_naming thy;
berghofe@13467
   130
wenzelm@45822
   131
    val ivs = rev (Term.add_vars (Logic.varify_global (Datatype_Prop.make_ind [descr])) []);
wenzelm@22691
   132
    val rvs = rev (Thm.fold_terms Term.add_vars thm' []);
wenzelm@45700
   133
    val ivs1 = map Var (filter_out (fn (_, T) => @{type_name bool} = tname_of (body_type T)) ivs);
wenzelm@33035
   134
    val ivs2 = map (fn (ixn, _) => Var (ixn, the (AList.lookup (op =) rvs ixn))) ivs;
berghofe@13467
   135
wenzelm@33338
   136
    val prf =
berghofe@37233
   137
      Extraction.abs_corr_shyps thy' induct vs ivs2
wenzelm@33338
   138
        (fold_rev (fn (f, p) => fn prf =>
wenzelm@45700
   139
            (case head_of (strip_abs_body f) of
wenzelm@45700
   140
              Free (s, T) =>
wenzelm@45700
   141
                let val T' = Logic.varifyT_global T in
wenzelm@45700
   142
                  Abst (s, SOME T', Proofterm.prf_abstract_over
wenzelm@45700
   143
                    (Var ((s, 0), T')) (AbsP ("H", SOME p, prf)))
wenzelm@45700
   144
                end
wenzelm@45700
   145
            | _ => AbsP ("H", SOME p, prf)))
wenzelm@45700
   146
          (rec_fns ~~ prems_of thm)
wenzelm@45700
   147
          (Proofterm.proof_combP
wenzelm@45700
   148
            (Reconstruct.proof_of thm', map PBound (length prems - 1 downto 0))));
berghofe@13467
   149
wenzelm@33338
   150
    val r' =
wenzelm@33338
   151
      if null is then r
wenzelm@45700
   152
      else
wenzelm@45700
   153
        Logic.varify_global (fold_rev lambda
wenzelm@45700
   154
          (map Logic.unvarify_global ivs1 @ filter_out is_unit
wenzelm@45700
   155
              (map (head_of o strip_abs_body) rec_fns)) r);
berghofe@13467
   156
berghofe@13467
   157
  in Extraction.add_realizers_i [(ind_name, (vs, r', prf))] thy' end;
berghofe@13467
   158
berghofe@13467
   159
wenzelm@45896
   160
fun make_casedists ({index, descr, case_name, case_rewrites, exhaust, ...} : Datatype.info) thy =
berghofe@13467
   161
  let
wenzelm@19806
   162
    val cert = cterm_of thy;
berghofe@13467
   163
    val rT = TFree ("'P", HOLogic.typeS);
berghofe@13467
   164
    val rT' = TVar (("'P", 0), HOLogic.typeS);
berghofe@13467
   165
berghofe@13467
   166
    fun make_casedist_prem T (cname, cargs) =
berghofe@13467
   167
      let
wenzelm@45822
   168
        val Ts = map (Datatype_Aux.typ_of_dtyp descr) cargs;
haftmann@33968
   169
        val frees = Name.variant_list ["P", "y"] (Datatype_Prop.make_tnames Ts) ~~ Ts;
berghofe@13467
   170
        val free_ts = map Free frees;
wenzelm@30364
   171
        val r = Free ("r" ^ Long_Name.base_name cname, Ts ---> rT)
wenzelm@45700
   172
      in
wenzelm@46215
   173
        (r, fold_rev Logic.all free_ts
wenzelm@46215
   174
          (Logic.mk_implies (HOLogic.mk_Trueprop
wenzelm@46215
   175
            (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))),
wenzelm@46215
   176
              HOLogic.mk_Trueprop (Free ("P", rT --> HOLogic.boolT) $
wenzelm@46215
   177
                list_comb (r, free_ts)))))
berghofe@13467
   178
      end;
berghofe@13467
   179
haftmann@17521
   180
    val SOME (_, _, constrs) = AList.lookup (op =) descr index;
wenzelm@45822
   181
    val T = nth (Datatype_Aux.get_rec_types descr) index;
berghofe@13467
   182
    val (rs, prems) = split_list (map (make_casedist_prem T) constrs);
berghofe@13467
   183
    val r = Const (case_name, map fastype_of rs ---> T --> rT);
berghofe@13467
   184
wenzelm@35845
   185
    val y = Var (("y", 0), Logic.varifyT_global T);
berghofe@13467
   186
    val y' = Free ("y", T);
berghofe@13467
   187
wenzelm@45700
   188
    val thm =
wenzelm@45700
   189
      Goal.prove_internal (map cert prems)
wenzelm@45700
   190
        (cert (HOLogic.mk_Trueprop (Free ("P", rT --> HOLogic.boolT) $ list_comb (r, rs @ [y']))))
wenzelm@45700
   191
        (fn prems =>
wenzelm@45700
   192
           EVERY [
wenzelm@45700
   193
            rtac (cterm_instantiate [(cert y, cert y')] exhaust) 1,
wenzelm@45700
   194
            ALLGOALS (EVERY'
wenzelm@45700
   195
              [asm_simp_tac (HOL_basic_ss addsimps case_rewrites),
wenzelm@45700
   196
               resolve_tac prems, asm_simp_tac HOL_basic_ss])])
wenzelm@38977
   197
      |> Drule.export_without_context;
berghofe@13467
   198
wenzelm@36744
   199
    val exh_name = Thm.derivation_name exhaust;
haftmann@18358
   200
    val (thm', thy') = thy
wenzelm@30435
   201
      |> Sign.root_path
wenzelm@39557
   202
      |> Global_Theory.store_thm (Binding.qualified_name (exh_name ^ "_P_correctness"), thm)
wenzelm@24712
   203
      ||> Sign.restore_naming thy;
berghofe@13467
   204
berghofe@13725
   205
    val P = Var (("P", 0), rT' --> HOLogic.boolT);
wenzelm@45700
   206
    val prf =
wenzelm@45700
   207
      Extraction.abs_corr_shyps thy' exhaust ["P"] [y, P]
wenzelm@45700
   208
        (fold_rev (fn (p, r) => fn prf =>
wenzelm@45700
   209
            Proofterm.forall_intr_proof' (Logic.varify_global r)
wenzelm@45700
   210
              (AbsP ("H", SOME (Logic.varify_global p), prf)))
wenzelm@45700
   211
          (prems ~~ rs)
wenzelm@45700
   212
          (Proofterm.proof_combP
wenzelm@45700
   213
            (Reconstruct.proof_of thm', map PBound (length prems - 1 downto 0))));
wenzelm@45700
   214
    val prf' =
wenzelm@45700
   215
      Extraction.abs_corr_shyps thy' exhaust []
wenzelm@45700
   216
        (map Var (Term.add_vars (prop_of exhaust) [])) (Reconstruct.proof_of exhaust);
wenzelm@45700
   217
    val r' =
wenzelm@45700
   218
      Logic.varify_global (Abs ("y", T,
wenzelm@45700
   219
        list_abs (map dest_Free rs, list_comb (r,
wenzelm@45700
   220
          map Bound ((length rs - 1 downto 0) @ [length rs])))));
wenzelm@45700
   221
  in
wenzelm@45700
   222
    Extraction.add_realizers_i
wenzelm@45700
   223
      [(exh_name, (["P"], r', prf)),
wenzelm@45700
   224
       (exh_name, ([], Extraction.nullt, prf'))] thy'
berghofe@13467
   225
  end;
berghofe@13467
   226
haftmann@31668
   227
fun add_dt_realizers config names thy =
wenzelm@41698
   228
  if not (Proofterm.proofs_enabled ()) then thy
wenzelm@38977
   229
  else
wenzelm@38977
   230
    let
wenzelm@41423
   231
      val _ = Datatype_Aux.message config "Adding realizers for induction and case analysis ...";
wenzelm@38977
   232
      val infos = map (Datatype.the_info thy) names;
wenzelm@38977
   233
      val info :: _ = infos;
wenzelm@38977
   234
    in
wenzelm@38977
   235
      thy
wenzelm@45822
   236
      |> fold_rev (make_ind info) (subsets 0 (length (#descr info) - 1))
wenzelm@45822
   237
      |> fold_rev make_casedists infos
wenzelm@38977
   238
    end;
berghofe@13467
   239
haftmann@31723
   240
val setup = Datatype.interpretation add_dt_realizers;
berghofe@13467
   241
berghofe@13467
   242
end;