688 antiquotations @{text "@{typ_pat \<dots>}"} and @{text "@{term_pat \<dots>}"} from |
688 antiquotations @{text "@{typ_pat \<dots>}"} and @{text "@{term_pat \<dots>}"} from |
689 Section~\ref{sec:antiquote} in order to construct examples involving |
689 Section~\ref{sec:antiquote} in order to construct examples involving |
690 schematic variables. |
690 schematic variables. |
691 |
691 |
692 Let us begin with describing the unification and matching function for |
692 Let us begin with describing the unification and matching function for |
693 types. Both return type environments, ML-type @{ML_type "Type.tyenv"}, |
693 types. Both return type environments (ML-type @{ML_type "Type.tyenv"}) |
694 which map schematic type variables to types (and sorts). Below we use the |
694 which map schematic type variables to types and sorts. Below we use the |
695 function @{ML_ind typ_unify in Sign} from the structure @{ML_struct Sign} |
695 function @{ML_ind typ_unify in Sign} from the structure @{ML_struct Sign} |
696 for unifying the types @{text "?'a * ?'b"} and @{text "?'b list * |
696 for unifying the types @{text "?'a * ?'b"} and @{text "?'b list * |
697 nat"}. This will produce the mapping, or type environment, @{text "[?'a := |
697 nat"}. This will produce the mapping, or type environment, @{text "[?'a := |
698 ?'b list, ?'b := nat]"}. |
698 ?'b list, ?'b := nat]"}. |
699 *} |
699 *} |
704 in |
704 in |
705 Sign.typ_unify @{theory} (ty1, ty2) (Vartab.empty, 0) |
705 Sign.typ_unify @{theory} (ty1, ty2) (Vartab.empty, 0) |
706 end*} |
706 end*} |
707 |
707 |
708 text {* |
708 text {* |
709 The environment @{ML_ind "Vartab.empty"} in line 5 stands for the |
709 The environment @{ML_ind "Vartab.empty"} in line 5 stands for the empty type |
710 empty type environment, which is needed for starting the unification |
710 environment, which is needed for starting the unification without any |
711 without any (pre)instantiations. In case of a failure |
711 (pre)instantiations. The @{text 0} is an integer index that we will explain |
712 @{ML typ_unify in Sign} will throw the exception @{text TUNIFY}. |
712 below. In case of failure @{ML typ_unify in Sign} will throw the exception |
713 We can print out the resulting type environment @{ML tyenv_unif} |
713 @{text TUNIFY}. We can print out the resulting type environment @{ML |
714 with the built-in function @{ML_ind dest in Vartab} from the structure |
714 tyenv_unif} with the built-in function @{ML_ind dest in Vartab} from the |
715 @{ML_struct Vartab}. |
715 structure @{ML_struct Vartab}. |
716 |
716 |
717 @{ML_response_fake [display,gray] |
717 @{ML_response_fake [display,gray] |
718 "Vartab.dest tyenv_unif" |
718 "Vartab.dest tyenv_unif" |
719 "[((\"'a\", 0), ([\"HOL.type\"], \"?'b List.list\")), |
719 "[((\"'a\", 0), ([\"HOL.type\"], \"?'b List.list\")), |
720 ((\"'b\", 0), ([\"HOL.type\"], \"nat\"))]"} |
720 ((\"'b\", 0), ([\"HOL.type\"], \"nat\"))]"} |
747 \end{figure} |
747 \end{figure} |
748 *} |
748 *} |
749 |
749 |
750 text {* |
750 text {* |
751 The first components in this list stand for the schematic type variables and |
751 The first components in this list stand for the schematic type variables and |
752 the second are the associated sorts and types. In this example the sort |
752 the second are the associated sorts and types. In this example the sort is |
753 is the default sort @{text "HOL.type"}. We will use in what follows |
753 the default sort @{text "HOL.type"}. We will use in what follows the |
754 the pretty-printing function from Figure~\ref{fig:prettyenv} for |
754 pretty-printing function from Figure~\ref{fig:prettyenv} for @{ML_type |
755 @{ML_type "Type.tyenv"}s. For the type environment in the example |
755 "Type.tyenv"}s. For the type environment in the example this function prints |
756 this function prints out the more legible: |
756 out the more legible: |
757 |
757 |
758 @{ML_response_fake [display, gray] |
758 @{ML_response_fake [display, gray] |
759 "pretty_tyenv @{context} tyenv_unif" |
759 "pretty_tyenv @{context} tyenv_unif" |
760 "[?'a := ?'b list, ?'b := nat]"} |
760 "[?'a := ?'b list, ?'b := nat]"} |
761 |
761 |
762 The index @{text 0} in Line 5 in the example above is the maximal index of |
762 The way the unification function @{ML typ_unify in Sign} is implemented |
763 the schematic type variables occuring in @{text ty1} and @{text ty2}. This |
763 using an initial type environment and initial index makes it easy to |
764 index will be increased whenever a new schematic type variable is introduced |
764 unify more than two terms. For example |
765 during unification. This is for example the case when two schematic type |
765 *} |
766 variables have different, incomparable sorts. Then a new schematic type |
766 |
767 variable is introduced with the combined sorts. To show this let us assume two |
767 ML %linenosgray{*val (tyenvs, _) = let |
768 sorts, say @{text "s1"} and @{text "s2"}, which we attach to the schematic type |
768 val tys1 = (@{typ_pat "?'a"}, @{typ_pat "?'b list"}) |
769 variables @{text "?'a"} and @{text "?'b"}. Since we do not make any assumption |
769 val tys2 = (@{typ_pat "?'b"}, @{typ_pat "nat"}) |
770 about the sors, they are incomparable, leading to the type environment: |
770 in |
|
771 fold (Sign.typ_unify @{theory}) [tys1, tys2] (Vartab.empty, 0) |
|
772 end*} |
|
773 |
|
774 text {* |
|
775 The index @{text 0} in Line 5 is the maximal index of the schematic type |
|
776 variables occurring in @{text ty1} and @{text ty2}. This index will be |
|
777 increased whenever a new schematic type variable is introduced during |
|
778 unification. This is for example the case when two schematic type variables |
|
779 have different, incomparable sorts. Then a new schematic type variable is |
|
780 introduced with the combined sorts. To show this let us assume two sorts, |
|
781 say @{text "s1"} and @{text "s2"}, which we attach to the schematic type |
|
782 variables @{text "?'a"} and @{text "?'b"}. Since we do not make any |
|
783 assumption about the sorts, they are incomparable. |
771 *} |
784 *} |
772 |
785 |
773 ML{*val (tyenv, index) = let |
786 ML{*val (tyenv, index) = let |
774 val ty2 = @{typ_pat "?'a::s1"} |
787 val ty1 = @{typ_pat "?'a::s1"} |
775 val ty1 = @{typ_pat "?'b::s2"} |
788 val ty2 = @{typ_pat "?'b::s2"} |
776 in |
789 in |
777 Sign.typ_unify @{theory} (ty1, ty2) (Vartab.empty, 0) |
790 Sign.typ_unify @{theory} (ty1, ty2) (Vartab.empty, 0) |
778 end*} |
791 end*} |
779 |
792 |
780 text {* |
793 text {* |
781 To print out this type environment we switch on the printing of sort |
794 To print out the result type environment we switch on the printing |
782 information. |
795 of sort information by setting @{ML_ind show_sorts in Syntax} to |
783 *} |
796 true. This allows us to inspect the typing environment. |
784 |
|
785 ML{*show_sorts := true*} |
|
786 |
|
787 text {* |
|
788 Now we can see |
|
789 |
797 |
790 @{ML_response_fake [display,gray] |
798 @{ML_response_fake [display,gray] |
791 "pretty_tyenv @{context} tyenv" |
799 "pretty_tyenv @{context} tyenv" |
792 "[?'a::s1 := ?'a1::{s1,s2}, ?'b::s2 := ?'a1::{s1,s2}]"} |
800 "[?'a::s1 := ?'a1::{s1,s2}, ?'b::s2 := ?'a1::{s1,s2}]"} |
793 |
801 |
794 the type variables @{text "?'a"} and @{text "?'b"} are instantiated |
802 As can be seen, the type variables @{text "?'a"} and @{text "?'b"} are instantiated |
795 with a new type variable @{text "?'a1"} with sort @{text "{s1,s2}"}. Since a new |
803 with a new type variable @{text "?'a1"} with sort @{text "{s1,s2}"}. Since a new |
796 type variable has been introduced the @{ML index}, originally being @{text 0}, |
804 type variable has been introduced the @{ML index}, originally being @{text 0}, |
797 has been increased by @{text 1}. |
805 has been increased by @{text 1}. |
798 |
806 |
799 @{ML_response [display,gray] |
807 @{ML_response [display,gray] |
800 "index" |
808 "index" |
801 "1"} |
809 "1"} |
802 *}(*<*)ML %linenos{*show_sorts := false*}(*>*) |
810 |
803 |
811 Let us now return again to the unification problem @{text "?'a * ?'b"} and |
804 text {* |
812 @{text "?'b list * nat"} from the beginning of the section, and the |
805 The way the unification function @{ML typ_unify in Sign} is implemented |
813 calculated type environment @{ML tyenv_unif}: |
806 using an initial type environment and initial index makes it easy to |
|
807 unify more than two terms. For example |
|
808 *} |
|
809 |
|
810 ML{*val (tyenvs, _) = let |
|
811 val tys1 = (@{typ_pat "?'a"}, @{typ_pat "?'b list"}) |
|
812 val tys2 = (@{typ_pat "?'b"}, @{typ_pat "nat"}) |
|
813 in |
|
814 fold (Sign.typ_unify @{theory}) [tys1, tys2] (Vartab.empty, 0) |
|
815 end*} |
|
816 |
|
817 text {* |
|
818 Let us now return again to the unifier from the first example in this |
|
819 section. |
|
820 |
814 |
821 @{ML_response_fake [display, gray] |
815 @{ML_response_fake [display, gray] |
822 "pretty_tyenv @{context} tyenv_unif" |
816 "pretty_tyenv @{context} tyenv_unif" |
823 "[?'a := ?'b list, ?'b := nat]"} |
817 "[?'a := ?'b list, ?'b := nat]"} |
824 |
818 |
879 Sign.typ_match @{theory} (pat, ty) Vartab.empty |
873 Sign.typ_match @{theory} (pat, ty) Vartab.empty |
880 end*} |
874 end*} |
881 |
875 |
882 text {* |
876 text {* |
883 Now @{ML tyenv_unif} is equal to @{ML tyenv_match'}. If we apply |
877 Now @{ML tyenv_unif} is equal to @{ML tyenv_match'}. If we apply |
884 @{ML norm_type in Envir} to the matcher we obtain |
878 @{ML norm_type in Envir} to the type @{text "?'a * ?'b"} we obtain |
885 |
879 |
886 @{ML_response_fake [display, gray] |
880 @{ML_response_fake [display, gray] |
887 "Envir.norm_type tyenv_match' @{typ_pat \"?'a * ?'b\"}" |
881 "Envir.norm_type tyenv_match' @{typ_pat \"?'a * ?'b\"}" |
888 "nat list * nat"} |
882 "nat list * nat"} |
889 |
883 |
890 which is not a matcher for the second matching problem, and if |
884 which does not solve the matching problem, and if |
891 we apply @{ML subst_type in Envir} to the unifyier we obtain |
885 we apply @{ML subst_type in Envir} to the same type we obtain |
892 |
886 |
893 @{ML_response_fake [display, gray] |
887 @{ML_response_fake [display, gray] |
894 "Envir.subst_type tyenv_unif @{typ_pat \"?'a * ?'b\"}" |
888 "Envir.subst_type tyenv_unif @{typ_pat \"?'a * ?'b\"}" |
895 "?'b list * nat"} |
889 "?'b list * nat"} |
896 |
890 |
897 which is not the correct unifier for the unification problem. |
891 which does not solve the unification problem. |
898 |
892 |
899 \begin{readmore} |
893 \begin{readmore} |
900 Unification and matching of types is implemented |
894 Unification and matching for types is implemented |
901 in @{ML_file "Pure/type.ML"}. The ``interface'' functions for them |
895 in @{ML_file "Pure/type.ML"}. The ``interface'' functions for them |
902 are in @{ML_file "Pure/sign.ML"}. Matching and unification produce type environments |
896 are in @{ML_file "Pure/sign.ML"}. Matching and unification produce type environments |
903 as results. These are implemented in @{ML_file "Pure/envir.ML"}. |
897 as results. These are implemented in @{ML_file "Pure/envir.ML"}. |
904 This file also includes the substitution and normalisation functions, |
898 This file also includes the substitution and normalisation functions, |
905 that apply a type environment to a type. Type environments are lookup |
899 that apply a type environment to a type. Type environments are lookup |
942 end *} |
936 end *} |
943 |
937 |
944 text {* |
938 text {* |
945 In this example we annotated explicitly types because then |
939 In this example we annotated explicitly types because then |
946 the type environment is empty and can be ignored. The |
940 the type environment is empty and can be ignored. The |
947 resulting term environment is: |
941 resulting term environment is |
948 |
942 |
949 @{ML_response_fake [display, gray] |
943 @{ML_response_fake [display, gray] |
950 "pretty_env @{context} fo_env" |
944 "pretty_env @{context} fo_env" |
951 "[?X := P, ?Y := \<lambda>a b. Q b a]"} |
945 "[?X := P, ?Y := \<lambda>a b. Q b a]"} |
952 |
946 |
953 The matcher can be applied to a term using the function @{ML_ind subst_term |
947 The matcher can be applied to a term using the function @{ML_ind subst_term |
954 in Envir} (remember the same convention for types applies to terms: @{ML |
948 in Envir} (remember the same convention for types applies to terms: @{ML |
955 subst_term in Envir} is for matchers and @{ML norm_term in Envir} is for |
949 subst_term in Envir} is for matchers and @{ML norm_term in Envir} for |
956 unifyiers). The function @{ML subst_term in Envir} expects a type environment, |
950 unifiers). The function @{ML subst_term in Envir} expects a type environment, |
957 which is set to empty in the example below, and a term environment. |
951 which is set to empty in the example below, and a term environment. |
958 |
952 |
959 @{ML_response_fake [display, gray] |
953 @{ML_response_fake [display, gray] |
960 "let |
954 "let |
961 val trm = @{term_pat \"(?X::(nat\<Rightarrow>nat\<Rightarrow>nat)\<Rightarrow>bool) ?Y\"} |
955 val trm = @{term_pat \"(?X::(nat\<Rightarrow>nat\<Rightarrow>nat)\<Rightarrow>bool) ?Y\"} |
985 end" |
979 end" |
986 "[?X := P]"} |
980 "[?X := P]"} |
987 *} |
981 *} |
988 |
982 |
989 text {* |
983 text {* |
990 In the context of unifying abstractions, more thouroughly studied is |
984 Unification of abstractions is more thoroughly studied in the context |
991 higher-order pattern unification and higher-order pattern matching. A |
985 of higher-order pattern unification and higher-order pattern matching. A |
992 \emph{\index*{pattern}} is a lambda term whose ``head symbol'' (that is the |
986 \emph{\index*{pattern}} is a lambda term whose ``head symbol'' (that is the |
993 first symbol under abstractions) is either a constant, or a schematic or a free |
987 first symbol under abstractions) is either a constant, or a schematic or a free |
994 variable. If it is a schematic variable then it can be only applied by |
988 variable. If it is a schematic variable then it can be only applied by |
995 distinct bound variables. This excludes that a schematic variable is an |
989 distinct bound variables. This excludes that a schematic variable is an |
996 argument of a schematic variable and that a schematic variable is applied |
990 argument of another one and that a schematic variable is applied |
997 twice to the same bound variable. The function @{ML_ind pattern in Pattern} |
991 twice with the same bound variable. The function @{ML_ind pattern in Pattern} |
998 in the structure @{ML_struct Pattern} tests whether a term satisfies these |
992 in the structure @{ML_struct Pattern} tests whether a term satisfies these |
999 restrictions. |
993 restrictions. |
1000 |
|
1001 |
994 |
1002 @{ML_response [display, gray] |
995 @{ML_response [display, gray] |
1003 "let |
996 "let |
1004 val trm_list = |
997 val trm_list = |
1005 [@{term_pat \"?X\"}, @{term_pat \"a\"}, |
998 [@{term_pat \"?X\"}, @{term_pat \"a\"}, |
1009 in |
1002 in |
1010 map Pattern.pattern trm_list |
1003 map Pattern.pattern trm_list |
1011 end" |
1004 end" |
1012 "[true, true, true, true, true, false, false, false]"} |
1005 "[true, true, true, true, true, false, false, false]"} |
1013 |
1006 |
1014 The point of restricting unification and matching to patterns is that |
1007 The point of the restriction to patterns is that unification and matching |
1015 it is decidable and produces most general unifiers. In this way |
1008 are decidable and produce most general unifiers, respectively matchers. |
1016 matching and unify can be implemented so that they produce a type |
1009 In this way, matching and unification can be implemented as functions that |
1017 and term environment (the latter actually produces only a term |
1010 produce a type and term environment (unification actually returns a |
1018 environment). |
1011 record of type @{ML_type Envir.env} containing a maxind, a type environment |
1019 *} |
1012 and a term environment). The former function is @{ML_ind match in Pattern}, |
1020 |
1013 the latter @{ML_ind unify in Pattern} both implemented in the structure |
1021 |
1014 @{ML_struct Pattern}. An example for higher-order pattern unification is |
1022 text {* |
1015 |
|
1016 \ldots |
|
1017 *} |
|
1018 |
|
1019 ML{*let |
|
1020 val pat = @{term_pat "?X"} |
|
1021 val trm = @{term "a"} |
|
1022 val init = (Vartab.empty, Vartab.empty) |
|
1023 in |
|
1024 Pattern.match @{theory} (pat, trm) init |
|
1025 end *} |
|
1026 |
|
1027 text {* |
|
1028 An assumption of this function is that the terms to be unified have |
|
1029 already the same type. In case of failure, the exceptions that are raised |
|
1030 are either @{text Pattern}, @{text MATCH} or @{text Unif}. |
|
1031 |
|
1032 As mentioned before, ``full'' higher-order unification, respectively, matching |
|
1033 problems are in general undecidable and might in general not posses a single |
|
1034 most general solution. Therefore Isabelle implements Huet's pre-unification |
|
1035 algorithm which does not return a single result, rather a lazy list of potentially |
|
1036 infinite unifiers. The corresponding function is called @{ML_ind unifiers in Unify}. |
|
1037 An example is as follows |
|
1038 |
|
1039 \ldots |
|
1040 |
|
1041 In case of failure this function does not raise an exception, rather returns |
|
1042 the empty sequence. In order to find a reasonable solution for a unification |
|
1043 problem, Isabelle also tries first to solve the problem by higher-order |
|
1044 pattern unification. |
|
1045 |
|
1046 \ldots |
|
1047 |
|
1048 For higher-order matching the function is called @{ML_ind matchers in Unify}. |
|
1049 Also this function might potentially return sequences with more than one |
|
1050 element. |
|
1051 |
|
1052 |
|
1053 Like @{ML unifiers in Unify}, this function does not raise an exception |
|
1054 in case of failure. It also tries out first whether the matching problem |
|
1055 can be solved by first-order matching. |
|
1056 |
1023 \begin{readmore} |
1057 \begin{readmore} |
1024 For term, unification and matching of higher-order patterns is implemented in |
1058 Unification and matching of higher-order patterns is implemented in |
1025 @{ML_file "Pure/pattern.ML"}. This file also contains a first-order matcher |
1059 @{ML_file "Pure/pattern.ML"}. This file also contains a first-order matcher |
1026 for terms. Full higher-order unification is implemented |
1060 for terms. Full higher-order unification is implemented |
1027 in @{ML_file "Pure/unify.ML"}. |
1061 in @{ML_file "Pure/unify.ML"}. It uses lazy sequences which are implemented |
|
1062 in @{ML_file "Pure/General/seq.ML"}. |
1028 \end{readmore} |
1063 \end{readmore} |
1029 *} |
1064 *} |
1030 |
1065 |
1031 |
1066 |
1032 |
1067 |