859 |
859 |
860 (*Substitute new for free occurrences of old in a term*) |
860 (*Substitute new for free occurrences of old in a term*) |
861 fun subst_free [] = (fn t=>t) |
861 fun subst_free [] = (fn t=>t) |
862 | subst_free pairs = |
862 | subst_free pairs = |
863 let fun substf u = |
863 let fun substf u = |
864 case gen_assoc (op aconv) (pairs, u) of |
864 case AList.lookup (op aconv) pairs u of |
865 SOME u' => u' |
865 SOME u' => u' |
866 | NONE => (case u of Abs(a,T,t) => Abs(a, T, substf t) |
866 | NONE => (case u of Abs(a,T,t) => Abs(a, T, substf t) |
867 | t$u' => substf t $ substf u' |
867 | t$u' => substf t $ substf u' |
868 | _ => u) |
868 | _ => u) |
869 in substf end; |
869 in substf end; |
914 fun subst_atomic [] tm = tm |
914 fun subst_atomic [] tm = tm |
915 | subst_atomic inst tm = |
915 | subst_atomic inst tm = |
916 let |
916 let |
917 fun subst (Abs (a, T, body)) = Abs (a, T, subst body) |
917 fun subst (Abs (a, T, body)) = Abs (a, T, subst body) |
918 | subst (t $ u) = subst t $ subst u |
918 | subst (t $ u) = subst t $ subst u |
919 | subst t = if_none (gen_assoc (op aconv) (inst, t)) t; |
919 | subst t = if_none (AList.lookup (op aconv) inst t) t; |
920 in subst tm end; |
920 in subst tm end; |
921 |
921 |
922 (*Replace the ATOMIC type Ti by Ui; inst = [(T1,U1), ..., (Tn,Un)].*) |
922 (*Replace the ATOMIC type Ti by Ui; inst = [(T1,U1), ..., (Tn,Un)].*) |
923 fun typ_subst_atomic [] ty = ty |
923 fun typ_subst_atomic [] ty = ty |
924 | typ_subst_atomic inst ty = |
924 | typ_subst_atomic inst ty = |
925 let |
925 let |
926 fun subst (Type (a, Ts)) = Type (a, map subst Ts) |
926 fun subst (Type (a, Ts)) = Type (a, map subst Ts) |
927 | subst T = if_none (gen_assoc (op = : typ * typ -> bool) (inst, T)) T; |
927 | subst T = if_none (AList.lookup (op = : typ * typ -> bool) inst T) T; |
928 in subst ty end; |
928 in subst ty end; |
929 |
929 |
930 fun subst_atomic_types [] tm = tm |
930 fun subst_atomic_types [] tm = tm |
931 | subst_atomic_types inst tm = map_term_types (typ_subst_atomic inst) tm; |
931 | subst_atomic_types inst tm = map_term_types (typ_subst_atomic inst) tm; |
932 |
932 |
975 fun instantiateT_same [] _ = raise SAME |
975 fun instantiateT_same [] _ = raise SAME |
976 | instantiateT_same instT ty = |
976 | instantiateT_same instT ty = |
977 let |
977 let |
978 fun subst_typ (Type (a, Ts)) = Type (a, subst_typs Ts) |
978 fun subst_typ (Type (a, Ts)) = Type (a, subst_typs Ts) |
979 | subst_typ (TVar v) = |
979 | subst_typ (TVar v) = |
980 (case gen_assoc eq_tvar (instT, v) of |
980 (case AList.lookup eq_tvar instT v of |
981 SOME T => T |
981 SOME T => T |
982 | NONE => raise SAME) |
982 | NONE => raise SAME) |
983 | subst_typ _ = raise SAME |
983 | subst_typ _ = raise SAME |
984 and subst_typs (T :: Ts) = |
984 and subst_typs (T :: Ts) = |
985 (subst_typ T :: (subst_typs Ts handle SAME => Ts) |
985 (subst_typ T :: (subst_typs Ts handle SAME => Ts) |
993 val substT = instantiateT_same instT; |
993 val substT = instantiateT_same instT; |
994 fun subst (Const (c, T)) = Const (c, substT T) |
994 fun subst (Const (c, T)) = Const (c, substT T) |
995 | subst (Free (x, T)) = Free (x, substT T) |
995 | subst (Free (x, T)) = Free (x, substT T) |
996 | subst (Var (xi, T)) = |
996 | subst (Var (xi, T)) = |
997 let val (T', same) = (substT T, false) handle SAME => (T, true) in |
997 let val (T', same) = (substT T, false) handle SAME => (T, true) in |
998 (case gen_assoc eq_var (inst, (xi, T')) of |
998 (case AList.lookup eq_var inst (xi, T') of |
999 SOME t => t |
999 SOME t => t |
1000 | NONE => if same then raise SAME else Var (xi, T')) |
1000 | NONE => if same then raise SAME else Var (xi, T')) |
1001 end |
1001 end |
1002 | subst (Bound _) = raise SAME |
1002 | subst (Bound _) = raise SAME |
1003 | subst (Abs (x, T, t)) = |
1003 | subst (Abs (x, T, t)) = |