src/HOL/Tools/datatype_realizer.ML
author berghofe
Wed, 13 Nov 2002 15:28:41 +0100
changeset 13708 a3a410782c95
parent 13656 58bb243dbafb
child 13725 12404b452034
permissions -rw-r--r--
prove_goal' -> Goal.simple_prove_goal_cterm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13467
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
     1
(*  Title:      HOL/Tools/datatype_realizer.ML
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
     2
    ID:         $Id$
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
     3
    Author:     Stefan Berghofer, TU Muenchen
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
     4
    License:    GPL (GNU GENERAL PUBLIC LICENSE)
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
     5
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
     6
Porgram extraction from proofs involving datatypes:
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
     7
Realizers for induction and case analysis
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
     8
*)
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
     9
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    10
signature DATATYPE_REALIZER =
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    11
sig
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    12
  val add_dt_realizers: (string * sort) list ->
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    13
    DatatypeAux.datatype_info list -> theory -> theory
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    14
end;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    15
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    16
structure DatatypeRealizer : DATATYPE_REALIZER =
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    17
struct
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    18
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    19
open DatatypeAux;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    20
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    21
fun subsets i j = if i <= j then
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    22
       let val is = subsets (i+1) j
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    23
       in map (fn ks => i::ks) is @ is end
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    24
     else [[]];
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    25
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    26
fun forall_intr_prf (t, prf) =
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    27
  let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p)
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    28
  in Abst (a, Some T, Proofterm.prf_abstract_over t prf) end;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    29
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    30
fun prf_of thm =
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    31
  let val {sign, prop, der = (_, prf), ...} = rep_thm thm
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    32
  in Reconstruct.reconstruct_proof sign prop prf end;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    33
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    34
fun prf_subst_vars inst =
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    35
  Proofterm.map_proof_terms (subst_vars ([], inst)) I;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    36
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    37
fun is_unit t = snd (strip_type (fastype_of t)) = HOLogic.unitT;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    38
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    39
fun mk_realizes T = Const ("realizes", T --> HOLogic.boolT --> HOLogic.boolT);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    40
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    41
fun make_ind sorts ({descr, rec_names, rec_rewrites, induction, ...} : datatype_info) (is, thy) =
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    42
  let
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    43
    val sg = sign_of thy;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    44
    val recTs = get_rec_types descr sorts;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    45
    val pnames = if length descr = 1 then ["P"]
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    46
      else map (fn i => "P" ^ string_of_int i) (1 upto length descr);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    47
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    48
    val rec_result_Ts = map (fn ((i, _), P) =>
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    49
      if i mem is then TFree ("'" ^ P, HOLogic.typeS) else HOLogic.unitT)
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    50
        (descr ~~ pnames);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    51
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    52
    fun make_pred i T U r x =
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    53
      if i mem is then
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    54
        Free (nth_elem (i, pnames), T --> U --> HOLogic.boolT) $ r $ x
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    55
      else Free (nth_elem (i, pnames), U --> HOLogic.boolT) $ x;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    56
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    57
    fun mk_all i s T t =
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    58
      if i mem is then list_all_free ([(s, T)], t) else t;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    59
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    60
    val (prems, rec_fns) = split_list (flat (snd (foldl_map
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    61
      (fn (j, ((i, (_, _, constrs)), T)) => foldl_map (fn (j, (cname, cargs)) =>
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    62
        let
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    63
          val Ts = map (typ_of_dtyp descr sorts) cargs;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    64
          val tnames = variantlist (DatatypeProp.make_tnames Ts, pnames);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    65
          val recs = filter (is_rec_type o fst o fst) (cargs ~~ tnames ~~ Ts);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    66
          val frees = tnames ~~ Ts;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    67
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    68
          fun mk_prems vs [] = 
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    69
                let
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    70
                  val rT = nth_elem (i, rec_result_Ts);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    71
                  val vs' = filter_out is_unit vs;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    72
                  val f = mk_Free "f" (map fastype_of vs' ---> rT) j;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    73
                  val f' = Pattern.eta_contract (list_abs_free
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    74
                    (map dest_Free vs, if i mem is then list_comb (f, vs')
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    75
                      else HOLogic.unit));
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    76
                in (HOLogic.mk_Trueprop (make_pred i rT T (list_comb (f, vs'))
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    77
                  (list_comb (Const (cname, Ts ---> T), map Free frees))), f')
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    78
                end
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13467
diff changeset
    79
            | mk_prems vs (((dt, s), T) :: ds) = 
13467
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    80
                let
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13467
diff changeset
    81
                  val k = body_index dt;
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13467
diff changeset
    82
                  val (Us, U) = strip_type T;
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13467
diff changeset
    83
                  val i = length Us;
13467
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    84
                  val rT = nth_elem (k, rec_result_Ts);
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13467
diff changeset
    85
                  val r = Free ("r" ^ s, Us ---> rT);
13467
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    86
                  val (p, f) = mk_prems (vs @ [r]) ds
13641
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13467
diff changeset
    87
                in (mk_all k ("r" ^ s) (Us ---> rT) (Logic.mk_implies
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13467
diff changeset
    88
                  (list_all (map (pair "x") Us, HOLogic.mk_Trueprop
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13467
diff changeset
    89
                    (make_pred k rT U (app_bnds r i)
63d1790a43ed Reimplemented parts of datatype package dealing with datatypes involving
berghofe
parents: 13467
diff changeset
    90
                      (app_bnds (Free (s, T)) i))), p)), f)
13467
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    91
                end
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    92
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    93
        in (j + 1,
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    94
          apfst (curry list_all_free frees) (mk_prems (map Free frees) recs))
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    95
        end) (j, constrs)) (1, descr ~~ recTs))));
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    96
 
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    97
    fun mk_proj j [] t = t
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    98
      | mk_proj j (i :: is) t = if null is then t else
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
    99
          if j = i then HOLogic.mk_fst t
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   100
          else mk_proj j is (HOLogic.mk_snd t);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   101
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   102
    val tnames = DatatypeProp.make_tnames recTs;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   103
    val fTs = map fastype_of rec_fns;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   104
    val ps = map (fn ((((i, _), T), U), s) => Abs ("x", T, make_pred i U T
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   105
      (list_comb (Const (s, fTs ---> T --> U), rec_fns) $ Bound 0) (Bound 0)))
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   106
        (descr ~~ recTs ~~ rec_result_Ts ~~ rec_names);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   107
    val r = if null is then Extraction.nullt else
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   108
      foldr1 HOLogic.mk_prod (mapfilter (fn (((((i, _), T), U), s), tname) =>
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   109
        if i mem is then Some
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   110
          (list_comb (Const (s, fTs ---> T --> U), rec_fns) $ Free (tname, T))
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   111
        else None) (descr ~~ recTs ~~ rec_result_Ts ~~ rec_names ~~ tnames));
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   112
    val concl = HOLogic.mk_Trueprop (foldr1 (HOLogic.mk_binop "op &")
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   113
      (map (fn ((((i, _), T), U), tname) =>
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   114
        make_pred i U T (mk_proj i is r) (Free (tname, T)))
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   115
          (descr ~~ recTs ~~ rec_result_Ts ~~ tnames)));
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   116
    val cert = cterm_of sg;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   117
    val inst = map (pairself cert) (map head_of (HOLogic.dest_conj
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   118
      (HOLogic.dest_Trueprop (concl_of induction))) ~~ ps);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   119
13708
a3a410782c95 prove_goal' -> Goal.simple_prove_goal_cterm
berghofe
parents: 13656
diff changeset
   120
    val thm = simple_prove_goal_cterm (cert (Logic.list_implies (prems, concl)))
13467
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   121
      (fn prems =>
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   122
         [rewrite_goals_tac (map mk_meta_eq [fst_conv, snd_conv]),
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   123
          rtac (cterm_instantiate inst induction) 1,
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   124
          ALLGOALS ObjectLogic.atomize_tac,
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   125
          rewrite_goals_tac (o_def :: map mk_meta_eq rec_rewrites),
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   126
          REPEAT ((resolve_tac prems THEN_ALL_NEW (fn i =>
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   127
            REPEAT (etac allE i) THEN atac i)) 1)]);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   128
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   129
    val {path, ...} = Sign.rep_sg sg;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   130
    val ind_name = Thm.name_of_thm induction;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   131
    val vs = map (fn i => nth_elem (i, pnames)) is;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   132
    val (thy', thm') = thy
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   133
      |> Theory.absolute_path
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   134
      |> PureThy.store_thm
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   135
        ((space_implode "_" (ind_name :: vs @ ["correctness"]), thm), [])
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   136
      |>> Theory.add_path (NameSpace.pack (if_none path []));
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   137
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   138
    val inst = map (fn ((((i, _), s), T), U) => ((s, 0), if i mem is then
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   139
        Abs ("r", U, Abs ("x", T, mk_realizes U $ Bound 1 $
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   140
          (Var ((s, 0), T --> HOLogic.boolT) $ Bound 0)))
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   141
      else Abs ("x", T, mk_realizes Extraction.nullT $ Extraction.nullt $
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   142
        (Var ((s, 0), T --> HOLogic.boolT) $
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   143
          Bound 0)))) (descr ~~ pnames ~~ map Type.varifyT recTs ~~
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   144
            map Type.varifyT rec_result_Ts);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   145
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   146
    val ivs = map Var (Drule.vars_of_terms
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   147
      [Logic.varify (DatatypeProp.make_ind [descr] sorts)]);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   148
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   149
    val prf = foldr forall_intr_prf (ivs,
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   150
      prf_subst_vars inst (foldr (fn ((f, p), prf) =>
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   151
        (case head_of (strip_abs_body f) of
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   152
           Free (s, T) =>
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   153
             let val T' = Type.varifyT T
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   154
             in Abst (s, Some T', Proofterm.prf_abstract_over
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   155
               (Var ((s, 0), T')) (AbsP ("H", Some p, prf)))
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   156
             end
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   157
         | _ => AbsP ("H", Some p, prf)))
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   158
           (rec_fns ~~ prems_of thm, Proofterm.proof_combP
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   159
             (prf_of thm', map PBound (length prems - 1 downto 0)))));
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   160
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   161
    val r' = if null is then r else Logic.varify (foldr (uncurry lambda)
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   162
      (map Logic.unvarify ivs @ filter_out is_unit
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   163
        (map (head_of o strip_abs_body) rec_fns), r));
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   164
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   165
  in Extraction.add_realizers_i [(ind_name, (vs, r', prf))] thy' end;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   166
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   167
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   168
fun make_casedists sorts ({index, descr, case_name, case_rewrites, exhaustion, ...} : datatype_info, thy) =
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   169
  let
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   170
    val sg = sign_of thy;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   171
    val sorts = map (rpair HOLogic.typeS) (distinct (flat (map
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   172
      (fn (_, (_, ds, _)) => mapfilter (try dest_DtTFree) ds) descr)));
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   173
    val cert = cterm_of sg;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   174
    val rT = TFree ("'P", HOLogic.typeS);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   175
    val rT' = TVar (("'P", 0), HOLogic.typeS);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   176
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   177
    fun make_casedist_prem T (cname, cargs) =
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   178
      let
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   179
        val Ts = map (typ_of_dtyp descr sorts) cargs;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   180
        val frees = variantlist
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   181
          (DatatypeProp.make_tnames Ts, ["P", "y"]) ~~ Ts;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   182
        val free_ts = map Free frees;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   183
        val r = Free ("r" ^ NameSpace.base cname, Ts ---> rT)
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   184
      in (r, list_all_free (frees, Logic.mk_implies (HOLogic.mk_Trueprop
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   185
        (HOLogic.mk_eq (Free ("y", T), list_comb (Const (cname, Ts ---> T), free_ts))),
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   186
          HOLogic.mk_Trueprop (Free ("P", rT --> HOLogic.boolT) $
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   187
            list_comb (r, free_ts)))))
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   188
      end;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   189
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   190
    val Some (_, _, constrs) = assoc (descr, index);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   191
    val T = nth_elem (index, get_rec_types descr sorts);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   192
    val (rs, prems) = split_list (map (make_casedist_prem T) constrs);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   193
    val r = Const (case_name, map fastype_of rs ---> T --> rT);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   194
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   195
    val y = Var (("y", 0), Type.varifyT T);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   196
    val y' = Free ("y", T);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   197
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   198
    val thm = prove_goalw_cterm [] (cert (Logic.list_implies (prems,
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   199
      HOLogic.mk_Trueprop (Free ("P", rT --> HOLogic.boolT) $
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   200
        list_comb (r, rs @ [y'])))))
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   201
      (fn prems =>
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   202
         [rtac (cterm_instantiate [(cert y, cert y')] exhaustion) 1,
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   203
          ALLGOALS (EVERY'
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   204
            [asm_simp_tac (HOL_basic_ss addsimps case_rewrites),
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   205
             resolve_tac prems, asm_simp_tac HOL_basic_ss])]);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   206
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   207
    val {path, ...} = Sign.rep_sg sg;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   208
    val exh_name = Thm.name_of_thm exhaustion;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   209
    val (thy', thm') = thy
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   210
      |> Theory.absolute_path
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   211
      |> PureThy.store_thm ((exh_name ^ "_P_correctness", thm), [])
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   212
      |>> Theory.add_path (NameSpace.pack (if_none path []));
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   213
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   214
    val P = Var (("P", 0), HOLogic.boolT);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   215
    val prf = forall_intr_prf (y, forall_intr_prf (P,
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   216
      prf_subst_vars [(("P", 0), Abs ("r", rT',
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   217
        mk_realizes rT' $ Bound 0 $ P))] (foldr (fn ((p, r), prf) =>
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   218
          forall_intr_prf (Logic.varify r, AbsP ("H", Some (Logic.varify p),
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   219
            prf))) (prems ~~ rs, Proofterm.proof_combP (prf_of thm',
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   220
              map PBound (length prems - 1 downto 0))))));
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   221
    val r' = Logic.varify (Abs ("y", Type.varifyT T,
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   222
      Abs ("P", HOLogic.boolT, list_abs (map dest_Free rs, list_comb (r,
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   223
        map Bound ((length rs - 1 downto 0) @ [length rs + 1]))))));
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   224
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   225
    val prf' = forall_intr_prf (y, forall_intr_prf (P, prf_subst_vars
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   226
      [(("P", 0), mk_realizes Extraction.nullT $ Extraction.nullt $ P)]
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   227
        (prf_of exhaustion)));
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   228
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   229
  in Extraction.add_realizers_i
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   230
    [(exh_name, (["P"], r', prf)),
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   231
     (exh_name, ([], Extraction.nullt, prf'))] thy'
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   232
  end;
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   233
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   234
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   235
fun add_dt_realizers sorts infos thy = if !proofs < 2 then thy else
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   236
  (message "Adding realizers for induction and case analysis ..."; thy
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   237
   |> curry (foldr (make_ind sorts (hd infos)))
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   238
     (subsets 0 (length (#descr (hd infos)) - 1))
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   239
   |> curry (foldr (make_casedists sorts)) infos);
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   240
d66b526192bf Module for defining realizers for induction and case analysis theorems
berghofe
parents:
diff changeset
   241
end;