ProgTutorial/FirstSteps.thy
changeset 343 8f73e80c8c6f
parent 339 c588e8422737
child 344 83d5bca38bec
equal deleted inserted replaced
342:930b1308fd96 343:8f73e80c8c6f
    11   ``We will most likely never realize the full importance of painting the Tower,\\ 
    11   ``We will most likely never realize the full importance of painting the Tower,\\ 
    12   that it is the essential element in the conservation of metal works and the\\ 
    12   that it is the essential element in the conservation of metal works and the\\ 
    13   more meticulous the paint job, the longer the Tower shall endure.''} \\[1ex]
    13   more meticulous the paint job, the longer the Tower shall endure.''} \\[1ex]
    14   Gustave Eiffel, In his book {\em The 300-Meter Tower}.\footnote{The Eiffel Tower has been 
    14   Gustave Eiffel, In his book {\em The 300-Meter Tower}.\footnote{The Eiffel Tower has been 
    15   re-painted 18 times since its initial construction, an average of once every 
    15   re-painted 18 times since its initial construction, an average of once every 
    16   seven years. It takes more than a year for a team of 25 painters to paint the Tower 
    16   seven years. It takes more than a year for a team of 25 painters to paint the tower 
    17   from top to bottom.}
    17   from top to bottom.}
    18   \end{flushright}
    18   \end{flushright}
    19 
    19 
    20   \medskip
    20   \medskip
    21   Isabelle programming is done in ML. Just like lemmas and proofs, ML-code for
    21   Isabelle programming is done in ML. Just like lemmas and proofs, ML-code for
    80   \isacommand{oops}
    80   \isacommand{oops}
    81   \end{isabelle}
    81   \end{isabelle}
    82   \end{quote}
    82   \end{quote}
    83 
    83 
    84   However, both commands will only play minor roles in this tutorial (we will
    84   However, both commands will only play minor roles in this tutorial (we will
    85   always arrange that the ML-code is defined outside of proofs, for example).
    85   always arrange that the ML-code is defined outside of proofs).
    86 
    86 
    87   Once a portion of code is relatively stable, you usually want to export it
    87   Once a portion of code is relatively stable, you usually want to export it
    88   to a separate ML-file. Such files can then be included somewhere inside a 
    88   to a separate ML-file. Such files can then be included somewhere inside a 
    89   theory by using the command \isacommand{use}. For example
    89   theory by using the command \isacommand{use}. For example
    90 
    90 
   236 
   236 
   237   @{ML_response_fake [display,gray]
   237   @{ML_response_fake [display,gray]
   238   "tracing (string_of_term @{context} @{term \"1::nat\"})"
   238   "tracing (string_of_term @{context} @{term \"1::nat\"})"
   239   "\"1\""}
   239   "\"1\""}
   240 
   240 
   241   If there are more than one @{ML_type term}s to be printed, you can use the 
   241   If there are more than one term to be printed, you can use the 
   242   function @{ML_ind  commas} to separate them.
   242   function @{ML_ind  commas} to separate them.
   243 *} 
   243 *} 
   244 
   244 
   245 ML{*fun string_of_terms ctxt ts =  
   245 ML{*fun string_of_terms ctxt ts =  
   246   commas (map (string_of_term ctxt) ts)*}
   246   commas (map (string_of_term ctxt) ts)*}
   269 ML{*fun string_of_thm ctxt thm =
   269 ML{*fun string_of_thm ctxt thm =
   270   string_of_term ctxt (prop_of thm)*}
   270   string_of_term ctxt (prop_of thm)*}
   271 
   271 
   272 text {*
   272 text {*
   273   Theorems also include schematic variables, such as @{text "?P"}, 
   273   Theorems also include schematic variables, such as @{text "?P"}, 
   274   @{text "?Q"} and so on. They are needed in order to able to
   274   @{text "?Q"} and so on. They are needed in Isabelle in order to able to
   275   instantiate theorems when they are applied. For example the theorem 
   275   instantiate theorems when they are applied. For example the theorem 
   276   @{thm [source] conjI} shown below can be used for any (typable) 
   276   @{thm [source] conjI} shown below can be used for any (typable) 
   277   instantiation of @{text "?P"} and @{text "?Q"}. 
   277   instantiation of @{text "?P"} and @{text "?Q"}. 
   278 
   278 
   279   @{ML_response_fake [display, gray]
   279   @{ML_response_fake [display, gray]
   281   "\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q"}
   281   "\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q"}
   282 
   282 
   283   However, in order to improve the readability when printing theorems, we
   283   However, in order to improve the readability when printing theorems, we
   284   convert these schematic variables into free variables using the function
   284   convert these schematic variables into free variables using the function
   285   @{ML_ind  import in Variable}. This is similar to statements like @{text
   285   @{ML_ind  import in Variable}. This is similar to statements like @{text
   286   "conjI[no_vars]"} from Isabelle's user-level.
   286   "conjI[no_vars]"} on Isabelle's user-level.
   287 *}
   287 *}
   288 
   288 
   289 ML{*fun no_vars ctxt thm =
   289 ML{*fun no_vars ctxt thm =
   290 let 
   290 let 
   291   val ((_, [thm']), _) = Variable.import true [thm] ctxt
   291   val ((_, [thm']), _) = Variable.import true [thm] ctxt
   314 
   314 
   315 text {*
   315 text {*
   316   Note, that when printing out several parcels of information that
   316   Note, that when printing out several parcels of information that
   317   semantically belong together, like a warning message consisting for example
   317   semantically belong together, like a warning message consisting for example
   318   of a term and a type, you should try to keep this information together in a
   318   of a term and a type, you should try to keep this information together in a
   319   single string. Therefore do not print out information as
   319   single string. Therefore do \emph{not} print out information as
   320 
   320 
   321 @{ML_response_fake [display,gray]
   321 @{ML_response_fake [display,gray]
   322 "tracing \"First half,\"; 
   322 "tracing \"First half,\"; 
   323 tracing \"and second half.\""
   323 tracing \"and second half.\""
   324 "First half,
   324 "First half,
   337 
   337 
   338   @{ML_response [display, gray]
   338   @{ML_response [display, gray]
   339   "cat_lines [\"foo\", \"bar\"]"
   339   "cat_lines [\"foo\", \"bar\"]"
   340   "\"foo\\nbar\""}
   340   "\"foo\\nbar\""}
   341 
   341 
   342   Section \ref{sec:pretty} will also explain infrastructure that helps 
   342   Section \ref{sec:pretty} will also explain the infrastructure of Isabelle 
   343   with more elaborate pretty printing. 
   343   that helps with more elaborate pretty printing. 
   344 *}
   344 *}
   345 
   345 
   346 
   346 
   347 section {* Combinators\label{sec:combinators} *}
   347 section {* Combinators\label{sec:combinators} *}
   348 
   348 
   427       |> curry list_comb f *}
   427       |> curry list_comb f *}
   428 
   428 
   429 text {*
   429 text {*
   430   This function takes a term and a context as argument. If the term is of function
   430   This function takes a term and a context as argument. If the term is of function
   431   type, then @{ML "apply_fresh_args"} returns the term with distinct variables 
   431   type, then @{ML "apply_fresh_args"} returns the term with distinct variables 
   432   applied to it. For example below variables are applied to the term 
   432   applied to it. For example below three variables are applied to the term 
   433   @{term [show_types] "P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool"}:
   433   @{term [show_types] "P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool"}:
   434 
   434 
   435   @{ML_response_fake [display,gray]
   435   @{ML_response_fake [display,gray]
   436 "let
   436 "let
   437   val t = @{term \"P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool\"}
   437   val t = @{term \"P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool\"}
   636   apply_fresh_args}, where @{ML curry} is needed for making the function @{ML
   636   apply_fresh_args}, where @{ML curry} is needed for making the function @{ML
   637   list_comb} that works over pairs to fit with the combinator @{ML "|>"}. Such 
   637   list_comb} that works over pairs to fit with the combinator @{ML "|>"}. Such 
   638   plumbing is also needed in situations where a function operate over lists, 
   638   plumbing is also needed in situations where a function operate over lists, 
   639   but one calculates only with a single element. An example is the function 
   639   but one calculates only with a single element. An example is the function 
   640   @{ML_ind  check_terms in Syntax}, whose purpose is to type-check a list 
   640   @{ML_ind  check_terms in Syntax}, whose purpose is to type-check a list 
   641   of terms.
   641   of terms. Consider the code:
   642 
   642 
   643   @{ML_response_fake [display, gray]
   643   @{ML_response_fake [display, gray]
   644   "let
   644   "let
   645   val ctxt = @{context}
   645   val ctxt = @{context}
   646 in
   646 in
   677   and @{ML_file "Pure/General/basics.ML"}. Also \isccite{sec:ML-linear-trans} 
   677   and @{ML_file "Pure/General/basics.ML"}. Also \isccite{sec:ML-linear-trans} 
   678   contains further information about combinators.
   678   contains further information about combinators.
   679   \end{readmore}
   679   \end{readmore}
   680 
   680 
   681   \footnote{\bf FIXME: find a good exercise for combinators}
   681   \footnote{\bf FIXME: find a good exercise for combinators}
   682 
       
   683   \footnote{\bf FIXME: say something about calling conventions} 
   682   \footnote{\bf FIXME: say something about calling conventions} 
   684 *}
   683 *}
   685 
   684 
   686 
   685 
   687 section {* ML-Antiquotations *}
   686 section {* ML-Antiquotations *}
   688 
   687 
   689 text {*
   688 text {*
   690   The main advantage of embedding all code in a theory is that the code can
   689   Recall that code in Isabelle is always embedded in a theory.  The main
   691   contain references to entities defined on the logical level of Isabelle. By
   690   advantage of this is that the code can contain references to entities
   692   this we mean definitions, theorems, terms and so on. This kind of reference
   691   defined on the logical level of Isabelle. By this we mean definitions,
   693   is realised in Isabelle with ML-antiquotations, often just called
   692   theorems, terms and so on. This kind of reference is realised in Isabelle
   694   antiquotations.\footnote{There are two kinds of antiquotations in Isabelle,
   693   with ML-antiquotations, often just called antiquotations.\footnote{There are
   695   which have very different purposes and infrastructures. The first kind,
   694   two kinds of antiquotations in Isabelle, which have very different purposes
   696   described in this section, are \emph{ML-antiquotations}. They are used to
   695   and infrastructures. The first kind, described in this section, are
   697   refer to entities (like terms, types etc) from Isabelle's logic layer inside
   696   \emph{ML-antiquotations}. They are used to refer to entities (like terms,
   698   ML-code. The other kind of antiquotations are \emph{document}
   697   types etc) from Isabelle's logic layer inside ML-code. The other kind of
   699   antiquotations. They are used only in the text parts of Isabelle and their
   698   antiquotations are \emph{document} antiquotations. They are used only in the
   700   purpose is to print logical entities inside \LaTeX-documents. Document
   699   text parts of Isabelle and their purpose is to print logical entities inside
   701   antiquotations are part of the user level and therefore we are not
   700   \LaTeX-documents. Document antiquotations are part of the user level and
   702   interested in them in this Tutorial, except in Appendix
   701   therefore we are not interested in them in this Tutorial, except in Appendix
   703   \ref{rec:docantiquotations} where we show how to implement your own document
   702   \ref{rec:docantiquotations} where we show how to implement your own document
   704   antiquotations.}  For example, one can print out the name of the current
   703   antiquotations.}  For example, one can print out the name of the current
   705   theory by typing
   704   theory by typing
   706 
       
   707   
   705   
   708   @{ML_response [display,gray] "Context.theory_name @{theory}" "\"FirstSteps\""}
   706   @{ML_response [display,gray] "Context.theory_name @{theory}" "\"FirstSteps\""}
   709  
   707  
   710   where @{text "@{theory}"} is an antiquotation that is substituted with the
   708   where @{text "@{theory}"} is an antiquotation that is substituted with the
   711   current theory (remember that we assumed we are inside the theory 
   709   current theory (remember that we assumed we are inside the theory 
   833 
   831 
   834 text {*
   832 text {*
   835   The parser in Line 2 provides us with a context and a string; this string is
   833   The parser in Line 2 provides us with a context and a string; this string is
   836   transformed into a term using the function @{ML_ind read_term_pattern in
   834   transformed into a term using the function @{ML_ind read_term_pattern in
   837   ProofContext} (Line 5); the next two lines transform the term into a string
   835   ProofContext} (Line 5); the next two lines transform the term into a string
   838   so that the ML-system can understand them. An example for the usage
   836   so that the ML-system can understand it. An example for the usage
   839   of this antiquotation is:
   837   of this antiquotation is:
   840 
   838 
   841   @{ML_response_fake [display,gray]
   839   @{ML_response_fake [display,gray]
   842   "@{term_pat \"Suc (?x::nat)\"}"
   840   "@{term_pat \"Suc (?x::nat)\"}"
   843   "Const (\"Suc\", \"nat \<Rightarrow> nat\") $ Var ((\"x\", 0), \"nat\")"}
   841   "Const (\"Suc\", \"nat \<Rightarrow> nat\") $ Var ((\"x\", 0), \"nat\")"}
   844 
   842 
   845   which shows the internal representation of the term @{text "Suc ?x"}.
   843   which shows the internal representation of the term @{text "Suc ?x"}.
       
   844   This is different from the build-in @{text "@{term \<dots>}"}-antiquotation.
       
   845 
       
   846   @{ML_response_fake [display,gray]
       
   847   "@{term \"Suc (x::nat)\"}"
       
   848   "Const (\"Suc\", \"nat \<Rightarrow> nat\") $ Free (\"x\", \"nat\")"}
   846 
   849 
   847   \begin{readmore}
   850   \begin{readmore}
   848   The file @{ML_file "Pure/ML/ml_antiquote.ML"} contains the the definitions
   851   The file @{ML_file "Pure/ML/ml_antiquote.ML"} contains the the definitions
   849   for most antiquotations. Most of the basic operations on ML-syntax are implemented 
   852   for most antiquotations. Most of the basic operations on ML-syntax are implemented 
   850   in @{ML_file "Pure/ML/ml_syntax.ML"}.
   853   in @{ML_file "Pure/ML/ml_syntax.ML"}.
  1236 
  1239 
  1237   \begin{readmore}
  1240   \begin{readmore}
  1238   For more information about configuration values see 
  1241   For more information about configuration values see 
  1239   @{ML_file "Pure/Isar/attrib.ML"} and @{ML_file "Pure/config.ML"}.
  1242   @{ML_file "Pure/Isar/attrib.ML"} and @{ML_file "Pure/config.ML"}.
  1240   \end{readmore}
  1243   \end{readmore}
       
  1244  
       
  1245 *}
       
  1246 
       
  1247 section {* Summary *}
       
  1248 
       
  1249 text {*
       
  1250   This chapter describes the combinators that are used in Isabelle, as well
       
  1251   as a simple printing infrastructure for @{ML_type term}, @{ML_type cterm}
       
  1252   and @{ML_type thm}. The section on ML-antiquotations shows how to refer 
       
  1253   statically to entities from the logic level of Isabelle. Isabelle also
       
  1254   contains mechanisms for storing arbitrary data in theory and proof 
       
  1255   contexts.
       
  1256 
       
  1257   This chapter also mentions two coding conventions: namely printing
       
  1258   entities belonging together as one string, and not using references
       
  1259   in any Isabelle code.
  1241 *}
  1260 *}
  1242 
  1261 
  1243 
  1262 
  1244 (**************************************************)
  1263 (**************************************************)
  1245 (* bak                                            *)
  1264 (* bak                                            *)