src/HOL/Tools/inductive_realizer.ML
author kleing
Mon Jun 21 10:25:57 2004 +0200 (2004-06-21)
changeset 14981 e73f8140af78
parent 14888 99ac3eb0f84e
child 15531 08c8dad8e399
permissions -rw-r--r--
Merged in license change from Isabelle2004
berghofe@13710
     1
(*  Title:      HOL/Tools/inductive_realizer.ML
berghofe@13710
     2
    ID:         $Id$
berghofe@13710
     3
    Author:     Stefan Berghofer, TU Muenchen
berghofe@13710
     4
berghofe@13710
     5
Porgram extraction from proofs involving inductive predicates:
berghofe@13710
     6
Realizers for induction and elimination rules
berghofe@13710
     7
*)
berghofe@13710
     8
berghofe@13710
     9
signature INDUCTIVE_REALIZER =
berghofe@13710
    10
sig
berghofe@13710
    11
  val add_ind_realizers: string -> string list -> theory -> theory
berghofe@13710
    12
  val setup: (theory -> theory) list
berghofe@13710
    13
end;
berghofe@13710
    14
berghofe@13710
    15
structure InductiveRealizer : INDUCTIVE_REALIZER =
berghofe@13710
    16
struct
berghofe@13710
    17
berghofe@13710
    18
val all_simps = map (symmetric o mk_meta_eq) (thms "HOL.all_simps");
berghofe@13710
    19
berghofe@13710
    20
fun prf_of thm =
berghofe@13710
    21
  let val {sign, prop, der = (_, prf), ...} = rep_thm thm
berghofe@13710
    22
  in Reconstruct.reconstruct_proof sign prop prf end;
berghofe@13710
    23
berghofe@13710
    24
fun forall_intr_prf (t, prf) =
berghofe@13710
    25
  let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p)
berghofe@13710
    26
  in Abst (a, Some T, Proofterm.prf_abstract_over t prf) end;
berghofe@13710
    27
berghofe@13710
    28
fun subsets [] = [[]]
berghofe@13710
    29
  | subsets (x::xs) =
berghofe@13710
    30
      let val ys = subsets xs
berghofe@13710
    31
      in ys @ map (cons x) ys end;
berghofe@13710
    32
berghofe@13710
    33
val set_of = fst o dest_Const o head_of o snd o HOLogic.dest_mem;
berghofe@13710
    34
berghofe@13710
    35
fun strip_all t =
berghofe@13710
    36
  let
berghofe@13710
    37
    fun strip used (Const ("all", _) $ Abs (s, T, t)) =
berghofe@13710
    38
          let val s' = variant used s
berghofe@13710
    39
          in strip (s'::used) (subst_bound (Free (s', T), t)) end
berghofe@13710
    40
      | strip used ((t as Const ("==>", _) $ P) $ Q) = t $ strip used Q
berghofe@13710
    41
      | strip _ t = t;
berghofe@13710
    42
  in strip (add_term_free_names (t, [])) t end;
berghofe@13710
    43
berghofe@13710
    44
fun relevant_vars prop = foldr (fn
berghofe@13710
    45
      (Var ((a, i), T), vs) => (case strip_type T of
berghofe@13710
    46
        (_, Type (s, _)) => if s mem ["bool", "set"] then (a, T) :: vs else vs
berghofe@13710
    47
      | _ => vs)
berghofe@13710
    48
    | (_, vs) => vs) (term_vars prop, []);
berghofe@13710
    49
berghofe@13710
    50
fun params_of intr = map (fst o fst o dest_Var) (term_vars
berghofe@13710
    51
  (snd (HOLogic.dest_mem (HOLogic.dest_Trueprop
berghofe@13710
    52
    (Logic.strip_imp_concl intr)))));
berghofe@13710
    53
berghofe@13710
    54
fun dt_of_intrs thy vs intrs =
berghofe@13710
    55
  let
berghofe@13710
    56
    val iTs = term_tvars (prop_of (hd intrs));
berghofe@13710
    57
    val Tvs = map TVar iTs;
berghofe@13710
    58
    val (_ $ (_ $ _ $ S)) = Logic.strip_imp_concl (prop_of (hd intrs));
berghofe@13710
    59
    val (Const (s, _), ts) = strip_comb S;
berghofe@13710
    60
    val params = map dest_Var ts;
berghofe@13710
    61
    val tname = space_implode "_" (Sign.base_name s ^ "T" :: vs);
berghofe@13710
    62
    fun constr_of_intr intr = (Sign.base_name (Thm.name_of_thm intr),
berghofe@13710
    63
      map (Type.unvarifyT o snd) (rev (Term.add_vars ([], prop_of intr)) \\ params) @
berghofe@13710
    64
        filter_out (equal Extraction.nullT) (map
berghofe@13710
    65
          (Type.unvarifyT o Extraction.etype_of thy vs []) (prems_of intr)),
berghofe@13710
    66
            NoSyn);
berghofe@13710
    67
  in (map (fn a => "'" ^ a) vs @ map (fst o fst) iTs, tname, NoSyn,
berghofe@13710
    68
    map constr_of_intr intrs)
berghofe@13710
    69
  end;
berghofe@13710
    70
berghofe@13710
    71
fun mk_rlz T = Const ("realizes", [T, HOLogic.boolT] ---> HOLogic.boolT);
berghofe@13710
    72
berghofe@13710
    73
(** turn "P" into "%r x. realizes r (P x)" or "%r x. realizes r (x : P)" **)
berghofe@13710
    74
berghofe@13710
    75
fun gen_rvar vs (t as Var ((a, 0), T)) =
berghofe@13710
    76
      let val U = TVar (("'" ^ a, 0), HOLogic.typeS)
berghofe@13710
    77
      in case try HOLogic.dest_setT T of
berghofe@13710
    78
          None => if body_type T <> HOLogic.boolT then t else
berghofe@13710
    79
            let
berghofe@13710
    80
              val Ts = binder_types T;
berghofe@13710
    81
              val i = length Ts;
berghofe@13710
    82
              val xs = map (pair "x") Ts;
berghofe@13710
    83
              val u = list_comb (t, map Bound (i - 1 downto 0))
berghofe@13710
    84
            in 
berghofe@13710
    85
              if a mem vs then
berghofe@13710
    86
                list_abs (("r", U) :: xs, mk_rlz U $ Bound i $ u)
berghofe@13710
    87
              else list_abs (xs, mk_rlz Extraction.nullT $ Extraction.nullt $ u)
berghofe@13710
    88
            end
berghofe@13710
    89
        | Some T' => if a mem vs then
berghofe@13710
    90
              Abs ("r", U, Abs ("x", T', mk_rlz U $ Bound 1 $
berghofe@13710
    91
                (HOLogic.mk_mem (Bound 0, t))))
berghofe@13710
    92
            else Abs ("x", T', mk_rlz Extraction.nullT $ Extraction.nullt $
berghofe@13710
    93
              (HOLogic.mk_mem (Bound 0, t)))
berghofe@13710
    94
      end
berghofe@13710
    95
  | gen_rvar _ t = t;
berghofe@13710
    96
berghofe@13710
    97
fun mk_realizes_eqn n vs intrs =
berghofe@13710
    98
  let
berghofe@13710
    99
    val iTs = term_tvars (prop_of (hd intrs));
berghofe@13710
   100
    val Tvs = map TVar iTs;
berghofe@13710
   101
    val _ $ (_ $ _ $ S) = concl_of (hd intrs);
berghofe@13710
   102
    val (Const (s, T), ts') = strip_comb S;
berghofe@13710
   103
    val setT = body_type T;
berghofe@13710
   104
    val elT = HOLogic.dest_setT setT;
berghofe@13710
   105
    val x = Var (("x", 0), elT);
berghofe@13710
   106
    val rT = if n then Extraction.nullT
berghofe@13710
   107
      else Type (space_implode "_" (s ^ "T" :: vs),
berghofe@13710
   108
        map (fn a => TVar (("'" ^ a, 0), HOLogic.typeS)) vs @ Tvs);
berghofe@13710
   109
    val r = if n then Extraction.nullt else Var ((Sign.base_name s, 0), rT);
berghofe@13710
   110
    val rvs = relevant_vars S;
berghofe@13710
   111
    val vs' = map fst rvs \\ vs;
berghofe@13710
   112
    val rname = space_implode "_" (s ^ "R" :: vs);
berghofe@13710
   113
berghofe@13710
   114
    fun mk_Tprem n v =
berghofe@13710
   115
      let val Some T = assoc (rvs, v)
berghofe@13710
   116
      in (Const ("typeof", T --> Type ("Type", [])) $ Var ((v, 0), T),
berghofe@13710
   117
        Extraction.mk_typ (if n then Extraction.nullT
berghofe@13710
   118
          else TVar (("'" ^ v, 0), HOLogic.typeS)))
berghofe@13710
   119
      end;
berghofe@13710
   120
berghofe@13710
   121
    val prems = map (mk_Tprem true) vs' @ map (mk_Tprem false) vs;
berghofe@13710
   122
    val ts = map (gen_rvar vs) ts';
berghofe@13710
   123
    val argTs = map fastype_of ts;
berghofe@13710
   124
berghofe@13710
   125
  in ((prems, (Const ("typeof", setT --> Type ("Type", [])) $ S,
berghofe@13710
   126
       Extraction.mk_typ rT)),
berghofe@13710
   127
    (prems, (mk_rlz rT $ r $ HOLogic.mk_mem (x, S),
berghofe@13710
   128
       if n then
berghofe@13710
   129
         HOLogic.mk_mem (x, list_comb (Const (rname, argTs ---> setT), ts))
berghofe@13710
   130
       else HOLogic.mk_mem (HOLogic.mk_prod (r, x), list_comb (Const (rname,
berghofe@13710
   131
         argTs ---> HOLogic.mk_setT (HOLogic.mk_prodT (rT, elT))), ts)))))
berghofe@13710
   132
  end;
berghofe@13710
   133
berghofe@13710
   134
fun fun_of_prem thy rsets vs params rule intr =
berghofe@13710
   135
  let
berghofe@13710
   136
    (* add_term_vars and Term.add_vars may return variables in different order *)
berghofe@13710
   137
    val args = map (Free o apfst fst o dest_Var)
berghofe@13710
   138
      (add_term_vars (prop_of intr, []) \\ map Var params);
berghofe@13710
   139
    val args' = map (Free o apfst fst)
berghofe@13710
   140
      (Term.add_vars ([], prop_of intr) \\ params);
berghofe@13710
   141
    val rule' = strip_all rule;
berghofe@13710
   142
    val conclT = Extraction.etype_of thy vs [] (Logic.strip_imp_concl rule');
berghofe@13710
   143
    val used = map (fst o dest_Free) args;
berghofe@13710
   144
berghofe@13710
   145
    fun is_rec t = not (null (term_consts t inter rsets));
berghofe@13710
   146
berghofe@13710
   147
    fun is_meta (Const ("all", _) $ Abs (s, _, P)) = is_meta P
berghofe@13710
   148
      | is_meta (Const ("==>", _) $ _ $ Q) = is_meta Q
berghofe@13710
   149
      | is_meta (Const ("Trueprop", _) $ (Const ("op :", _) $ _ $ _)) = true
berghofe@13710
   150
      | is_meta _ = false;
berghofe@13710
   151
berghofe@13710
   152
    fun fun_of ts rts args used (prem :: prems) =
berghofe@13710
   153
          let
berghofe@13710
   154
            val T = Extraction.etype_of thy vs [] prem;
berghofe@13710
   155
            val [x, r] = variantlist (["x", "r"], used)
berghofe@13710
   156
          in if T = Extraction.nullT
berghofe@13710
   157
            then fun_of ts rts args used prems
berghofe@13710
   158
            else if is_rec prem then
berghofe@13710
   159
              if is_meta prem then
berghofe@13710
   160
                let
berghofe@13710
   161
                  val prem' :: prems' = prems;
berghofe@13710
   162
                  val U = Extraction.etype_of thy vs [] prem';
berghofe@13710
   163
                in if U = Extraction.nullT
berghofe@13710
   164
                  then fun_of (Free (x, T) :: ts)
berghofe@13710
   165
                    (Free (r, binder_types T ---> HOLogic.unitT) :: rts)
berghofe@13710
   166
                    (Free (x, T) :: args) (x :: r :: used) prems'
berghofe@13710
   167
                  else fun_of (Free (x, T) :: ts) (Free (r, U) :: rts)
berghofe@13710
   168
                    (Free (r, U) :: Free (x, T) :: args) (x :: r :: used) prems'
berghofe@13710
   169
                end
berghofe@13710
   170
              else (case strip_type T of
berghofe@13710
   171
                  (Ts, Type ("*", [T1, T2])) =>
berghofe@13710
   172
                    let
berghofe@13710
   173
                      val fx = Free (x, Ts ---> T1);
berghofe@13710
   174
                      val fr = Free (r, Ts ---> T2);
berghofe@13710
   175
                      val bs = map Bound (length Ts - 1 downto 0);
berghofe@13710
   176
                      val t = list_abs (map (pair "z") Ts,
berghofe@13710
   177
                        HOLogic.mk_prod (list_comb (fx, bs), list_comb (fr, bs)))
berghofe@13710
   178
                    in fun_of (fx :: ts) (fr :: rts) (t::args)
berghofe@13710
   179
                      (x :: r :: used) prems
berghofe@13710
   180
                    end
berghofe@13710
   181
                | (Ts, U) => fun_of (Free (x, T) :: ts)
berghofe@13710
   182
                    (Free (r, binder_types T ---> HOLogic.unitT) :: rts)
berghofe@13710
   183
                    (Free (x, T) :: args) (x :: r :: used) prems)
berghofe@13710
   184
            else fun_of (Free (x, T) :: ts) rts (Free (x, T) :: args)
berghofe@13710
   185
              (x :: used) prems
berghofe@13710
   186
          end
berghofe@13710
   187
      | fun_of ts rts args used [] =
berghofe@13710
   188
          let val xs = rev (rts @ ts)
berghofe@13710
   189
          in if conclT = Extraction.nullT
berghofe@13710
   190
            then list_abs_free (map dest_Free xs, HOLogic.unit)
berghofe@13710
   191
            else list_abs_free (map dest_Free xs, list_comb
berghofe@13710
   192
              (Free ("r" ^ Sign.base_name (Thm.name_of_thm intr),
berghofe@13710
   193
                map fastype_of (rev args) ---> conclT), rev args))
berghofe@13710
   194
          end
berghofe@13710
   195
berghofe@13921
   196
  in fun_of args' [] (rev args) used (Logic.strip_imp_prems rule') end;
berghofe@13710
   197
berghofe@13710
   198
fun indrule_realizer thy induct raw_induct rsets params vs rec_names rss intrs dummies =
berghofe@13710
   199
  let
berghofe@13710
   200
    val concls = HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of raw_induct));
berghofe@13710
   201
    val premss = mapfilter (fn (s, rs) => if s mem rsets then
berghofe@13710
   202
      Some (map (fn r => nth_elem (find_index_eq (prop_of r) (map prop_of intrs),
berghofe@13710
   203
        prems_of raw_induct)) rs) else None) rss;
berghofe@13710
   204
    val concls' = mapfilter (fn (s, _) => if s mem rsets then
berghofe@13710
   205
        find_first (fn concl => s mem term_consts concl) concls
berghofe@13710
   206
      else None) rss;
berghofe@13710
   207
    val fs = flat (snd (foldl_map (fn (intrs, (prems, dummy)) =>
berghofe@13710
   208
      let
berghofe@13710
   209
        val (intrs1, intrs2) = splitAt (length prems, intrs);
berghofe@13710
   210
        val fs = map (fn (rule, intr) =>
berghofe@13710
   211
          fun_of_prem thy rsets vs params rule intr) (prems ~~ intrs1)
berghofe@13710
   212
      in (intrs2, if dummy then Const ("arbitrary",
berghofe@13710
   213
          HOLogic.unitT --> body_type (fastype_of (hd fs))) :: fs
berghofe@13710
   214
        else fs)
berghofe@13710
   215
      end) (intrs, (premss ~~ dummies))));
berghofe@13710
   216
    val frees = foldl Term.add_frees ([], fs);
berghofe@13710
   217
    val Ts = map fastype_of fs;
berghofe@13710
   218
    val rlzs = mapfilter (fn (a, concl) =>
berghofe@13710
   219
      let val T = Extraction.etype_of thy vs [] concl
berghofe@13710
   220
      in if T = Extraction.nullT then None
berghofe@13710
   221
        else Some (list_comb (Const (a, Ts ---> T), fs))
berghofe@13710
   222
      end) (rec_names ~~ concls')
berghofe@13710
   223
  in if null rlzs then Extraction.nullt else
berghofe@13710
   224
    let
berghofe@13710
   225
      val r = foldr1 HOLogic.mk_prod rlzs;
berghofe@13710
   226
      val x = Free ("x", Extraction.etype_of thy vs [] (hd (prems_of induct)));
berghofe@13710
   227
      fun name_of_fn intr = "r" ^ Sign.base_name (Thm.name_of_thm intr);
berghofe@13710
   228
      val r' = list_abs_free (mapfilter (fn intr =>
berghofe@13710
   229
        apsome (pair (name_of_fn intr)) (assoc (frees, name_of_fn intr))) intrs,
berghofe@13710
   230
          if length concls = 1 then r $ x else r)
berghofe@13710
   231
    in
berghofe@13710
   232
      if length concls = 1 then lambda x r' else r'
berghofe@13710
   233
    end
berghofe@13710
   234
  end;
berghofe@13710
   235
berghofe@13710
   236
fun add_dummy name dname (x as (_, (vs, s, mfx, cs))) =
berghofe@13710
   237
  if name = s then (true, (vs, s, mfx, (dname, [HOLogic.unitT], NoSyn) :: cs))
berghofe@13710
   238
  else x;
berghofe@13710
   239
berghofe@13710
   240
fun add_dummies f dts used thy =
berghofe@14888
   241
  apsnd (pair (map fst dts)) (f (map snd dts) thy)
berghofe@14888
   242
  handle DatatypeAux.Datatype_Empty name' =>
berghofe@13710
   243
      let
berghofe@14888
   244
        val name = Sign.base_name name';
berghofe@13710
   245
        val dname = variant used "Dummy"
berghofe@13710
   246
      in add_dummies f (map (add_dummy name dname) dts) (dname :: used) thy
berghofe@14888
   247
      end;
berghofe@13710
   248
berghofe@13710
   249
fun mk_realizer thy vs params ((rule, rrule), rt) =
berghofe@13710
   250
  let
berghofe@13725
   251
    val prems = prems_of rule ~~ prems_of rrule;
berghofe@13725
   252
    val rvs = map fst (relevant_vars (prop_of rule));
berghofe@13710
   253
    val xs = rev (Term.add_vars ([], prop_of rule));
berghofe@13725
   254
    val vs1 = map Var (filter_out (fn ((a, _), _) => a mem rvs) xs);
berghofe@13725
   255
    val rlzvs = rev (Term.add_vars ([], prop_of rrule));
berghofe@13725
   256
    val vs2 = map (fn (ixn, _) => Var (ixn, the (assoc (rlzvs, ixn)))) xs;
berghofe@13725
   257
    val rs = gen_rems (op = o pairself fst) (rlzvs, xs);
berghofe@13710
   258
berghofe@13710
   259
    fun mk_prf _ [] prf = prf
berghofe@13725
   260
      | mk_prf rs ((prem, rprem) :: prems) prf =
berghofe@13725
   261
          if Extraction.etype_of thy vs [] prem = Extraction.nullT
berghofe@13725
   262
          then AbsP ("H", Some rprem, mk_prf rs prems prf)
berghofe@13725
   263
          else forall_intr_prf (Var (hd rs), AbsP ("H", Some rprem,
berghofe@13725
   264
            mk_prf (tl rs) prems prf));
berghofe@13710
   265
berghofe@13710
   266
  in (Thm.name_of_thm rule, (vs,
berghofe@13710
   267
    if rt = Extraction.nullt then rt else
berghofe@13725
   268
      foldr (uncurry lambda) (vs1, rt),
berghofe@13725
   269
    foldr forall_intr_prf (vs2, mk_prf rs prems (Proofterm.proof_combP
berghofe@13725
   270
      (prf_of rrule, map PBound (length prems - 1 downto 0))))))
berghofe@13710
   271
  end;
berghofe@13710
   272
berghofe@13710
   273
fun add_rule (rss, r) =
berghofe@13710
   274
  let
berghofe@13710
   275
    val _ $ (_ $ _ $ S) = concl_of r;
berghofe@13710
   276
    val (Const (s, _), _) = strip_comb S;
berghofe@13710
   277
    val rs = if_none (assoc (rss, s)) [];
berghofe@13710
   278
  in overwrite (rss, (s, rs @ [r])) end;
berghofe@13710
   279
berghofe@13710
   280
fun add_ind_realizer rsets intrs induct raw_induct elims (thy, vs) =
berghofe@13710
   281
  let
berghofe@13710
   282
    val iTs = term_tvars (prop_of (hd intrs));
berghofe@13710
   283
    val ar = length vs + length iTs;
berghofe@13710
   284
    val (_ $ (_ $ _ $ S)) = Logic.strip_imp_concl (prop_of (hd intrs));
berghofe@13710
   285
    val (_, params) = strip_comb S;
berghofe@13710
   286
    val params' = map dest_Var params;
berghofe@13710
   287
    val rss = foldl add_rule ([], intrs);
berghofe@13710
   288
    val (prfx, _) = split_last (NameSpace.unpack (fst (hd rss)));
berghofe@13710
   289
    val tnames = map (fn s => space_implode "_" (s ^ "T" :: vs)) rsets;
berghofe@13710
   290
    val {path, ...} = Sign.rep_sg (sign_of thy);
berghofe@13710
   291
    val thy1 = thy |>
berghofe@13710
   292
      Theory.root_path |>
berghofe@13710
   293
      Theory.add_path (NameSpace.pack prfx);
berghofe@13710
   294
    val (ty_eqs, rlz_eqs) = split_list
berghofe@13710
   295
      (map (fn (s, rs) => mk_realizes_eqn (not (s mem rsets)) vs rs) rss);
berghofe@13710
   296
berghofe@13710
   297
    val thy1' = thy1 |>
berghofe@13710
   298
      Theory.copy |>
berghofe@13710
   299
      Theory.add_types (map (fn s => (Sign.base_name s, ar, NoSyn)) tnames) |>
berghofe@13710
   300
      Theory.add_arities_i (map (fn s =>
berghofe@13710
   301
        (s, replicate ar HOLogic.typeS, HOLogic.typeS)) tnames) |>
berghofe@13710
   302
        Extraction.add_typeof_eqns_i ty_eqs;
berghofe@13710
   303
    val dts = mapfilter (fn (s, rs) => if s mem rsets then
berghofe@13710
   304
      Some (dt_of_intrs thy1' vs rs) else None) rss;
berghofe@13710
   305
berghofe@13710
   306
    (** datatype representing computational content of inductive set **)
berghofe@13710
   307
berghofe@13710
   308
    val (thy2, (dummies, dt_info)) = thy1 |>
berghofe@13710
   309
      (if null dts then rpair ([], None) else
berghofe@14888
   310
        apsnd (apsnd Some) o add_dummies (DatatypePackage.add_datatype_i false false
berghofe@13710
   311
          (map #2 dts)) (map (pair false) dts) []) |>>
berghofe@13710
   312
      Extraction.add_typeof_eqns_i ty_eqs |>>
berghofe@13710
   313
      Extraction.add_realizes_eqns_i rlz_eqs;
berghofe@13710
   314
    fun get f x = if_none (apsome f x) [];
berghofe@13710
   315
    val rec_names = distinct (map (fst o dest_Const o head_of o fst o
berghofe@13710
   316
      HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) (get #rec_thms dt_info));
berghofe@13710
   317
    val (_, constrss) = foldl_map (fn ((recs, dummies), (s, rs)) =>
berghofe@13710
   318
      if s mem rsets then
berghofe@13710
   319
        let
berghofe@13710
   320
          val (d :: dummies') = dummies;
berghofe@13710
   321
          val (recs1, recs2) = splitAt (length rs, if d then tl recs else recs)
berghofe@13710
   322
        in ((recs2, dummies'), map (head_of o hd o rev o snd o strip_comb o
berghofe@13710
   323
          fst o HOLogic.dest_eq o HOLogic.dest_Trueprop o prop_of) recs1)
berghofe@13710
   324
        end
berghofe@13710
   325
      else ((recs, dummies), replicate (length rs) Extraction.nullt))
berghofe@13710
   326
        ((get #rec_thms dt_info, dummies), rss);
berghofe@13725
   327
    val rintrs = map (fn (intr, c) => Pattern.eta_contract
berghofe@13710
   328
      (Extraction.realizes_of thy2 vs
berghofe@13710
   329
        c (prop_of (forall_intr_list (map (cterm_of (sign_of thy2) o Var)
berghofe@13725
   330
          (rev (Term.add_vars ([], prop_of intr)) \\ params')) intr))))
berghofe@13710
   331
            (intrs ~~ flat constrss);
berghofe@13710
   332
    val rlzsets = distinct (map (fn rintr => snd (HOLogic.dest_mem
berghofe@13710
   333
      (HOLogic.dest_Trueprop (Logic.strip_assums_concl rintr)))) rintrs);
berghofe@13710
   334
berghofe@13710
   335
    (** realizability predicate **)
berghofe@13710
   336
berghofe@13710
   337
    val (thy3', ind_info) = thy2 |>
berghofe@13710
   338
      InductivePackage.add_inductive_i false true "" false false false
berghofe@13710
   339
        (map Logic.unvarify rlzsets) (map (fn (rintr, intr) =>
berghofe@13710
   340
          ((Sign.base_name (Thm.name_of_thm intr), strip_all
berghofe@13710
   341
            (Logic.unvarify rintr)), [])) (rintrs ~~ intrs)) [] |>>
berghofe@13710
   342
      Theory.absolute_path;
berghofe@13710
   343
    val thy3 = PureThy.hide_thms false
berghofe@13710
   344
      (map Thm.name_of_thm (#intrs ind_info)) thy3';
berghofe@13710
   345
berghofe@13710
   346
    (** realizer for induction rule **)
berghofe@13710
   347
berghofe@13710
   348
    val Ps = mapfilter (fn _ $ M $ P => if set_of M mem rsets then
berghofe@13710
   349
      Some (fst (fst (dest_Var (head_of P)))) else None)
berghofe@13710
   350
        (HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of raw_induct)));
berghofe@13710
   351
berghofe@13710
   352
    fun add_ind_realizer (thy, Ps) =
berghofe@13710
   353
      let
berghofe@13710
   354
        val r = indrule_realizer thy induct raw_induct rsets params'
berghofe@13710
   355
          (vs @ Ps) rec_names rss intrs dummies;
berghofe@13725
   356
        val rlz = strip_all (Logic.unvarify
berghofe@13725
   357
          (Extraction.realizes_of thy (vs @ Ps) r (prop_of induct)));
berghofe@13710
   358
        val rews = map mk_meta_eq
berghofe@13710
   359
          (fst_conv :: snd_conv :: get #rec_thms dt_info);
berghofe@13710
   360
        val thm = simple_prove_goal_cterm (cterm_of (sign_of thy) rlz) (fn prems =>
berghofe@13710
   361
          [if length rss = 1 then
berghofe@13710
   362
             cut_facts_tac [hd prems] 1 THEN etac (#induct ind_info) 1
berghofe@13710
   363
           else EVERY [rewrite_goals_tac (rews @ all_simps),
berghofe@13710
   364
             REPEAT (rtac allI 1), rtac (#induct ind_info) 1],
berghofe@13710
   365
           rewrite_goals_tac rews,
berghofe@13710
   366
           REPEAT ((resolve_tac prems THEN_ALL_NEW EVERY'
berghofe@13710
   367
             [K (rewrite_goals_tac rews), ObjectLogic.atomize_tac,
berghofe@13710
   368
              DEPTH_SOLVE_1 o FIRST' [atac, etac allE, etac impE]]) 1)]);
berghofe@13710
   369
        val (thy', thm') = PureThy.store_thm ((space_implode "_"
berghofe@13710
   370
          (Thm.name_of_thm induct :: vs @ Ps @ ["correctness"]), thm), []) thy
berghofe@13710
   371
      in
berghofe@13710
   372
        Extraction.add_realizers_i
berghofe@13710
   373
          [mk_realizer thy' (vs @ Ps) params' ((induct, thm'), r)] thy'
berghofe@13710
   374
      end;
berghofe@13710
   375
berghofe@13710
   376
    (** realizer for elimination rules **)
berghofe@13710
   377
berghofe@13710
   378
    val case_names = map (fst o dest_Const o head_of o fst o HOLogic.dest_eq o
berghofe@13710
   379
      HOLogic.dest_Trueprop o prop_of o hd) (get #case_thms dt_info);
berghofe@13710
   380
berghofe@13921
   381
    fun add_elim_realizer Ps
berghofe@13921
   382
      (((((elim, elimR), intrs), case_thms), case_name), dummy) thy =
berghofe@13710
   383
      let
berghofe@13710
   384
        val (prem :: prems) = prems_of elim;
berghofe@13921
   385
        fun reorder1 (p, intr) =
berghofe@13921
   386
          foldl (fn (t, ((s, _), T)) => all T $ lambda (Free (s, T)) t)
berghofe@13928
   387
            (strip_all p, Term.add_vars ([], prop_of intr) \\ params');
berghofe@13921
   388
        fun reorder2 (intr, i) =
berghofe@13921
   389
          let
berghofe@13928
   390
            val fs1 = term_vars (prop_of intr) \\ params;
berghofe@13928
   391
            val fs2 = Term.add_vars ([], prop_of intr) \\ params'
berghofe@13921
   392
          in foldl (fn (t, x) => lambda (Var x) t)
berghofe@13921
   393
            (list_comb (Bound (i + length fs1), fs1), fs2)
berghofe@13921
   394
          end;
berghofe@13921
   395
        val p = Logic.list_implies
berghofe@13921
   396
          (map reorder1 (prems ~~ intrs) @ [prem], concl_of elim);
berghofe@13710
   397
        val T' = Extraction.etype_of thy (vs @ Ps) [] p;
berghofe@13710
   398
        val T = if dummy then (HOLogic.unitT --> body_type T') --> T' else T';
berghofe@13921
   399
        val Ts = map (Extraction.etype_of thy (vs @ Ps) []) (prems_of elim);
berghofe@13710
   400
        val r = if null Ps then Extraction.nullt
berghofe@13710
   401
          else list_abs (map (pair "x") Ts, list_comb (Const (case_name, T),
berghofe@13710
   402
            (if dummy then
berghofe@13710
   403
               [Abs ("x", HOLogic.unitT, Const ("arbitrary", body_type T))]
berghofe@13710
   404
             else []) @
berghofe@13921
   405
            map reorder2 (intrs ~~ (length prems - 1 downto 0)) @
berghofe@13921
   406
            [Bound (length prems)]));
berghofe@13725
   407
        val rlz = strip_all (Logic.unvarify
berghofe@13725
   408
          (Extraction.realizes_of thy (vs @ Ps) r (prop_of elim)));
berghofe@13710
   409
        val rews = map mk_meta_eq case_thms;
berghofe@13710
   410
        val thm = simple_prove_goal_cterm (cterm_of (sign_of thy) rlz) (fn prems =>
berghofe@13710
   411
          [cut_facts_tac [hd prems] 1,
berghofe@13710
   412
           etac elimR 1,
berghofe@13710
   413
           ALLGOALS (EVERY' [etac Pair_inject, asm_simp_tac HOL_basic_ss]),
berghofe@13710
   414
           rewrite_goals_tac rews,
berghofe@13710
   415
           REPEAT ((resolve_tac prems THEN_ALL_NEW (ObjectLogic.atomize_tac THEN'
berghofe@13710
   416
             DEPTH_SOLVE_1 o FIRST' [atac, etac allE, etac impE])) 1)]);
berghofe@13710
   417
        val (thy', thm') = PureThy.store_thm ((space_implode "_"
berghofe@13710
   418
          (Thm.name_of_thm elim :: vs @ Ps @ ["correctness"]), thm), []) thy
berghofe@13710
   419
      in
berghofe@13710
   420
        Extraction.add_realizers_i
berghofe@13710
   421
          [mk_realizer thy' (vs @ Ps) params' ((elim, thm'), r)] thy'
berghofe@13710
   422
      end;
berghofe@13710
   423
berghofe@13710
   424
    (** add realizers to theory **)
berghofe@13710
   425
berghofe@13710
   426
    val rintr_thms = flat (map (fn (_, rs) => map (fn r => nth_elem
berghofe@13710
   427
      (find_index_eq r intrs, #intrs ind_info)) rs) rss);
berghofe@13710
   428
    val thy4 = foldl add_ind_realizer (thy3, subsets Ps);
berghofe@13710
   429
    val thy5 = Extraction.add_realizers_i
berghofe@13710
   430
      (map (mk_realizer thy4 vs params')
berghofe@13710
   431
         (map (fn ((rule, rrule), c) => ((rule, rrule), list_comb (c,
berghofe@13710
   432
            map Var (rev (Term.add_vars ([], prop_of rule)) \\ params')))) 
berghofe@13710
   433
              (flat (map snd rss) ~~ rintr_thms ~~ flat constrss))) thy4;
berghofe@13921
   434
    val elimps = mapfilter (fn (s, intrs) => if s mem rsets then
berghofe@13921
   435
        apsome (rpair intrs) (find_first (fn (thm, _) =>
berghofe@13921
   436
          s mem term_consts (hd (prems_of thm))) (elims ~~ #elims ind_info))
berghofe@13710
   437
      else None) rss;
berghofe@13921
   438
    val thy6 = foldl (fn (thy, p as (((((elim, _), _), _), _), _)) => thy |>
berghofe@13710
   439
      add_elim_realizer [] p |> add_elim_realizer [fst (fst (dest_Var
berghofe@13710
   440
        (HOLogic.dest_Trueprop (concl_of elim))))] p) (thy5,
berghofe@13710
   441
           elimps ~~ get #case_thms dt_info ~~ case_names ~~ dummies)
berghofe@13710
   442
berghofe@13710
   443
  in Theory.add_path (NameSpace.pack (if_none path [])) thy6 end;
berghofe@13710
   444
berghofe@13710
   445
fun add_ind_realizers name rsets thy =
berghofe@13710
   446
  let
berghofe@13710
   447
    val (_, {intrs, induct, raw_induct, elims, ...}) =
berghofe@13710
   448
      (case InductivePackage.get_inductive thy name of
berghofe@13710
   449
         None => error ("Unknown inductive set " ^ quote name)
berghofe@13710
   450
       | Some info => info);
berghofe@13710
   451
    val _ $ (_ $ _ $ S) = concl_of (hd intrs);
berghofe@13710
   452
    val vss = sort (int_ord o pairself length)
berghofe@13710
   453
      (subsets (map fst (relevant_vars S)))
berghofe@13710
   454
  in
berghofe@13710
   455
    foldl (add_ind_realizer rsets intrs induct raw_induct elims) (thy, vss)
berghofe@13710
   456
  end
berghofe@13710
   457
berghofe@13710
   458
fun rlz_attrib arg (thy, thm) =
berghofe@13710
   459
  let
berghofe@13710
   460
    fun err () = error "ind_realizer: bad rule";
berghofe@13710
   461
    val sets =
berghofe@13710
   462
      (case HOLogic.dest_conj (HOLogic.dest_Trueprop (concl_of thm)) of
berghofe@13710
   463
           [_] => [set_of (HOLogic.dest_Trueprop (hd (prems_of thm)))]
berghofe@13710
   464
         | xs => map (set_of o fst o HOLogic.dest_imp) xs)
berghofe@13710
   465
         handle TERM _ => err () | LIST _ => err ();
berghofe@13710
   466
  in 
berghofe@13710
   467
    (add_ind_realizers (hd sets) (case arg of
berghofe@13710
   468
        None => sets | Some None => []
berghofe@13710
   469
      | Some (Some sets') => sets \\ map (Sign.intern_const (sign_of thy)) sets')
berghofe@13710
   470
      thy, thm)
berghofe@13710
   471
  end;
berghofe@13710
   472
berghofe@13710
   473
val rlz_attrib_global = Attrib.syntax (Scan.lift
berghofe@13710
   474
  (Scan.option (Args.$$$ "irrelevant" |--
berghofe@13710
   475
    Scan.option (Args.colon |-- Scan.repeat1 Args.name))) >> rlz_attrib);
berghofe@13710
   476
berghofe@13710
   477
val setup = [Attrib.add_attributes [("ind_realizer",
berghofe@13710
   478
  (rlz_attrib_global, K Attrib.undef_local_attribute),
berghofe@13710
   479
  "add realizers for inductive set")]];
berghofe@13710
   480
berghofe@13710
   481
end;