CookBook/FirstSteps.thy
changeset 54 1783211b3494
parent 52 a04bdee4fb1e
child 58 f3794c231898
equal deleted inserted replaced
53:0c3580c831a4 54:1783211b3494
     5 
     5 
     6 chapter {* First Steps *}
     6 chapter {* First Steps *}
     7 
     7 
     8 text {*
     8 text {*
     9 
     9 
    10   Isabelle programming is done in SML.  Just like lemmas and proofs, SML-code
    10   Isabelle programming is done in ML.  Just like lemmas and proofs, ML-code
    11   in Isabelle is part of a theory. If you want to follow the code written in
    11   in Isabelle is part of a theory. If you want to follow the code written in
    12   this chapter, we assume you are working inside the theory defined by
    12   this chapter, we assume you are working inside the theory starting with
    13 
    13 
    14   \begin{center}
    14   \begin{center}
    15   \begin{tabular}{@ {}l}
    15   \begin{tabular}{@ {}l}
    16   \isacommand{theory} FirstSteps\\
    16   \isacommand{theory} FirstSteps\\
    17   \isacommand{imports} Main\\
    17   \isacommand{imports} Main\\
    47   export it to a separate ML-file. Such files can then be included in a 
    47   export it to a separate ML-file. Such files can then be included in a 
    48   theory by using \isacommand{uses} in the header of the theory, like
    48   theory by using \isacommand{uses} in the header of the theory, like
    49 
    49 
    50   \begin{center}
    50   \begin{center}
    51   \begin{tabular}{@ {}l}
    51   \begin{tabular}{@ {}l}
    52   \isacommand{theory} CookBook\\
    52   \isacommand{theory} FirstSteps\\
    53   \isacommand{imports} Main\\
    53   \isacommand{imports} Main\\
    54   \isacommand{uses} @{ML_text "\"file_to_be_included.ML\""} @{text "\<dots>"}\\
    54   \isacommand{uses} @{ML_text "\"file_to_be_included.ML\""} @{text "\<dots>"}\\
    55   \isacommand{begin}\\
    55   \isacommand{begin}\\
    56   \ldots
    56   \ldots
    57   \end{tabular}
    57   \end{tabular}
    80   a function.
    80   a function.
    81 
    81 
    82   The function @{ML "warning"} should only be used for testing purposes, because any
    82   The function @{ML "warning"} should only be used for testing purposes, because any
    83   output this function generates will be overwritten as soon as an error is
    83   output this function generates will be overwritten as soon as an error is
    84   raised. For printing anything more serious and elaborate, the
    84   raised. For printing anything more serious and elaborate, the
    85   function @{ML tracing} should be used. This function writes all output into
    85   function @{ML tracing} is more appropriate. This function writes all output into
    86   a separate tracing buffer. For example
    86   a separate tracing buffer. For example
    87 
    87 
    88   @{ML [display] "tracing \"foo\""}
    88   @{ML [display] "tracing \"foo\""}
    89 
    89 
    90   It is also possible to redirect the ``channel'' where the @{ML_text "foo"} is 
    90   It is also possible to redirect the ``channel'' where the string @{ML_text "foo"} is 
    91   printed to a separate file, e.g. to prevent Proof General from choking on massive 
    91   printed to a separate file, e.g. to prevent Proof General from choking on massive 
    92   amounts of trace output. This redirection can be achieved using the code
    92   amounts of trace output. This redirection can be achieved using the code
    93 *}
    93 *}
    94 
    94 
    95 ML{* 
    95 ML{* 
   150   some data structure and return it. Instead, it is literally
   150   some data structure and return it. Instead, it is literally
   151   replaced with the value representing the theory name.
   151   replaced with the value representing the theory name.
   152 
   152 
   153   In a similar way you can use antiquotations to refer to theorems or simpsets:
   153   In a similar way you can use antiquotations to refer to theorems or simpsets:
   154 
   154 
   155   @{ML [display] "@{thm allI}"}
   155   @{ML_response_fake [display] "@{thm allI}" "(\<And>x. ?P x) \<Longrightarrow> \<forall>x. ?P x"}
   156   @{ML [display] "@{simpset}"}
   156   @{ML_response_fake [display] "@{simpset}" "\<dots>"}
       
   157 
       
   158   (FIXME: what does a simpset look like? It seems to be the same problem
       
   159   like tokens.)
   157 
   160 
   158   While antiquotations have many applications, they were originally introduced to 
   161   While antiquotations have many applications, they were originally introduced to 
   159   avoid explicit bindings for theorems such as
   162   avoid explicit bindings for theorems such as
   160 *}
   163 *}
   161 
   164 
   239 
   242 
   240   @{ML_response_fake [display] "@{typ \"bool \<Rightarrow> nat\"}" "bool \<Rightarrow> nat"}
   243   @{ML_response_fake [display] "@{typ \"bool \<Rightarrow> nat\"}" "bool \<Rightarrow> nat"}
   241 
   244 
   242   \begin{readmore}
   245   \begin{readmore}
   243   Types are described in detail in \isccite{sec:types}. Their
   246   Types are described in detail in \isccite{sec:types}. Their
   244   definition and many useful operations can be found in @{ML_file "Pure/type.ML"}.
   247   definition and many useful operations can also be found in @{ML_file "Pure/type.ML"}.
   245   \end{readmore}
   248   \end{readmore}
   246 *}
   249 *}
   247 
   250 
   248 
   251 
   249 section {* Constructing Terms and Types Manually *} 
   252 section {* Constructing Terms and Types Manually *} 
   348 
   351 
   349 text {* 
   352 text {* 
   350   
   353   
   351   We can freely construct and manipulate terms, since they are just
   354   We can freely construct and manipulate terms, since they are just
   352   arbitrary unchecked trees. However, we eventually want to see if a
   355   arbitrary unchecked trees. However, we eventually want to see if a
   353   term is well-formed, or type checks, relative to a theory.
   356   term is well-formed, or type-checks, relative to a theory.
   354   Type-checking is done via the function @{ML cterm_of}, which converts 
   357   Type-checking is done via the function @{ML cterm_of}, which converts 
   355   a @{ML_type term} into a  @{ML_type cterm}, a \emph{certified} term. 
   358   a @{ML_type term} into a  @{ML_type cterm}, a \emph{certified} term. 
   356   Unlike @{ML_type term}s, which are just trees, @{ML_type
   359   Unlike @{ML_type term}s, which are just trees, @{ML_type
   357   "cterm"}s are abstract objects that are guaranteed to be
   360   "cterm"}s are abstract objects that are guaranteed to be
   358   type-correct, and that can only be constructed via the ``official
   361   type-correct, and that can only be constructed via the ``official
   359   interfaces''.
   362   interfaces''.
   360 
   363 
   361   Type checking is always relative to a theory context. For now we use
   364   Type-checking is always relative to a theory context. For now we use
   362   the @{ML "@{theory}"} antiquotation to get hold of the current theory.
   365   the @{ML "@{theory}"} antiquotation to get hold of the current theory.
   363   For example we can write
   366   For example we can write
   364 
   367 
   365   @{ML_response_fake [display] "cterm_of @{theory} @{term \"a + b = c\"}" "a + b = c"}
   368   @{ML_response_fake [display] "cterm_of @{theory} @{term \"a + b = c\"}" "a + b = c"}
   366 
   369 
   367   or use the antiquotation
   370   or use the antiquotation
   368 
   371 
   369   @{ML_response_fake [display] "@{cterm \"(a::nat) + b = c\"}" "a + b = c"}
   372   @{ML_response_fake [display] "@{cterm \"(a::nat) + b = c\"}" "a + b = c"}
   370 
   373 
   371   A slightly more elaborate example is
   374   Attempting
       
   375 
       
   376   @{ML_response_fake_both [display] "@{cterm \"1 + True\"}" "Type unification failed \<dots>"}
       
   377 
       
   378   yields an error. A slightly more elaborate example is
   372 
   379 
   373 @{ML_response_fake [display] 
   380 @{ML_response_fake [display] 
   374 "let
   381 "let
   375   val natT = @{typ \"nat\"}
   382   val natT = @{typ \"nat\"}
   376   val zero = @{term \"0::nat\"}
   383   val zero = @{term \"0::nat\"}
   389 
   396 
   390 section {* Theorems *}
   397 section {* Theorems *}
   391 
   398 
   392 text {*
   399 text {*
   393   Just like @{ML_type cterm}s, theorems are abstract objects of type @{ML_type thm} 
   400   Just like @{ML_type cterm}s, theorems are abstract objects of type @{ML_type thm} 
   394   that can only be built by going through interfaces, which means that all your proofs 
   401   that can only be built by going through interfaces. In effect all proofs 
   395   will be checked. 
   402   are checked. 
   396 
   403 
   397   To see theorems in ``action'', let us give a proof for the following statement
   404   To see theorems in ``action'', let us give a proof for the following statement
   398 *}
   405 *}
   399 
   406 
   400   lemma 
   407   lemma 
   470   @{text "#"} wrapped around it, which prevents that premises are 
   477   @{text "#"} wrapped around it, which prevents that premises are 
   471   misinterpreted as open subgoals. The wrapper @{text "# :: prop \<Rightarrow>
   478   misinterpreted as open subgoals. The wrapper @{text "# :: prop \<Rightarrow>
   472   prop"} is just the identity function and used as a syntactic marker. 
   479   prop"} is just the identity function and used as a syntactic marker. 
   473   
   480   
   474   \begin{readmore}
   481   \begin{readmore}
   475   For more on goals see \isccite{sec:tactical-goals}. 
   482   For more on goals see \isccite{sec:tactical-goals}. (FIXME: in which
       
   483   file is most code for dealing with goals?)
   476   \end{readmore}
   484   \end{readmore}
   477 
   485 
   478   Tactics are functions that map a goal state to a (lazy)
   486   Tactics are functions that map a goal state to a (lazy)
   479   sequence of successor states, hence the type of a tactic is
   487   sequence of successor states, hence the type of a tactic is
   480   @{ML_type[display] "thm -> thm Seq.seq"}
   488   @{ML_type[display] "thm -> thm Seq.seq"}
   523     THEN resolve_tac [disjI1] 1
   531     THEN resolve_tac [disjI1] 1
   524     THEN assume_tac 1)
   532     THEN assume_tac 1)
   525 end" "?P \<or> ?Q \<Longrightarrow> ?Q \<or> ?P"}
   533 end" "?P \<or> ?Q \<Longrightarrow> ?Q \<or> ?P"}
   526 
   534 
   527   \begin{readmore}
   535   \begin{readmore}
   528   To learn more about the function @{ML Goal.prove} see \isccite{sec:results}.
   536   To learn more about the function @{ML Goal.prove} see \isccite{sec:results} and
       
   537   the file @{ML_file "Pure/goal.ML"}.
   529   \end{readmore}
   538   \end{readmore}
   530 
   539 
   531 
   540 
   532   An alternative way to transcribe this proof is as follows 
   541   An alternative way to transcribe this proof is as follows 
   533 
   542