src/Pure/drule.ML
changeset 15570 8d8c70b41bab
parent 15545 0efa7126003f
child 15574 b1d1b5bfc464
equal deleted inserted replaced
15569:1b3115d1a8df 15570:8d8c70b41bab
   219 
   219 
   220 fun read_insts sign (rtypes,rsorts) (types,sorts) used insts =
   220 fun read_insts sign (rtypes,rsorts) (types,sorts) used insts =
   221 let
   221 let
   222     fun is_tv ((a, _), _) =
   222     fun is_tv ((a, _), _) =
   223       (case Symbol.explode a of "'" :: _ => true | _ => false);
   223       (case Symbol.explode a of "'" :: _ => true | _ => false);
   224     val (tvs, vs) = partition is_tv insts;
   224     val (tvs, vs) = List.partition is_tv insts;
   225     fun readT (ixn, st) =
   225     fun readT (ixn, st) =
   226         let val S = case rsorts ixn of SOME S => S | NONE => absent ixn;
   226         let val S = case rsorts ixn of SOME S => S | NONE => absent ixn;
   227             val T = Sign.read_typ (sign,sorts) st;
   227             val T = Sign.read_typ (sign,sorts) st;
   228         in if Sign.typ_instance sign (T, TVar(ixn,S)) then (ixn,T)
   228         in if Sign.typ_instance sign (T, TVar(ixn,S)) then (ixn,T)
   229            else inst_failure ixn
   229            else inst_failure ixn
   338 fun gen_all thm =
   338 fun gen_all thm =
   339   let
   339   let
   340     val {sign, prop, maxidx, ...} = Thm.rep_thm thm;
   340     val {sign, prop, maxidx, ...} = Thm.rep_thm thm;
   341     fun elim (th, (x, T)) = Thm.forall_elim (Thm.cterm_of sign (Var ((x, maxidx + 1), T))) th;
   341     fun elim (th, (x, T)) = Thm.forall_elim (Thm.cterm_of sign (Var ((x, maxidx + 1), T))) th;
   342     val vs = Term.strip_all_vars prop;
   342     val vs = Term.strip_all_vars prop;
   343   in foldl elim (thm, Term.variantlist (map #1 vs, []) ~~ map #2 vs) end;
   343   in Library.foldl elim (thm, Term.variantlist (map #1 vs, []) ~~ map #2 vs) end;
   344 
   344 
   345 (*Specialization over a list of cterms*)
   345 (*Specialization over a list of cterms*)
   346 fun forall_elim_list cts th = foldr (uncurry forall_elim) (rev cts, th);
   346 fun forall_elim_list cts th = Library.foldr (uncurry forall_elim) (rev cts, th);
   347 
   347 
   348 (* maps A1,...,An |- B   to   [| A1;...;An |] ==> B  *)
   348 (* maps A1,...,An |- B   to   [| A1;...;An |] ==> B  *)
   349 fun implies_intr_list cAs th = foldr (uncurry implies_intr) (cAs,th);
   349 fun implies_intr_list cAs th = Library.foldr (uncurry implies_intr) (cAs,th);
   350 
   350 
   351 (* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
   351 (* maps [| A1;...;An |] ==> B and [A1,...,An]   to   B *)
   352 fun implies_elim_list impth ths = foldl (uncurry implies_elim) (impth,ths);
   352 fun implies_elim_list impth ths = Library.foldl (uncurry implies_elim) (impth,ths);
   353 
   353 
   354 (* maps |- B to A1,...,An |- B *)
   354 (* maps |- B to A1,...,An |- B *)
   355 fun impose_hyps chyps th =
   355 fun impose_hyps chyps th =
   356   let val chyps' = gen_rems (op aconv o apfst Thm.term_of) (chyps, #hyps (Thm.rep_thm th))
   356   let val chyps' = gen_rems (op aconv o apfst Thm.term_of) (chyps, #hyps (Thm.rep_thm th))
   357   in implies_elim_list (implies_intr_list chyps' th) (map Thm.assume chyps') end;
   357   in implies_elim_list (implies_intr_list chyps' th) (map Thm.assume chyps') end;
   362 
   362 
   363 (*Reset Var indexes to zero, renaming to preserve distinctness*)
   363 (*Reset Var indexes to zero, renaming to preserve distinctness*)
   364 fun zero_var_indexes th =
   364 fun zero_var_indexes th =
   365     let val {prop,sign,tpairs,...} = rep_thm th;
   365     let val {prop,sign,tpairs,...} = rep_thm th;
   366         val (tpair_l, tpair_r) = Library.split_list tpairs;
   366         val (tpair_l, tpair_r) = Library.split_list tpairs;
   367         val vars = foldr add_term_vars 
   367         val vars = Library.foldr add_term_vars 
   368                          (tpair_r, foldr add_term_vars (tpair_l, (term_vars prop)));
   368                          (tpair_r, Library.foldr add_term_vars (tpair_l, (term_vars prop)));
   369         val bs = foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
   369         val bs = Library.foldl add_new_id ([], map (fn Var((a,_),_)=>a) vars)
   370         val inrs = 
   370         val inrs = 
   371             foldr add_term_tvars 
   371             Library.foldr add_term_tvars 
   372                   (tpair_r, foldr add_term_tvars (tpair_l, (term_tvars prop)));
   372                   (tpair_r, Library.foldr add_term_tvars (tpair_l, (term_tvars prop)));
   373         val nms' = rev(foldl add_new_id ([], map (#1 o #1) inrs));
   373         val nms' = rev(Library.foldl add_new_id ([], map (#1 o #1) inrs));
   374         val tye = ListPair.map (fn ((v,rs),a) => (v, TVar((a,0),rs)))
   374         val tye = ListPair.map (fn ((v,rs),a) => (v, TVar((a,0),rs)))
   375                      (inrs, nms')
   375                      (inrs, nms')
   376         val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
   376         val ctye = map (fn (v,T) => (v,ctyp_of sign T)) tye;
   377         fun varpairs([],[]) = []
   377         fun varpairs([],[]) = []
   378           | varpairs((var as Var(v,T)) :: vars, b::bs) =
   378           | varpairs((var as Var(v,T)) :: vars, b::bs) =
   421 
   421 
   422 fun freeze_thaw_robust th =
   422 fun freeze_thaw_robust th =
   423  let val fth = freezeT th
   423  let val fth = freezeT th
   424      val {prop, tpairs, sign, ...} = rep_thm fth
   424      val {prop, tpairs, sign, ...} = rep_thm fth
   425  in
   425  in
   426    case foldr add_term_vars (prop :: Thm.terms_of_tpairs tpairs, []) of
   426    case Library.foldr add_term_vars (prop :: Thm.terms_of_tpairs tpairs, []) of
   427        [] => (fth, fn i => fn x => x)   (*No vars: nothing to do!*)
   427        [] => (fth, fn i => fn x => x)   (*No vars: nothing to do!*)
   428      | vars =>
   428      | vars =>
   429          let fun newName (Var(ix,_), pairs) =
   429          let fun newName (Var(ix,_), pairs) =
   430                    let val v = gensym (string_of_indexname ix)
   430                    let val v = gensym (string_of_indexname ix)
   431                    in  ((ix,v)::pairs)  end;
   431                    in  ((ix,v)::pairs)  end;
   432              val alist = foldr newName (vars,[])
   432              val alist = Library.foldr newName (vars,[])
   433              fun mk_inst (Var(v,T)) =
   433              fun mk_inst (Var(v,T)) =
   434                  (cterm_of sign (Var(v,T)),
   434                  (cterm_of sign (Var(v,T)),
   435                   cterm_of sign (Free(the (assoc(alist,v)), T)))
   435                   cterm_of sign (Free(valOf (assoc(alist,v)), T)))
   436              val insts = map mk_inst vars
   436              val insts = map mk_inst vars
   437              fun thaw i th' = (*i is non-negative increment for Var indexes*)
   437              fun thaw i th' = (*i is non-negative increment for Var indexes*)
   438                  th' |> forall_intr_list (map #2 insts)
   438                  th' |> forall_intr_list (map #2 insts)
   439                      |> forall_elim_list (map (Thm.cterm_incr_indexes i o #1) insts)
   439                      |> forall_elim_list (map (Thm.cterm_incr_indexes i o #1) insts)
   440          in  (Thm.instantiate ([],insts) fth, thaw)  end
   440          in  (Thm.instantiate ([],insts) fth, thaw)  end
   444   The Frees created from Vars have nice names.*)
   444   The Frees created from Vars have nice names.*)
   445 fun freeze_thaw th =
   445 fun freeze_thaw th =
   446  let val fth = freezeT th
   446  let val fth = freezeT th
   447      val {prop, tpairs, sign, ...} = rep_thm fth
   447      val {prop, tpairs, sign, ...} = rep_thm fth
   448  in
   448  in
   449    case foldr add_term_vars (prop :: Thm.terms_of_tpairs tpairs, []) of
   449    case Library.foldr add_term_vars (prop :: Thm.terms_of_tpairs tpairs, []) of
   450        [] => (fth, fn x => x)
   450        [] => (fth, fn x => x)
   451      | vars =>
   451      | vars =>
   452          let fun newName (Var(ix,_), (pairs,used)) =
   452          let fun newName (Var(ix,_), (pairs,used)) =
   453                    let val v = variant used (string_of_indexname ix)
   453                    let val v = variant used (string_of_indexname ix)
   454                    in  ((ix,v)::pairs, v::used)  end;
   454                    in  ((ix,v)::pairs, v::used)  end;
   455              val (alist, _) = foldr newName (vars, ([], foldr add_term_names
   455              val (alist, _) = Library.foldr newName (vars, ([], Library.foldr add_term_names
   456                (prop :: Thm.terms_of_tpairs tpairs, [])))
   456                (prop :: Thm.terms_of_tpairs tpairs, [])))
   457              fun mk_inst (Var(v,T)) =
   457              fun mk_inst (Var(v,T)) =
   458                  (cterm_of sign (Var(v,T)),
   458                  (cterm_of sign (Var(v,T)),
   459                   cterm_of sign (Free(the (assoc(alist,v)), T)))
   459                   cterm_of sign (Free(valOf (assoc(alist,v)), T)))
   460              val insts = map mk_inst vars
   460              val insts = map mk_inst vars
   461              fun thaw th' =
   461              fun thaw th' =
   462                  th' |> forall_intr_list (map #2 insts)
   462                  th' |> forall_intr_list (map #2 insts)
   463                      |> forall_elim_list (map #1 insts)
   463                      |> forall_elim_list (map #1 insts)
   464          in  (Thm.instantiate ([],insts) fth, thaw)  end
   464          in  (Thm.instantiate ([],insts) fth, thaw)  end
   639 val imp_cong' = combination o combination (reflexive implies)
   639 val imp_cong' = combination o combination (reflexive implies)
   640 
   640 
   641 fun abs_def thm =
   641 fun abs_def thm =
   642   let
   642   let
   643     val (_, cvs) = strip_comb (fst (dest_equals (cprop_of thm)));
   643     val (_, cvs) = strip_comb (fst (dest_equals (cprop_of thm)));
   644     val thm' = foldr (fn (ct, thm) => Thm.abstract_rule
   644     val thm' = Library.foldr (fn (ct, thm) => Thm.abstract_rule
   645       (case term_of ct of Var ((a, _), _) => a | Free (a, _) => a | _ => "x")
   645       (case term_of ct of Var ((a, _), _) => a | Free (a, _) => a | _ => "x")
   646         ct thm) (cvs, thm)
   646         ct thm) (cvs, thm)
   647   in transitive
   647   in transitive
   648     (symmetric (eta_conversion (fst (dest_equals (cprop_of thm'))))) thm'
   648     (symmetric (eta_conversion (fst (dest_equals (cprop_of thm'))))) thm'
   649   end;
   649   end;
   833         val (tye',maxi') = Type.unify (Sign.tsig_of sign') (tye, maxi) (T, U)
   833         val (tye',maxi') = Type.unify (Sign.tsig_of sign') (tye, maxi) (T, U)
   834           handle Type.TUNIFY => raise TYPE("Ill-typed instantiation", [T,U], [t,u])
   834           handle Type.TUNIFY => raise TYPE("Ill-typed instantiation", [T,U], [t,u])
   835     in  (sign', tye', maxi')  end;
   835     in  (sign', tye', maxi')  end;
   836 in
   836 in
   837 fun cterm_instantiate ctpairs0 th =
   837 fun cterm_instantiate ctpairs0 th =
   838   let val (sign,tye,_) = foldr add_types (ctpairs0, (Thm.sign_of_thm th, Vartab.empty, 0))
   838   let val (sign,tye,_) = Library.foldr add_types (ctpairs0, (Thm.sign_of_thm th, Vartab.empty, 0))
   839       fun instT(ct,cu) = 
   839       fun instT(ct,cu) = 
   840         let val inst = cterm_of sign o subst_TVars_Vartab tye o term_of
   840         let val inst = cterm_of sign o subst_TVars_Vartab tye o term_of
   841         in (inst ct, inst cu) end
   841         in (inst ct, inst cu) end
   842       fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
   842       fun ctyp2 (ix,T) = (ix, ctyp_of sign T)
   843   in  instantiate (map ctyp2 (Vartab.dest tye), map instT ctpairs0) th  end
   843   in  instantiate (map ctyp2 (Vartab.dest tye), map instT ctpairs0) th  end
   860            (transitive combth (beta_conversion false (cterm (absu$a))))
   860            (transitive combth (beta_conversion false (cterm (absu$a))))
   861   end
   861   end
   862   handle THM _ => raise THM("equal_abs_elim", 0, [eqth]);
   862   handle THM _ => raise THM("equal_abs_elim", 0, [eqth]);
   863 
   863 
   864 (*Calling equal_abs_elim with multiple terms*)
   864 (*Calling equal_abs_elim with multiple terms*)
   865 fun equal_abs_elim_list cts th = foldr (uncurry equal_abs_elim) (rev cts, th);
   865 fun equal_abs_elim_list cts th = Library.foldr (uncurry equal_abs_elim) (rev cts, th);
   866 
   866 
   867 
   867 
   868 (*** Goal (PROP A) <==> PROP A ***)
   868 (*** Goal (PROP A) <==> PROP A ***)
   869 
   869 
   870 local
   870 local
   894 fun inst x t = read_instantiate_sg (sign_of (the_context())) [(x,t)];
   894 fun inst x t = read_instantiate_sg (sign_of (the_context())) [(x,t)];
   895 
   895 
   896 
   896 
   897 (* collect vars in left-to-right order *)
   897 (* collect vars in left-to-right order *)
   898 
   898 
   899 fun tvars_of_terms ts = rev (foldl Term.add_tvars ([], ts));
   899 fun tvars_of_terms ts = rev (Library.foldl Term.add_tvars ([], ts));
   900 fun vars_of_terms ts = rev (foldl Term.add_vars ([], ts));
   900 fun vars_of_terms ts = rev (Library.foldl Term.add_vars ([], ts));
   901 
   901 
   902 fun tvars_of thm = tvars_of_terms [prop_of thm];
   902 fun tvars_of thm = tvars_of_terms [prop_of thm];
   903 fun vars_of thm = vars_of_terms [prop_of thm];
   903 fun vars_of thm = vars_of_terms [prop_of thm];
   904 
   904 
   905 
   905 
   907 
   907 
   908 fun instantiate' cTs cts thm =
   908 fun instantiate' cTs cts thm =
   909   let
   909   let
   910     fun err msg =
   910     fun err msg =
   911       raise TYPE ("instantiate': " ^ msg,
   911       raise TYPE ("instantiate': " ^ msg,
   912         mapfilter (apsome Thm.typ_of) cTs,
   912         List.mapPartial (Option.map Thm.typ_of) cTs,
   913         mapfilter (apsome Thm.term_of) cts);
   913         List.mapPartial (Option.map Thm.term_of) cts);
   914 
   914 
   915     fun inst_of (v, ct) =
   915     fun inst_of (v, ct) =
   916       (Thm.cterm_of (#sign (Thm.rep_cterm ct)) (Var v), ct)
   916       (Thm.cterm_of (#sign (Thm.rep_cterm ct)) (Var v), ct)
   917         handle TYPE (msg, _, _) => err msg;
   917         handle TYPE (msg, _, _) => err msg;
   918 
   918 
   939 
   939 
   940 fun rename_bvars [] thm = thm
   940 fun rename_bvars [] thm = thm
   941   | rename_bvars vs thm =
   941   | rename_bvars vs thm =
   942     let
   942     let
   943       val {sign, prop, ...} = rep_thm thm;
   943       val {sign, prop, ...} = rep_thm thm;
   944       fun ren (Abs (x, T, t)) = Abs (if_none (assoc (vs, x)) x, T, ren t)
   944       fun ren (Abs (x, T, t)) = Abs (getOpt (assoc (vs, x), x), T, ren t)
   945         | ren (t $ u) = ren t $ ren u
   945         | ren (t $ u) = ren t $ ren u
   946         | ren t = t;
   946         | ren t = t;
   947     in equal_elim (reflexive (cterm_of sign (ren prop))) thm end;
   947     in equal_elim (reflexive (cterm_of sign (ren prop))) thm end;
   948 
   948 
   949 
   949 
   953   let
   953   let
   954     val {sign, prop, ...} = rep_thm thm;
   954     val {sign, prop, ...} = rep_thm thm;
   955     fun rename [] t = ([], t)
   955     fun rename [] t = ([], t)
   956       | rename (x' :: xs) (Abs (x, T, t)) =
   956       | rename (x' :: xs) (Abs (x, T, t)) =
   957           let val (xs', t') = rename xs t
   957           let val (xs', t') = rename xs t
   958           in (xs', Abs (if_none x' x, T, t')) end
   958           in (xs', Abs (getOpt (x',x), T, t')) end
   959       | rename xs (t $ u) =
   959       | rename xs (t $ u) =
   960           let
   960           let
   961             val (xs', t') = rename xs t;
   961             val (xs', t') = rename xs t;
   962             val (xs'', u') = rename xs' u
   962             val (xs'', u') = rename xs' u
   963           in (xs'', t' $ u') end
   963           in (xs'', t' $ u') end
   989 
   989 
   990 (* tvars_intr_list *)
   990 (* tvars_intr_list *)
   991 
   991 
   992 fun tfrees_of thm =
   992 fun tfrees_of thm =
   993   let val {hyps, prop, ...} = Thm.rep_thm thm
   993   let val {hyps, prop, ...} = Thm.rep_thm thm
   994   in foldr Term.add_term_tfree_names (prop :: hyps, []) end;
   994   in Library.foldr Term.add_term_tfree_names (prop :: hyps, []) end;
   995 
   995 
   996 fun tvars_intr_list tfrees thm =
   996 fun tvars_intr_list tfrees thm =
   997   Thm.varifyT' (tfrees_of thm \\ tfrees) thm;
   997   Thm.varifyT' (tfrees_of thm \\ tfrees) thm;
   998 
   998 
   999 
   999 
  1000 (* increment var indexes *)
  1000 (* increment var indexes *)
  1001 
  1001 
  1002 fun incr_indexes_wrt is cTs cts thms =
  1002 fun incr_indexes_wrt is cTs cts thms =
  1003   let
  1003   let
  1004     val maxidx =
  1004     val maxidx =
  1005       foldl Int.max (~1, is @
  1005       Library.foldl Int.max (~1, is @
  1006         map (maxidx_of_typ o #T o Thm.rep_ctyp) cTs @
  1006         map (maxidx_of_typ o #T o Thm.rep_ctyp) cTs @
  1007         map (#maxidx o Thm.rep_cterm) cts @
  1007         map (#maxidx o Thm.rep_cterm) cts @
  1008         map (#maxidx o Thm.rep_thm) thms);
  1008         map (#maxidx o Thm.rep_thm) thms);
  1009   in Thm.incr_indexes (maxidx + 1) end;
  1009   in Thm.incr_indexes (maxidx + 1) end;
  1010 
  1010