ProgTutorial/First_Steps.thy
changeset 559 ffa5c4ec9611
parent 557 77ea2de0ca62
child 562 daf404920ab9
equal deleted inserted replaced
558:84aef87b348a 559:ffa5c4ec9611
   103   for converting values into strings, namely the antiquotation 
   103   for converting values into strings, namely the antiquotation 
   104   @{text "@{make_string}"}:
   104   @{text "@{make_string}"}:
   105 
   105 
   106   @{ML_response_fake [display,gray] "writeln (@{make_string} 1)" "\"1\""} 
   106   @{ML_response_fake [display,gray] "writeln (@{make_string} 1)" "\"1\""} 
   107 
   107 
   108   However, @{text "@{makes_tring}"} only works if the type of what
   108   However, @{text "@{make_string}"} only works if the type of what
   109   is converted is monomorphic and not a function. 
   109   is converted is monomorphic and not a function. 
   110 
   110 
   111   You can print out error messages with the function @{ML_ind error in Library}; 
   111   You can print out error messages with the function @{ML_ind error in Library}; 
   112   for example:
   112   for example:
   113 
   113 
   118 
   118 
   119   This function raises the exception @{text ERROR}, which will then 
   119   This function raises the exception @{text ERROR}, which will then 
   120   be displayed by the infrastructure indicating that it is an error by
   120   be displayed by the infrastructure indicating that it is an error by
   121   painting the output red. Note that this exception is meant 
   121   painting the output red. Note that this exception is meant 
   122   for ``user-level'' error messages seen by the ``end-user''. 
   122   for ``user-level'' error messages seen by the ``end-user''. 
   123   For messages where you want to indicate a genuine program error, then
   123   For messages where you want to indicate a genuine program error
   124   use the exception @{text Fail}. 
   124   use the exception @{text Fail}. 
   125 
   125 
   126   Most often you want to inspect data of Isabelle's basic data structures,
   126   Most often you want to inspect contents of Isabelle's basic data structures,
   127   namely @{ML_type term}, @{ML_type typ}, @{ML_type cterm}, @{ML_type ctyp}
   127   namely @{ML_type term}, @{ML_type typ}, @{ML_type cterm}, @{ML_type ctyp}
   128   and @{ML_type thm}. Isabelle contains elaborate pretty-printing functions,
   128   and @{ML_type thm}. Isabelle provides elaborate pretty-printing functions,
   129   which we will explain in more detail in Section \ref{sec:pretty}. For now
   129   which we will explain in more detail in Section \ref{sec:pretty}. For now
   130   we just use the functions @{ML_ind writeln in Pretty}  from the structure
   130   we just use the functions @{ML_ind writeln in Pretty}  from the structure
   131   @{ML_struct Pretty} and @{ML_ind pretty_term in Syntax} from the structure
   131   @{ML_struct Pretty} and @{ML_ind pretty_term in Syntax} from the structure
   132   @{ML_struct Syntax}. For more convenience, we bind them to the toplevel.
   132   @{ML_struct Syntax}. For more convenience, we bind them to the toplevel.
   133 *}
   133 *}
   134 
   134 
   135 ML %grayML{*val pretty_term = Syntax.pretty_term
   135 ML %grayML{*val pretty_term = Syntax.pretty_term
   136 val pwriteln = Pretty.writeln*}
   136 val pwriteln = Pretty.writeln*}
   137 
   137 
   138 text {*
   138 text {*
   139   They can now be used as follows
   139   They can be used as follows
   140 
   140 
   141   @{ML_response_fake [display,gray]
   141   @{ML_response_fake [display,gray]
   142   "pwriteln (pretty_term @{context} @{term \"1::nat\"})"
   142   "pwriteln (pretty_term @{context} @{term \"1::nat\"})"
   143   "\"1\""}
   143   "\"1\""}
   144 
   144 
   149 
   149 
   150 ML %grayML{*fun pretty_terms ctxt trms =  
   150 ML %grayML{*fun pretty_terms ctxt trms =  
   151   Pretty.block (Pretty.commas (map (pretty_term ctxt) trms))*}
   151   Pretty.block (Pretty.commas (map (pretty_term ctxt) trms))*}
   152 
   152 
   153 text {*
   153 text {*
   154   You can also print out terms together with their typing information.
   154   To print out terms together with their typing information,
   155   For this you need to set the configuration value  
   155   set the configuration value
   156   @{ML_ind show_types in Syntax} to @{ML true}.
   156   @{ML_ind show_types in Syntax} to @{ML true}.
   157 *}
   157 *}
   158 
   158 
   159 ML %grayML{*val show_types_ctxt = Config.put show_types true @{context}*}
   159 ML %grayML{*val show_types_ctxt = Config.put show_types true @{context}*}
   160 
   160 
   163 
   163 
   164   @{ML_response_fake [display, gray]
   164   @{ML_response_fake [display, gray]
   165   "pwriteln (pretty_term show_types_ctxt @{term \"(1::nat, x)\"})"
   165   "pwriteln (pretty_term show_types_ctxt @{term \"(1::nat, x)\"})"
   166   "(1::nat, x::'a)"}
   166   "(1::nat, x::'a)"}
   167 
   167 
   168   where @{text 1} and @{text x} are displayed with their inferred type.
   168   where @{text 1} and @{text x} are displayed with their inferred types.
   169   Other configuration values that influence
   169   Other configuration values that influence
   170   printing of terms include 
   170   printing of terms include 
   171 
   171 
   172   \begin{itemize}
   172   \begin{itemize}
   173   \item @{ML_ind show_brackets in Syntax} 
   173   \item @{ML_ind show_brackets in Syntax} 
   198 ML %grayML{*fun pretty_thm ctxt thm =
   198 ML %grayML{*fun pretty_thm ctxt thm =
   199   pretty_term ctxt (prop_of thm)*}
   199   pretty_term ctxt (prop_of thm)*}
   200 
   200 
   201 text {*
   201 text {*
   202   Theorems include schematic variables, such as @{text "?P"}, 
   202   Theorems include schematic variables, such as @{text "?P"}, 
   203   @{text "?Q"} and so on. They are needed in Isabelle in order to able to
   203   @{text "?Q"} and so on. They are instantiated by Isabelle when a theorem is applied.
   204   instantiate theorems when they are applied. For example the theorem 
   204   For example, the theorem 
   205   @{thm [source] conjI} shown below can be used for any (typable) 
   205   @{thm [source] conjI} shown below can be used for any (typable) 
   206   instantiation of @{text "?P"} and @{text "?Q"}. 
   206   instantiation of @{text "?P"} and @{text "?Q"}. 
   207 
   207 
   208   @{ML_response_fake [display, gray]
   208   @{ML_response_fake [display, gray]
   209   "pwriteln (pretty_thm @{context} @{thm conjI})"
   209   "pwriteln (pretty_thm @{context} @{thm conjI})"
   210   "\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q"}
   210   "\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q"}
   211 
   211 
   212   However, in order to improve the readability when printing theorems, we
   212   However, to improve the readability when printing theorems, we
   213   can switch off the question marks as follows:
   213   can switch off the question marks as follows:
   214 *}
   214 *}
   215 
   215 
   216 ML %grayML{*fun pretty_thm_no_vars ctxt thm =
   216 ML %grayML{*fun pretty_thm_no_vars ctxt thm =
   217 let
   217 let
   365     val y3 = fst y2
   365     val y3 = fst y2
   366     val y4 = y3 + 4
   366     val y4 = y3 + 4
   367 in y4 end*}
   367 in y4 end*}
   368 
   368 
   369 text {* 
   369 text {* 
   370   Another reason why the let-bindings in the code above are better to be
   370   Another reasons to avoid the let-bindings in the code above:
   371   avoided: it is more than easy to get the intermediate values wrong, not to 
   371   it is easy to get the intermediate values wrong and
   372   mention the nightmares the maintenance of this code causes!
   372   the resulting code is difficult to maintain.
   373 
   373 
   374   In Isabelle a ``real world'' example for a function written in 
   374   In Isabelle a ``real world'' example for a function written in 
   375   the waterfall fashion might be the following code:
   375   the waterfall fashion might be the following code:
   376 *}
   376 *}
   377 
   377 
   382       |> Variable.variant_frees ctxt [f]
   382       |> Variable.variant_frees ctxt [f]
   383       |> map Free  
   383       |> map Free  
   384       |> curry list_comb f *}
   384       |> curry list_comb f *}
   385 
   385 
   386 text {*
   386 text {*
   387   This function takes a term and a context as argument. If the term is of function
   387   This function takes a term and a context as arguments. If the term is of function
   388   type, then @{ML "apply_fresh_args"} returns the term with distinct variables 
   388   type, then @{ML "apply_fresh_args"} returns the term with distinct variables 
   389   applied to it. For example below three variables are applied to the term 
   389   applied to it. For example, below three variables are applied to the term 
   390   @{term [show_types] "P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool"}:
   390   @{term [show_types] "P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool"}:
   391 
   391 
   392   @{ML_response_fake [display,gray]
   392   @{ML_response_fake [display,gray]
   393 "let
   393 "let
   394   val trm = @{term \"P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool\"}
   394   val trm = @{term \"P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool\"}
   461 end *}
   461 end *}
   462 
   462 
   463 text {*
   463 text {*
   464   The function @{ML_text "my_note"} in line 3 is just a wrapper for the function 
   464   The function @{ML_text "my_note"} in line 3 is just a wrapper for the function 
   465   @{ML_ind note in Local_Theory} in the structure @{ML_struct Local_Theory}; 
   465   @{ML_ind note in Local_Theory} in the structure @{ML_struct Local_Theory}; 
   466   its purpose is to store a theorem under a name. 
   466   it stores a theorem under a name. 
   467   In lines 5 to 6 we call this function to give alternative names for the three
   467   In lines 5 to 6 we call this function to give alternative names for the three
   468   theorems. The point of @{ML "#>"} is that you can sequence such function calls. 
   468   theorems. The point of @{ML "#>"} is that you can sequence such function calls. 
   469 
   469 
   470   The remaining combinators we describe in this section add convenience for
   470   The remaining combinators we describe in this section add convenience for
   471   the ``waterfall method'' of writing functions. The combinator @{ML_ind tap
   471   the ``waterfall method'' of writing functions. The combinator @{ML_ind tap
   768   some data structure and return it. Instead, it is literally
   768   some data structure and return it. Instead, it is literally
   769   replaced with the value representing the theory.
   769   replaced with the value representing the theory.
   770 
   770 
   771   Another important antiquotation is @{text "@{context}"}. (What the
   771   Another important antiquotation is @{text "@{context}"}. (What the
   772   difference between a theory and a context is will be described in Chapter
   772   difference between a theory and a context is will be described in Chapter
   773   \ref{chp:advanced}.) A context is for example needed in order to use the
   773   \ref{chp:advanced}.) A context is for example needed to use the
   774   function @{ML print_abbrevs in Proof_Context} that list of all currently
   774   function @{ML print_abbrevs in Proof_Context} that list of all currently
   775   defined abbreviations. For example
   775   defined abbreviations. For example
   776 
   776 
   777   @{ML_response_fake [display, gray]
   777   @{ML_response_fake [display, gray]
   778   "Proof_Context.print_abbrevs @{context}"
   778   "Proof_Context.print_abbrevs @{context}"
   851   of lemmas to the simpset by the user, which can potentially cause loops in your
   851   of lemmas to the simpset by the user, which can potentially cause loops in your
   852   code.
   852   code.
   853 
   853 
   854   It is also possible to define your own antiquotations. But you should
   854   It is also possible to define your own antiquotations. But you should
   855   exercise care when introducing new ones, as they can also make your code
   855   exercise care when introducing new ones, as they can also make your code
   856   also difficult to read. In the next chapter we describe how to construct
   856   difficult to read. In the next chapter we describe how to construct
   857   terms with the (build in) antiquotation @{text "@{term \<dots>}"}.  A restriction
   857   terms with the (built-in) antiquotation @{text "@{term \<dots>}"}.  A restriction
   858   of this antiquotation is that it does not allow you to use schematic
   858   of this antiquotation is that it does not allow you to use schematic
   859   variables in terms. If you want to have an antiquotation that does not have
   859   variables in terms. If you want to have an antiquotation that does not have
   860   this restriction, you can implement your own using the function @{ML_ind
   860   this restriction, you can implement your own using the function @{ML_ind
   861   inline in ML_Antiquotation} from the structure @{ML_struct ML_Antiquotation}. The code
   861   inline in ML_Antiquotation} from the structure @{ML_struct ML_Antiquotation}. The code
   862   for the antiquotation @{text "term_pat"} is as follows.
   862   for the antiquotation @{text "term_pat"} is as follows.
   916 *}
   916 *}
   917 
   917 
   918 setup %gray {* type_pat_setup *}
   918 setup %gray {* type_pat_setup *}
   919 
   919 
   920 text {* 
   920 text {* 
   921 However, a word of warning is in order: Introducing new antiquotations should be done only after
   921 However, a word of warning is in order: new antiquotations should be introduced only after
   922 careful deliberations. They can potentially make your code harder to read, than making it easier.
   922 careful deliberations. They can potentially make your code harder, rather than easier, to read.
   923 
   923 
   924   \begin{readmore}
   924   \begin{readmore}
   925   The files @{ML_file "Pure/ML/ml_antiquotation.ML"} and @{ML_file "Pure/ML/ml_antiquotations.ML"}
   925   The files @{ML_file "Pure/ML/ml_antiquotation.ML"} and @{ML_file "Pure/ML/ml_antiquotations.ML"}
   926   contain the infrastructure and definitions for most antiquotations. Most of the basic operations 
   926   contain the infrastructure and definitions for most antiquotations. Most of the basic operations 
   927   on ML-syntax are implemented in @{ML_file "Pure/ML/ml_syntax.ML"}.
   927   on ML-syntax are implemented in @{ML_file "Pure/ML/ml_syntax.ML"}.
  1008   in a theory (simpsets need to be maintained across proofs and even across
  1008   in a theory (simpsets need to be maintained across proofs and even across
  1009   theories).  On the other hand, data such as facts change inside a proof and
  1009   theories).  On the other hand, data such as facts change inside a proof and
  1010   are only relevant to the proof at hand. Therefore such data needs to be 
  1010   are only relevant to the proof at hand. Therefore such data needs to be 
  1011   maintained inside a proof context, which represents ``local'' data.
  1011   maintained inside a proof context, which represents ``local'' data.
  1012   You can think of a theory as the ``longterm memory'' of Isabelle (nothing will
  1012   You can think of a theory as the ``longterm memory'' of Isabelle (nothing will
  1013   be deleted from it), and a proof-context as a ``shortterm memory'' (it dynamically
  1013   be deleted from it), and a proof-context as a ``shortterm memory'' (it
  1014   changes according to what is needed at the time).
  1014   changes according to what is needed at the time).
  1015 
  1015 
  1016   For theories and proof contexts there are, respectively, the functors 
  1016   For theories and proof contexts there are, respectively, the functors 
  1017   @{ML_funct_ind Theory_Data} and @{ML_funct_ind Proof_Data} that help
  1017   @{ML_funct_ind Theory_Data} and @{ML_funct_ind Proof_Data} that help
  1018   with the data storage. Below we show how to implement a table in which you
  1018   with the data storage. Below we show how to implement a table in which you
  1040   tables should be merged (Line 5). These functions correspond roughly to the
  1040   tables should be merged (Line 5). These functions correspond roughly to the
  1041   operations performed on theories and we just give some sensible 
  1041   operations performed on theories and we just give some sensible 
  1042   defaults.\footnote{\bf FIXME: Say more about the
  1042   defaults.\footnote{\bf FIXME: Say more about the
  1043   assumptions of these operations.} The result structure @{ML_text Data}
  1043   assumptions of these operations.} The result structure @{ML_text Data}
  1044   contains functions for accessing the table (@{ML Data.get}) and for updating
  1044   contains functions for accessing the table (@{ML Data.get}) and for updating
  1045   it (@{ML Data.map}). There is also the functions @{ML Data.put}, which however is 
  1045   it (@{ML Data.map}). There is also the function @{ML Data.put}, but it is 
  1046   not relevant here. Below we define two
  1046   not relevant here. Below we define two
  1047   auxiliary functions, which help us with accessing the table.
  1047   auxiliary functions, which help us with accessing the table.
  1048 *}
  1048 *}
  1049 
  1049 
  1050 ML %grayML{*val lookup = Symtab.lookup o Data.get
  1050 ML %grayML{*val lookup = Symtab.lookup o Data.get
  1062 *}
  1062 *}
  1063 
  1063 
  1064 text {*
  1064 text {*
  1065   The use of the command \isacommand{setup} makes sure the table in the 
  1065   The use of the command \isacommand{setup} makes sure the table in the 
  1066   \emph{current} theory is updated (this is explained further in 
  1066   \emph{current} theory is updated (this is explained further in 
  1067   section~\ref{sec:theories}). The lookup can now be performed as follows.
  1067   Section~\ref{sec:theories}). The lookup can now be performed as follows.
  1068 
  1068 
  1069   @{ML_response_fake [display, gray]
  1069   @{ML_response_fake [display, gray]
  1070 "lookup @{theory} \"conj\""
  1070 "lookup @{theory} \"conj\""
  1071 "SOME \"\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q\""}
  1071 "SOME \"\<lbrakk>?P; ?Q\<rbrakk> \<Longrightarrow> ?P \<and> ?Q\""}
  1072 
  1072