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