ProgTutorial/General.thy
changeset 350 364fffa80794
parent 349 9e374cd891e1
child 351 f118240ab44a
equal deleted inserted replaced
349:9e374cd891e1 350:364fffa80794
    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
    20   structures involved in this core are certified terms and types, as well as
    20   structures involved in this core are certified terms and certified types, 
    21   theorems.
    21   as well as theorems.
    22 *}
    22 *}
    23 
    23 
    24 
    24 
    25 section {* Terms and Types *}
    25 section {* Terms and Types *}
    26 
    26 
    27 text {*
    27 text {*
    28   There are certified terms and uncertified terms (respectively types). 
    28   In Isabelle, there are certified terms and uncertified terms (respectively types). 
    29   Uncertified terms are just called terms. One way to construct them is by 
    29   Uncertified terms are often just called terms. One way to construct them is by 
    30   using the antiquotation \mbox{@{text "@{term \<dots>}"}}. For example
    30   using the antiquotation \mbox{@{text "@{term \<dots>}"}}. For example
    31 
    31 
    32   @{ML_response [display,gray] 
    32   @{ML_response [display,gray] 
    33 "@{term \"(a::nat) + b = c\"}" 
    33 "@{term \"(a::nat) + b = c\"}" 
    34 "Const (\"op =\", \<dots>) $ 
    34 "Const (\"op =\", \<dots>) $ 
    35   (Const (\"HOL.plus_class.plus\", \<dots>) $ \<dots> $ \<dots>) $ \<dots>"}
    35   (Const (\"HOL.plus_class.plus\", \<dots>) $ \<dots> $ \<dots>) $ \<dots>"}
    36 
    36 
    37   constructs the term @{term "(a::nat) + b = c"}, but it printed using the internal
    37   constructs the term @{term "(a::nat) + b = c"}. The resulting term is printed using 
    38   representation corresponding to the datatype @{ML_type_ind "term"} defined as follows: 
    38   the internal representation corresponding to the datatype @{ML_type_ind "term"}, 
       
    39   which is defined as follows: 
    39 *}  
    40 *}  
    40 
    41 
    41 ML_val %linenosgray{*datatype term =
    42 ML_val %linenosgray{*datatype term =
    42   Const of string * typ 
    43   Const of string * typ 
    43 | Free of string * typ 
    44 | Free of string * typ 
    46 | Abs of string * typ * term 
    47 | Abs of string * typ * term 
    47 | $ of term * term *}
    48 | $ of term * term *}
    48 
    49 
    49 text {*
    50 text {*
    50   This datatype implements Church-style lambda-terms, where types are
    51   This datatype implements Church-style lambda-terms, where types are
    51   explicitly recorded in the variables, constants and abstractions.  As can be
    52   explicitly recorded in variables, constants and abstractions.  As can be
    52   seen in Line 5, terms use the usual de Bruijn index mechanism for
    53   seen in Line 5, terms use the usual de Bruijn index mechanism for
    53   representing bound variables.  For example in
    54   representing bound variables.  For example in
    54 
    55 
    55   @{ML_response_fake [display, gray]
    56   @{ML_response_fake [display, gray]
    56   "@{term \"\<lambda>x y. x y\"}"
    57   "@{term \"\<lambda>x y. x y\"}"
   151   "(Const (\"==>\", \<dots>) $ \<dots> $ \<dots>, 
   152   "(Const (\"==>\", \<dots>) $ \<dots> $ \<dots>, 
   152  Const (\"==>\", \<dots>) $ \<dots> $ \<dots>)"}
   153  Const (\"==>\", \<dots>) $ \<dots> $ \<dots>)"}
   153 
   154 
   154   where it is not (since it is already constructed by a meta-implication). 
   155   where it is not (since it is already constructed by a meta-implication). 
   155   The purpose of the @{text "Trueprop"}-coercion is to embed formulae of
   156   The purpose of the @{text "Trueprop"}-coercion is to embed formulae of
   156   an object logic, for example HOL, into the meta-logic of Isabelle. It
   157   an object logic, for example HOL, into the meta-logic of Isabelle. The coercion
   157   is needed whenever a term is constructed that will be proved as a theorem. 
   158   is needed whenever a term is constructed that will be proved as a theorem. 
   158 
   159 
   159   As already seen above, types can be constructed using the antiquotation 
   160   As already seen above, types can be constructed using the antiquotation 
   160   @{text "@{typ \<dots>}"}. For example:
   161   @{text "@{typ \<dots>}"}. For example:
   161 
   162 
   169 | TFree of string * sort 
   170 | TFree of string * sort 
   170 | TVar  of indexname * sort *}
   171 | TVar  of indexname * sort *}
   171 
   172 
   172 text {*
   173 text {*
   173   Like with terms, there is the distinction between free type
   174   Like with terms, there is the distinction between free type
   174   variables (term-constructor @{ML "TFree"} and schematic
   175   variables (term-constructor @{ML "TFree"}) and schematic
   175   type variables (term-constructor @{ML "TVar"}). A type constant,
   176   type variables (term-constructor @{ML "TVar"}). A type constant,
   176   like @{typ "int"} or @{typ bool}, are types with an empty list
   177   like @{typ "int"} or @{typ bool}, are types with an empty list
   177   of argument types. However, it is a bit difficult to show an
   178   of argument types. However, it is a bit difficult to show an
   178   example, because Isabelle always pretty-prints types (unlike terms).
   179   example, because Isabelle always pretty-prints types (unlike terms).
   179   Here is a contrived example:
   180   Here is a contrived example:
   234     Const \<dots> $ (Const \<dots> $ (Free (\"P\",\<dots>) $ \<dots>)) $ 
   235     Const \<dots> $ (Const \<dots> $ (Free (\"P\",\<dots>) $ \<dots>)) $ 
   235                 (Const \<dots> $ (Free (\"Q\",\<dots>) $ \<dots>)))"}
   236                 (Const \<dots> $ (Free (\"Q\",\<dots>) $ \<dots>)))"}
   236 
   237 
   237   There are a number of handy functions that are frequently used for
   238   There are a number of handy functions that are frequently used for
   238   constructing terms. One is the function @{ML_ind list_comb in Term}, which
   239   constructing terms. One is the function @{ML_ind list_comb in Term}, which
   239   takes a term and a list of terms as arguments, and produces as output the
   240   takes as argument a term and a list of terms, and produces as output the
   240   term list applied to the term. For example
   241   term list applied to the term. For example
   241 
   242 
   242 
   243 
   243 @{ML_response_fake [display,gray]
   244 @{ML_response_fake [display,gray]
   244 "let
   245 "let
   260   lambda x_nat trm
   261   lambda x_nat trm
   261 end"
   262 end"
   262   "Abs (\"x\", \"nat\", Free (\"P\", \"bool \<Rightarrow> bool\") $ Bound 0)"}
   263   "Abs (\"x\", \"nat\", Free (\"P\", \"bool \<Rightarrow> bool\") $ Bound 0)"}
   263 
   264 
   264   In this example, @{ML lambda} produces a de Bruijn index (i.e.~@{ML "Bound 0"}), 
   265   In this example, @{ML lambda} produces a de Bruijn index (i.e.~@{ML "Bound 0"}), 
   265   and an abstraction. It also records the type of the abstracted
   266   and an abstraction, where it also records the type of the abstracted
   266   variable and for printing purposes also its name.  Note that because of the
   267   variable and for printing purposes also its name.  Note that because of the
   267   typing annotation on @{text "P"}, the variable @{text "x"} in @{text "P x"}
   268   typing annotation on @{text "P"}, the variable @{text "x"} in @{text "P x"}
   268   is of the same type as the abstracted variable. If it is of different type,
   269   is of the same type as the abstracted variable. If it is of different type,
   269   as in
   270   as in
   270 
   271 
   318                                     strip_alls t |>> cons (Free (n, T))
   319                                     strip_alls t |>> cons (Free (n, T))
   319   | strip_alls t = ([], t) *}
   320   | strip_alls t = ([], t) *}
   320 
   321 
   321 text {*
   322 text {*
   322   The function returns a pair consisting of the stripped off variables and
   323   The function returns a pair consisting of the stripped off variables and
   323   the body of the universal quantifications. For example
   324   the body of the universal quantification. For example
   324 
   325 
   325   @{ML_response_fake [display, gray]
   326   @{ML_response_fake [display, gray]
   326   "strip_alls @{term \"\<forall>x y. x = (y::bool)\"}"
   327   "strip_alls @{term \"\<forall>x y. x = (y::bool)\"}"
   327 "([Free (\"x\", \"bool\"), Free (\"y\", \"bool\")],
   328 "([Free (\"x\", \"bool\"), Free (\"y\", \"bool\")],
   328   Const (\"op =\", \<dots>) $ Bound 1 $ Bound 0)"}
   329   Const (\"op =\", \<dots>) $ Bound 1 $ Bound 0)"}
   344   Note that in Line 4 we had to reverse the list of variables that @{ML strip_alls}
   345   Note that in Line 4 we had to reverse the list of variables that @{ML strip_alls}
   345   returned. The reason is that the head of the list the function @{ML subst_bounds}
   346   returned. The reason is that the head of the list the function @{ML subst_bounds}
   346   takes is the replacement for @{ML "Bound 0"}, the next element for @{ML "Bound 1"}
   347   takes is the replacement for @{ML "Bound 0"}, the next element for @{ML "Bound 1"}
   347   and so on. 
   348   and so on. 
   348 
   349 
   349   There are many convenient functions that construct specific HOL-terms. For
   350   There are also many convenient functions that construct specific HOL-terms
       
   351   in the structure @{ML_struct HOLogic}. For
   350   example @{ML_ind mk_eq in HOLogic} constructs an equality out of two terms.
   352   example @{ML_ind mk_eq in HOLogic} constructs an equality out of two terms.
   351   The types needed in this equality are calculated from the type of the
   353   The types needed in this equality are calculated from the type of the
   352   arguments. For example
   354   arguments. For example
   353 
   355 
   354 @{ML_response_fake [gray,display]
   356 @{ML_response_fake [gray,display]
   543   the @{text "t\<^isub>i"} can be arbitrary expressions and also note that @{text "+"} 
   545   the @{text "t\<^isub>i"} can be arbitrary expressions and also note that @{text "+"} 
   544   associates to the left. Try your function on some examples. 
   546   associates to the left. Try your function on some examples. 
   545   \end{exercise}
   547   \end{exercise}
   546 
   548 
   547   \begin{exercise}\label{fun:makesum}
   549   \begin{exercise}\label{fun:makesum}
   548   Write a function which takes two terms representing natural numbers
   550   Write a function that takes two terms representing natural numbers
   549   in unary notation (like @{term "Suc (Suc (Suc 0))"}), and produces the
   551   in unary notation (like @{term "Suc (Suc (Suc 0))"}), and produces the
   550   number representing their sum.
   552   number representing their sum.
   551   \end{exercise}
   553   \end{exercise}
   552 
   554 
   553   \begin{exercise}\label{ex:debruijn}
   555   \begin{exercise}\label{ex:debruijn}
   554   Implement the function, which we below name deBruijn\footnote{According to 
   556   Implement the function, which we below name deBruijn, that depends on a natural
   555   personal communication of de Bruijn to Dyckhoff.}, that depends on a natural
       
   556   number n$>$0 and constructs terms of the form:
   557   number n$>$0 and constructs terms of the form:
   557   
   558   
   558   \begin{center}
   559   \begin{center}
   559   \begin{tabular}{r@ {\hspace{2mm}}c@ {\hspace{2mm}}l}
   560   \begin{tabular}{r@ {\hspace{2mm}}c@ {\hspace{2mm}}l}
   560   {\it rhs n} & $\dn$ & {\large$\bigwedge$}{\it i=1\ldots n.  P\,i}\\
   561   {\it rhs n} & $\dn$ & {\large$\bigwedge$}{\it i=1\ldots n.  P\,i}\\
   573   (P 3 = P 1 $\longrightarrow$ P 1 $\wedge$ P 2 $\wedge$ P 3) $\longrightarrow$ P 1 $\wedge$ P 2 $\wedge$ P 3
   574   (P 3 = P 1 $\longrightarrow$ P 1 $\wedge$ P 2 $\wedge$ P 3) $\longrightarrow$ P 1 $\wedge$ P 2 $\wedge$ P 3
   574   \end{tabular}
   575   \end{tabular}
   575   \end{center}
   576   \end{center}
   576 
   577 
   577   Make sure you use the functions defined in @{ML_file "HOL/Tools/hologic.ML"}
   578   Make sure you use the functions defined in @{ML_file "HOL/Tools/hologic.ML"}
   578   for constructing the terms for the logical connectives. 
   579   for constructing the terms for the logical connectives.\footnote{Thanks to Roy
       
   580   Dyckhoff for this exercise.} 
   579   \end{exercise}
   581   \end{exercise}
   580 *}
   582 *}
   581 
   583 
   582 
   584 
   583 section {* Type-Checking\label{sec:typechecking} *}
   585 section {* Type-Checking\label{sec:typechecking} *}
  1380   use the commands \isacommand{method\_setup} for installing methods in the
  1382   use the commands \isacommand{method\_setup} for installing methods in the
  1381   current theory and \isacommand{simproc\_setup} for adding new simprocs to
  1383   current theory and \isacommand{simproc\_setup} for adding new simprocs to
  1382   the current simpset.
  1384   the current simpset.
  1383 *}
  1385 *}
  1384 
  1386 
  1385 section {* Theories (TBD) *}
  1387 section {* Theories\label{sec:theories} (TBD) *}
  1386 
  1388 
  1387 text {*
  1389 text {*
  1388   There are theories, proof contexts and local theories (in this order, if you
  1390   There are theories, proof contexts and local theories (in this order, if you
  1389   want to order them). 
  1391   want to order them). 
  1390 
  1392