src/Provers/IsaPlanner/rw_inst.ML
author wenzelm
Thu Dec 07 00:42:04 2006 +0100 (2006-12-07)
changeset 21687 f689f729afab
parent 20664 ffbc5a57191a
child 22578 b0eb5652f210
permissions -rw-r--r--
reorganized structure Goal vs. Tactic;
dixon@19835
     1
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
dixon@19835
     2
(*  Title:      Pure/IsaPlanner/rw_inst.ML
dixon@19835
     3
    ID:         $Id$
dixon@19835
     4
    Author:     Lucas Dixon, University of Edinburgh
dixon@19835
     5
                lucas.dixon@ed.ac.uk
dixon@19835
     6
    Created:    25 Aug 2004
dixon@19835
     7
*)
dixon@19835
     8
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *) 
dixon@19835
     9
(*  DESCRIPTION:
dixon@19835
    10
dixon@19835
    11
    rewriting using a conditional meta-equality theorem which supports 
dixon@19835
    12
    schematic variable instantiation.
dixon@19835
    13
dixon@19835
    14
*)   
dixon@19835
    15
(* -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- *)
dixon@19835
    16
signature RW_INST =
dixon@19835
    17
sig
dixon@19835
    18
dixon@19835
    19
  (* Rewrite: give it instantiation infromation, a rule, and the
dixon@19835
    20
  target thm, and it will return the rewritten target thm *)
dixon@19835
    21
  val rw :
dixon@19835
    22
      ((Term.indexname * (Term.sort * Term.typ)) list *  (* type var instantiations *)
dixon@19835
    23
       (Term.indexname * (Term.typ * Term.term)) list)  (* schematic var instantiations *)
dixon@19835
    24
      * (string * Term.typ) list           (* Fake named bounds + types *)
dixon@19835
    25
      * (string * Term.typ) list           (* names of bound + types *)
dixon@19835
    26
      * Term.term ->                       (* outer term for instantiation *)
dixon@19835
    27
      Thm.thm ->                           (* rule with indexies lifted *)
dixon@19835
    28
      Thm.thm ->                           (* target thm *)
dixon@19835
    29
      Thm.thm                              (* rewritten theorem possibly 
dixon@19835
    30
                                              with additional premises for 
dixon@19835
    31
                                              rule conditions *)
dixon@19835
    32
dixon@19835
    33
  (* used tools *)
dixon@19835
    34
  val mk_abstractedrule :
dixon@19835
    35
      (string * Term.typ) list (* faked outer bound *)
dixon@19835
    36
      -> (string * Term.typ) list (* hopeful name of outer bounds *)
dixon@19835
    37
      -> Thm.thm -> Thm.cterm list * Thm.thm
dixon@19835
    38
  val mk_fixtvar_tyinsts :
dixon@19835
    39
      (Term.indexname * (Term.sort * Term.typ)) list ->
dixon@19835
    40
      Term.term list -> ((string * int) * (Term.sort * Term.typ)) list 
dixon@19835
    41
                        * (string * Term.sort) list
dixon@19835
    42
  val mk_renamings :
dixon@19835
    43
      Term.term -> Thm.thm -> (((string * int) * Term.typ) * Term.term) list
dixon@19835
    44
  val new_tfree :
dixon@19835
    45
      ((string * int) * Term.sort) *
dixon@19835
    46
      (((string * int) * (Term.sort * Term.typ)) list * string list) ->
dixon@19835
    47
      ((string * int) * (Term.sort * Term.typ)) list * string list
dixon@19835
    48
  val cross_inst : (Term.indexname * (Term.typ * Term.term)) list 
dixon@19835
    49
                   -> (Term.indexname *(Term.typ * Term.term)) list
dixon@19835
    50
  val cross_inst_typs : (Term.indexname * (Term.sort * Term.typ)) list 
dixon@19835
    51
                   -> (Term.indexname * (Term.sort * Term.typ)) list
dixon@19835
    52
dixon@19835
    53
  val beta_contract : Thm.thm -> Thm.thm
dixon@19835
    54
  val beta_eta_contract : Thm.thm -> Thm.thm
dixon@19835
    55
dixon@19835
    56
end;
dixon@19835
    57
dixon@19835
    58
structure RWInst 
dixon@19835
    59
: RW_INST
dixon@19835
    60
= struct
dixon@19835
    61
dixon@19835
    62
dixon@19835
    63
(* beta contract the theorem *)
dixon@19835
    64
fun beta_contract thm = 
dixon@19835
    65
    equal_elim (Thm.beta_conversion true (Thm.cprop_of thm)) thm;
dixon@19835
    66
dixon@19835
    67
(* beta-eta contract the theorem *)
dixon@19835
    68
fun beta_eta_contract thm = 
dixon@19835
    69
    let
dixon@19835
    70
      val thm2 = equal_elim (Thm.beta_conversion true (Thm.cprop_of thm)) thm
dixon@19835
    71
      val thm3 = equal_elim (Thm.eta_conversion (Thm.cprop_of thm2)) thm2
dixon@19835
    72
    in thm3 end;
dixon@19835
    73
dixon@19835
    74
dixon@19835
    75
(* to get the free names of a theorem (including hyps and flexes) *)
dixon@19835
    76
fun usednames_of_thm th =
dixon@19835
    77
    let val rep = Thm.rep_thm th
dixon@19835
    78
      val hyps = #hyps rep
dixon@19835
    79
      val (tpairl,tpairr) = Library.split_list (#tpairs rep)
dixon@19835
    80
      val prop = #prop rep
dixon@19835
    81
    in
dixon@19835
    82
      List.foldr Term.add_term_names [] (prop :: (tpairl @ (tpairr @ hyps)))
dixon@19835
    83
    end;
dixon@19835
    84
dixon@19835
    85
(* Given a list of variables that were bound, and a that has been
dixon@19835
    86
instantiated with free variable placeholders for the bound vars, it
dixon@19835
    87
creates an abstracted version of the theorem, with local bound vars as
dixon@19835
    88
lambda-params:
dixon@19835
    89
dixon@19835
    90
Ts: 
dixon@19835
    91
("x", ty)
dixon@19835
    92
dixon@19835
    93
rule::
dixon@19835
    94
C :x ==> P :x = Q :x
dixon@19835
    95
dixon@19835
    96
results in:
dixon@19835
    97
("!! x. C x", (%x. p x = %y. p y) [!! x. C x])
dixon@19835
    98
dixon@19835
    99
note: assumes rule is instantiated
dixon@19835
   100
*)
dixon@19835
   101
(* Note, we take abstraction in the order of last abstraction first *)
dixon@19835
   102
fun mk_abstractedrule TsFake Ts rule = 
dixon@19835
   103
    let 
dixon@19835
   104
      val ctermify = Thm.cterm_of (Thm.sign_of_thm rule);
dixon@19835
   105
dixon@19835
   106
      (* now we change the names of temporary free vars that represent 
dixon@19835
   107
         bound vars with binders outside the redex *)
dixon@19835
   108
      val prop = Thm.prop_of rule;
dixon@19835
   109
      val names = usednames_of_thm rule;
dixon@19835
   110
      val (fromnames,tonames,names2,Ts') = 
dixon@19835
   111
          Library.foldl (fn ((rnf,rnt,names, Ts''),((faken,_),(n,ty))) => 
wenzelm@20071
   112
                    let val n2 = Name.variant names n in
dixon@19835
   113
                      (ctermify (Free(faken,ty)) :: rnf,
dixon@19835
   114
                       ctermify (Free(n2,ty)) :: rnt, 
dixon@19835
   115
                       n2 :: names,
dixon@19835
   116
                       (n2,ty) :: Ts'')
dixon@19835
   117
                    end)
dixon@19835
   118
                (([],[],names, []), TsFake~~Ts);
dixon@19835
   119
dixon@19835
   120
      (* rename conflicting free's in the rule to avoid cconflicts
dixon@19835
   121
      with introduced vars from bounds outside in redex *)
dixon@19835
   122
      val rule' = rule |> Drule.forall_intr_list fromnames
dixon@19835
   123
                       |> Drule.forall_elim_list tonames;
dixon@19835
   124
      
dixon@19835
   125
      (* make unconditional rule and prems *)
dixon@19835
   126
      val (uncond_rule, cprems) = IsaND.allify_conditions ctermify (rev Ts') 
dixon@19835
   127
                                                          rule';
dixon@19835
   128
dixon@19835
   129
      (* using these names create lambda-abstracted version of the rule *)
dixon@19835
   130
      val abstractions = rev (Ts' ~~ tonames);
dixon@19835
   131
      val abstract_rule = Library.foldl (fn (th,((n,ty),ct)) => 
dixon@19835
   132
                                    Thm.abstract_rule n ct th)
dixon@19835
   133
                                (uncond_rule, abstractions);
dixon@19835
   134
    in (cprems, abstract_rule) end;
dixon@19835
   135
dixon@19835
   136
dixon@19835
   137
(* given names to avoid, and vars that need to be fixed, it gives
dixon@19835
   138
unique new names to the vars so that they can be fixed as free
dixon@19835
   139
variables *)
dixon@19835
   140
(* make fixed unique free variable instantiations for non-ground vars *)
dixon@19835
   141
(* Create a table of vars to be renamed after instantiation - ie
dixon@19835
   142
      other uninstantiated vars in the hyps of the rule 
dixon@19835
   143
      ie ?z in C ?z ?x ==> A ?x ?y = B ?x ?y *)
dixon@19835
   144
fun mk_renamings tgt rule_inst = 
dixon@19835
   145
    let
dixon@19835
   146
      val rule_conds = Thm.prems_of rule_inst
dixon@19835
   147
      val names = foldr Term.add_term_names [] (tgt :: rule_conds);
dixon@19835
   148
      val (conds_tyvs,cond_vs) = 
dixon@19835
   149
          Library.foldl (fn ((tyvs, vs), t) => 
dixon@19835
   150
                    (Library.union
dixon@19835
   151
                       (Term.term_tvars t, tyvs),
dixon@19835
   152
                     Library.union 
dixon@19835
   153
                       (map Term.dest_Var (Term.term_vars t), vs))) 
dixon@19835
   154
                (([],[]), rule_conds);
dixon@19835
   155
      val termvars = map Term.dest_Var (Term.term_vars tgt); 
dixon@19835
   156
      val vars_to_fix = Library.union (termvars, cond_vs);
dixon@19835
   157
      val (renamings, names2) = 
dixon@19835
   158
          foldr (fn (((n,i),ty), (vs, names')) => 
wenzelm@20071
   159
                    let val n' = Name.variant names' n in
dixon@19835
   160
                      ((((n,i),ty), Free (n', ty)) :: vs, n'::names')
dixon@19835
   161
                    end)
dixon@19835
   162
                ([], names) vars_to_fix;
dixon@19835
   163
    in renamings end;
dixon@19835
   164
dixon@19835
   165
(* make a new fresh typefree instantiation for the given tvar *)
dixon@19835
   166
fun new_tfree (tv as (ix,sort), (pairs,used)) =
wenzelm@20071
   167
      let val v = Name.variant used (string_of_indexname ix)
dixon@19835
   168
      in  ((ix,(sort,TFree(v,sort)))::pairs, v::used)  end;
dixon@19835
   169
dixon@19835
   170
dixon@19835
   171
(* make instantiations to fix type variables that are not 
dixon@19835
   172
   already instantiated (in ignore_ixs) from the list of terms. *)
dixon@19835
   173
fun mk_fixtvar_tyinsts ignore_insts ts = 
dixon@19835
   174
    let 
dixon@19835
   175
      val ignore_ixs = map fst ignore_insts;
dixon@19835
   176
      val (tvars, tfrees) = 
dixon@19835
   177
            foldr (fn (t, (varixs, tfrees)) => 
dixon@19835
   178
                      (Term.add_term_tvars (t,varixs),
dixon@19835
   179
                       Term.add_term_tfrees (t,tfrees)))
dixon@19835
   180
                  ([],[]) ts;
dixon@19835
   181
        val unfixed_tvars = 
wenzelm@20664
   182
            List.filter (fn (ix,s) => not (member (op =) ignore_ixs ix)) tvars;
dixon@19835
   183
        val (fixtyinsts, _) = foldr new_tfree ([], map fst tfrees) unfixed_tvars
dixon@19835
   184
    in (fixtyinsts, tfrees) end;
dixon@19835
   185
dixon@19835
   186
dixon@19835
   187
(* cross-instantiate the instantiations - ie for each instantiation
dixon@19835
   188
replace all occurances in other instantiations - no loops are possible
dixon@19835
   189
and thus only one-parsing of the instantiations is necessary. *)
dixon@19835
   190
fun cross_inst insts = 
dixon@19835
   191
    let 
dixon@19835
   192
      fun instL (ix, (ty,t)) = 
dixon@19835
   193
          map (fn (ix2,(ty2,t2)) => 
dixon@19835
   194
                  (ix2, (ty2,Term.subst_vars ([], [(ix, t)]) t2)));
dixon@19835
   195
dixon@19835
   196
      fun cross_instL ([], l) = rev l
dixon@19835
   197
        | cross_instL ((ix, t) :: insts, l) = 
dixon@19835
   198
          cross_instL (instL (ix, t) insts, (ix, t) :: (instL (ix, t) l));
dixon@19835
   199
dixon@19835
   200
    in cross_instL (insts, []) end;
dixon@19835
   201
dixon@19835
   202
(* as above but for types -- I don't know if this is needed, will we ever incur mixed up types? *)
dixon@19835
   203
fun cross_inst_typs insts = 
dixon@19835
   204
    let 
dixon@19835
   205
      fun instL (ix, (srt,ty)) = 
dixon@19835
   206
          map (fn (ix2,(srt2,ty2)) => 
dixon@19835
   207
                  (ix2, (srt2,Term.typ_subst_TVars [(ix, ty)] ty2)));
dixon@19835
   208
dixon@19835
   209
      fun cross_instL ([], l) = rev l
dixon@19835
   210
        | cross_instL ((ix, t) :: insts, l) = 
dixon@19835
   211
          cross_instL (instL (ix, t) insts, (ix, t) :: (instL (ix, t) l));
dixon@19835
   212
dixon@19835
   213
    in cross_instL (insts, []) end;
dixon@19835
   214
dixon@19835
   215
dixon@19835
   216
(* assume that rule and target_thm have distinct var names. THINK:
dixon@19835
   217
efficient version with tables for vars for: target vars, introduced
dixon@19835
   218
vars, and rule vars, for quicker instantiation?  The outerterm defines
dixon@19835
   219
which part of the target_thm was modified.  Note: we take Ts in the
dixon@19835
   220
upterm order, ie last abstraction first., and with an outeterm where
dixon@19835
   221
the abstracted subterm has the arguments in the revered order, ie
dixon@19835
   222
first abstraction first.  FakeTs has abstractions using the fake name
dixon@19835
   223
- ie the name distinct from all other abstractions. *)
dixon@19835
   224
dixon@19835
   225
fun rw ((nonfixed_typinsts, unprepinsts), FakeTs, Ts, outerterm) rule target_thm = 
dixon@19835
   226
    let 
dixon@19835
   227
      (* general signature info *)
dixon@19835
   228
      val target_sign = (Thm.sign_of_thm target_thm);
dixon@19835
   229
      val ctermify = Thm.cterm_of target_sign;
dixon@19835
   230
      val ctypeify = Thm.ctyp_of target_sign;
dixon@19835
   231
dixon@19835
   232
      (* fix all non-instantiated tvars *)
dixon@19835
   233
      val (fixtyinsts, othertfrees) = 
dixon@19835
   234
          mk_fixtvar_tyinsts nonfixed_typinsts
dixon@19835
   235
                             [Thm.prop_of rule, Thm.prop_of target_thm];
dixon@19835
   236
      val new_fixed_typs = map (fn ((s,i),(srt,ty)) => (Term.dest_TFree ty))
dixon@19835
   237
                               fixtyinsts;
dixon@19835
   238
      val typinsts = cross_inst_typs (nonfixed_typinsts @ fixtyinsts);
dixon@19835
   239
dixon@19835
   240
      (* certified instantiations for types *)
dixon@19835
   241
      val ctyp_insts = 
dixon@19835
   242
          map (fn (ix,(s,ty)) => (ctypeify (TVar (ix,s)), ctypeify ty)) 
dixon@19835
   243
              typinsts;
dixon@19835
   244
dixon@19835
   245
      (* type instantiated versions *)
dixon@19835
   246
      val tgt_th_tyinst = Thm.instantiate (ctyp_insts,[]) target_thm;
dixon@19835
   247
      val rule_tyinst =  Thm.instantiate (ctyp_insts,[]) rule;
dixon@19835
   248
dixon@19835
   249
      val term_typ_inst = map (fn (ix,(srt,ty)) => (ix,ty)) typinsts;
dixon@19835
   250
      (* type instanitated outer term *)
dixon@19835
   251
      val outerterm_tyinst = Term.subst_TVars term_typ_inst outerterm;
dixon@19835
   252
dixon@19835
   253
      val FakeTs_tyinst = map (apsnd (Term.typ_subst_TVars term_typ_inst)) 
dixon@19835
   254
                              FakeTs;
dixon@19835
   255
      val Ts_tyinst = map (apsnd (Term.typ_subst_TVars term_typ_inst)) 
dixon@19835
   256
                          Ts;
dixon@19835
   257
dixon@19835
   258
      (* type-instantiate the var instantiations *)
dixon@19835
   259
      val insts_tyinst = foldr (fn ((ix,(ty,t)),insts_tyinst) => 
dixon@19835
   260
                            (ix, (Term.typ_subst_TVars term_typ_inst ty, 
dixon@19835
   261
                                  Term.subst_TVars term_typ_inst t))
dixon@19835
   262
                            :: insts_tyinst)
dixon@19835
   263
                        [] unprepinsts;
dixon@19835
   264
dixon@19835
   265
      (* cross-instantiate *)
dixon@19835
   266
      val insts_tyinst_inst = cross_inst insts_tyinst;
dixon@19835
   267
dixon@19835
   268
      (* create certms of instantiations *)
dixon@19835
   269
      val cinsts_tyinst = 
dixon@19835
   270
          map (fn (ix,(ty,t)) => (ctermify (Var (ix, ty)), 
dixon@19835
   271
                                  ctermify t)) insts_tyinst_inst;
dixon@19835
   272
dixon@19835
   273
      (* The instantiated rule *)
dixon@19835
   274
      val rule_inst = rule_tyinst |> Thm.instantiate ([], cinsts_tyinst);
dixon@19835
   275
dixon@19835
   276
      (* Create a table of vars to be renamed after instantiation - ie
dixon@19835
   277
      other uninstantiated vars in the hyps the *instantiated* rule 
dixon@19835
   278
      ie ?z in C ?z ?x ==> A ?x ?y = B ?x ?y *)
dixon@19835
   279
      val renamings = mk_renamings (Thm.prop_of tgt_th_tyinst) 
dixon@19835
   280
                                   rule_inst;
dixon@19835
   281
      val cterm_renamings = 
dixon@19835
   282
          map (fn (x,y) => (ctermify (Var x), ctermify y)) renamings;
dixon@19835
   283
dixon@19835
   284
      (* Create the specific version of the rule for this target application *)
dixon@19835
   285
      val outerterm_inst = 
dixon@19835
   286
          outerterm_tyinst 
dixon@19835
   287
            |> Term.subst_Vars (map (fn (ix,(ty,t)) => (ix,t)) insts_tyinst_inst)
dixon@19835
   288
            |> Term.subst_Vars (map (fn ((ix,ty),t) => (ix,t)) renamings);
dixon@19835
   289
      val couter_inst = Thm.reflexive (ctermify outerterm_inst);
dixon@19835
   290
      val (cprems, abstract_rule_inst) = 
dixon@19835
   291
          rule_inst |> Thm.instantiate ([], cterm_renamings)
dixon@19835
   292
                    |> mk_abstractedrule FakeTs_tyinst Ts_tyinst;
dixon@19835
   293
      val specific_tgt_rule = 
dixon@19835
   294
          beta_eta_contract
dixon@19835
   295
            (Thm.combination couter_inst abstract_rule_inst);
dixon@19835
   296
dixon@19835
   297
      (* create an instantiated version of the target thm *)
dixon@19835
   298
      val tgt_th_inst = 
dixon@19835
   299
          tgt_th_tyinst |> Thm.instantiate ([], cinsts_tyinst)
dixon@19835
   300
                        |> Thm.instantiate ([], cterm_renamings);
dixon@19835
   301
dixon@19835
   302
      val (vars,frees_of_fixed_vars) = Library.split_list cterm_renamings;
dixon@19835
   303
dixon@19835
   304
    in
dixon@19835
   305
      (beta_eta_contract tgt_th_inst)
dixon@19835
   306
        |> Thm.equal_elim specific_tgt_rule
dixon@19835
   307
        |> Drule.implies_intr_list cprems
dixon@19835
   308
        |> Drule.forall_intr_list frees_of_fixed_vars
dixon@19835
   309
        |> Drule.forall_elim_list vars
dixon@19835
   310
        |> Thm.varifyT' othertfrees
dixon@19835
   311
        |-> K Drule.zero_var_indexes
dixon@19835
   312
    end;
dixon@19835
   313
dixon@19835
   314
dixon@19835
   315
end; (* struct *)