216 |
216 |
217 ML {* Toplevel.program (fn () => innocent ()) *} |
217 ML {* Toplevel.program (fn () => innocent ()) *} |
218 *) |
218 *) |
219 |
219 |
220 text {* |
220 text {* |
221 Most often you want to inspect data of Isabelle's basic data |
221 Most often you want to inspect data of Isabelle's basic data structures, |
222 structures, namely @{ML_type term}, @{ML_type cterm} and @{ML_type |
222 namely @{ML_type term}, @{ML_type typ}, @{ML_type cterm}, @{ML_type ctyp} |
223 thm}. Isabelle contains elaborate pretty-printing functions for printing |
223 and @{ML_type thm}. Isabelle contains elaborate pretty-printing functions |
224 them (see Section \ref{sec:pretty}), but for quick-and-dirty solutions they |
224 for printing them (see Section \ref{sec:pretty}), but for quick-and-dirty |
225 are a bit unwieldy. One way to transform a term into a string is to use the |
225 solutions they are a bit unwieldy. One way to transform a term into a string |
226 function @{ML_ind string_of_term in Syntax} from the structure @{ML_struct |
226 is to use the function @{ML_ind string_of_term in Syntax} from the structure |
227 Syntax}. For more convenience, we bind this function to the toplevel. |
227 @{ML_struct Syntax}. For more convenience, we bind this function to the |
|
228 toplevel. |
228 *} |
229 *} |
229 |
230 |
230 ML{*val string_of_term = Syntax.string_of_term*} |
231 ML{*val string_of_term = Syntax.string_of_term*} |
231 |
232 |
232 text {* |
233 text {* |
356 |
357 |
357 fun string_of_thms_no_vars ctxt thms = |
358 fun string_of_thms_no_vars ctxt thms = |
358 commas (map (string_of_thm_no_vars ctxt) thms) *} |
359 commas (map (string_of_thm_no_vars ctxt) thms) *} |
359 |
360 |
360 text {* |
361 text {* |
|
362 The printing functions for types are |
|
363 *} |
|
364 |
|
365 ML{*fun string_of_typ ctxt ty = Syntax.string_of_typ ctxt ty |
|
366 fun string_of_typs ctxt tys = commas (map (string_of_typ ctxt) tys)*} |
|
367 |
|
368 text {* |
|
369 respectively ctypes |
|
370 *} |
|
371 |
|
372 ML{*fun string_of_ctyp ctxt cty = string_of_typ ctxt (typ_of cty) |
|
373 fun string_of_ctyps ctxt ctys = commas (map (string_of_ctyp ctxt) ctys)*} |
|
374 |
|
375 text {* |
361 \begin{readmore} |
376 \begin{readmore} |
362 The simple conversion functions from Isabelle's main datatypes to |
377 The simple conversion functions from Isabelle's main datatypes to |
363 @{ML_type string}s are implemented in @{ML_file "Pure/Syntax/syntax.ML"}. |
378 @{ML_type string}s are implemented in @{ML_file "Pure/Syntax/syntax.ML"}. |
364 The references that change the printing information are declared in |
379 The references that change the printing information are declared in |
365 @{ML_file "Pure/Syntax/printer.ML"}. |
380 @{ML_file "Pure/Syntax/printer.ML"}. |
366 \end{readmore} |
381 \end{readmore} |
367 |
382 |
368 Note that for printing out several ``parcels'' of information that |
383 Note that for printing out several ``parcels'' of information that belong |
369 semantically belong together, like a warning message consisting of |
384 together, like a warning message consisting of a term and its type, you |
370 a term and its type, you should try to keep this information together in a |
385 should try to print these parcels together in a single string. Therefore do |
371 single string. Therefore do \emph{not} print out information as |
386 \emph{not} print out information as |
372 |
387 |
373 @{ML_response_fake [display,gray] |
388 @{ML_response_fake [display,gray] |
374 "tracing \"First half,\"; |
389 "tracing \"First half,\"; |
375 tracing \"and second half.\"" |
390 tracing \"and second half.\"" |
376 "First half, |
391 "First half, |
386 To ease this kind of string manipulations, there are a number |
401 To ease this kind of string manipulations, there are a number |
387 of library functions in Isabelle. For example, the function |
402 of library functions in Isabelle. For example, the function |
388 @{ML_ind cat_lines in Library} concatenates a list of strings |
403 @{ML_ind cat_lines in Library} concatenates a list of strings |
389 and inserts newlines in between each element. |
404 and inserts newlines in between each element. |
390 |
405 |
391 @{ML_response [display, gray] |
406 @{ML_response_fake [display, gray] |
392 "cat_lines [\"foo\", \"bar\"]" |
407 "tracing (cat_lines [\"foo\", \"bar\"])" |
393 "\"foo\\nbar\""} |
408 "foo |
394 |
409 bar"} |
395 Section \ref{sec:pretty} will also explain the infrastructure that Isabelle |
410 |
|
411 Section \ref{sec:pretty} will explain the infrastructure that Isabelle |
396 provides for more elaborate pretty printing. |
412 provides for more elaborate pretty printing. |
397 |
413 |
398 \begin{readmore} |
414 \begin{readmore} |
399 Most of the basic string functions of Isabelle are defined in |
415 Most of the basic string functions of Isabelle are defined in |
400 @{ML_file "Pure/library.ML"}. |
416 @{ML_file "Pure/library.ML"}. |
441 |> (fn x => (x, x)) |
457 |> (fn x => (x, x)) |
442 |> fst |
458 |> fst |
443 |> (fn x => x + 4)*} |
459 |> (fn x => x + 4)*} |
444 |
460 |
445 text {* |
461 text {* |
446 which increments its argument @{text x} by 5. It does this by first incrementing |
462 which increments its argument @{text x} by 5. It does this by first |
447 the argument by 1 (Line 2); then storing the result in a pair (Line 3); taking |
463 incrementing the argument by 1 (Line 2); then storing the result in a pair |
448 the first component of the pair (Line 4) and finally incrementing the first |
464 (Line 3); taking the first component of the pair (Line 4) and finally |
449 component by 4 (Line 5). This kind of cascading manipulations of values is quite |
465 incrementing the first component by 4 (Line 5). This kind of cascading |
450 common when dealing with theories (for example by adding a definition, followed by |
466 manipulations of values is quite common when dealing with theories. The |
451 lemmas and so on). The reverse application allows you to read what happens in |
467 reverse application allows you to read what happens in a top-down |
452 a top-down manner. This kind of coding should also be familiar, |
468 manner. This kind of coding should be familiar, if you have been exposed to |
453 if you have been exposed to Haskell's {\it do}-notation. Writing the function |
469 Haskell's {\it do}-notation. Writing the function @{ML inc_by_five} using |
454 @{ML inc_by_five} using the reverse application is much clearer than writing |
470 the reverse application is much clearer than writing |
455 *} |
471 *} |
456 |
472 |
457 ML{*fun inc_by_five x = fst ((fn x => (x, x)) (x + 1)) + 4*} |
473 ML{*fun inc_by_five x = fst ((fn x => (x, x)) (x + 1)) + 4*} |
458 |
474 |
459 text {* or *} |
475 text {* or *} |
493 applied to it. For example below three variables are applied to the term |
509 applied to it. For example below three variables are applied to the term |
494 @{term [show_types] "P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool"}: |
510 @{term [show_types] "P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool"}: |
495 |
511 |
496 @{ML_response_fake [display,gray] |
512 @{ML_response_fake [display,gray] |
497 "let |
513 "let |
498 val t = @{term \"P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool\"} |
514 val trm = @{term \"P::nat \<Rightarrow> int \<Rightarrow> unit \<Rightarrow> bool\"} |
499 val ctxt = @{context} |
515 val ctxt = @{context} |
500 in |
516 in |
501 apply_fresh_args t ctxt |
517 apply_fresh_args trm ctxt |
502 |> string_of_term ctxt |
518 |> string_of_term ctxt |
503 |> tracing |
519 |> tracing |
504 end" |
520 end" |
505 "P z za zb"} |
521 "P z za zb"} |
506 |
522 |
510 argument types (in the case above the list @{text "[nat, int, unit]"}); Line |
526 argument types (in the case above the list @{text "[nat, int, unit]"}); Line |
511 4 pairs up each type with the string @{text "z"}; the function @{ML_ind |
527 4 pairs up each type with the string @{text "z"}; the function @{ML_ind |
512 variant_frees in Variable} generates for each @{text "z"} a unique name |
528 variant_frees in Variable} generates for each @{text "z"} a unique name |
513 avoiding the given @{text f}; the list of name-type pairs is turned into a |
529 avoiding the given @{text f}; the list of name-type pairs is turned into a |
514 list of variable terms in Line 6, which in the last line is applied by the |
530 list of variable terms in Line 6, which in the last line is applied by the |
515 function @{ML_ind list_comb in Term} to the term. In this last step we have |
531 function @{ML_ind list_comb in Term} to the original term. In this last step we have |
516 to use the function @{ML_ind curry in Library}, because @{ML list_comb} |
532 to use the function @{ML_ind curry in Library}, because @{ML list_comb} |
517 expects the function and the variables list as a pair. |
533 expects the function and the variables list as a pair. |
518 |
534 |
519 Functions like @{ML apply_fresh_args} are often needed when constructing |
535 Functions like @{ML apply_fresh_args} are often needed when constructing |
520 terms with fresh variables. The |
536 terms involving fresh variables. For this the infrastructure helps |
521 infrastructure helps tremendously to avoid any name clashes. Consider for |
537 tremendously to avoid any name clashes. Consider for example: |
522 example: |
|
523 |
538 |
524 @{ML_response_fake [display,gray] |
539 @{ML_response_fake [display,gray] |
525 "let |
540 "let |
526 val t = @{term \"za::'a \<Rightarrow> 'b \<Rightarrow> 'c\"} |
541 val trm = @{term \"za::'a \<Rightarrow> 'b \<Rightarrow> 'c\"} |
527 val ctxt = @{context} |
542 val ctxt = @{context} |
528 in |
543 in |
529 apply_fresh_args t ctxt |
544 apply_fresh_args trm ctxt |
530 |> string_of_term ctxt |
545 |> string_of_term ctxt |
531 |> tracing |
546 |> tracing |
532 end" |
547 end" |
533 "za z zb"} |
548 "za z zb"} |
534 |
549 |
542 (fn x => x + 1) #> |
557 (fn x => x + 1) #> |
543 (fn x => x + 2) #> |
558 (fn x => x + 2) #> |
544 (fn x => x + 3)*} |
559 (fn x => x + 3)*} |
545 |
560 |
546 text {* |
561 text {* |
547 which is the function composed of first the increment-by-one function and then |
562 which is the function composed of first the increment-by-one function and |
548 increment-by-two, followed by increment-by-three. Again, the reverse function |
563 then increment-by-two, followed by increment-by-three. Again, the reverse |
549 composition allows you to read the code top-down. This combinator is often used |
564 function composition allows you to read the code top-down. This combinator |
550 for setup function inside the \isacommand{setup}-command. These function have to be |
565 is often used for setup functions inside the |
551 of type @{ML_type "theory -> theory"} in order to install, for example, some new |
566 \isacommand{setup}-command. These functions have to be of type @{ML_type |
552 data inside the current theory. More than one such setup function can be composed |
567 "theory -> theory"}. More than one such setup function can be composed with |
553 with @{ML "#>"}. For example |
568 @{ML "#>"}. For example |
554 *} |
569 *} |
555 |
570 |
556 setup %gray {* let |
571 setup %gray {* let |
557 val (ival1, setup_ival1) = Attrib.config_int "ival1" 1 |
572 val (ival1, setup_ival1) = Attrib.config_int "ival1" 1 |
558 val (ival2, setup_ival2) = Attrib.config_int "ival2" 2 |
573 val (ival2, setup_ival2) = Attrib.config_int "ival2" 2 |
622 text {* |
637 text {* |
623 These two functions can, for example, be used to avoid explicit @{text "lets"} for |
638 These two functions can, for example, be used to avoid explicit @{text "lets"} for |
624 intermediate values in functions that return pairs. As an example, suppose you |
639 intermediate values in functions that return pairs. As an example, suppose you |
625 want to separate a list of integers into two lists according to a |
640 want to separate a list of integers into two lists according to a |
626 treshold. If the treshold is @{ML "5"}, the list @{ML "[1,6,2,5,3,4]"} |
641 treshold. If the treshold is @{ML "5"}, the list @{ML "[1,6,2,5,3,4]"} |
627 should be separated to @{ML "([1,2,3,4], [6,5])"}. This function can be |
642 should be separated as @{ML "([1,2,3,4], [6,5])"}. Such a function can be |
628 implemented as |
643 implemented as |
629 *} |
644 *} |
630 |
645 |
631 ML{*fun separate i [] = ([], []) |
646 ML{*fun separate i [] = ([], []) |
632 | separate i (x::xs) = |
647 | separate i (x::xs) = |
636 if i <= x then (los, x::grs) else (x::los, grs) |
651 if i <= x then (los, x::grs) else (x::los, grs) |
637 end*} |
652 end*} |
638 |
653 |
639 text {* |
654 text {* |
640 where the return value of the recursive call is bound explicitly to |
655 where the return value of the recursive call is bound explicitly to |
641 the pair @{ML "(los, grs)" for los grs}. You can implement this function |
656 the pair @{ML "(los, grs)" for los grs}. However, this function |
642 more concisely as |
657 can be implemented more concisely as |
643 *} |
658 *} |
644 |
659 |
645 ML{*fun separate i [] = ([], []) |
660 ML{*fun separate i [] = ([], []) |
646 | separate i (x::xs) = |
661 | separate i (x::xs) = |
647 if i <= x |
662 if i <= x |
719 text {* |
734 text {* |
720 When using combinators for writing functions in waterfall fashion, it is |
735 When using combinators for writing functions in waterfall fashion, it is |
721 sometimes necessary to do some ``plumbing'' in order to fit functions |
736 sometimes necessary to do some ``plumbing'' in order to fit functions |
722 together. We have already seen such plumbing in the function @{ML |
737 together. We have already seen such plumbing in the function @{ML |
723 apply_fresh_args}, where @{ML curry} is needed for making the function @{ML |
738 apply_fresh_args}, where @{ML curry} is needed for making the function @{ML |
724 list_comb}, which works over pairs to fit with the combinator @{ML "|>"}. Such |
739 list_comb}, which works over pairs, to fit with the combinator @{ML "|>"}. Such |
725 plumbing is also needed in situations where a function operate over lists, |
740 plumbing is also needed in situations where a function operates over lists, |
726 but one calculates only with a single element. An example is the function |
741 but one calculates only with a single element. An example is the function |
727 @{ML_ind check_terms in Syntax}, whose purpose is to simultaneously type-check |
742 @{ML_ind check_terms in Syntax}, whose purpose is to simultaneously type-check |
728 a list of terms. Consider the code: |
743 a list of terms. Consider the code: |
729 |
744 |
730 @{ML_response_fake [display, gray] |
745 @{ML_response_fake [display, gray] |
809 *} |
824 *} |
810 |
825 |
811 ML{*fun not_current_thyname () = Context.theory_name @{theory} *} |
826 ML{*fun not_current_thyname () = Context.theory_name @{theory} *} |
812 |
827 |
813 text {* |
828 text {* |
814 |
|
815 does \emph{not} return the name of the current theory, if it is run in a |
829 does \emph{not} return the name of the current theory, if it is run in a |
816 different theory. Instead, the code above defines the constant function |
830 different theory. Instead, the code above defines the constant function |
817 that always returns the string @{text [quotes] "FirstSteps"}, no matter where the |
831 that always returns the string @{text [quotes] "FirstSteps"}, no matter where the |
818 function is called. Operationally speaking, the antiquotation @{text "@{theory}"} is |
832 function is called. Operationally speaking, the antiquotation @{text "@{theory}"} is |
819 \emph{not} replaced with code that will look up the current theory in |
833 \emph{not} replaced with code that will look up the current theory in |
820 some data structure and return it. Instead, it is literally |
834 some data structure and return it. Instead, it is literally |
821 replaced with the value representing the theory name. |
835 replaced with the value representing the theory. |
822 |
836 |
823 In a similar way you can use antiquotations to refer to proved theorems: |
837 Another important antiquotation is @{text "@{context}"}. (What the |
|
838 difference between a theory and a context is will be described in Chapter |
|
839 \ref{chp:advanced}.) A context is for example needed in order to use the |
|
840 function @{ML print_abbrevs in ProofContext} that list of all currently |
|
841 defined abbreviations. |
|
842 |
|
843 @{ML_response_fake [display, gray] |
|
844 "ProofContext.print_abbrevs @{context}" |
|
845 "Code_Evaluation.valtermify \<equiv> \<lambda>x. (x, \<lambda>u. Code_Evaluation.termify x) |
|
846 INTER \<equiv> INFI |
|
847 Inter \<equiv> Inf |
|
848 \<dots>"} |
|
849 |
|
850 You can also use antiquotations to refer to proved theorems: |
824 @{text "@{thm \<dots>}"} for a single theorem |
851 @{text "@{thm \<dots>}"} for a single theorem |
825 |
852 |
826 @{ML_response_fake [display,gray] "@{thm allI}" "(\<And>x. ?P x) \<Longrightarrow> \<forall>x. ?P x"} |
853 @{ML_response_fake [display,gray] "@{thm allI}" "(\<And>x. ?P x) \<Longrightarrow> \<forall>x. ?P x"} |
827 |
854 |
828 and @{text "@{thms \<dots>}"} for more than one |
855 and @{text "@{thms \<dots>}"} for more than one |
829 |
856 |
830 @{ML_response_fake [display,gray] "@{thms conj_ac}" |
857 @{ML_response_fake [display,gray] |
|
858 "@{thms conj_ac}" |
831 "(?P \<and> ?Q) = (?Q \<and> ?P) |
859 "(?P \<and> ?Q) = (?Q \<and> ?P) |
832 (?P \<and> ?Q \<and> ?R) = (?Q \<and> ?P \<and> ?R) |
860 (?P \<and> ?Q \<and> ?R) = (?Q \<and> ?P \<and> ?R) |
833 ((?P \<and> ?Q) \<and> ?R) = (?P \<and> ?Q \<and> ?R)"} |
861 ((?P \<and> ?Q) \<and> ?R) = (?P \<and> ?Q \<and> ?R)"} |
|
862 |
|
863 The thm-antiquotations can also be used for manipulating theorems. For |
|
864 example, if you need the version of te theorem @{thm [source] refl} that |
|
865 has a meta-equality instead of an equality, you can write |
|
866 |
|
867 @{ML_response_fake [display,gray] |
|
868 "@{thm refl[THEN eq_reflection]}" |
|
869 "?x \<equiv> ?x"} |
834 |
870 |
835 The point of these antiquotations is that referring to theorems in this way |
871 The point of these antiquotations is that referring to theorems in this way |
836 makes your code independent from what theorems the user might have stored |
872 makes your code independent from what theorems the user might have stored |
837 under this name (this becomes especially important when you deal with |
873 under this name (this becomes especially important when you deal with |
838 theorem lists; see Section \ref{sec:storing}). |
874 theorem lists; see Section \ref{sec:storing}). |
864 map #1 simps |
900 map #1 simps |
865 end*} |
901 end*} |
866 |
902 |
867 text {* |
903 text {* |
868 The function @{ML_ind dest_ss in MetaSimplifier} returns a record containing all |
904 The function @{ML_ind dest_ss in MetaSimplifier} returns a record containing all |
869 information stored in the simpset, but we are only interested in the names of the |
905 information stored in the simpset, but here we are only interested in the names of the |
870 simp-rules. Now you can feed in the current simpset into this function. |
906 simp-rules. Now you can feed in the current simpset into this function. |
871 The current simpset can be referred to using the antiquotation @{text "@{simpset}"}. |
907 The current simpset can be referred to using the antiquotation @{text "@{simpset}"}. |
872 |
908 |
873 @{ML_response_fake [display,gray] |
909 @{ML_response_fake [display,gray] |
874 "get_thm_names_from_ss @{simpset}" |
910 "get_thm_names_from_ss @{simpset}" |
875 "[\"Nat.of_nat_eq_id\", \"Int.of_int_eq_id\", \"Nat.One_nat_def\", \<dots>]"} |
911 "[\"Nat.of_nat_eq_id\", \"Int.of_int_eq_id\", \"Nat.One_nat_def\", \<dots>]"} |
876 |
912 |
877 Again, this way of referencing simpsets makes you independent from additions |
913 Again, this way of referencing simpsets makes you independent from additions |
878 of lemmas to the simpset by the user, which can potentially cause loops in your |
914 of lemmas to the simpset by the user, which can potentially cause loops in your |
879 code. |
915 code. |
880 |
|
881 On the ML-level of Isabelle, you often have to work with qualified names. |
|
882 These are strings with some additional information, such as positional |
|
883 information and qualifiers. Such qualified names can be generated with the |
|
884 antiquotation @{text "@{binding \<dots>}"}. For example |
|
885 |
|
886 @{ML_response [display,gray] |
|
887 "@{binding \"name\"}" |
|
888 "name"} |
|
889 |
|
890 An example where a qualified name is needed is the function |
|
891 @{ML_ind define in Local_Theory}. This function is used below to define |
|
892 the constant @{term "TrueConj"} as the conjunction @{term "True \<and> True"}. |
|
893 *} |
|
894 |
|
895 local_setup %gray {* |
|
896 Local_Theory.define ((@{binding "TrueConj"}, NoSyn), |
|
897 (Attrib.empty_binding, @{term "True \<and> True"})) #> snd *} |
|
898 |
|
899 text {* |
|
900 Now querying the definition you obtain: |
|
901 |
|
902 \begin{isabelle} |
|
903 \isacommand{thm}~@{text "TrueConj_def"}\\ |
|
904 @{text "> "}~@{thm TrueConj_def} |
|
905 \end{isabelle} |
|
906 |
|
907 \begin{readmore} |
|
908 The basic operations on bindings are implemented in |
|
909 @{ML_file "Pure/General/binding.ML"}. |
|
910 \end{readmore} |
|
911 |
|
912 \footnote{\bf FIXME give a better example why bindings are important} |
|
913 \footnote{\bf FIXME give a pointer to \isacommand{local\_setup}; if not, then explain |
|
914 why @{ML snd} is needed.} |
|
915 \footnote{\bf FIXME: There should probably a separate section on binding, long-names |
|
916 and sign.} |
|
917 |
916 |
918 It is also possible to define your own antiquotations. But you should |
917 It is also possible to define your own antiquotations. But you should |
919 exercise care when introducing new ones, as they can also make your code |
918 exercise care when introducing new ones, as they can also make your code |
920 also difficult to read. In the next chapter we describe how to construct |
919 also difficult to read. In the next chapter we describe how to construct |
921 terms with the (build in) antiquotation @{text "@{term \<dots>}"}. A restriction |
920 terms with the (build in) antiquotation @{text "@{term \<dots>}"}. A restriction |
1237 True \<and> True, False |
1236 True \<and> True, False |
1238 1 |
1237 1 |
1239 2, 1"} |
1238 2, 1"} |
1240 |
1239 |
1241 Many functions in Isabelle manage and update data in a similar |
1240 Many functions in Isabelle manage and update data in a similar |
1242 fashion. Consequently, such calculation with contexts occur frequently in |
1241 fashion. Consequently, such calculations with contexts occur frequently in |
1243 Isabelle code, although the ``context flow'' is usually only linear. |
1242 Isabelle code, although the ``context flow'' is usually only linear. |
1244 Note also that the calculation above has no effect on the underlying |
1243 Note also that the calculation above has no effect on the underlying |
1245 theory. Once we throw away the contexts, we have no access to their |
1244 theory. Once we throw away the contexts, we have no access to their |
1246 associated data. This is different to theories, where the command |
1245 associated data. This is different for theories, where the command |
1247 \isacommand{setup} registers the data with the current and future |
1246 \isacommand{setup} registers the data with the current and future |
1248 theories, and therefore one can access the data potentially |
1247 theories, and therefore one can access the data potentially |
1249 indefinitely. |
1248 indefinitely. |
1250 |
1249 |
1251 For convenience there is an abstract layer, namely the type @{ML_type Context.generic}, |
1250 For convenience there is an abstract layer, namely the type @{ML_type Context.generic}, |