125 end" |
125 end" |
126 "?x3, ?x1.3, x"} |
126 "?x3, ?x1.3, x"} |
127 |
127 |
128 When constructing terms, you are usually concerned with free |
128 When constructing terms, you are usually concerned with free |
129 variables (as mentioned earlier, you cannot construct schematic |
129 variables (as mentioned earlier, you cannot construct schematic |
130 variables using the built in antiquotation \mbox{@{text "@{term |
130 variables using the built-in antiquotation \mbox{@{text "@{term |
131 \<dots>}"}}). If you deal with theorems, you have to, however, observe the |
131 \<dots>}"}}). If you deal with theorems, you have to, however, observe the |
132 distinction. The reason is that only schematic variables can be |
132 distinction. The reason is that only schematic variables can be |
133 instantiated with terms when a theorem is applied. A similar |
133 instantiated with terms when a theorem is applied. A similar |
134 distinction between free and schematic variables holds for types |
134 distinction between free and schematic variables holds for types |
135 (see below). |
135 (see below). |
146 |
146 |
147 @{ML_response_fake_both [display,gray] |
147 @{ML_response_fake_both [display,gray] |
148 "@{term \"x x\"}" |
148 "@{term \"x x\"}" |
149 "Type unification failed: Occurs check!"} |
149 "Type unification failed: Occurs check!"} |
150 |
150 |
151 raises a typing error, while it perfectly ok to construct the term |
151 raises a typing error, while it is perfectly ok to construct the term |
152 with the raw ML-constructors: |
152 with the raw ML-constructors: |
153 |
153 |
154 @{ML_response_fake [display,gray] |
154 @{ML_response_fake [display,gray] |
155 "let |
155 "let |
156 val omega = Free (\"x\", @{typ \"nat \<Rightarrow> nat\"}) $ Free (\"x\", @{typ nat}) |
156 val omega = Free (\"x\", @{typ \"nat \<Rightarrow> nat\"}) $ Free (\"x\", @{typ nat}) |
274 |
274 |
275 we can see the full name of the type is actually @{text "List.list"}, indicating |
275 we can see the full name of the type is actually @{text "List.list"}, indicating |
276 that it is defined in the theory @{text "List"}. However, one has to be |
276 that it is defined in the theory @{text "List"}. However, one has to be |
277 careful with names of types, because even if |
277 careful with names of types, because even if |
278 @{text "fun"} is defined in the theory @{text "HOL"}, it is |
278 @{text "fun"} is defined in the theory @{text "HOL"}, it is |
279 still represented by their simple name. |
279 still represented by its simple name. |
280 |
280 |
281 @{ML_response [display,gray] |
281 @{ML_response [display,gray] |
282 "@{typ \"bool \<Rightarrow> nat\"}" |
282 "@{typ \"bool \<Rightarrow> nat\"}" |
283 "Type (\"fun\", [Type (\"HOL.bool\", []), Type (\"Nat.nat\", [])])"} |
283 "Type (\"fun\", [Type (\"HOL.bool\", []), Type (\"Nat.nat\", [])])"} |
284 |
284 |
377 in |
377 in |
378 lambda x_nat trm |
378 lambda x_nat trm |
379 end" |
379 end" |
380 "Abs (\"x\", \"Nat.nat\", Free (\"P\", \"bool \<Rightarrow> bool\") $ Bound 0)"} |
380 "Abs (\"x\", \"Nat.nat\", Free (\"P\", \"bool \<Rightarrow> bool\") $ Bound 0)"} |
381 |
381 |
382 In this example, @{ML lambda} produces a de Bruijn index (i.e.~@{ML "Bound 0"}), |
382 In this example, @{ML lambda} produces a de Bruijn index (i.e.\ @{ML "Bound 0"}), |
383 and an abstraction, where it also records the type of the abstracted |
383 and an abstraction, where it also records the type of the abstracted |
384 variable and for printing purposes also its name. Note that because of the |
384 variable and for printing purposes also its name. Note that because of the |
385 typing annotation on @{text "P"}, the variable @{text "x"} in @{text "P x"} |
385 typing annotation on @{text "P"}, the variable @{text "x"} in @{text "P x"} |
386 is of the same type as the abstracted variable. If it is of different type, |
386 is of the same type as the abstracted variable. If it is of different type, |
387 as in |
387 as in |
790 |
790 |
791 text {* |
791 text {* |
792 The environment @{ML_ind "Vartab.empty"} in line 5 stands for the empty type |
792 The environment @{ML_ind "Vartab.empty"} in line 5 stands for the empty type |
793 environment, which is needed for starting the unification without any |
793 environment, which is needed for starting the unification without any |
794 (pre)instantiations. The @{text 0} is an integer index that will be explained |
794 (pre)instantiations. The @{text 0} is an integer index that will be explained |
795 below. In case of failure, @{ML typ_unify in Sign} will throw the exception |
795 below. In case of failure, @{ML typ_unify in Sign} will raise the exception |
796 @{text TUNIFY}. We can print out the resulting type environment bound to |
796 @{text TUNIFY}. We can print out the resulting type environment bound to |
797 @{ML tyenv_unif} with the built-in function @{ML_ind dest in Vartab} from the |
797 @{ML tyenv_unif} with the built-in function @{ML_ind dest in Vartab} from the |
798 structure @{ML_struct Vartab}. |
798 structure @{ML_struct Vartab}. |
799 |
799 |
800 @{ML_response_fake [display,gray] |
800 @{ML_response_fake [display,gray] |
1121 unified have already the same type. In case of failure, the exceptions that |
1121 unified have already the same type. In case of failure, the exceptions that |
1122 are raised are either @{text Pattern}, @{text MATCH} or @{text Unif}. |
1122 are raised are either @{text Pattern}, @{text MATCH} or @{text Unif}. |
1123 |
1123 |
1124 As mentioned before, unrestricted higher-order unification, respectively |
1124 As mentioned before, unrestricted higher-order unification, respectively |
1125 unrestricted higher-order matching, is in general undecidable and might also |
1125 unrestricted higher-order matching, is in general undecidable and might also |
1126 not posses a single most general solution. Therefore Isabelle implements the |
1126 not possess a single most general solution. Therefore Isabelle implements the |
1127 unification function @{ML_ind unifiers in Unify} so that it returns a lazy |
1127 unification function @{ML_ind unifiers in Unify} so that it returns a lazy |
1128 list of potentially infinite unifiers. An example is as follows |
1128 list of potentially infinite unifiers. An example is as follows |
1129 *} |
1129 *} |
1130 |
1130 |
1131 ML %grayML{*val uni_seq = |
1131 ML %grayML{*val uni_seq = |
1198 Unify}, this function does not raise an exception in case of failure, but |
1198 Unify}, this function does not raise an exception in case of failure, but |
1199 returns an empty sequence. It also first tries out whether the matching |
1199 returns an empty sequence. It also first tries out whether the matching |
1200 problem can be solved by first-order matching. |
1200 problem can be solved by first-order matching. |
1201 |
1201 |
1202 Higher-order matching might be necessary for instantiating a theorem |
1202 Higher-order matching might be necessary for instantiating a theorem |
1203 appropriately. More on this will be given in Sections~\ref{sec:theorems}. |
1203 appropriately. More on this will be given in Section~\ref{sec:theorems}. |
1204 Here we only have a look at a simple case, namely the theorem |
1204 Here we only have a look at a simple case, namely the theorem |
1205 @{thm [source] spec}: |
1205 @{thm [source] spec}: |
1206 |
1206 |
1207 \begin{isabelle} |
1207 \begin{isabelle} |
1208 \isacommand{thm}~@{thm [source] spec}\\ |
1208 \isacommand{thm}~@{thm [source] spec}\\ |
1210 \end{isabelle} |
1210 \end{isabelle} |
1211 |
1211 |
1212 as an introduction rule. Applying it directly can lead to unexpected |
1212 as an introduction rule. Applying it directly can lead to unexpected |
1213 behaviour since the unification has more than one solution. One way round |
1213 behaviour since the unification has more than one solution. One way round |
1214 this problem is to instantiate the schematic variables @{text "?P"} and |
1214 this problem is to instantiate the schematic variables @{text "?P"} and |
1215 @{text "?x"}. instantiation function for theorems is |
1215 @{text "?x"}. Instantiation function for theorems is |
1216 @{ML_ind instantiate_normalize in Drule} from the structure |
1216 @{ML_ind instantiate_normalize in Drule} from the structure |
1217 @{ML_struct Drule}. One problem, however, is |
1217 @{ML_struct Drule}. One problem, however, is |
1218 that this function expects the instantiations as lists of @{ML_type ctyp} |
1218 that this function expects the instantiations as lists of @{ML_type ctyp} |
1219 and @{ML_type cterm} pairs: |
1219 and @{ML_type cterm} pairs: |
1220 |
1220 |
1307 and may use so-called type class parameters. These are type-indexed constants |
1307 and may use so-called type class parameters. These are type-indexed constants |
1308 dependent on the sole type variable and are implemented via overloading. |
1308 dependent on the sole type variable and are implemented via overloading. |
1309 Overloading a constant means specifying its value on a type based on |
1309 Overloading a constant means specifying its value on a type based on |
1310 a well-founded reduction towards other values of constants on types. |
1310 a well-founded reduction towards other values of constants on types. |
1311 When instantiating type classes |
1311 When instantiating type classes |
1312 (i.e. proving arities) you are specifying overloading via primitive recursion. |
1312 (i.e.\ proving arities) you are specifying overloading via primitive recursion. |
1313 |
1313 |
1314 Sorts are finite intersections of type classes and are implemented as lists |
1314 Sorts are finite intersections of type classes and are implemented as lists |
1315 of type class names. The empty intersection, i.e. the empty list, is therefore |
1315 of type class names. The empty intersection, i.e.\ the empty list, is therefore |
1316 inhabited by all types and is called the topsort. |
1316 inhabited by all types and is called the topsort. |
1317 |
1317 |
1318 Free and schematic type variables are always annotated with sorts, thereby restricting |
1318 Free and schematic type variables are always annotated with sorts, thereby restricting |
1319 the domain of types they quantify over and corresponding to an implicit hypothesis |
1319 the domain of types they quantify over and corresponding to an implicit hypothesis |
1320 about the type variable. |
1320 about the type variable. |
1346 |
1346 |
1347 |
1347 |
1348 section {* Type-Checking\label{sec:typechecking} *} |
1348 section {* Type-Checking\label{sec:typechecking} *} |
1349 |
1349 |
1350 text {* |
1350 text {* |
1351 Remember Isabelle follows the Church-style typing for terms, i.e., a term contains |
1351 Remember Isabelle follows the Church-style typing for terms, i.e.\ a term contains |
1352 enough typing information (constants, free variables and abstractions all have typing |
1352 enough typing information (constants, free variables and abstractions all have typing |
1353 information) so that it is always clear what the type of a term is. |
1353 information) so that it is always clear what the type of a term is. |
1354 Given a well-typed term, the function @{ML_ind type_of in Term} returns the |
1354 Given a well-typed term, the function @{ML_ind type_of in Term} returns the |
1355 type of a term. Consider for example: |
1355 type of a term. Consider for example: |
1356 |
1356 |