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