ProgTutorial/General.thy
changeset 358 9cf3bc448210
parent 356 43df2d59fb98
child 359 be6538c7b41d
equal deleted inserted replaced
357:80b56d9b322f 358:9cf3bc448210
     9   ["theory General", "imports Base FirstSteps", "begin"]
     9   ["theory General", "imports Base FirstSteps", "begin"]
    10 *}
    10 *}
    11 (*>*)
    11 (*>*)
    12 
    12 
    13 
    13 
    14 chapter {* Isabelle in More Detail *}
    14 chapter {* Isabelle Essentials *}
    15 
    15 
    16 text {*
    16 text {*
    17   Isabelle is build around a few central ideas. One central idea is the
    17   Isabelle is build around a few central ideas. One central idea is the
    18   LCF-approach to theorem proving where there is a small trusted core and
    18   LCF-approach to theorem proving where there is a small trusted core and
    19   everything else is build on top of this trusted core 
    19   everything else is build on top of this trusted core 
   884   or with the @{text "@{thm \<dots>}"}-antiquotation on the ML-level. Otherwise the 
   884   or with the @{text "@{thm \<dots>}"}-antiquotation on the ML-level. Otherwise the 
   885   user has no access to these theorems. 
   885   user has no access to these theorems. 
   886 
   886 
   887   Recall that Isabelle does not let you call @{ML note in LocalTheory} twice
   887   Recall that Isabelle does not let you call @{ML note in LocalTheory} twice
   888   with the same theorem name. In effect, once a theorem is stored under a name, 
   888   with the same theorem name. In effect, once a theorem is stored under a name, 
   889   this association will be fixed. While this is a ``safety-net'' to make sure a
   889   this association is fixed. While this is a ``safety-net'' to make sure a
   890   theorem name refers to a particular theorem or collection of theorems, it is 
   890   theorem name refers to a particular theorem or collection of theorems, it is 
   891   also a bit too restrictive in cases where a theorem name should refer to a 
   891   also a bit too restrictive in cases where a theorem name should refer to a 
   892   dynamically expanding list of theorems (like a simpset). Therefore Isabelle 
   892   dynamically expanding list of theorems (like a simpset). Therefore Isabelle 
   893   also implements a mechanism where a theorem name can refer to a custom theorem 
   893   also implements a mechanism where a theorem name can refer to a custom theorem 
   894   list. For this you can use the function @{ML_ind add_thms_dynamic in PureThy}. 
   894   list. For this you can use the function @{ML_ind add_thms_dynamic in PureThy}. 
   895   To see how it works let us assume we defined our own theorem list maintained 
   895   To see how it works let us assume we defined our own theorem list @{text MyThmList}.
   896   in the data storage @{text MyThmList}.
       
   897 *}
   896 *}
   898 
   897 
   899 ML{*structure MyThmList = GenericDataFun
   898 ML{*structure MyThmList = GenericDataFun
   900   (type T = thm list
   899   (type T = thm list
   901    val empty = []
   900    val empty = []
   943   \isacommand{thm}~@{text "mythmlist"}\\
   942   \isacommand{thm}~@{text "mythmlist"}\\
   944   @{text "> False \<Longrightarrow> ?P"}\\
   943   @{text "> False \<Longrightarrow> ?P"}\\
   945   @{text "> True"}
   944   @{text "> True"}
   946   \end{isabelle}
   945   \end{isabelle}
   947 
   946 
   948   There is a multitude of functions that manage or manipulate theorems in the 
   947   There is a multitude of functions in the structures @{ML_struct Thm} and @{ML_struct Drule} 
   949   structures @{ML_struct Thm} and @{ML_struct Drule}. For example 
   948   for managing or manipulating theorems. For example 
   950   we can test theorems for alpha equality. Suppose you proved the following three 
   949   we can test theorems for alpha equality. Suppose you proved the following three 
   951   theorems.
   950   theorems.
   952 *}
   951 *}
   953 
   952 
   954 lemma
   953 lemma
  1006 Conclusion: ?A \<longrightarrow> ?B \<longrightarrow> ?C"}
  1005 Conclusion: ?A \<longrightarrow> ?B \<longrightarrow> ?C"}
  1007 
  1006 
  1008   Note that in the second case, there is no premise.
  1007   Note that in the second case, there is no premise.
  1009 
  1008 
  1010   \begin{readmore}
  1009   \begin{readmore}
  1011   For the functions @{text "assume"}, @{text "forall_elim"} etc 
  1010   The basic functions for theorems are defined in
  1012   see \isccite{sec:thms}. The basic functions for theorems are defined in
       
  1013   @{ML_file "Pure/thm.ML"}, @{ML_file "Pure/more_thm.ML"} and @{ML_file "Pure/drule.ML"}. 
  1011   @{ML_file "Pure/thm.ML"}, @{ML_file "Pure/more_thm.ML"} and @{ML_file "Pure/drule.ML"}. 
  1014   \end{readmore}
  1012   \end{readmore}
  1015 
  1013 
  1016   The simplifier can be used to unfold definition in theorems. To show
  1014   The simplifier can be used to unfold definition in theorems. To show
  1017   this we build the theorem @{term "True \<equiv> True"} (Line 1) and then 
  1015   this we build the theorem @{term "True \<equiv> True"} (Line 1) and then 
  1189   readers. For such situations Isabelle allows the installation of \emph{\index*{theorem 
  1187   readers. For such situations Isabelle allows the installation of \emph{\index*{theorem 
  1190   styles}}. These are, roughly speaking, functions from terms to terms. The input 
  1188   styles}}. These are, roughly speaking, functions from terms to terms. The input 
  1191   term stands for the theorem to be presented; the output can be constructed to
  1189   term stands for the theorem to be presented; the output can be constructed to
  1192   ones wishes.  Let us, for example, assume we want to quote theorems without
  1190   ones wishes.  Let us, for example, assume we want to quote theorems without
  1193   leading @{text \<forall>}-quantifiers. For this we can implement the following function 
  1191   leading @{text \<forall>}-quantifiers. For this we can implement the following function 
  1194   that strips off meta-quantifiers.
  1192   that strips off @{text "\<forall>"}s.
  1195 *}
  1193 *}
  1196 
  1194 
  1197 ML %linenosgray {*fun strip_allq (Const (@{const_name "All"}, _) $ Abs body) = 
  1195 ML %linenosgray{*fun strip_allq (Const (@{const_name "All"}, _) $ Abs body) = 
  1198       Term.dest_abs body |> snd |> strip_allq
  1196       Term.dest_abs body |> snd |> strip_allq
  1199   | strip_allq (Const (@{const_name "Trueprop"}, _) $ t) = 
  1197   | strip_allq (Const (@{const_name "Trueprop"}, _) $ t) = 
  1200       strip_allq t
  1198       strip_allq t
  1201   | strip_allq t = t*}
  1199   | strip_allq t = t*}
  1202 
  1200 
  1241   giving a list of strings that should be used for the replacement of the
  1239   giving a list of strings that should be used for the replacement of the
  1242   variables. For this we implement the function which takes a list of strings
  1240   variables. For this we implement the function which takes a list of strings
  1243   and uses them as name in the outermost abstractions.
  1241   and uses them as name in the outermost abstractions.
  1244 *}
  1242 *}
  1245 
  1243 
  1246 ML {*fun rename_allq [] t = t
  1244 ML{*fun rename_allq [] t = t
  1247   | rename_allq (x::xs) (Const (@{const_name "All"}, U) $ Abs (_, T, t)) = 
  1245   | rename_allq (x::xs) (Const (@{const_name "All"}, U) $ Abs (_, T, t)) = 
  1248       Const (@{const_name "All"}, U) $ Abs (x, T, rename_allq xs t)
  1246       Const (@{const_name "All"}, U) $ Abs (x, T, rename_allq xs t)
  1249   | rename_allq xs (Const (@{const_name "Trueprop"}, U) $ t) =
  1247   | rename_allq xs (Const (@{const_name "Trueprop"}, U) $ t) =
  1250       rename_allq xs t
  1248       rename_allq xs t
  1251   | rename_allq _ t = t*}
  1249   | rename_allq _ t = t*}
  1504   @{ML_file "Pure/Isar/attrib.ML"}...also explained in the chapter about
  1502   @{ML_file "Pure/Isar/attrib.ML"}...also explained in the chapter about
  1505   parsing.
  1503   parsing.
  1506   \end{readmore}
  1504   \end{readmore}
  1507 *}
  1505 *}
  1508 
  1506 
  1509 section {* Setups (TBD) *}
  1507 section {* Theories\label{sec:theories} (TBD) *}
  1510 
  1508 
  1511 text {*
  1509 
  1512   In the previous section we used \isacommand{setup} in order to make
  1510 text {*
  1513   a theorem attribute known to Isabelle. What happens behind the scenes
  1511   Theories are the most fundamental building blocks in Isabelle. They 
  1514   is that \isacommand{setup} expects a function of type 
  1512   contain definitions, syntax declarations, axioms, proofs etc. If a definition
  1515   @{ML_type "theory -> theory"}: the input theory is the current theory and the 
  1513   is stated, it must be stored in a theory in order to be usable later
  1516   output the theory where the theory attribute has been stored.
  1514   on. Similar with proofs: once a proof is finished, the proved theorem
  1517   
  1515   needs to be stored in the theorem database of the theory in order to
  1518   This is a fundamental principle in Isabelle. A similar situation occurs 
  1516   be usable. All relevant data of a theort can be querried as follows.
  1519   for example with declaring constants. The function that declares a 
       
  1520   constant on the ML-level is @{ML_ind  add_consts_i in Sign}. 
       
  1521   If you write\footnote{Recall that ML-code  needs to be 
       
  1522   enclosed in \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}.} 
       
  1523 *}  
       
  1524 
       
  1525 ML{*Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] @{theory} *}
       
  1526 
       
  1527 text {*
       
  1528   for declaring the constant @{text "BAR"} with type @{typ nat} and 
       
  1529   run the code, then you indeed obtain a theory as result. But if you 
       
  1530   query the constant on the Isabelle level using the command \isacommand{term}
       
  1531 
  1517 
  1532   \begin{isabelle}
  1518   \begin{isabelle}
  1533   \isacommand{term}~@{text [quotes] "BAR"}\\
  1519   \isacommand{print\_theory}\\
  1534   @{text "> \"BAR\" :: \"'a\""}
  1520   @{text "> names: Pure Code_Generator HOL \<dots>"}\\
       
  1521   @{text "> classes: Inf < type \<dots>"}\\
       
  1522   @{text "> default sort: type"}\\
       
  1523   @{text "> syntactic types: #prop \<dots>"}\\
       
  1524   @{text "> logical types: 'a \<times> 'b \<dots>"}\\
       
  1525   @{text "> type arities: * :: (random, random) random \<dots>"}\\
       
  1526   @{text "> logical constants: == :: 'a \<Rightarrow> 'a \<Rightarrow> prop \<dots>"}\\
       
  1527   @{text "> abbreviations: \<dots>"}\\
       
  1528   @{text "> axioms: \<dots>"}\\
       
  1529   @{text "> oracles: \<dots>"}\\
       
  1530   @{text "> definitions: \<dots>"}\\
       
  1531   @{text "> theorems: \<dots>"}
  1535   \end{isabelle}
  1532   \end{isabelle}
  1536 
  1533 
  1537   you do not obtain a constant of type @{typ nat}, but a free variable (printed in 
  1534   \begin{center}
  1538   blue) of polymorphic type. The problem is that the ML-expression above did 
  1535   \begin{tikzpicture}
  1539   not register the declaration with the current theory. This is what the command
  1536   \node[top color=white, bottom color=gray!30, draw=black!100, drop shadow] {A};
  1540   \isacommand{setup} is for. The constant is properly declared with
  1537   \end{tikzpicture}
  1541 *}
  1538   \end{center}
  1542 
  1539 
  1543 setup %gray {* Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] *}
       
  1544 
       
  1545 text {* 
       
  1546   Now 
       
  1547   
       
  1548   \begin{isabelle}
       
  1549   \isacommand{term}~@{text [quotes] "BAR"}\\
       
  1550   @{text "> \"BAR\" :: \"nat\""}
       
  1551   \end{isabelle}
       
  1552 
       
  1553   returns a (black) constant with the type @{typ nat}.
       
  1554 
       
  1555   A similar command is \isacommand{local\_setup}, which expects a function
       
  1556   of type @{ML_type "local_theory -> local_theory"}. Later on we will also
       
  1557   use the commands \isacommand{method\_setup} for installing methods in the
       
  1558   current theory and \isacommand{simproc\_setup} for adding new simprocs to
       
  1559   the current simpset.
       
  1560 *}
       
  1561 
       
  1562 section {* Theories\label{sec:theories} (TBD) *}
       
  1563 
       
  1564 text {*
       
  1565   There are theories, proof contexts and local theories (in this order, if you
       
  1566   want to order them). 
       
  1567 
  1540 
  1568   In contrast to an ordinary theory, which simply consists of a type
  1541   In contrast to an ordinary theory, which simply consists of a type
  1569   signature, as well as tables for constants, axioms and theorems, a local
  1542   signature, as well as tables for constants, axioms and theorems, a local
  1570   theory contains additional context information, such as locally fixed
  1543   theory contains additional context information, such as locally fixed
  1571   variables and local assumptions that may be used by the package. The type
  1544   variables and local assumptions that may be used by the package. The type
  1572   @{ML_type local_theory} is identical to the type of \emph{proof contexts}
  1545   @{ML_type local_theory} is identical to the type of \emph{proof contexts}
  1573   @{ML_type "Proof.context"}, although not every proof context constitutes a
  1546   @{ML_type "Proof.context"}, although not every proof context constitutes a
  1574   valid local theory.
  1547   valid local theory.
       
  1548 *}
       
  1549 
       
  1550 section {* Setups (TBD) *}
       
  1551 
       
  1552 text {*
       
  1553   In the previous section we used \isacommand{setup} in order to make
       
  1554   a theorem attribute known to Isabelle. What happens behind the scenes
       
  1555   is that \isacommand{setup} expects a function of type 
       
  1556   @{ML_type "theory -> theory"}: the input theory is the current theory and the 
       
  1557   output the theory where the theory attribute has been stored.
       
  1558   
       
  1559   This is a fundamental principle in Isabelle. A similar situation occurs 
       
  1560   for example with declaring constants. The function that declares a 
       
  1561   constant on the ML-level is @{ML_ind  add_consts_i in Sign}. 
       
  1562   If you write\footnote{Recall that ML-code  needs to be 
       
  1563   enclosed in \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}.} 
       
  1564 *}  
       
  1565 
       
  1566 ML{*Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] @{theory} *}
       
  1567 
       
  1568 text {*
       
  1569   for declaring the constant @{text "BAR"} with type @{typ nat} and 
       
  1570   run the code, then you indeed obtain a theory as result. But if you 
       
  1571   query the constant on the Isabelle level using the command \isacommand{term}
       
  1572 
       
  1573   \begin{isabelle}
       
  1574   \isacommand{term}~@{text [quotes] "BAR"}\\
       
  1575   @{text "> \"BAR\" :: \"'a\""}
       
  1576   \end{isabelle}
       
  1577 
       
  1578   you do not obtain a constant of type @{typ nat}, but a free variable (printed in 
       
  1579   blue) of polymorphic type. The problem is that the ML-expression above did 
       
  1580   not register the declaration with the current theory. This is what the command
       
  1581   \isacommand{setup} is for. The constant is properly declared with
       
  1582 *}
       
  1583 
       
  1584 setup %gray {* Sign.add_consts_i [(@{binding "BAR"}, @{typ "nat"}, NoSyn)] *}
       
  1585 
       
  1586 text {* 
       
  1587   Now 
       
  1588   
       
  1589   \begin{isabelle}
       
  1590   \isacommand{term}~@{text [quotes] "BAR"}\\
       
  1591   @{text "> \"BAR\" :: \"nat\""}
       
  1592   \end{isabelle}
       
  1593 
       
  1594   returns a (black) constant with the type @{typ nat}.
       
  1595 
       
  1596   A similar command is \isacommand{local\_setup}, which expects a function
       
  1597   of type @{ML_type "local_theory -> local_theory"}. Later on we will also
       
  1598   use the commands \isacommand{method\_setup} for installing methods in the
       
  1599   current theory and \isacommand{simproc\_setup} for adding new simprocs to
       
  1600   the current simpset.
  1575 *}
  1601 *}
  1576 
  1602 
  1577 section {* Contexts (TBD) *}
  1603 section {* Contexts (TBD) *}
  1578 
  1604 
  1579 section {* Local Theories (TBD) *}
  1605 section {* Local Theories (TBD) *}