687 the unification and matching algorithms. Below we shall use the |
687 the unification and matching algorithms. Below we shall use the |
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 the 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 *} |
700 |
700 |
701 ML{*val (tyenv_unif, _) = let |
701 ML %linenosgray{*val (tyenv_unif, _) = let |
702 val ty1 = @{typ_pat "?'a * ?'b"} |
702 val ty1 = @{typ_pat "?'a * ?'b"} |
703 val ty2 = @{typ_pat "?'b list * nat"} |
703 val ty2 = @{typ_pat "?'b list * nat"} |
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"} stands for the empty type environment, |
709 The environment @{ML_ind "Vartab.empty"} in line 5 stands for the |
710 which is needed for starting the unification without any |
710 empty type environment, which is needed for starting the unification |
711 (pre)instantiations. In case of a failure |
711 without any (pre)instantiations. In case of a failure |
712 @{ML typ_unify in Sign} will throw the exception @{text TUNIFY}. |
712 @{ML typ_unify in Sign} will throw the exception @{text TUNIFY}. |
713 We can print out the resulting type environment @{ML tyenv_unif} |
713 We can print out the resulting type environment @{ML tyenv_unif} |
714 with the built-in function @{ML_ind dest in Vartab} from the structure |
714 with the built-in function @{ML_ind dest in Vartab} from the structure |
715 @{ML_struct Vartab}. |
715 @{ML_struct Vartab}. |
716 |
716 |
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 what follows we will use |
752 the second are the associated sorts and types. In this example the sort |
753 the pretty-printing function in Figure~\ref{fig:prettyenv} for @{ML_type "Type.tyenv"}s. |
753 is the default sort @{text "HOL.type"}. We will use in what follows |
754 For the type environment from the example this function prints out the |
754 the pretty-printing function from Figure~\ref{fig:prettyenv} for |
755 more legible: |
755 @{ML_type "Type.tyenv"}s. For the type environment in the example |
|
756 this function prints out the more legible: |
756 |
757 |
757 @{ML_response_fake [display, gray] |
758 @{ML_response_fake [display, gray] |
758 "pretty_tyenv @{context} tyenv_unif" |
759 "pretty_tyenv @{context} tyenv_unif" |
759 "[?'a := ?'b list, ?'b := nat]"} |
760 "[?'a := ?'b list, ?'b := nat]"} |
760 |
761 |
761 The index @{text 0} in the example above is the maximal index of the schematic |
762 The index @{text 0} in Line 5 in the example above is the maximal index of |
762 type variables occuring in @{text ty1} and @{text ty2}. It will be increased |
763 the schematic type variables occuring in @{text ty1} and @{text ty2}. This |
763 whenever a new schematic type variable is introduced during unification. |
764 index will be increased whenever a new schematic type variable is introduced |
764 This is for example the case when two schematic type variables have different, |
765 during unification. This is for example the case when two schematic type |
765 incomparable sorts. Then a new schematic type variable is introduced with |
766 variables have different, incomparable sorts. Then a new schematic type |
766 the combined sorts. To show this let us use two sorts, @{text "s1"} and @{text "s2"}, |
767 variable is introduced with the combined sorts. To show this let us assume two |
767 which we attach to the schematic type variables @{text "?'a"} and @{text "?'b"}. |
768 sorts, say @{text "s1"} and @{text "s2"}, which we attach to the schematic type |
|
769 variables @{text "?'a"} and @{text "?'b"}. Since we do not make any assumption |
|
770 about the sors, they are incomparable, leading to the type environment: |
768 *} |
771 *} |
769 |
772 |
770 ML{*val (tyenv, index) = let |
773 ML{*val (tyenv, index) = let |
771 val ty2 = @{typ_pat "?'a::s1"} |
774 val ty2 = @{typ_pat "?'a::s1"} |
772 val ty1 = @{typ_pat "?'b::s2"} |
775 val ty1 = @{typ_pat "?'b::s2"} |
773 in |
776 in |
774 Sign.typ_unify @{theory} (ty1, ty2) (Vartab.empty, 0) |
777 Sign.typ_unify @{theory} (ty1, ty2) (Vartab.empty, 0) |
775 end*} |
778 end*} |
776 |
779 |
777 text {* |
780 text {* |
778 To print out the calculated type environment we also switch on the printing |
781 To print out this type environment we switch on the printing of sort |
779 of sort information. |
782 information. |
780 *} |
783 *} |
781 |
784 |
782 ML{*show_sorts := true*} |
785 ML{*show_sorts := true*} |
783 |
786 |
784 text {* |
787 text {* |
799 *}(*<*)ML %linenos{*show_sorts := false*}(*>*) |
802 *}(*<*)ML %linenos{*show_sorts := false*}(*>*) |
800 |
803 |
801 text {* |
804 text {* |
802 The way the unification function @{ML typ_unify in Sign} is implemented |
805 The way the unification function @{ML typ_unify in Sign} is implemented |
803 using an initial type environment and initial index makes it easy to |
806 using an initial type environment and initial index makes it easy to |
804 unifying of more than two terms. For example |
807 unify more than two terms. For example |
805 *} |
808 *} |
806 |
809 |
807 ML{*val (tyenvs, _) = let |
810 ML{*val (tyenvs, _) = let |
808 val tys1 = (@{typ_pat "?'a"}, @{typ_pat "?'b list"}) |
811 val tys1 = (@{typ_pat "?'a"}, @{typ_pat "?'b list"}) |
809 val tys2 = (@{typ_pat "?'b"}, @{typ_pat "nat"}) |
812 val tys2 = (@{typ_pat "?'b"}, @{typ_pat "nat"}) |
810 in |
813 in |
811 fold (Sign.typ_unify @{theory}) [tys1, tys2] (Vartab.empty, 0) |
814 fold (Sign.typ_unify @{theory}) [tys1, tys2] (Vartab.empty, 0) |
812 end*} |
815 end*} |
813 |
816 |
814 text {* |
817 text {* |
815 Let us now return again to the unifier from the first example. |
818 Let us now return again to the unifier from the first example in this |
|
819 section. |
816 |
820 |
817 @{ML_response_fake [display, gray] |
821 @{ML_response_fake [display, gray] |
818 "pretty_tyenv @{context} tyenv_unif" |
822 "pretty_tyenv @{context} tyenv_unif" |
819 "[?'a := ?'b list, ?'b := nat]"} |
823 "[?'a := ?'b list, ?'b := nat]"} |
820 |
824 |
822 Sign} returns is in \emph{not} an instantiation in fully solved form: while @{text |
826 Sign} returns is in \emph{not} an instantiation in fully solved form: while @{text |
823 "?'b"} is instantiated to @{typ nat}, this is not propagated to the |
827 "?'b"} is instantiated to @{typ nat}, this is not propagated to the |
824 instantiation for @{text "?'a"}. In unification theory, this is often |
828 instantiation for @{text "?'a"}. In unification theory, this is often |
825 called an instantiation in \emph{triangular form}. These not fully solved |
829 called an instantiation in \emph{triangular form}. These not fully solved |
826 instantiations are used because of performance reasons. |
830 instantiations are used because of performance reasons. |
827 |
|
828 To apply a type environment in triangular form to a type, say @{text "?'a * |
831 To apply a type environment in triangular form to a type, say @{text "?'a * |
829 ?'b"}, you should use the function @{ML_ind norm_type in Envir}: |
832 ?'b"}, you should use the function @{ML_ind norm_type in Envir}: |
830 |
833 |
831 @{ML_response_fake [display, gray] |
834 @{ML_response_fake [display, gray] |
832 "Envir.norm_type tyenv_unif @{typ_pat \"?'a * ?'b\"}" |
835 "Envir.norm_type tyenv_unif @{typ_pat \"?'a * ?'b\"}" |
1435 The basic functions for theorems are defined in |
1438 The basic functions for theorems are defined in |
1436 @{ML_file "Pure/thm.ML"}, @{ML_file "Pure/more_thm.ML"} and @{ML_file "Pure/drule.ML"}. |
1439 @{ML_file "Pure/thm.ML"}, @{ML_file "Pure/more_thm.ML"} and @{ML_file "Pure/drule.ML"}. |
1437 \end{readmore} |
1440 \end{readmore} |
1438 |
1441 |
1439 The simplifier can be used to unfold definition in theorems. To show |
1442 The simplifier can be used to unfold definition in theorems. To show |
1440 this we build the theorem @{term "True \<equiv> True"} (Line 1) and then |
1443 this, we build the theorem @{term "True \<equiv> True"} (Line 1) and then |
1441 unfold the constant @{term "True"} according to its definition (Line 2). |
1444 unfold the constant @{term "True"} according to its definition (Line 2). |
1442 |
1445 |
1443 @{ML_response_fake [display,gray,linenos] |
1446 @{ML_response_fake [display,gray,linenos] |
1444 "Thm.reflexive @{cterm \"True\"} |
1447 "Thm.reflexive @{cterm \"True\"} |
1445 |> Simplifier.rewrite_rule [@{thm True_def}] |
1448 |> Simplifier.rewrite_rule [@{thm True_def}] |