CookBook/FirstSteps.thy
changeset 104 5dcad9348e4d
parent 102 5e309df58557
child 107 258ce361ba1b
equal deleted inserted replaced
103:fe10da5354a3 104:5dcad9348e4d
   114 
   114 
   115   You can print out error messages with the function @{ML error}, as in:
   115   You can print out error messages with the function @{ML error}, as in:
   116 
   116 
   117   @{ML_response_fake [display,gray] "if 0=1 then 1 else (error \"foo\")" "\"foo\""}
   117   @{ML_response_fake [display,gray] "if 0=1 then 1 else (error \"foo\")" "\"foo\""}
   118 
   118 
   119   See leter on in Section~\ref{sec:printing} for information about printing 
   119   Section~\ref{sec:printing} will give more information about printing 
   120   out data of type @{ML_type term}, @{ML_type cterm} and @{ML_type thm}.
   120   the main datas tructures of Isabelle, namely @{ML_type term}, @{ML_type cterm} 
       
   121   and @{ML_type thm}.
   121 *}
   122 *}
   122 
   123 
   123 
   124 
   124 
   125 
   125 
   126 
   171 
   172 
   172   The code about simpsets extracts the theorem names stored in the
   173   The code about simpsets extracts the theorem names stored in the
   173   current simpset.  We get hold of the current simpset with the antiquotation 
   174   current simpset.  We get hold of the current simpset with the antiquotation 
   174   @{text "@{simpset}"}.  The function @{ML rep_ss in MetaSimplifier} returns a record
   175   @{text "@{simpset}"}.  The function @{ML rep_ss in MetaSimplifier} returns a record
   175   containing all information about the simpset. The rules of a simpset are
   176   containing all information about the simpset. The rules of a simpset are
   176   stored in a \emph{discrimination net} (a datastructure for fast
   177   stored in a \emph{discrimination net} (a data structure for fast
   177   indexing). From this net we can extract the entries using the function @{ML
   178   indexing). From this net we can extract the entries using the function @{ML
   178   Net.entries}.
   179   Net.entries}.
   179 
   180 
   180 
   181 
   181   \begin{readmore}
   182   \begin{readmore}
   242   \item @{term "{ [x::int] | x. x \<le> -2 }"}  
   243   \item @{term "{ [x::int] | x. x \<le> -2 }"}  
   243   \end{itemize}
   244   \end{itemize}
   244 
   245 
   245   Hint: The third term is already quite big, and the pretty printer
   246   Hint: The third term is already quite big, and the pretty printer
   246   may omit parts of it by default. If you want to see all of it, you
   247   may omit parts of it by default. If you want to see all of it, you
   247   can use the following ML function to set the limit to a value high 
   248   can use the following ML-function to set the limit to a value high 
   248   enough:
   249   enough:
   249 
   250 
   250   @{ML [display,gray] "print_depth 50"}
   251   @{ML [display,gray] "print_depth 50"}
   251   \end{exercise}
   252   \end{exercise}
   252 
   253 
   295     Logic.all x (Logic.mk_implies (P $ x, Q $ x))
   296     Logic.all x (Logic.mk_implies (P $ x, Q $ x))
   296   end *}
   297   end *}
   297 
   298 
   298 text {*
   299 text {*
   299   The reason is that you cannot pass the arguments @{term P}, @{term Q} and 
   300   The reason is that you cannot pass the arguments @{term P}, @{term Q} and 
   300   @{term "tau"} into an antiquotation. For example the following does \emph{not} work:
   301   @{term "tau"} into an antiquotation. For example the following does \emph{not} work.
   301 *}
   302 *}
   302 
   303 
   303 ML{*fun make_wrong_imp P Q tau = @{prop "\<And>x. P x \<Longrightarrow> Q x"} *}
   304 ML{*fun make_wrong_imp P Q tau = @{prop "\<And>x. P x \<Longrightarrow> Q x"} *}
   304 
   305 
   305 text {*
   306 text {*
   342 
   343 
   343   @{ML_response_fake [display,gray] "@{const_name \"Nil\"}" "List.list.Nil"}
   344   @{ML_response_fake [display,gray] "@{const_name \"Nil\"}" "List.list.Nil"}
   344 
   345 
   345   (FIXME: Is it useful to explain @{text "@{const_syntax}"}?)
   346   (FIXME: Is it useful to explain @{text "@{const_syntax}"}?)
   346 
   347 
   347   Similarly, you occasionally need to construct types manually. For example 
   348   Although to some extend types of terms can be inferred, there are many
   348   the function returning a function type is as follows:
   349   situations where you need to construct types manually, especially  
       
   350   when defining constants. For example the function returning a function 
       
   351   type is as follows:
   349 
   352 
   350 *} 
   353 *} 
   351 
   354 
   352 ML{*fun make_fun_type tau1 tau2 = Type ("fun",[tau1,tau2]) *}
   355 ML{*fun make_fun_type tau1 tau2 = Type ("fun",[tau1,tau2]) *}
   353 
   356 
   354 text {* This can be equally written as *}
   357 text {* This can be equally written as: *}
   355 
   358 
   356 ML{*fun make_fun_type tau1 tau2 = tau1 --> tau2 *}
   359 ML{*fun make_fun_type tau1 tau2 = tau1 --> tau2 *}
   357 
   360 
   358 text {*
   361 text {*
   359 
   362 
   481         assume assm1
   484         assume assm1
   482         |> forall_elim (cterm_of thy @{term \"t::nat\"});
   485         |> forall_elim (cterm_of thy @{term \"t::nat\"});
   483   
   486   
   484   val Qt = implies_elim Pt_implies_Qt (assume assm2);
   487   val Qt = implies_elim Pt_implies_Qt (assume assm2);
   485 in
   488 in
   486 
       
   487   Qt 
   489   Qt 
   488   |> implies_intr assm2
   490   |> implies_intr assm2
   489   |> implies_intr assm1
   491   |> implies_intr assm1
   490 end" "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
   492 end" "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
   491 
   493 
   518 
   520 
   519 section {* Storing Theorems *}
   521 section {* Storing Theorems *}
   520 
   522 
   521 section {* Theorem Attributes *}
   523 section {* Theorem Attributes *}
   522 
   524 
   523 section {* Printing Terms, CTerms and Theorems\label{sec:printing} *}
   525 section {* Printing Terms and Theorems\label{sec:printing} *}
   524 
   526 
   525 text {* 
   527 text {* 
   526   During development, you will occationally feel the need to inspect terms, cterms 
   528   During development, you often want to inspect terms, cterms 
   527   or theorems. Isabelle contains elaborate pretty-printing functions for that, but 
   529   or theorems. Isabelle contains elaborate pretty-printing functions for printing them, 
   528   for quick-and-dirty solutions they are way too unwieldy. A simple way to transform 
   530   but  for quick-and-dirty solutions they are far too unwieldy. A simple way to transform 
   529   a term into a string is to use the function @{ML Syntax.string_of_term}.
   531   a term into a string is to use the function @{ML Syntax.string_of_term}.
   530 
   532 
   531   @{ML_response_fake [display,gray]
   533   @{ML_response_fake [display,gray]
   532   "Syntax.string_of_term @{context} @{term \"1::nat\"}"
   534   "Syntax.string_of_term @{context} @{term \"1::nat\"}"
   533   "\"\\^E\\^Fterm\\^E\\^E\\^Fconst\\^Fname=HOL.one_class.one\\^E1\\^E\\^F\\^E\\^E\\^F\\^E\""}
   535   "\"\\^E\\^Fterm\\^E\\^E\\^Fconst\\^Fname=HOL.one_class.one\\^E1\\^E\\^F\\^E\\^E\\^F\\^E\""}
   534 
   536 
   535   This produces a string, though with printing directions encoded in it. The string
   537   This produces a string with some printing directions encoded in it. The string
   536   can be properly printed, when enclosed in a @{ML warning}.
   538   can be properly printed by using @{ML warning} foe example.
   537 
   539 
   538   @{ML_response_fake [display,gray]
   540   @{ML_response_fake [display,gray]
   539   "warning (Syntax.string_of_term @{context} @{term \"1::nat\"})"
   541   "warning (Syntax.string_of_term @{context} @{term \"1::nat\"})"
   540   "\"1\""}
   542   "\"1\""}
   541 
   543 
   606   actually ease the understanding and also the programming.
   608   actually ease the understanding and also the programming.
   607 
   609 
   608   \begin{readmore}
   610   \begin{readmore}
   609   The most frequently used combinator are defined in the files @{ML_file "Pure/library.ML"}
   611   The most frequently used combinator are defined in the files @{ML_file "Pure/library.ML"}
   610   and @{ML_file "Pure/General/basics.ML"}. Also \isccite{sec:ML-linear-trans} 
   612   and @{ML_file "Pure/General/basics.ML"}. Also \isccite{sec:ML-linear-trans} 
   611   contains further information about them.
   613   contains further information about combinators.
   612   \end{readmore}
   614   \end{readmore}
   613 
   615 
   614   The simplest combinator is @{ML I}, which is just the identity function.
   616   The simplest combinator is @{ML I}, which is just the identity function defined as
   615 *}
   617 *}
   616 
   618 
   617 ML{*fun I x = x*}
   619 ML{*fun I x = x*}
   618 
   620 
   619 text {* Another simple combinator is @{ML K}, defined as *}
   621 text {* Another simple combinator is @{ML K}, defined as *}
   621 ML{*fun K x = fn _ => x*}
   623 ML{*fun K x = fn _ => x*}
   622 
   624 
   623 text {*
   625 text {*
   624   @{ML K} ``wraps'' a function around the argument @{text "x"}. However, this 
   626   @{ML K} ``wraps'' a function around the argument @{text "x"}. However, this 
   625   function ignores its argument. As a result, @{ML K} defines a constant function 
   627   function ignores its argument. As a result, @{ML K} defines a constant function 
   626   returning @{text x}.
   628   always returning @{text x}.
   627 
   629 
   628   The next combinator is reverse application, @{ML "|>"}, defined as: 
   630   The next combinator is reverse application, @{ML "|>"}, defined as: 
   629 *}
   631 *}
   630 
   632 
   631 ML{*fun x |> f = f x*}
   633 ML{*fun x |> f = f x*}
   639     |> (fn x => (x, x))
   641     |> (fn x => (x, x))
   640     |> fst
   642     |> fst
   641     |> (fn x => x + 4)*}
   643     |> (fn x => x + 4)*}
   642 
   644 
   643 text {*
   645 text {*
   644   which increments the argument @{text x} by 5. It does this by first incrementing 
   646   which increments its argument @{text x} by 5. It does this by first incrementing 
   645   the argument by 1 (Line 2); then storing the result in a pair (Line 3); taking 
   647   the argument by 1 (Line 2); then storing the result in a pair (Line 3); taking 
   646   the first component of the pair (Line 4) and finally incrementing the first 
   648   the first component of the pair (Line 4) and finally incrementing the first 
   647   component by 4 (Line 5). This kind of cascading manipulations of values is quite
   649   component by 4 (Line 5). This kind of cascading manipulations of values is quite
   648   common when dealing with theories (for example by adding a definition, followed by
   650   common when dealing with theories (for example by adding a definition, followed by
   649   lemmas and so on). The reverse application allows you to read what happens in 
   651   lemmas and so on). The reverse application allows you to read what happens in