ProgTutorial/General.thy
changeset 347 01e71cddf6a3
parent 346 0fea8b7a14a1
child 348 2f2018927f2a
equal deleted inserted replaced
346:0fea8b7a14a1 347:01e71cddf6a3
     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 \mbox{or, the Good, the Bad and the Ugly} *}
    14 chapter {* Isabelle in More Detail *}
    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. The fundamental data
    19   everything else is build on top of this trusted core. The fundamental data
   691 "let
   691 "let
   692   val natT = @{typ \"nat\"}
   692   val natT = @{typ \"nat\"}
   693   val zero = @{term \"0::nat\"}
   693   val zero = @{term \"0::nat\"}
   694 in
   694 in
   695   cterm_of @{theory} 
   695   cterm_of @{theory} 
   696       (Const (@{const_name plus}, natT --> natT --> natT) $ zero $ zero)
   696       (Const (@{const_name plus}, [natT, natT] ---> natT) $ zero $ zero)
   697 end" "0 + 0"}
   697 end" "0 + 0"}
   698 
   698 
   699   In Isabelle not just terms need to be certified, but also types. For example, 
   699   In Isabelle not just terms need to be certified, but also types. For example, 
   700   you obtain the certified type for the Isabelle type @{typ "nat \<Rightarrow> bool"} on 
   700   you obtain the certified type for the Isabelle type @{typ "nat \<Rightarrow> bool"} on 
   701   the ML-level as follows:
   701   the ML-level as follows:
   777 text {* 
   777 text {* 
   778   If we print out the value of @{ML my_thm} then we see only the 
   778   If we print out the value of @{ML my_thm} then we see only the 
   779   final statement of the theorem.
   779   final statement of the theorem.
   780 
   780 
   781   @{ML_response_fake [display, gray]
   781   @{ML_response_fake [display, gray]
   782   "string_of_thm @{context} my_thm |> tracing"
   782   "string_of_thm @{context} my_thm 
       
   783 |> tracing"
   783   "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
   784   "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
   784 
   785 
   785   However, internally the code-snippet constructs the following 
   786   However, internally the code-snippet constructs the following 
   786   proof.
   787   proof.
   787 
   788 
   809      ((@{binding "my_thm"}, []), [my_thm]) #> snd *}
   810      ((@{binding "my_thm"}, []), [my_thm]) #> snd *}
   810 
   811 
   811 text {*
   812 text {*
   812   The first argument @{ML_ind theoremK in Thm} is a kind indicator, which
   813   The first argument @{ML_ind theoremK in Thm} is a kind indicator, which
   813   classifies the theorem. For a theorem arising from a definition we should
   814   classifies the theorem. For a theorem arising from a definition we should
   814   state @{ML_ind definitionK in Thm}, instead. The second argument is the 
   815   use @{ML_ind definitionK in Thm}, for an axiom @{ML_ind axiomK in Thm}, for
   815   name under which we store the theorem or theorems. The third can contain 
   816   ``normal'' theorems the kinds @{ML_ind theoremK in Thm} or @{ML_ind lemmaK
   816   a list of (theorem) attributes. Above it is empty, but if we want to store
   817   in Thm}.  The second argument is the name under which we store the theorem
   817   the theorem and at the same time add it to the simpset we have to declare:
   818   or theorems. The third can contain a list of (theorem) attributes. We will
       
   819   explain them in detail in Section~\ref{sec:attributes}. Below we
       
   820   use such an attribute in order add the theorem to the simpset. 
       
   821   have to declare:
   818 *}
   822 *}
   819 
   823 
   820 local_setup %gray {*
   824 local_setup %gray {*
   821   LocalTheory.note Thm.theoremK
   825   LocalTheory.note Thm.theoremK
   822      ((@{binding "my_thm_simp"}, 
   826     ((@{binding "my_thm_simp"}, 
   823           [Attrib.internal (K Simplifier.simp_add)]), 
   827        [Attrib.internal (K Simplifier.simp_add)]), [my_thm]) #> snd *}
   824              [my_thm]) #> snd *}
       
   825 
   828 
   826 text {*
   829 text {*
   827   Note that we have to use another name for the theorem, since Isabelle does
   830   Note that we have to use another name for the theorem, since Isabelle does
   828   not allow to add another theorem under the same name.  The attribute can be
   831   not allow to store another theorem under the same name. The attribute needs to
   829   given @{ML_ind internal in Attrib}. If we use the function @{ML
   832   be wrapped inside the function @{ML_ind internal in Attrib}. If we use the 
   830   get_thm_names_from_ss} from the previous chapter, we can check whether the
   833   function @{ML get_thm_names_from_ss} from the previous chapter, we can check 
   831   theorem has been added.
   834   whether the theorem has actually been added.
   832 
   835 
   833   @{ML_response [display,gray]
   836   @{ML_response [display,gray]
   834   "let
   837   "let
   835   fun pred s = match_string \"my_thm_simp\" s
   838   fun pred s = match_string \"my_thm_simp\" s
   836 in
   839 in
   837   exists pred (get_thm_names_from_ss @{simpset})
   840   exists pred (get_thm_names_from_ss @{simpset})
   838 end"
   841 end"
   839   "true"}
   842   "true"}
   840 
   843 
   841   Now the theorems @{thm [source] my_thm} and @{thm [source] my_thm_simp} can 
   844   The main point of storing the theorems @{thm [source] my_thm} and @{thm
   842   also be referenced  with the \isacommand{thm}-command on the user-level of 
   845   [source] my_thm_simp} is that they can now also be referenced with the
   843   Isabelle
   846   \isacommand{thm}-command on the user-level of Isabelle
       
   847 
   844 
   848 
   845   \begin{isabelle}
   849   \begin{isabelle}
   846   \isacommand{thm}~@{text "my_thm"}\isanewline
   850   \isacommand{thm}~@{text "my_thm"}\isanewline
   847    @{text ">"}~@{prop "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
   851    @{text ">"}~@{prop "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
   848   \end{isabelle}
   852   \end{isabelle}
   849 
   853 
   850   or with the @{text "@{thm \<dots>}"}-antiquotation on the ML-level. Note that the
   854   or with the @{text "@{thm \<dots>}"}-antiquotation on the ML-level. As can be seen
   851   theorem does not have any meta-variables that would be present if we proved
   855   the theorem does not have any meta-variables that would be present if we proved
   852   this theorem on the user-level. As we shall see later on, we have to construct
   856   this theorem on the user-level. We will see later on, we have to construct
   853   meta-variables explicitly.
   857   meta-variables in a theorem explicitly.
   854 
   858 *}
       
   859 
       
   860 text {*
   855   There is a multitude of functions that manage or manipulate theorems. For example 
   861   There is a multitude of functions that manage or manipulate theorems. For example 
   856   we can test theorems for (alpha) equality. Suppose you proved the following three 
   862   we can test theorems for (alpha) equality. Suppose you proved the following three 
   857   facts.
   863   facts.
   858 *}
   864 *}
   859 
   865 
   920   For the functions @{text "assume"}, @{text "forall_elim"} etc 
   926   For the functions @{text "assume"}, @{text "forall_elim"} etc 
   921   see \isccite{sec:thms}. The basic functions for theorems are defined in
   927   see \isccite{sec:thms}. The basic functions for theorems are defined in
   922   @{ML_file "Pure/thm.ML"}, @{ML_file "Pure/more_thm.ML"} and @{ML_file "Pure/drule.ML"}. 
   928   @{ML_file "Pure/thm.ML"}, @{ML_file "Pure/more_thm.ML"} and @{ML_file "Pure/drule.ML"}. 
   923   \end{readmore}
   929   \end{readmore}
   924 
   930 
       
   931   The simplifier can be used to unfold definition in theorms. To show
       
   932   this we build the theorem @{term "True \<equiv> True"} (Line 1) and then 
       
   933   unfold the constant according to its definition (Line 2).
       
   934 
       
   935   @{ML_response_fake [display,gray,linenos]
       
   936   "Thm.reflexive @{cterm \"True\"}
       
   937   |> Simplifier.rewrite_rule [@{thm True_def}]
       
   938   |> string_of_thm @{context}
       
   939   |> tracing"
       
   940   "(\<lambda>x. x) = (\<lambda>x. x) \<equiv> (\<lambda>x. x) = (\<lambda>x. x)"}
   925 
   941 
   926   Often it is necessary to transform theorems to and from the object 
   942   Often it is necessary to transform theorems to and from the object 
   927   logic.  For example, the function @{ML_ind rulify in ObjectLogic}
   943   logic.  For example, the function @{ML_ind rulify in ObjectLogic}
   928   replaces all @{text "\<longrightarrow>"} and @{text "\<forall>"} into the equivalents of the 
   944   replaces all @{text "\<longrightarrow>"} and @{text "\<forall>"} into the equivalents of the 
   929   meta logic. For example
   945   meta logic. For example
   944 end"
   960 end"
   945   "?A \<longrightarrow> ?B \<longrightarrow> ?C"}
   961   "?A \<longrightarrow> ?B \<longrightarrow> ?C"}
   946 
   962 
   947   In this code the function @{ML atomize in ObjectLogic} produces 
   963   In this code the function @{ML atomize in ObjectLogic} produces 
   948   a meta-equation between the given theorem and the theorem transformed
   964   a meta-equation between the given theorem and the theorem transformed
   949   into the object logic. The function @{ML_ind rewrite_rule in MetaSimplifier}
   965   into the object logic. The result is the theorem with object logic 
   950   unfolds this meta-equation in the given theorem. The result is
   966   connectives. However, in order to completely transform a theorem
   951   the theorem with object logic connectives.
   967   such as @{thm [source] list.induct}
   952 x
   968 
       
   969   @{thm [display] list.induct}
       
   970 
       
   971   we have to first abstract over the variables @{text "?P"} and 
       
   972   @{text "?list"}. For this we can use the function 
       
   973   @{ML_ind forall_intr_vars in Drule}. 
       
   974 *}
       
   975 
       
   976 ML{*fun atomize_thm thm =
       
   977 let
       
   978   val thm' = forall_intr_vars thm
       
   979   val thm'' = ObjectLogic.atomize (cprop_of thm')
       
   980 in
       
   981   MetaSimplifier.rewrite_rule [thm''] thm'
       
   982 end*}
       
   983 
       
   984 text {*
       
   985   For @{thm [source] list.induct} it produces:
       
   986 
       
   987   @{ML_response_fake [display, gray]
       
   988   "atomize_thm @{thm list.induct}"
       
   989   "\<forall>P list. P [] \<longrightarrow> (\<forall>a list. P list \<longrightarrow> P (a # list)) \<longrightarrow> P list"}
       
   990 
   953   Theorems can also be produced from terms by giving an explicit proof. 
   991   Theorems can also be produced from terms by giving an explicit proof. 
   954   One way to achive this is by using the function @{ML_ind prove in Goal}. 
   992   One way to achive this is by using the function @{ML_ind prove in Goal}. 
   955   For example
   993   For example
   956   
   994   
   957   @{ML_response_fake [display,gray]
   995   @{ML_response_fake [display,gray]