ProgTutorial/General.thy
changeset 356 43df2d59fb98
parent 355 42a1c230daff
child 358 9cf3bc448210
equal deleted inserted replaced
355:42a1c230daff 356:43df2d59fb98
   712 
   712 
   713 @{ML_response_fake [display,gray] 
   713 @{ML_response_fake [display,gray] 
   714 "let
   714 "let
   715   val natT = @{typ \"nat\"}
   715   val natT = @{typ \"nat\"}
   716   val zero = @{term \"0::nat\"}
   716   val zero = @{term \"0::nat\"}
   717 in
   717   val plus = Const (@{const_name plus}, [natT, natT] ---> natT)
   718   cterm_of @{theory} 
   718 in
   719       (Const (@{const_name plus}, [natT, natT] ---> natT) $ zero $ zero)
   719   cterm_of @{theory} (plus $ zero $ zero)
   720 end" "0 + 0"}
   720 end" 
       
   721   "0 + 0"}
   721 
   722 
   722   In Isabelle not just terms need to be certified, but also types. For example, 
   723   In Isabelle not just terms need to be certified, but also types. For example, 
   723   you obtain the certified type for the Isabelle type @{typ "nat \<Rightarrow> bool"} on 
   724   you obtain the certified type for the Isabelle type @{typ "nat \<Rightarrow> bool"} on 
   724   the ML-level as follows:
   725   the ML-level as follows:
   725 
   726 
   897 
   898 
   898 ML{*structure MyThmList = GenericDataFun
   899 ML{*structure MyThmList = GenericDataFun
   899   (type T = thm list
   900   (type T = thm list
   900    val empty = []
   901    val empty = []
   901    val extend = I
   902    val extend = I
   902    val merge = K (op @) 
   903    val merge = K (op @))
   903 )
       
   904 
   904 
   905 fun update thm = Context.theory_map (MyThmList.map (fn thms => thm::thms))*}
   905 fun update thm = Context.theory_map (MyThmList.map (fn thms => thm::thms))*}
   906 
   906 
   907 text {*
   907 text {*
   908   The function @{ML update} allows us to update the theorem list, for example
   908   The function @{ML update} allows us to update the theorem list, for example
  1252 
  1252 
  1253 text {*
  1253 text {*
  1254   We can now install a the modified theorem style as follows
  1254   We can now install a the modified theorem style as follows
  1255 *}
  1255 *}
  1256 
  1256 
  1257 setup %gray {*
  1257 setup %gray {* let
  1258 let
       
  1259   val parser = Scan.repeat Args.name
  1258   val parser = Scan.repeat Args.name
  1260   fun action xs = K (rename_allq xs #> strip_allq)
  1259   fun action xs = K (rename_allq xs #> strip_allq)
  1261 in
  1260 in
  1262   Term_Style.setup "my_strip_allq2" (parser >> action)
  1261   Term_Style.setup "my_strip_allq2" (parser >> action)
  1263 end *}
  1262 end *}
  1286 section {* Theorem Attributes\label{sec:attributes} *}
  1285 section {* Theorem Attributes\label{sec:attributes} *}
  1287 
  1286 
  1288 text {*
  1287 text {*
  1289   Theorem attributes are @{text "[symmetric]"}, @{text "[THEN \<dots>]"}, @{text
  1288   Theorem attributes are @{text "[symmetric]"}, @{text "[THEN \<dots>]"}, @{text
  1290   "[simp]"} and so on. Such attributes are \emph{neither} tags \emph{nor} flags
  1289   "[simp]"} and so on. Such attributes are \emph{neither} tags \emph{nor} flags
  1291   annotated to theorems, but functions that do further processing once a
  1290   annotated to theorems, but functions that do further processing of 
  1292   theorem is proved. In particular, it is not possible to find out
  1291   theorems. In particular, it is not possible to find out
  1293   what are all theorems that have a given attribute in common, unless of course
  1292   what are all theorems that have a given attribute in common, unless of course
  1294   the function behind the attribute stores the theorems in a retrievable 
  1293   the function behind the attribute stores the theorems in a retrievable 
  1295   data structure. 
  1294   data structure. 
  1296 
  1295 
  1297   If you want to print out all currently known attributes a theorem can have, 
  1296   If you want to print out all currently known attributes a theorem can have, 
  1304   @{text "> HOL.elim:  declaration of Classical elimination rule"}\\
  1303   @{text "> HOL.elim:  declaration of Classical elimination rule"}\\
  1305   @{text "> \<dots>"}
  1304   @{text "> \<dots>"}
  1306   \end{isabelle}
  1305   \end{isabelle}
  1307   
  1306   
  1308   The theorem attributes fall roughly into two categories: the first category manipulates
  1307   The theorem attributes fall roughly into two categories: the first category manipulates
  1309   the proved theorem (for example @{text "[symmetric]"} and @{text "[THEN \<dots>]"}), and the second
  1308   theorems (for example @{text "[symmetric]"} and @{text "[THEN \<dots>]"}), and the second
  1310   stores the proved theorem somewhere as data (for example @{text "[simp]"}, which adds
  1309   stores theorems somewhere as data (for example @{text "[simp]"}, which adds
  1311   the theorem to the current simpset).
  1310   theorems to the current simpset).
  1312 
  1311 
  1313   To explain how to write your own attribute, let us start with an extremely simple 
  1312   To explain how to write your own attribute, let us start with an extremely simple 
  1314   version of the attribute @{text "[symmetric]"}. The purpose of this attribute is
  1313   version of the attribute @{text "[symmetric]"}. The purpose of this attribute is
  1315   to produce the ``symmetric'' version of an equation. The main function behind 
  1314   to produce the ``symmetric'' version of an equation. The main function behind 
  1316   this attribute is
  1315   this attribute is
  1328 
  1327 
  1329   Before we can use the attribute, we need to set it up. This can be done
  1328   Before we can use the attribute, we need to set it up. This can be done
  1330   using the Isabelle command \isacommand{attribute\_setup} as follows:
  1329   using the Isabelle command \isacommand{attribute\_setup} as follows:
  1331 *}
  1330 *}
  1332 
  1331 
  1333 attribute_setup %gray my_sym = {* Scan.succeed my_symmetric *} 
  1332 attribute_setup %gray my_sym = 
  1334   "applying the sym rule"
  1333   {* Scan.succeed my_symmetric *} "applying the sym rule"
  1335 
  1334 
  1336 text {*
  1335 text {*
  1337   Inside the @{text "\<verbopen> \<dots> \<verbclose>"}, we have to specify a parser
  1336   Inside the @{text "\<verbopen> \<dots> \<verbclose>"}, we have to specify a parser
  1338   for the theorem attribute. Since the attribute does not expect any further 
  1337   for the theorem attribute. Since the attribute does not expect any further 
  1339   arguments (unlike @{text "[THEN \<dots>]"}, for example), we use the parser @{ML
  1338   arguments (unlike @{text "[THEN \<dots>]"}, for instance), we use the parser @{ML
  1340   Scan.succeed}. Later on we will also consider attributes taking further
  1339   Scan.succeed}. An example for the attribute @{text "[my_sym]"} is the proof
  1341   arguments. An example for the attribute @{text "[my_sym]"} is the proof
       
  1342 *} 
  1340 *} 
  1343 
  1341 
  1344 lemma test[my_sym]: "2 = Suc (Suc 0)" by simp
  1342 lemma test[my_sym]: "2 = Suc (Suc 0)" by simp
  1345 
  1343 
  1346 text {*
  1344 text {*
  1366 
  1364 
  1367 ML{*Attrib.setup @{binding "my_sym"} (Scan.succeed my_symmetric)
  1365 ML{*Attrib.setup @{binding "my_sym"} (Scan.succeed my_symmetric)
  1368   "applying the sym rule" *}
  1366   "applying the sym rule" *}
  1369 
  1367 
  1370 text {*
  1368 text {*
  1371   This gives a function from @{ML_type "Context.theory -> Context.theory"}, which
  1369   This gives a function from @{ML_type "theory -> theory"}, which
  1372   can be used for example with \isacommand{setup}.
  1370   can be used for example with \isacommand{setup}.
  1373 
  1371 
  1374   As an example of a slightly more complicated theorem attribute, we implement 
  1372   As an example of a slightly more complicated theorem attribute, we implement 
  1375   our own version of @{text "[THEN \<dots>]"}. This attribute will take a list of theorems
  1373   our own version of @{text "[THEN \<dots>]"}. This attribute will take a list of theorems
  1376   as argument and resolve the proved theorem with this list (one theorem 
  1374   as argument and resolve the theorem with this list (one theorem 
  1377   after another). The code for this attribute is
  1375   after another). The code for this attribute is
  1378 *}
  1376 *}
  1379 
  1377 
  1380 ML{*fun MY_THEN thms = 
  1378 ML{*fun MY_THEN thms = 
  1381   Thm.rule_attribute (fn _ => fn thm => foldl ((op RS) o swap) thm thms)*}
  1379   Thm.rule_attribute (fn _ => fn thm => List.foldl ((op RS) o swap) thm thms)*}
  1382 
  1380 
  1383 text {* 
  1381 text {* 
  1384   where @{ML swap} swaps the components of a pair. The setup of this theorem
  1382   where @{ML swap} swaps the components of a pair. The setup of this theorem
  1385   attribute uses the parser @{ML thms in Attrib}, which parses a list of
  1383   attribute uses the parser @{ML thms in Attrib}, which parses a list of
  1386   theorems. 
  1384   theorems. 
  1387 *}
  1385 *}
  1388 
  1386 
  1389 attribute_setup %gray MY_THEN = {* Attrib.thms >> MY_THEN *} 
  1387 attribute_setup %gray MY_THEN = {* Attrib.thms >> MY_THEN *} 
  1390   "resolving the list of theorems with the proved theorem"
  1388   "resolving the list of theorems with the theorem"
  1391 
  1389 
  1392 text {* 
  1390 text {* 
  1393   You can, for example, use this theorem attribute to turn an equation into a 
  1391   You can, for example, use this theorem attribute to turn an equation into a 
  1394   meta-equation:
  1392   meta-equation:
  1395 
  1393 
  1861 end"
  1859 end"
  1862 "ab
  1860 "ab
  1863 a
  1861 a
  1864 b"}
  1862 b"}
  1865   
  1863   
  1866   \footnote{\bf What happens with printing big formulae?}
  1864   \footnote{\bf FIXME: What happens with printing big formulae?}
  1867 
  1865 
  1868   
  1866   
  1869 
  1867 
  1870   \begin{readmore}
  1868   \begin{readmore}
  1871   The general infrastructure for pretty-printing is implemented in the file
  1869   The general infrastructure for pretty-printing is implemented in the file