src/HOL/Tools/reification.ML
author wenzelm
Wed Jul 08 21:33:00 2015 +0200 (2015-07-08)
changeset 60696 8304fb4fb823
parent 60642 48dd1cefb4ae
child 60752 b48830b670a1
permissions -rw-r--r--
clarified context;
haftmann@52286
     1
(*  Title:      HOL/Tools/reification.ML
haftmann@51726
     2
    Author:     Amine Chaieb, TU Muenchen
haftmann@51726
     3
haftmann@51726
     4
A trial for automatical reification.
haftmann@51726
     5
*)
haftmann@51726
     6
haftmann@52286
     7
signature REIFICATION =
haftmann@51726
     8
sig
haftmann@52286
     9
  val conv: Proof.context -> thm list -> conv
haftmann@52286
    10
  val tac: Proof.context -> thm list -> term option -> int -> tactic
haftmann@52286
    11
  val lift_conv: Proof.context -> conv -> term option -> int -> tactic
haftmann@52286
    12
  val dereify: Proof.context -> thm list -> conv
haftmann@51726
    13
end;
haftmann@51726
    14
haftmann@52286
    15
structure Reification : REIFICATION =
haftmann@51726
    16
struct
haftmann@51726
    17
haftmann@52275
    18
fun dest_listT (Type (@{type_name "list"}, [T])) = T;
haftmann@52275
    19
haftmann@51726
    20
val FWD = curry (op OF);
haftmann@51726
    21
haftmann@52275
    22
fun rewrite_with ctxt eqs = Simplifier.rewrite (put_simpset HOL_basic_ss ctxt addsimps eqs);
haftmann@52275
    23
haftmann@52275
    24
val pure_subst = @{lemma "x == y ==> PROP P y ==> PROP P x" by simp}
haftmann@51726
    25
wenzelm@60696
    26
fun lift_conv ctxt conv some_t = Subgoal.FOCUS (fn {context = ctxt', concl, ...} =>
haftmann@52275
    27
  let
wenzelm@60696
    28
    val ct =
wenzelm@60696
    29
      (case some_t of
wenzelm@60696
    30
        NONE => Thm.dest_arg concl
wenzelm@60696
    31
      | SOME t => Thm.cterm_of ctxt' t)
haftmann@52275
    32
    val thm = conv ct;
haftmann@52275
    33
  in
haftmann@52275
    34
    if Thm.is_reflexive thm then no_tac
haftmann@52275
    35
    else ALLGOALS (rtac (pure_subst OF [thm]))
haftmann@52275
    36
  end) ctxt;
haftmann@51726
    37
haftmann@51726
    38
(* Make a congruence rule out of a defining equation for the interpretation
haftmann@51726
    39
haftmann@51726
    40
   th is one defining equation of f,
haftmann@51726
    41
     i.e. th is "f (Cp ?t1 ... ?tn) = P(f ?t1, .., f ?tn)" 
haftmann@51726
    42
   Cp is a constructor pattern and P is a pattern 
haftmann@51726
    43
haftmann@51726
    44
   The result is:
haftmann@51726
    45
     [|?A1 = f ?t1 ; .. ; ?An= f ?tn |] ==> P (?A1, .., ?An) = f (Cp ?t1 .. ?tn)
haftmann@51726
    46
       + the a list of names of the A1 .. An, Those are fresh in the ctxt *)
haftmann@51726
    47
haftmann@51726
    48
fun mk_congeq ctxt fs th =
haftmann@51726
    49
  let
wenzelm@59582
    50
    val Const (fN, _) = th |> Thm.prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq
haftmann@51726
    51
      |> fst |> strip_comb |> fst;
haftmann@51726
    52
    val ((_, [th']), ctxt') = Variable.import true [th] ctxt;
haftmann@51726
    53
    val (lhs, rhs) = HOLogic.dest_eq (HOLogic.dest_Trueprop (Thm.prop_of th'));
haftmann@51726
    54
    fun add_fterms (t as t1 $ t2) =
haftmann@51726
    55
          if exists (fn f => Term.could_unify (t |> strip_comb |> fst, f)) fs
haftmann@51726
    56
          then insert (op aconv) t
haftmann@51726
    57
          else add_fterms t1 #> add_fterms t2
haftmann@51726
    58
      | add_fterms (t as Abs _) =
haftmann@51726
    59
          if exists_Const (fn (c, _) => c = fN) t
haftmann@51726
    60
          then K [t]
haftmann@51726
    61
          else K []
haftmann@51726
    62
      | add_fterms _ = I;
haftmann@51726
    63
    val fterms = add_fterms rhs [];
haftmann@51726
    64
    val (xs, ctxt'') = Variable.variant_fixes (replicate (length fterms) "x") ctxt';
haftmann@51726
    65
    val tys = map fastype_of fterms;
haftmann@51726
    66
    val vs = map Free (xs ~~ tys);
haftmann@51726
    67
    val env = fterms ~~ vs; (*FIXME*)
haftmann@51726
    68
    fun replace_fterms (t as t1 $ t2) =
haftmann@51726
    69
        (case AList.lookup (op aconv) env t of
haftmann@51726
    70
            SOME v => v
haftmann@51726
    71
          | NONE => replace_fterms t1 $ replace_fterms t2)
haftmann@51726
    72
      | replace_fterms t =
haftmann@51726
    73
        (case AList.lookup (op aconv) env t of
haftmann@51726
    74
            SOME v => v
haftmann@51726
    75
          | NONE => t);
haftmann@51726
    76
    fun mk_def (Abs (x, xT, t), v) =
haftmann@51726
    77
          HOLogic.mk_Trueprop (HOLogic.all_const xT $ Abs (x, xT, HOLogic.mk_eq (v $ Bound 0, t)))
haftmann@51726
    78
      | mk_def (t, v) = HOLogic.mk_Trueprop (HOLogic.mk_eq (v, t));
haftmann@51726
    79
    fun tryext x =
haftmann@51726
    80
      (x RS @{lemma "(\<forall>x. f x = g x) \<Longrightarrow> f = g" by blast} handle THM _ => x);
haftmann@51726
    81
    val cong =
haftmann@51726
    82
      (Goal.prove ctxt'' [] (map mk_def env)
haftmann@51726
    83
        (HOLogic.mk_Trueprop (HOLogic.mk_eq (lhs, replace_fterms rhs)))
haftmann@51726
    84
        (fn {context, prems, ...} =>
haftmann@51726
    85
          Local_Defs.unfold_tac context (map tryext prems) THEN rtac th' 1)) RS sym;
haftmann@51726
    86
    val (cong' :: vars') =
wenzelm@59621
    87
      Variable.export ctxt'' ctxt (cong :: map (Drule.mk_term o Thm.cterm_of ctxt'') vs);
haftmann@51726
    88
    val vs' = map (fst o fst o Term.dest_Var o Thm.term_of o Drule.dest_term) vars';
haftmann@51726
    89
haftmann@51726
    90
  in (vs', cong') end;
haftmann@51726
    91
haftmann@51726
    92
(* congs is a list of pairs (P,th) where th is a theorem for
haftmann@51726
    93
     [| f p1 = A1; ...; f pn = An|] ==> f (C p1 .. pn) = P *)
haftmann@51726
    94
haftmann@51726
    95
fun rearrange congs =
haftmann@51726
    96
  let
haftmann@51726
    97
    fun P (_, th) =
wenzelm@59582
    98
      let val @{term "Trueprop"} $ (Const (@{const_name HOL.eq}, _) $ l $ _) = Thm.concl_of th
haftmann@51726
    99
      in can dest_Var l end;
haftmann@51726
   100
    val (yes, no) = List.partition P congs;
haftmann@51726
   101
  in no @ yes end;
haftmann@51726
   102
haftmann@52275
   103
fun dereify ctxt eqs =
haftmann@52275
   104
  rewrite_with ctxt (eqs @ @{thms nth_Cons_0 nth_Cons_Suc});
haftmann@52275
   105
haftmann@52288
   106
fun index_of t bds =
haftmann@52288
   107
  let
haftmann@52288
   108
    val tt = HOLogic.listT (fastype_of t);
haftmann@52288
   109
  in
haftmann@52288
   110
    (case AList.lookup Type.could_unify bds tt of
haftmann@52288
   111
        NONE => error "index_of: type not found in environements!"
haftmann@52288
   112
      | SOME (tbs, tats) =>
haftmann@52288
   113
          let
haftmann@52288
   114
            val i = find_index (fn t' => t' = t) tats;
haftmann@52288
   115
            val j = find_index (fn t' => t' = t) tbs;
haftmann@52288
   116
          in
haftmann@52288
   117
            if j = ~1 then
haftmann@52288
   118
              if i = ~1
haftmann@52288
   119
              then (length tbs + length tats, AList.update Type.could_unify (tt, (tbs, tats @ [t])) bds)
haftmann@52288
   120
              else (i, bds)
haftmann@52288
   121
            else (j, bds)
haftmann@52288
   122
          end)
haftmann@52288
   123
  end;
haftmann@52288
   124
haftmann@52288
   125
(* Generic decomp for reification : matches the actual term with the
haftmann@52288
   126
   rhs of one cong rule. The result of the matching guides the
haftmann@52288
   127
   proof synthesis: The matches of the introduced Variables A1 .. An are
haftmann@52288
   128
   processed recursively
haftmann@52288
   129
   The rest is instantiated in the cong rule,i.e. no reification is needed *)
haftmann@52288
   130
haftmann@52288
   131
(* da is the decomposition for atoms, ie. it returns ([],g) where g
haftmann@52288
   132
   returns the right instance f (AtC n) = t , where AtC is the Atoms
haftmann@52288
   133
   constructor and n is the number of the atom corresponding to t *)
haftmann@52288
   134
fun decomp_reify da cgns (ct, ctxt) bds =
haftmann@51726
   135
  let
haftmann@52288
   136
    val thy = Proof_Context.theory_of ctxt;
haftmann@52288
   137
    fun tryabsdecomp (ct, ctxt) bds =
haftmann@52288
   138
      (case Thm.term_of ct of
haftmann@52288
   139
        Abs (_, xT, ta) =>
haftmann@52288
   140
          let
haftmann@52288
   141
            val ([raw_xn], ctxt') = Variable.variant_fixes ["x"] ctxt;
haftmann@52288
   142
            val (xn, ta) = Syntax_Trans.variant_abs (raw_xn, xT, ta);  (* FIXME !? *)
haftmann@52288
   143
            val x = Free (xn, xT);
wenzelm@59621
   144
            val cx = Thm.cterm_of ctxt' x;
wenzelm@59621
   145
            val cta = Thm.cterm_of ctxt' ta;
haftmann@52288
   146
            val bds = (case AList.lookup Type.could_unify bds (HOLogic.listT xT) of
haftmann@52288
   147
                NONE => error "tryabsdecomp: Type not found in the Environement"
haftmann@52288
   148
              | SOME (bsT, atsT) => AList.update Type.could_unify (HOLogic.listT xT,
haftmann@52288
   149
                  (x :: bsT, atsT)) bds);
haftmann@52288
   150
           in (([(cta, ctxt')],
haftmann@52288
   151
                fn ([th], bds) =>
haftmann@52288
   152
                  (hd (Variable.export ctxt' ctxt [(Thm.forall_intr cx th) COMP allI]),
haftmann@52288
   153
                   let
haftmann@52288
   154
                     val (bsT, asT) = the (AList.lookup Type.could_unify bds (HOLogic.listT xT));
haftmann@52288
   155
                   in
haftmann@52288
   156
                     AList.update Type.could_unify (HOLogic.listT xT, (tl bsT, asT)) bds
haftmann@52288
   157
                   end)),
haftmann@52288
   158
               bds)
haftmann@52288
   159
           end
haftmann@52288
   160
       | _ => da (ct, ctxt) bds)
haftmann@52288
   161
  in
haftmann@52288
   162
    (case cgns of
haftmann@52288
   163
      [] => tryabsdecomp (ct, ctxt) bds
haftmann@52288
   164
    | ((vns, cong) :: congs) =>
haftmann@52288
   165
        (let
haftmann@52288
   166
          val (tyenv, tmenv) =
haftmann@52288
   167
            Pattern.match thy
wenzelm@59582
   168
              ((fst o HOLogic.dest_eq o HOLogic.dest_Trueprop) (Thm.concl_of cong), Thm.term_of ct)
haftmann@52288
   169
              (Vartab.empty, Vartab.empty);
haftmann@52288
   170
          val (fnvs, invs) = List.partition (fn ((vn, _),_) => member (op =) vns vn) (Vartab.dest tmenv);
haftmann@52288
   171
          val (fts, its) =
haftmann@52288
   172
            (map (snd o snd) fnvs,
wenzelm@60642
   173
             map (fn ((vn, vi), (tT, t)) => (((vn, vi), tT), Thm.cterm_of ctxt t)) invs);
wenzelm@59617
   174
          val ctyenv =
wenzelm@60642
   175
            map (fn ((vn, vi), (s, ty)) => (((vn, vi), s), Thm.ctyp_of ctxt ty))
wenzelm@59617
   176
              (Vartab.dest tyenv);
haftmann@52288
   177
        in
wenzelm@59642
   178
          ((map (Thm.cterm_of ctxt) fts ~~ replicate (length fts) ctxt,
haftmann@52288
   179
             apfst (FWD (Drule.instantiate_normalize (ctyenv, its) cong))), bds)
haftmann@52288
   180
        end handle Pattern.MATCH => decomp_reify da congs (ct, ctxt) bds))
haftmann@52288
   181
  end;
haftmann@52288
   182
haftmann@52288
   183
fun get_nths (t as (Const (@{const_name "List.nth"}, _) $ vs $ n)) =
haftmann@52288
   184
      AList.update (op aconv) (t, (vs, n))
haftmann@52288
   185
  | get_nths (t1 $ t2) = get_nths t1 #> get_nths t2
haftmann@52288
   186
  | get_nths (Abs (_, _, t')) = get_nths t'
haftmann@52288
   187
  | get_nths _ = I;
haftmann@52288
   188
haftmann@52288
   189
fun tryeqs [] (ct, ctxt) bds = error "Cannot find the atoms equation"
haftmann@52288
   190
  | tryeqs (eq :: eqs) (ct, ctxt) bds = ((
haftmann@51726
   191
      let
wenzelm@59582
   192
        val rhs = eq |> Thm.prop_of |> HOLogic.dest_Trueprop  |> HOLogic.dest_eq |> snd;
haftmann@52288
   193
        val nths = get_nths rhs [];
haftmann@52288
   194
        val (vss, _) = fold_rev (fn (_, (vs, n)) => fn (vss, ns) =>
haftmann@52288
   195
          (insert (op aconv) vs vss, insert (op aconv) n ns)) nths ([], []);
haftmann@52288
   196
        val (vsns, ctxt') = Variable.variant_fixes (replicate (length vss) "vs") ctxt;
haftmann@52288
   197
        val (xns, ctxt'') = Variable.variant_fixes (replicate (length nths) "x") ctxt';
haftmann@52288
   198
        val thy = Proof_Context.theory_of ctxt'';
haftmann@52288
   199
        val vsns_map = vss ~~ vsns;
haftmann@52288
   200
        val xns_map = fst (split_list nths) ~~ xns;
haftmann@52288
   201
        val subst = map (fn (nt, xn) => (nt, Var ((xn, 0), fastype_of nt))) xns_map;
haftmann@52288
   202
        val rhs_P = subst_free subst rhs;
haftmann@52288
   203
        val (tyenv, tmenv) = Pattern.match thy (rhs_P, Thm.term_of ct) (Vartab.empty, Vartab.empty);
haftmann@52288
   204
        val sbst = Envir.subst_term (tyenv, tmenv);
haftmann@52288
   205
        val sbsT = Envir.subst_type tyenv;
wenzelm@59617
   206
        val subst_ty =
wenzelm@60642
   207
          map (fn (n, (s, t)) => ((n, s), Thm.ctyp_of ctxt'' t)) (Vartab.dest tyenv)
haftmann@52288
   208
        val tml = Vartab.dest tmenv;
haftmann@52288
   209
        val (subst_ns, bds) = fold_map
haftmann@52288
   210
          (fn (Const _ $ _ $ n, Var (xn0, _)) => fn bds =>
haftmann@52288
   211
            let
haftmann@52288
   212
              val name = snd (the (AList.lookup (op =) tml xn0));
haftmann@52288
   213
              val (idx, bds) = index_of name bds;
wenzelm@59642
   214
            in (apply2 (Thm.cterm_of ctxt'') (n, idx |> HOLogic.mk_nat), bds) end) subst bds;
haftmann@52288
   215
        val subst_vs =
haftmann@51726
   216
          let
haftmann@52288
   217
            fun h (Const _ $ (vs as Var (_, lT)) $ _, Var (_, T)) =
haftmann@51726
   218
              let
haftmann@52288
   219
                val cns = sbst (Const (@{const_name "List.Cons"}, T --> lT --> lT));
haftmann@52288
   220
                val lT' = sbsT lT;
haftmann@52288
   221
                val (bsT, _) = the (AList.lookup Type.could_unify bds lT);
haftmann@52288
   222
                val vsn = the (AList.lookup (op =) vsns_map vs);
wenzelm@59617
   223
                val vs' = fold_rev (fn x => fn xs => cns $ x $xs) bsT (Free (vsn, lT'));
wenzelm@59642
   224
              in apply2 (Thm.cterm_of ctxt'') (vs, vs') end;
haftmann@52288
   225
          in map h subst end;
wenzelm@59617
   226
        val cts =
wenzelm@59642
   227
          map (fn ((vn, vi), (tT, t)) => apply2 (Thm.cterm_of ctxt'') (Var ((vn, vi), tT), t))
wenzelm@59617
   228
            (fold (AList.delete (fn (((a : string), _), (b, _)) => a = b))
wenzelm@59617
   229
              (map (fn n => (n, 0)) xns) tml);
haftmann@52288
   230
        val substt =
haftmann@52288
   231
          let
haftmann@52288
   232
            val ih = Drule.cterm_rule (Thm.instantiate (subst_ty, []));
wenzelm@59058
   233
          in map (apply2 ih) (subst_ns @ subst_vs @ cts) end;
wenzelm@60642
   234
        val th =
wenzelm@60642
   235
          (Drule.instantiate_normalize (subst_ty, map (apfst (dest_Var o Thm.term_of)) substt) eq)
wenzelm@60642
   236
            RS sym;
haftmann@52288
   237
      in (hd (Variable.export ctxt'' ctxt [th]), bds) end)
haftmann@52288
   238
      handle Pattern.MATCH => tryeqs eqs (ct, ctxt) bds);
haftmann@52288
   239
haftmann@52288
   240
(* looks for the atoms equation and instantiates it with the right number *)
haftmann@51726
   241
haftmann@52288
   242
fun mk_decompatom eqs (ct, ctxt) bds = (([], fn (_, bds) =>
haftmann@52288
   243
  let
haftmann@52288
   244
    val tT = fastype_of (Thm.term_of ct);
haftmann@52288
   245
    fun isat eq =
haftmann@51726
   246
      let
wenzelm@59582
   247
        val rhs = eq |> Thm.prop_of |> HOLogic.dest_Trueprop |> HOLogic.dest_eq |> snd;
haftmann@52288
   248
      in exists_Const
haftmann@52288
   249
        (fn (n, ty) => n = @{const_name "List.nth"}
haftmann@52288
   250
          andalso AList.defined Type.could_unify bds (domain_type ty)) rhs
haftmann@52288
   251
          andalso Type.could_unify (fastype_of rhs, tT)
haftmann@52288
   252
      end;
haftmann@52288
   253
  in tryeqs (filter isat eqs) (ct, ctxt) bds end), bds);
haftmann@52288
   254
haftmann@52288
   255
(* Generic reification procedure: *)
haftmann@52288
   256
(* creates all needed cong rules and then just uses the theorem synthesis *)
haftmann@51726
   257
haftmann@52288
   258
fun mk_congs ctxt eqs =
haftmann@52288
   259
  let
wenzelm@59582
   260
    val fs = fold_rev (fn eq => insert (op =) (eq |> Thm.prop_of |> HOLogic.dest_Trueprop
haftmann@52288
   261
      |> HOLogic.dest_eq |> fst |> strip_comb
haftmann@52288
   262
      |> fst)) eqs [];
haftmann@52288
   263
    val tys = fold_rev (fn f => fold (insert (op =)) (f |> fastype_of |> binder_types |> tl)) fs [];
haftmann@52288
   264
    val (vs, ctxt') = Variable.variant_fixes (replicate (length tys) "vs") ctxt;
haftmann@52288
   265
    val subst =
wenzelm@59617
   266
      the o AList.lookup (op =)
wenzelm@59621
   267
        (map2 (fn T => fn v => (T, Thm.cterm_of ctxt' (Free (v, T)))) tys vs);
haftmann@52288
   268
    fun prep_eq eq =
haftmann@52288
   269
      let
wenzelm@59582
   270
        val (_, _ :: vs) = eq |> Thm.prop_of |> HOLogic.dest_Trueprop
haftmann@52288
   271
          |> HOLogic.dest_eq |> fst |> strip_comb;
wenzelm@60642
   272
        val subst = map_filter (fn Var v => SOME (v, subst (#2 v)) | _ => NONE) vs;
haftmann@52288
   273
      in Thm.instantiate ([], subst) eq end;
haftmann@52288
   274
    val (ps, congs) = map_split (mk_congeq ctxt' fs o prep_eq) eqs;
haftmann@52288
   275
    val bds = AList.make (K ([], [])) tys;
haftmann@52288
   276
  in (ps ~~ Variable.export ctxt' ctxt congs, bds) end
haftmann@52288
   277
haftmann@52288
   278
fun conv ctxt eqs ct =
haftmann@52288
   279
  let
haftmann@51726
   280
    val (congs, bds) = mk_congs ctxt eqs;
haftmann@51726
   281
    val congs = rearrange congs;
wenzelm@59058
   282
    val (th, bds') =
wenzelm@59058
   283
      apfst mk_eq (divide_and_conquer' (decomp_reify (mk_decompatom eqs) congs) (ct, ctxt) bds);
haftmann@52273
   284
    fun is_list_var (Var (_, t)) = can dest_listT t
haftmann@52273
   285
      | is_list_var _ = false;
wenzelm@59582
   286
    val vars = th |> Thm.prop_of |> Logic.dest_equals |> snd
haftmann@52273
   287
      |> strip_comb |> snd |> filter is_list_var;
wenzelm@60642
   288
    val vs = map (fn Var (v as (_, T)) =>
haftmann@52275
   289
      (v, the (AList.lookup Type.could_unify bds' T) |> snd |> HOLogic.mk_list (dest_listT T))) vars;
wenzelm@60642
   290
    val th' =
wenzelm@60642
   291
      Drule.instantiate_normalize ([], map (apsnd (Thm.cterm_of ctxt)) vs) th;
haftmann@52275
   292
    val th'' = Thm.symmetric (dereify ctxt [] (Thm.lhs_of th'));
haftmann@52275
   293
  in Thm.transitive th'' th' end;
haftmann@51726
   294
haftmann@52286
   295
fun tac ctxt eqs =
haftmann@52286
   296
  lift_conv ctxt (conv ctxt eqs);
haftmann@51726
   297
haftmann@52276
   298
end;