ProgTutorial/Essential.thy
changeset 415 dc76ba24e81b
parent 414 5fc2fb34c323
child 416 c6b5d1e25cdd
equal deleted inserted replaced
414:5fc2fb34c323 415:dc76ba24e81b
  1154   In Line 3 we obtain the higher-order matcher. We assume there is a finite
  1154   In Line 3 we obtain the higher-order matcher. We assume there is a finite
  1155   number of them and select the one we are interested in via the parameter 
  1155   number of them and select the one we are interested in via the parameter 
  1156   @{text i} in the next line. In Lines 5 and 6 we destruct the resulting 
  1156   @{text i} in the next line. In Lines 5 and 6 we destruct the resulting 
  1157   environments using the function @{ML_ind dest in Vartab}. Finally, we need 
  1157   environments using the function @{ML_ind dest in Vartab}. Finally, we need 
  1158   to map the functions @{ML prep_trm} and @{ML prep_ty} over the respective 
  1158   to map the functions @{ML prep_trm} and @{ML prep_ty} over the respective 
  1159   environments (Line 8). As a simple example we instantiate the 
  1159   environments (Line 8). As a simple example we instantiate the
  1160   @{thm [source] conjI}-rule as follows
  1160   @{thm [source] spec} rule so that its conclusion is of the form 
  1161 
  1161   @{term "Q True"}. 
  1162   @{ML_response_fake [gray,display] 
  1162  
       
  1163 
       
  1164   @{ML_response_fake [gray,display,linenos] 
  1163   "let  
  1165   "let  
  1164   val pat = Logic.strip_imp_concl (prop_of @{thm spec})
  1166   val pat = Logic.strip_imp_concl (prop_of @{thm spec})
  1165   val trm = @{term \"Trueprop (P True)\"}
  1167   val trm = @{term \"Trueprop (Q True)\"}
  1166   val inst = matcher_inst @{theory} pat trm 1
  1168   val inst = matcher_inst @{theory} pat trm 1
  1167 in
  1169 in
  1168   Drule.instantiate inst @{thm conjI}
  1170   Drule.instantiate inst @{thm spec}
  1169 end"
  1171 end"
  1170   "\<forall>x. P x \<Longrightarrow> P True"}
  1172   "\<forall>x. Q x \<Longrightarrow> Q True"}
  1171 
  1173 
       
  1174   Note that we had to insert a @{text "Trueprop"}-coercion in Line 3 since the 
       
  1175   conclusion of @{thm [source] spec} contains one.
       
  1176  
  1172   \begin{readmore}
  1177   \begin{readmore}
  1173   Unification and matching of higher-order patterns is implemented in 
  1178   Unification and matching of higher-order patterns is implemented in 
  1174   @{ML_file "Pure/pattern.ML"}. This file also contains a first-order matcher
  1179   @{ML_file "Pure/pattern.ML"}. This file also contains a first-order matcher
  1175   for terms. Full higher-order unification is implemented
  1180   for terms. Full higher-order unification is implemented
  1176   in @{ML_file "Pure/unify.ML"}. It uses lazy sequences which are implemented
  1181   in @{ML_file "Pure/unify.ML"}. It uses lazy sequences which are implemented
  1261   checking and pretty-printing of terms are defined. Functions related to
  1266   checking and pretty-printing of terms are defined. Functions related to
  1262   type-inference are implemented in @{ML_file "Pure/type.ML"} and 
  1267   type-inference are implemented in @{ML_file "Pure/type.ML"} and 
  1263   @{ML_file "Pure/type_infer.ML"}. 
  1268   @{ML_file "Pure/type_infer.ML"}. 
  1264   \end{readmore}
  1269   \end{readmore}
  1265 
  1270 
  1266   \footnote{\bf FIXME: say something about sorts.}
       
  1267   \footnote{\bf FIXME: give a ``readmore''.}
       
  1268 
  1271 
  1269   \begin{exercise}
  1272   \begin{exercise}
  1270   Check that the function defined in Exercise~\ref{fun:revsum} returns a
  1273   Check that the function defined in Exercise~\ref{fun:revsum} returns a
  1271   result that type-checks. See what happens to the  solutions of this 
  1274   result that type-checks. See what happens to the  solutions of this 
  1272   exercise given in Appendix \ref{ch:solutions} when they receive an 
  1275   exercise given in Appendix \ref{ch:solutions} when they receive an 
  1378 
  1381 
  1379 text {*
  1382 text {*
  1380   The corresponding ML-code is as follows:
  1383   The corresponding ML-code is as follows:
  1381 *}
  1384 *}
  1382 
  1385 
  1383 ML{*val my_thm = 
  1386 ML %linenosgray{*val my_thm = 
  1384 let
  1387 let
  1385   val assm1 = @{cprop "\<And>(x::nat). P x \<Longrightarrow> Q x"}
  1388   val assm1 = @{cprop "\<And>(x::nat). P x \<Longrightarrow> Q x"}
  1386   val assm2 = @{cprop "(P::nat \<Rightarrow> bool) t"}
  1389   val assm2 = @{cprop "(P::nat \<Rightarrow> bool) t"}
  1387 
  1390 
  1388   val Pt_implies_Qt = 
  1391   val Pt_implies_Qt = 
  1395   |> implies_intr assm2
  1398   |> implies_intr assm2
  1396   |> implies_intr assm1
  1399   |> implies_intr assm1
  1397 end*}
  1400 end*}
  1398 
  1401 
  1399 text {*
  1402 text {*
       
  1403   Note that in Line 3 and 4 we use the antiquotation @{text "@{cprop \<dots>}"}, which
       
  1404   inserts necessary @{text "Trueprop"}s.
       
  1405 
  1400   If we print out the value of @{ML my_thm} then we see only the 
  1406   If we print out the value of @{ML my_thm} then we see only the 
  1401   final statement of the theorem.
  1407   final statement of the theorem.
  1402 
  1408 
  1403   @{ML_response_fake [display, gray]
  1409   @{ML_response_fake [display, gray]
  1404   "tracing (string_of_thm @{context} my_thm)"
  1410   "tracing (string_of_thm @{context} my_thm)"
  1422 
  1428 
  1423   While we obtained a theorem as result, this theorem is not
  1429   While we obtained a theorem as result, this theorem is not
  1424   yet stored in Isabelle's theorem database. Consequently, it cannot be 
  1430   yet stored in Isabelle's theorem database. Consequently, it cannot be 
  1425   referenced on the user level. One way to store it in the theorem database is
  1431   referenced on the user level. One way to store it in the theorem database is
  1426   by using the function @{ML_ind note in Local_Theory}.\footnote{\bf FIXME: make sure a pointer
  1432   by using the function @{ML_ind note in Local_Theory}.\footnote{\bf FIXME: make sure a pointer
  1427   to the section about local-setup is given earlier.}
  1433   to the section about local-setup is given here.}
  1428 *}
  1434 *}
  1429 
  1435 
  1430 local_setup %gray {*
  1436 local_setup %gray {*
  1431   Local_Theory.note ((@{binding "my_thm"}, []), [my_thm]) #> snd *}
  1437   Local_Theory.note ((@{binding "my_thm"}, []), [my_thm]) #> snd *}
  1432 
  1438 
  1466   [source] my_thm_simp} is that they can now also be referenced with the
  1472   [source] my_thm_simp} is that they can now also be referenced with the
  1467   \isacommand{thm}-command on the user-level of Isabelle
  1473   \isacommand{thm}-command on the user-level of Isabelle
  1468 
  1474 
  1469 
  1475 
  1470   \begin{isabelle}
  1476   \begin{isabelle}
  1471   \isacommand{thm}~@{text "my_thm"}\isanewline
  1477   \isacommand{thm}~@{text "my_thm my_thm_simp"}\isanewline
  1472    @{text ">"}~@{prop "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
  1478   @{text ">"}~@{prop "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}\isanewline
       
  1479   @{text ">"}~@{prop "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
  1473   \end{isabelle}
  1480   \end{isabelle}
  1474 
  1481 
  1475   or with the @{text "@{thm \<dots>}"}-antiquotation on the ML-level. Otherwise the 
  1482   or with the @{text "@{thm \<dots>}"}-antiquotation on the ML-level. Otherwise the 
  1476   user has no access to these theorems. 
  1483   user has no access to these theorems. 
  1477 
  1484 
  1542 
  1549 
  1543 text {*
  1550 text {*
  1544   we still obtain the same list. The reason is that we used the function @{ML_ind
  1551   we still obtain the same list. The reason is that we used the function @{ML_ind
  1545   add_thm in Thm} in our update function. This is a dedicated function which
  1552   add_thm in Thm} in our update function. This is a dedicated function which
  1546   tests whether the theorem is already in the list.  This test is done
  1553   tests whether the theorem is already in the list.  This test is done
  1547   according to alpha-equivalence of the proposition behind the theorem. The
  1554   according to alpha-equivalence of the proposition of the theorem. The
  1548   corresponding testing function is @{ML_ind eq_thm_prop in Thm}.
  1555   corresponding testing function is @{ML_ind eq_thm_prop in Thm}.
  1549   Suppose you proved the following three theorems.
  1556   Suppose you proved the following three theorems.
  1550 *}
  1557 *}
  1551 
  1558 
  1552 lemma
  1559 lemma
  1601 "Premises: ?A, ?B
  1608 "Premises: ?A, ?B
  1602 Conclusion: ?C
  1609 Conclusion: ?C
  1603 Premises: 
  1610 Premises: 
  1604 Conclusion: ?A \<longrightarrow> ?B \<longrightarrow> ?C"}
  1611 Conclusion: ?A \<longrightarrow> ?B \<longrightarrow> ?C"}
  1605 
  1612 
  1606   Note that in the second case, there is no premise.
  1613   Note that in the second case, there is no premise. The reason is that @{text "\<Longrightarrow>"}
       
  1614   separates premises and conclusion, while @{text "\<longrightarrow>"} is the object implication
       
  1615   from HOL, which just constructs a formula.
  1607 
  1616 
  1608   \begin{readmore}
  1617   \begin{readmore}
  1609   The basic functions for theorems are defined in
  1618   The basic functions for theorems are defined in
  1610   @{ML_file "Pure/thm.ML"}, @{ML_file "Pure/more_thm.ML"} and @{ML_file "Pure/drule.ML"}. 
  1619   @{ML_file "Pure/thm.ML"}, @{ML_file "Pure/more_thm.ML"} and @{ML_file "Pure/drule.ML"}. 
  1611   \end{readmore}
  1620   \end{readmore}
  1711   reasons. An example of this function is as follows:
  1720   reasons. An example of this function is as follows:
  1712 
  1721 
  1713   @{ML_response_fake [display, gray]
  1722   @{ML_response_fake [display, gray]
  1714   "Skip_Proof.make_thm @{theory} @{prop \"True = False\"}"
  1723   "Skip_Proof.make_thm @{theory} @{prop \"True = False\"}"
  1715   "True = False"}
  1724   "True = False"}
       
  1725 
       
  1726   \begin{readmore}
       
  1727   Functions that setup goal states and prove theorems are implemented in 
       
  1728   @{ML_file "Pure/goal.ML"}. A function and a tactic that allow one to
       
  1729   skip proofs of theorems are implemented in @{ML_file "Pure/Isar/skip_proof.ML"}.
       
  1730   \end{readmore}
  1716 
  1731 
  1717   Theorems also contain auxiliary data, such as the name of the theorem, its
  1732   Theorems also contain auxiliary data, such as the name of the theorem, its
  1718   kind, the names for cases and so on. This data is stored in a string-string
  1733   kind, the names for cases and so on. This data is stored in a string-string
  1719   list and can be retrieved with the function @{ML_ind get_tags in
  1734   list and can be retrieved with the function @{ML_ind get_tags in
  1720   Thm}. Assume you prove the following lemma.
  1735   Thm}. Assume you prove the following lemma.