ProgTutorial/Essential.thy
author Norbert Schirmer <norbert.schirmer@web.de>
Fri, 17 May 2019 10:38:01 +0200
changeset 569 f875a25aa72d
parent 567 f7c97e64cc2a
child 572 438703674711
permissions -rw-r--r--
prefer cartouches over " in ML antiquotations
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
395
2c392f61f400 spilt the Essential's chapter
Christian Urban <urbanc@in.tum.de>
parents: 394
diff changeset
     1
theory Essential
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
     2
imports Base First_Steps
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
begin
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
     5
chapter \<open>Isabelle Essentials\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
     6
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
     7
text \<open>
410
Christian Urban <urbanc@in.tum.de>
parents: 409
diff changeset
     8
   \begin{flushright}
Christian Urban <urbanc@in.tum.de>
parents: 409
diff changeset
     9
  {\em One man's obfuscation is another man's abstraction.} \\[1ex]
Christian Urban <urbanc@in.tum.de>
parents: 409
diff changeset
    10
  Frank Ch.~Eigler on the Linux Kernel Mailing List,\\ 
Christian Urban <urbanc@in.tum.de>
parents: 409
diff changeset
    11
  24~Nov.~2009
Christian Urban <urbanc@in.tum.de>
parents: 409
diff changeset
    12
  \end{flushright}
Christian Urban <urbanc@in.tum.de>
parents: 409
diff changeset
    13
Christian Urban <urbanc@in.tum.de>
parents: 409
diff changeset
    14
  \medskip
534
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
    15
  Isabelle is built around a few central ideas. One central idea is the
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
    16
  LCF-approach to theorem proving \cite{GordonMilnerWadsworth79} where there
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
    17
  is a small trusted core and everything else is built on top of this trusted
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
    18
  core. The fundamental data structures involved in this core are certified
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
    19
  terms and certified types, as well as theorems.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    20
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    21
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    22
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    23
section \<open>Terms and Types\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    24
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    25
text \<open>
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    26
  In Isabelle, there are certified terms and uncertified terms (respectively types). 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    27
  Uncertified terms are often just called terms. One way to construct them is by 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    28
  using the antiquotation \mbox{\<open>@{term \<dots>}\<close>}. For example
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    29
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
    30
  @{ML_matchresult [display,gray] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    31
\<open>@{term "(a::nat) + b = c"}\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    32
\<open>Const ("HOL.eq", _) $ 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    33
  (Const ("Groups.plus_class.plus", _) $ _ $ _) $ _\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    34
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    35
  constructs the term @{term "(a::nat) + b = c"}. The resulting term is printed using 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    36
  the internal representation corresponding to the datatype @{ML_type_ind "term"}, 
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
    37
  which is defined as follows: 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    38
\<close>  
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    39
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    40
ML_val %linenosgray\<open>datatype term =
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    41
  Const of string * typ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    42
| Free of string * typ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    43
| Var of indexname * typ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    44
| Bound of int 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    45
| Abs of string * typ * term 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    46
| $ of term * term\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    47
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
    48
text \<open>
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
    49
  This datatype implements Church-style lambda-terms, where types are
534
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
    50
  explicitly recorded in variables, constants and abstractions.  The
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
    51
  important point of having terms is that you can pattern-match against them;
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
    52
  this cannot be done with certified terms. As can be seen in Line 5,
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
    53
  terms use the usual de Bruijn index mechanism for representing bound
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
    54
  variables.  For example in
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    55
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
    56
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    57
  \<open>@{term "\<lambda>x y. x y"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    58
  \<open>Abs ("x", "'a \<Rightarrow> 'b", Abs ("y", "'a", Bound 1 $ Bound 0))\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    59
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
  the indices refer to the number of Abstractions (@{ML Abs}) that we need to
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    61
  skip until we hit the @{ML Abs} that binds the corresponding
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
  variable. Constructing a term with dangling de Bruijn indices is possible,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    63
  but will be flagged as ill-formed when you try to typecheck or certify it
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    64
  (see Section~\ref{sec:typechecking}). Note that the names of bound variables
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    65
  are kept at abstractions for printing purposes, and so should be treated
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    66
  only as ``comments''.  Application in Isabelle is realised with the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    67
  term-constructor @{ML $}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    68
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
    69
  Be careful if you pretty-print terms. Consider pretty-printing the abstraction
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
    70
  term shown above:
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
    71
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
    72
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    73
\<open>@{term "\<lambda>x y. x y"}
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
    74
  |> pretty_term @{context}
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    75
  |> pwriteln\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    76
  \<open>\<lambda>x. x\<close>}
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
    77
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
    78
  This is one common source for puzzlement in Isabelle, which has 
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
    79
  tacitly eta-contracted the output. You obtain a similar result 
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
    80
  with beta-redexes
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
    81
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
    82
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    83
\<open>let 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    84
  val redex = @{term "(\<lambda>(x::int) (y::int). x)"} 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    85
  val arg1 = @{term "1::int"} 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    86
  val arg2 = @{term "2::int"}
534
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
    87
in
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
    88
  pretty_term @{context} (redex $ arg1 $ arg2)
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
    89
  |> pwriteln
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    90
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
    91
  \<open>1\<close>}
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
    92
534
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
    93
  There is a dedicated configuration value for switching off tacit
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
    94
  eta-contractions, namely @{ML_ind eta_contract in Syntax} (see Section
507
d770a7b31aeb modified the passage on beta-contractions
Christian Urban <urbanc@in.tum.de>
parents: 505
diff changeset
    95
  \ref{sec:printing}), but none for beta-contractions. However you can avoid
534
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
    96
  the beta-contractions by switching off abbreviations using the configuration
507
d770a7b31aeb modified the passage on beta-contractions
Christian Urban <urbanc@in.tum.de>
parents: 505
diff changeset
    97
  value @{ML_ind show_abbrevs in Syntax}. For example
d770a7b31aeb modified the passage on beta-contractions
Christian Urban <urbanc@in.tum.de>
parents: 505
diff changeset
    98
d770a7b31aeb modified the passage on beta-contractions
Christian Urban <urbanc@in.tum.de>
parents: 505
diff changeset
    99
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   100
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   101
  \<open>let 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   102
  val redex = @{term "(\<lambda>(x::int) (y::int). x)"} 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   103
  val arg1 = @{term "1::int"} 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   104
  val arg2 = @{term "2::int"}
534
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
   105
  val ctxt = Config.put show_abbrevs false @{context}
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
   106
in
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
   107
  pretty_term ctxt (redex $ arg1 $ arg2)
Christian Urban <urbanc@in.tum.de>
parents: 533
diff changeset
   108
  |> pwriteln
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   109
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   110
  \<open>(\<lambda>x y. x) 1 2\<close>}
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   111
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   112
  Isabelle makes a distinction between \emph{free} variables (term-constructor
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   113
  @{ML Free} and written on the user level in blue colour) and
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   114
  \emph{schematic} variables (term-constructor @{ML Var} and written with a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   115
  leading question mark). Consider the following two examples
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   116
  
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   117
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   118
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   119
  val v1 = Var (("x", 3), @{typ bool})
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   120
  val v2 = Var (("x1", 3), @{typ bool})
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   121
  val v3 = Free ("x", @{typ bool})
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   122
in
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   123
  pretty_terms @{context} [v1, v2, v3]
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   124
  |> pwriteln
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   125
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   126
\<open>?x3, ?x1.3, x\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   127
502
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   128
  When constructing terms, you are usually concerned with free
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   129
  variables (as mentioned earlier, you cannot construct schematic
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   130
  variables using the built-in antiquotation \mbox{\<open>@{term
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   131
  \<dots>}\<close>}). If you deal with theorems, you have to, however, observe the
502
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   132
  distinction. The reason is that only schematic variables can be
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   133
  instantiated with terms when a theorem is applied. A similar
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
   134
  distinction between free and schematic variables holds for types
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   135
  (see below).
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   136
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   137
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   138
  Terms and types are described in detail in \isccite{sec:terms}. Their
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   139
  definition and many useful operations are implemented in @{ML_file "Pure/term.ML"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   140
  For constructing terms involving HOL constants, many helper functions are defined
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
  in @{ML_file "HOL/Tools/hologic.ML"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
  Constructing terms via antiquotations has the advantage that only typable
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
  terms can be constructed. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   147
  @{ML_matchresult_fake_both [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   148
  \<open>@{term "x x"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   149
  \<open>Type unification failed: Occurs check!\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
559
ffa5c4ec9611 improvements by Piotr Trojanek
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 557
diff changeset
   151
  raises a typing error, while it is perfectly ok to construct the term
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   152
  with the raw ML-constructors:
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   153
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   154
  @{ML_matchresult_fake [display,gray] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   155
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   156
  val omega = Free ("x", @{typ "nat \<Rightarrow> nat"}) $ Free ("x", @{typ nat})
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   157
in 
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   158
  pwriteln (pretty_term @{context} omega)
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   159
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   160
  \<open>x x\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   161
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   162
  Sometimes the internal representation of terms can be surprisingly different
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   163
  from what you see at the user-level, because the layers of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   164
  parsing/type-checking/pretty printing can be quite elaborate. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   165
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   166
  \begin{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   167
  Look at the internal term representation of the following terms, and
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   168
  find out why they are represented like this:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   169
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   170
  \begin{itemize}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   171
  \item @{term "case x of 0 \<Rightarrow> 0 | Suc y \<Rightarrow> y"}  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   172
  \item @{term "\<lambda>(x,y). P y x"}  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   173
  \item @{term "{ [x::int] | x. x \<le> -2 }"}  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   174
  \end{itemize}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   175
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   176
  Hint: The third term is already quite big, and the pretty printer
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   177
  may omit parts of it by default. If you want to see all of it, you
557
77ea2de0ca62 updated for Isabelle 2014
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 555
diff changeset
   178
  need to set the printing depth to a higher value by 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   179
  \end{exercise}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   180
\<close>
557
77ea2de0ca62 updated for Isabelle 2014
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 555
diff changeset
   181
77ea2de0ca62 updated for Isabelle 2014
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 555
diff changeset
   182
declare [[ML_print_depth = 50]]
77ea2de0ca62 updated for Isabelle 2014
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 555
diff changeset
   183
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   184
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   185
  The antiquotation \<open>@{prop \<dots>}\<close> constructs terms by inserting the 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   186
  usually invisible \<open>Trueprop\<close>-coercions whenever necessary. 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   187
  Consider for example the pairs
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   188
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   189
@{ML_matchresult [display,gray] \<open>(@{term "P x"}, @{prop "P x"})\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   190
\<open>(Free ("P", _) $ Free ("x", _),
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   191
 Const ("HOL.Trueprop", _) $ (Free ("P", _) $ Free ("x", _)))\<close>}
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
   192
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   193
  where a coercion is inserted in the second component and 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   194
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   195
  @{ML_matchresult [display,gray] \<open>(@{term "P x \<Longrightarrow> Q x"}, @{prop "P x \<Longrightarrow> Q x"})\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   196
  \<open>(Const ("Pure.imp", _) $ _ $ _, 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   197
 Const ("Pure.imp", _) $ _ $ _)\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   198
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   199
  where it is not (since it is already constructed by a meta-implication). 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   200
  The purpose of the \<open>Trueprop\<close>-coercion is to embed formulae of
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   201
  an object logic, for example HOL, into the meta-logic of Isabelle. The coercion
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
  is needed whenever a term is constructed that will be proved as a theorem. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   203
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   204
  As already seen above, types can be constructed using the antiquotation 
566
6103b0eadbf2 tuned parser for patterns in ML_response... antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 565
diff changeset
   205
  \<open>@{typ _}\<close>. For example:
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   206
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   207
  @{ML_matchresult_fake [display,gray] \<open>@{typ "bool \<Rightarrow> nat"}\<close> \<open>bool \<Rightarrow> nat\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   208
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   209
  The corresponding datatype is
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   210
\<close>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   211
  
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   212
ML_val %grayML\<open>datatype typ =
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   213
  Type  of string * typ list 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   214
| TFree of string * sort 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   215
| TVar  of indexname * sort\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   216
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   217
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   218
  Like with terms, there is the distinction between free type
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   219
  variables (term-constructor @{ML \<open>TFree\<close>}) and schematic
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   220
  type variables (term-constructor @{ML \<open>TVar\<close>} and printed with
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   221
  a leading question mark). A type constant,
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   222
  like @{typ "int"} or @{typ bool}, are types with an empty list
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   223
  of argument types. However, it needs a bit of effort to show an
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   224
  example, because Isabelle always pretty prints types (unlike terms).
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   225
  Using just the antiquotation \<open>@{typ "bool"}\<close> we only see
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   226
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   227
  @{ML_matchresult [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   228
  \<open>@{typ "bool"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   229
  \<open>bool\<close>}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   230
  which is the pretty printed version of \<open>bool\<close>. However, in PolyML
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   231
  (version \<open>\<ge>\<close>5.3) it is easy to install your own pretty printer. With the
393
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   232
  function below we mimic the behaviour of the usual pretty printer for
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   233
  datatypes (it uses pretty-printing functions which will be explained in more
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   234
  detail in Section~\ref{sec:pretty}).
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   235
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   236
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   237
ML %grayML\<open>local
393
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   238
  fun pp_pair (x, y) = Pretty.list "(" ")" [x, y]
392
47e5b71c7f6c modified the typ-pretty-printer and added parser exercise
Christian Urban <urbanc@in.tum.de>
parents: 389
diff changeset
   239
  fun pp_list xs = Pretty.list "[" "]" xs
47e5b71c7f6c modified the typ-pretty-printer and added parser exercise
Christian Urban <urbanc@in.tum.de>
parents: 389
diff changeset
   240
  fun pp_str s   = Pretty.str s
47e5b71c7f6c modified the typ-pretty-printer and added parser exercise
Christian Urban <urbanc@in.tum.de>
parents: 389
diff changeset
   241
  fun pp_qstr s  = Pretty.quote (pp_str s)
47e5b71c7f6c modified the typ-pretty-printer and added parser exercise
Christian Urban <urbanc@in.tum.de>
parents: 389
diff changeset
   242
  fun pp_int i   = pp_str (string_of_int i)
47e5b71c7f6c modified the typ-pretty-printer and added parser exercise
Christian Urban <urbanc@in.tum.de>
parents: 389
diff changeset
   243
  fun pp_sort S  = pp_list (map pp_qstr S)
393
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   244
  fun pp_constr a args = Pretty.block [pp_str a, Pretty.brk 1, args]
392
47e5b71c7f6c modified the typ-pretty-printer and added parser exercise
Christian Urban <urbanc@in.tum.de>
parents: 389
diff changeset
   245
in
393
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   246
fun raw_pp_typ (TVar ((a, i), S)) = 
392
47e5b71c7f6c modified the typ-pretty-printer and added parser exercise
Christian Urban <urbanc@in.tum.de>
parents: 389
diff changeset
   247
      pp_constr "TVar" (pp_pair (pp_pair (pp_qstr a, pp_int i), pp_sort S))
393
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   248
  | raw_pp_typ (TFree (a, S)) = 
392
47e5b71c7f6c modified the typ-pretty-printer and added parser exercise
Christian Urban <urbanc@in.tum.de>
parents: 389
diff changeset
   249
      pp_constr "TFree" (pp_pair (pp_qstr a, pp_sort S))
393
Christian Urban <urbanc@in.tum.de>
parents: 392
diff changeset
   250
  | raw_pp_typ (Type (a, tys)) = 
392
47e5b71c7f6c modified the typ-pretty-printer and added parser exercise
Christian Urban <urbanc@in.tum.de>
parents: 389
diff changeset
   251
      pp_constr "Type" (pp_pair (pp_qstr a, pp_list (map raw_pp_typ tys)))
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   252
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   253
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   254
text \<open>
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   255
  We can install this pretty printer with the function 
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
   256
  @{ML_ind ML_system_pp} as follows.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   257
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   258
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   259
ML %grayML\<open>ML_system_pp
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   260
  (fn _ => fn _ => Pretty.to_polyml o raw_pp_typ)\<close>
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
   261
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
   262
ML \<open>@{typ "bool"}\<close>
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   263
text \<open>
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   264
  Now the type bool is printed out in full detail.
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   265
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   266
  @{ML_matchresult [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   267
  \<open>@{typ "bool"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   268
  \<open>Type ("HOL.bool", [])\<close>}
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   269
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   270
  When printing out a list-type
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   271
  
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   272
  @{ML_matchresult [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   273
  \<open>@{typ "'a list"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   274
  \<open>Type ("List.list", [TFree ("'a", ["HOL.type"])])\<close>}
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   275
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   276
  we can see the full name of the type is actually \<open>List.list\<close>, indicating
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   277
  that it is defined in the theory \<open>List\<close>. However, one has to be 
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   278
  careful with names of types, because even if
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   279
  \<open>fun\<close> is defined in the theory \<open>HOL\<close>, it is  
559
ffa5c4ec9611 improvements by Piotr Trojanek
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 557
diff changeset
   280
  still represented by its simple name.
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   281
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   282
   @{ML_matchresult [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   283
  \<open>@{typ "bool \<Rightarrow> nat"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   284
  \<open>Type ("fun", [Type ("HOL.bool", []), Type ("Nat.nat", [])])\<close>}
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   285
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   286
  We can restore the usual behaviour of Isabelle's pretty printer
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   287
  with the code
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   288
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   289
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   290
ML %grayML\<open>ML_system_pp
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   291
  (fn depth => fn _ => ML_Pretty.to_polyml o Pretty.to_ML depth o Proof_Display.pp_typ Theory.get_pure)\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   292
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   293
text \<open>
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   294
  After that the types for booleans, lists and so on are printed out again 
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   295
  the standard Isabelle way.
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
   296
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   297
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   298
  \<open>@{typ "bool"};
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   299
@{typ "'a list"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   300
  \<open>"bool"
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   301
"'a List.list"\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   302
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   303
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   304
  Types are described in detail in \isccite{sec:types}. Their
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   305
  definition and many useful operations are implemented 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   306
  in @{ML_file "Pure/type.ML"}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   307
  \end{readmore}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   308
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   309
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   310
section \<open>Constructing Terms and Types Manually\label{sec:terms_types_manually}\<close> 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   311
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   312
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   313
  While antiquotations are very convenient for constructing terms, they can
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   314
  only construct fixed terms (remember they are ``linked'' at compile-time).
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
   315
  However, you often need to construct terms manually. For example, a
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   316
  function that returns the implication \<open>\<And>(x::nat). P x \<Longrightarrow> Q x\<close> taking
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   317
  @{term P} and @{term Q} as arguments can only be written as:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   318
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   319
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   320
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   321
ML %grayML\<open>fun make_imp P Q =
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   322
let
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   323
  val x = Free ("x", @{typ nat})
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   324
in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   325
  Logic.all x (Logic.mk_implies (P $ x, Q $ x))
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   326
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   327
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   328
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   329
  The reason is that you cannot pass the arguments @{term P} and @{term Q} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   330
  into an antiquotation.\footnote{At least not at the moment.} For example 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   331
  the following does \emph{not} work.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   332
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   333
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   334
ML %grayML\<open>fun make_wrong_imp P Q = @{prop "\<And>(x::nat). P x \<Longrightarrow> Q x"}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   335
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   336
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   337
  To see this, apply \<open>@{term S}\<close> and \<open>@{term T}\<close> 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   338
  to both functions. With @{ML make_imp} you obtain the intended term involving 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   339
  the given arguments
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   340
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   341
  @{ML_matchresult [display,gray] \<open>make_imp @{term S} @{term T}\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   342
\<open>Const _ $ 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   343
  Abs ("x", Type ("Nat.nat",[]),
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   344
    Const _ $ (Free ("S",_) $ _) $ (Free ("T",_) $ _))\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   345
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   346
  whereas with @{ML make_wrong_imp} you obtain a term involving the @{term "P"} 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   347
  and \<open>Q\<close> from the antiquotation.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   348
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   349
  @{ML_matchresult [display,gray] \<open>make_wrong_imp @{term S} @{term T}\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   350
\<open>Const _ $ 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   351
  Abs ("x", _,
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   352
    Const _ $ (Const _ $ (Free ("P",_) $ _)) $ 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   353
                (Const _ $ (Free ("Q",_) $ _)))\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   354
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   355
  There are a number of handy functions that are frequently used for
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   356
  constructing terms. One is the function @{ML_ind list_comb in Term}, which
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   357
  takes as argument a term and a list of terms, and produces as output the
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   358
  term list applied to the term. For example
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   359
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   360
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   361
@{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   362
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   363
  val trm = @{term "P::bool \<Rightarrow> bool \<Rightarrow> bool"}
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   364
  val args = [@{term "True"}, @{term "False"}]
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   365
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   366
  list_comb (trm, args)
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   367
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   368
\<open>Free ("P", "bool \<Rightarrow> bool \<Rightarrow> bool") 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   369
   $ Const ("True", "bool") $ Const ("False", "bool")\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   370
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   371
  Another handy function is @{ML_ind lambda in Term}, which abstracts a variable 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   372
  in a term. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   373
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   374
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   375
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   376
  val x_nat = @{term "x::nat"}
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   377
  val trm = @{term "(P::nat \<Rightarrow> bool) x"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   378
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   379
  lambda x_nat trm
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   380
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   381
  \<open>Abs ("x", "Nat.nat", Free ("P", "bool \<Rightarrow> bool") $ Bound 0)\<close>}
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   382
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   383
  In this example, @{ML lambda} produces a de Bruijn index (i.e.\ @{ML \<open>Bound 0\<close>}), 
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   384
  and an abstraction, where it also records the type of the abstracted
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   385
  variable and for printing purposes also its name.  Note that because of the
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   386
  typing annotation on \<open>P\<close>, the variable \<open>x\<close> in \<open>P x\<close>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   387
  is of the same type as the abstracted variable. If it is of different type,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   388
  as in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   389
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   390
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   391
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   392
  val x_int = @{term "x::int"}
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   393
  val trm = @{term "(P::nat \<Rightarrow> bool) x"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   394
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   395
  lambda x_int trm
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   396
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   397
  \<open>Abs ("x", "int", Free ("P", "nat \<Rightarrow> bool") $ Free ("x", "nat"))\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   398
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   399
  then the variable \<open>Free ("x", "nat")\<close> is \emph{not} abstracted. 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   400
  This is a fundamental principle
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   401
  of Church-style typing, where variables with the same name still differ, if they 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   402
  have different type.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   403
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   404
  There is also the function @{ML_ind subst_free in Term} with which terms can be
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   405
  replaced by other terms. For example below, we will replace in @{term
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   406
  "(f::nat \<Rightarrow> nat \<Rightarrow> nat) 0 x"} the subterm @{term "(f::nat \<Rightarrow> nat \<Rightarrow> nat) 0"} by
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   407
  @{term y}, and @{term x} by @{term True}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   408
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   409
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   410
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   411
  val sub1 = (@{term "(f::nat \<Rightarrow> nat \<Rightarrow> nat) 0"}, @{term "y::nat \<Rightarrow> nat"})
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   412
  val sub2 = (@{term "x::nat"}, @{term "True"})
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   413
  val trm = @{term "((f::nat \<Rightarrow> nat \<Rightarrow> nat) 0) x"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   414
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   415
  subst_free [sub1, sub2] trm
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   416
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   417
  \<open>Free ("y", "nat \<Rightarrow> nat") $ Const ("True", "bool")\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   418
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   419
  As can be seen, @{ML subst_free} does not take typability into account.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   420
  However it takes alpha-equivalence into account:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   421
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   422
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   423
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   424
  val sub = (@{term "(\<lambda>y::nat. y)"}, @{term "x::nat"})
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   425
  val trm = @{term "(\<lambda>x::nat. x)"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   426
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   427
  subst_free [sub] trm
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   428
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   429
  \<open>Free ("x", "nat")\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   430
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   431
  Similarly the function @{ML_ind subst_bounds in Term}, replaces lose bound 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   432
  variables with terms. To see how this function works, let us implement a
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   433
  function that strips off the outermost forall quantifiers in a term.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   434
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   435
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   436
ML %grayML\<open>fun strip_alls t =
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   437
let 
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   438
  fun aux (x, T, t) = strip_alls t |>> cons (Free (x, T))
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   439
in
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   440
  case t of
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   441
    Const (@{const_name All}, _) $ Abs body => aux body
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   442
  | _ => ([], t)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   443
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   444
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   445
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   446
  The function returns a pair consisting of the stripped off variables and
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   447
  the body of the universal quantification. For example
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   448
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   449
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   450
  \<open>strip_alls @{term "\<forall>x y. x = (y::bool)"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   451
\<open>([Free ("x", "bool"), Free ("y", "bool")],
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   452
  Const ("op =", _) $ Bound 1 $ Bound 0)\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   453
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   454
  Note that we produced in the body two dangling de Bruijn indices. 
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   455
  Later on we will also use the inverse function that
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   456
  builds the quantification from a body and a list of (free) variables.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   457
\<close>
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   458
  
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   459
ML %grayML\<open>fun build_alls ([], t) = t
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   460
  | build_alls (Free (x, T) :: vs, t) = 
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   461
      Const (@{const_name "All"}, (T --> @{typ bool}) --> @{typ bool}) 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   462
        $ Abs (x, T, build_alls (vs, t))\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   463
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   464
text \<open>
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   465
  As said above, after calling @{ML strip_alls}, you obtain a term with loose
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   466
  bound variables. With the function @{ML subst_bounds}, you can replace these
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   467
  loose @{ML_ind Bound in Term}s with the stripped off variables.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   468
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   469
  @{ML_matchresult_fake [display, gray, linenos]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   470
  \<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   471
  val (vrs, trm) = strip_alls @{term "\<forall>x y. x = (y::bool)"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   472
in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   473
  subst_bounds (rev vrs, trm) 
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   474
  |> pretty_term @{context}
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   475
  |> pwriteln
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   476
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   477
  \<open>x = y\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   478
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   479
  Note that in Line 4 we had to reverse the list of variables that @{ML
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   480
  strip_alls} returned. The reason is that the head of the list the function
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   481
  @{ML subst_bounds} takes is the replacement for @{ML \<open>Bound 0\<close>}, the next
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   482
  element for @{ML \<open>Bound 1\<close>} and so on. 
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   483
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   484
  Notice also that this function might introduce name clashes, since we
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   485
  substitute just a variable with the name recorded in an abstraction. This
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   486
  name is by no means unique. If clashes need to be avoided, then we should
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   487
  use the function @{ML_ind dest_abs in Term}, which returns the body where
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   488
  the loose de Bruijn index is replaced by a unique free variable. For example
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   489
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   490
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   491
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   492
  \<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   493
  val body = Bound 0 $ Free ("x", @{typ nat})
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
   494
in
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   495
  Term.dest_abs ("x", @{typ "nat \<Rightarrow> bool"}, body)
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   496
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   497
  \<open>("xa", Free ("xa", "Nat.nat \<Rightarrow> bool") $ Free ("x", "Nat.nat"))\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   498
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   499
  Sometimes it is necessary to manipulate de Bruijn indices in terms directly.
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   500
  There are many functions to do this. We describe only two. The first,
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   501
  @{ML_ind incr_boundvars in Term}, increases by an integer the indices 
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   502
  of the loose bound variables in a term. In the code below
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   503
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   504
@{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   505
\<open>@{term "\<forall>x y z u. z = u"}
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   506
  |> strip_alls
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   507
  ||> incr_boundvars 2
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   508
  |> build_alls
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   509
  |> pretty_term @{context}
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   510
  |> pwriteln\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   511
  \<open>\<forall>x y z u. x = y\<close>}
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   512
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   513
  we first strip off the forall-quantified variables (thus creating two loose
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   514
  bound variables in the body); then we increase the indices of the loose
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   515
  bound variables by @{ML 2} and finally re-quantify the variables. As a
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   516
  result of @{ML incr_boundvars}, we obtain now a term that has the equation
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   517
  between the first two quantified variables.
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   518
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   519
  The second function, @{ML_ind loose_bvar1 in Text}, tests whether a term
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   520
  contains a loose bound of a certain index. For example
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   521
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   522
  @{ML_matchresult_fake [gray,display]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   523
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   524
  val body = snd (strip_alls @{term "\<forall>x y. x = (y::bool)"})
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   525
in
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   526
  [loose_bvar1 (body, 0),
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   527
   loose_bvar1 (body, 1),
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   528
   loose_bvar1 (body, 2)]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   529
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   530
  \<open>[true, true, false]\<close>}
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   531
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   532
  There are also many convenient functions that construct specific HOL-terms
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   533
  in the structure @{ML_structure HOLogic}. For example @{ML_ind mk_eq in
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   534
  HOLogic} constructs an equality out of two terms.  The types needed in this
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   535
  equality are calculated from the type of the arguments. For example
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   536
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   537
@{ML_matchresult_fake [gray,display]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   538
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   539
  val eq = HOLogic.mk_eq (@{term "True"}, @{term "False"})
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   540
in
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   541
  eq |> pretty_term @{context}
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   542
     |> pwriteln
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   543
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   544
  \<open>True = False\<close>}
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
   545
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   546
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   547
  There are many functions in @{ML_file "Pure/term.ML"}, @{ML_file
374
Christian Urban <urbanc@in.tum.de>
parents: 369
diff changeset
   548
  "Pure/logic.ML"} and @{ML_file "HOL/Tools/hologic.ML"} that make manual
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   549
  constructions of terms and types easier.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   550
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   551
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   552
  When constructing terms manually, there are a few subtle issues with
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   553
  constants. They usually crop up when pattern matching terms or types, or
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   554
  when constructing them. While it is perfectly ok to write the function
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   555
  \<open>is_true\<close> as follows
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   556
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   557
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   558
ML %grayML\<open>fun is_true @{term True} = true
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   559
  | is_true _ = false\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   560
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   561
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   562
  this does not work for picking out \<open>\<forall>\<close>-quantified terms. Because
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   563
  the function 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   564
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   566
ML %grayML\<open>fun is_all (@{term All} $ _) = true
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   567
  | is_all _ = false\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   568
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   569
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   570
  will not correctly match the formula @{prop[source] "\<forall>x::nat. P x"}: 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   571
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   572
  @{ML_matchresult [display,gray] \<open>is_all @{term "\<forall>x::nat. P x"}\<close> \<open>false\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   573
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   574
  The problem is that the \<open>@term\<close>-antiquotation in the pattern 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   575
  fixes the type of the constant @{term "All"} to be @{typ "('a \<Rightarrow> bool) \<Rightarrow> bool"} for 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   576
  an arbitrary, but fixed type @{typ "'a"}. A properly working alternative 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   577
  for this function is
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   578
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   579
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   580
ML %grayML\<open>fun is_all (Const ("HOL.All", _) $ _) = true
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   581
  | is_all _ = false\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   582
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   583
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   584
  because now
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   585
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   586
  @{ML_matchresult [display,gray] \<open>is_all @{term "\<forall>x::nat. P x"}\<close> \<open>true\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   587
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   588
  matches correctly (the first wildcard in the pattern matches any type and the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   589
  second any term).
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   590
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   591
  However there is still a problem: consider the similar function that
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   592
  attempts to pick out \<open>Nil\<close>-terms:
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   593
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   594
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   595
ML %grayML\<open>fun is_nil (Const ("Nil", _)) = true
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   596
  | is_nil _ = false\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   597
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   598
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   599
  Unfortunately, also this function does \emph{not} work as expected, since
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   600
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   601
  @{ML_matchresult [display,gray] \<open>is_nil @{term "Nil"}\<close> \<open>false\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   602
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   603
  The problem is that on the ML-level the name of a constant is more
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   604
  subtle than you might expect. The function @{ML is_all} worked correctly,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   605
  because @{term "All"} is such a fundamental constant, which can be referenced
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   606
  by @{ML \<open>Const ("All", some_type)\<close> for some_type}. However, if you look at
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   607
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   608
  @{ML_matchresult [display,gray] \<open>@{term "Nil"}\<close> \<open>Const ("List.list.Nil", _)\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   609
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   610
  the name of the constant \<open>Nil\<close> depends on the theory in which the
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   611
  term constructor is defined (\<open>List\<close>) and also in which datatype
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   612
  (\<open>list\<close>). Even worse, some constants have a name involving
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   613
  type-classes. Consider for example the constants for @{term "zero"} and
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 552
diff changeset
   614
  \mbox{@{term "times"}}:
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 552
diff changeset
   615
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   616
  @{ML_matchresult [display,gray] \<open>(@{term "0::nat"}, @{term "times"})\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   617
 \<open>(Const ("Groups.zero_class.zero", _), 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   618
 Const ("Groups.times_class.times", _))\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   619
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   620
  While you could use the complete name, for example 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   621
  @{ML \<open>Const ("List.list.Nil", some_type)\<close> for some_type}, for referring to or
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   622
  matching against \<open>Nil\<close>, this would make the code rather brittle. 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   623
  The reason is that the theory and the name of the datatype can easily change. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   624
  To make the code more robust, it is better to use the antiquotation 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   625
  \<open>@{const_name \<dots>}\<close>. With this antiquotation you can harness the 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   626
  variable parts of the constant's name. Therefore a function for 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   627
  matching against constants that have a polymorphic type should 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   628
  be written as follows.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   629
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   630
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   631
ML %grayML\<open>fun is_nil_or_all (Const (@{const_name "Nil"}, _)) = true
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   632
  | is_nil_or_all (Const (@{const_name "All"}, _) $ _) = true
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   633
  | is_nil_or_all _ = false\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   634
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   635
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   636
  The antiquotation for properly referencing type constants is \<open>@{type_name \<dots>}\<close>.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   637
  For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   638
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   639
  @{ML_matchresult [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   640
  \<open>@{type_name "list"}\<close> \<open>"List.list"\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   641
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   642
  Although types of terms can often be inferred, there are many
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   643
  situations where you need to construct types manually, especially  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   644
  when defining constants. For example the function returning a function 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   645
  type is as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   646
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   647
\<close> 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   648
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   649
ML %grayML\<open>fun make_fun_type ty1 ty2 = Type ("fun", [ty1, ty2])\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   650
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   651
text \<open>This can be equally written with the combinator @{ML_ind "-->" in Term} as:\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   652
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   653
ML %grayML\<open>fun make_fun_type ty1 ty2 = ty1 --> ty2\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   654
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   655
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   656
  If you want to construct a function type with more than one argument
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
   657
  type, then you can use @{ML_ind "--->" in Term}.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   658
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   659
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   660
ML %grayML\<open>fun make_fun_types tys ty = tys ---> ty\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   661
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   662
text \<open>
369
74ba778b09c9 tuned index
Christian Urban <urbanc@in.tum.de>
parents: 368
diff changeset
   663
  A handy function for manipulating terms is @{ML_ind map_types in Term}: it takes a 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   664
  function and applies it to every type in a term. You can, for example,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   665
  change every @{typ nat} in a term into an @{typ int} using the function:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   666
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   667
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   668
ML %grayML\<open>fun nat_to_int ty =
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   669
  (case ty of
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   670
     @{typ nat} => @{typ int}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   671
   | Type (s, tys) => Type (s, map nat_to_int tys)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   672
   | _ => ty)\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   673
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   674
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   675
  Here is an example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   676
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   677
@{ML_matchresult_fake [display,gray] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   678
\<open>map_types nat_to_int @{term "a = (1::nat)"}\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   679
\<open>Const ("op =", "int \<Rightarrow> int \<Rightarrow> bool")
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   680
           $ Free ("a", "int") $ Const ("HOL.one_class.one", "int")\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   681
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   682
  If you want to obtain the list of free type-variables of a term, you
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   683
  can use the function @{ML_ind  add_tfrees in Term} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   684
  (similarly @{ML_ind  add_tvars in Term} for the schematic type-variables). 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   685
  One would expect that such functions
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   686
  take a term as input and return a list of types. But their type is actually 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   687
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   688
  @{text[display] "Term.term -> (string * Term.sort) list -> (string * Term.sort) list"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   689
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   690
  that is they take, besides a term, also a list of type-variables as input. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   691
  So in order to obtain the list of type-variables of a term you have to 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   692
  call them as follows
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   693
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   694
  @{ML_matchresult [gray,display]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   695
  \<open>Term.add_tfrees @{term "(a, b)"} []\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   696
  \<open>[("'b", ["HOL.type"]), ("'a", ["HOL.type"])]\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   697
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   698
  The reason for this definition is that @{ML add_tfrees in Term} can
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   699
  be easily folded over a list of terms. Similarly for all functions
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   700
  named \<open>add_*\<close> in @{ML_file "Pure/term.ML"}.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   701
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   702
  \begin{exercise}\label{fun:revsum}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   703
  Write a function \<open>rev_sum : term -> term\<close> that takes a
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   704
  term of the form \<open>t\<^sub>1 + t\<^sub>2 + \<dots> + t\<^sub>n\<close> (whereby \<open>n\<close> might be one)
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   705
  and returns the reversed sum \<open>t\<^sub>n + \<dots> + t\<^sub>2 + t\<^sub>1\<close>. Assume
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   706
  the \<open>t\<^sub>i\<close> can be arbitrary expressions and also note that \<open>+\<close> 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   707
  associates to the left. Try your function on some examples. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   708
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   709
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   710
  \begin{exercise}\label{fun:makesum}
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   711
  Write a function that takes two terms representing natural numbers
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   712
  in unary notation (like @{term "Suc (Suc (Suc 0))"}), and produces the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   713
  number representing their sum.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   714
  \end{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   715
469
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   716
  \begin{exercise}\label{fun:killqnt}
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   717
  Write a function that removes trivial forall and exists quantifiers that do not
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   718
  quantify over any variables.  For example the term @{term "\<forall>x y z. P x = P
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   719
  z"} should be transformed to @{term "\<forall>x z. P x = P z"}, deleting the
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   720
  quantification @{term "y"}. Hint: use the functions @{ML incr_boundvars}
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   721
  and @{ML loose_bvar1}.
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   722
  \end{exercise}
7a558c5119b2 added an excercise originally by Jasmin Blanchette
Christian Urban <urbanc@in.tum.de>
parents: 465
diff changeset
   723
446
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   724
  \begin{exercise}\label{fun:makelist}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   725
  Write a function that takes an integer \<open>i\<close> and
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   726
  produces an Isabelle integer list from \<open>1\<close> upto \<open>i\<close>, 
446
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   727
  and then builds the reverse of this list using @{const rev}. 
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   728
  The relevant helper functions are @{ML upto}, 
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   729
  @{ML HOLogic.mk_number} and @{ML HOLogic.mk_list}.
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   730
  \end{exercise}
4c32349b9875 added an example to be used for conversions later on
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   731
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   732
  \begin{exercise}\label{ex:debruijn}
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   733
  Implement the function, which we below name deBruijn, that depends on a natural
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   734
  number n$>$0 and constructs terms of the form:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   735
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   736
  \begin{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   737
  \begin{tabular}{r@ {\hspace{2mm}}c@ {\hspace{2mm}}l}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   738
  {\it rhs n} & $\dn$ & {\large$\bigwedge$}{\it i=1\ldots n.  P\,i}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   739
  {\it lhs n} & $\dn$ & {\large$\bigwedge$}{\it i=1\ldots n. P\,i = P (i + 1 mod n)}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   740
                        $\longrightarrow$ {\it rhs n}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   741
  {\it deBruijn n} & $\dn$ & {\it lhs n} $\longrightarrow$ {\it rhs n}\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   742
  \end{tabular}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   743
  \end{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   744
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
   745
  This function returns for n=3 the term
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   746
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   747
  \begin{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   748
  \begin{tabular}{l}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   749
  (P 1 = P 2 $\longrightarrow$ P 1 $\wedge$ P 2 $\wedge$ P 3) $\wedge$\\
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   750
  (P 2 = P 3 $\longrightarrow$ P 1 $\wedge$ P 2 $\wedge$ P 3) $\wedge$\\ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   751
  (P 3 = P 1 $\longrightarrow$ P 1 $\wedge$ P 2 $\wedge$ P 3) $\longrightarrow$ P 1 $\wedge$ P 2 $\wedge$ P 3
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   752
  \end{tabular}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   753
  \end{center}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   754
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   755
  Make sure you use the functions defined in @{ML_file "HOL/Tools/hologic.ML"}
350
364fffa80794 polished
Christian Urban <urbanc@in.tum.de>
parents: 349
diff changeset
   756
  for constructing the terms for the logical connectives.\footnote{Thanks to Roy
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
   757
  Dyckhoff for suggesting this exercise and working out the details.} 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   758
  \end{exercise}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   759
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   760
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   761
section \<open>Unification and Matching\label{sec:univ}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   762
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   763
text \<open>
386
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   764
  As seen earlier, Isabelle's terms and types may contain schematic term variables
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   765
  (term-constructor @{ML Var}) and schematic type variables (term-constructor
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   766
  @{ML TVar}). These variables stand for unknown entities, which can be made
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   767
  more concrete by instantiations. Such instantiations might be a result of
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   768
  unification or matching. While in case of types, unification and matching is
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   769
  relatively straightforward, in case of terms the algorithms are
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   770
  substantially more complicated, because terms need higher-order versions of
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   771
  the unification and matching algorithms. Below we shall use the
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   772
  antiquotations \<open>@{typ_pat \<dots>}\<close> and \<open>@{term_pat \<dots>}\<close> from
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   773
  Section~\ref{sec:antiquote} in order to construct examples involving
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   774
  schematic variables.
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   775
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
   776
  Let us begin with describing the unification and matching functions for
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   777
  types.  Both return type environments (ML-type @{ML_type "Type.tyenv"})
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   778
  which map schematic type variables to types and sorts. Below we use the
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   779
  function @{ML_ind typ_unify in Sign} from the structure @{ML_structure Sign}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   780
  for unifying the types \<open>?'a * ?'b\<close> and \<open>?'b list *
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   781
  nat\<close>. This will produce the mapping, or type environment, \<open>[?'a :=
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   782
  ?'b list, ?'b := nat]\<close>.
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   783
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   784
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   785
ML %linenosgray\<open>val (tyenv_unif, _) = let
379
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
   786
  val ty1 = @{typ_pat "?'a * ?'b"}
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
   787
  val ty2 = @{typ_pat "?'b list * nat"}
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   788
in
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   789
  Sign.typ_unify @{theory} (ty1, ty2) (Vartab.empty, 0) 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   790
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   791
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   792
text \<open>
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   793
  The environment @{ML_ind "Vartab.empty"} in line 5 stands for the empty type
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   794
  environment, which is needed for starting the unification without any
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   795
  (pre)instantiations. The \<open>0\<close> is an integer index that will be explained
559
ffa5c4ec9611 improvements by Piotr Trojanek
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 557
diff changeset
   796
  below. In case of failure, @{ML typ_unify in Sign} will raise the exception
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   797
  \<open>TUNIFY\<close>.  We can print out the resulting type environment bound to 
386
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   798
  @{ML tyenv_unif} with the built-in function @{ML_ind dest in Vartab} from the
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   799
  structure @{ML_structure Vartab}.
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   800
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   801
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   802
  \<open>Vartab.dest tyenv_unif\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   803
  \<open>[(("'a", 0), (["HOL.type"], "?'b List.list")), 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   804
 (("'b", 0), (["HOL.type"], "nat"))]\<close>} 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   805
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   806
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   807
text_raw \<open>
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   808
  \begin{figure}[t]
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   809
  \begin{minipage}{\textwidth}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   810
  \begin{isabelle}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   811
ML %grayML\<open>fun pretty_helper aux env =
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   812
  env |> Vartab.dest  
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
   813
      |> map aux
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
   814
      |> map (fn (s1, s2) => Pretty.block [s1, Pretty.str " := ", s2]) 
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
   815
      |> Pretty.enum "," "[" "]" 
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
   816
      |> pwriteln
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   817
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   818
fun pretty_tyenv ctxt tyenv =
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   819
let
389
Christian Urban <urbanc@in.tum.de>
parents: 388
diff changeset
   820
  fun get_typs (v, (s, T)) = (TVar (v, s), T)
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
   821
  val print = apply2 (pretty_typ ctxt)
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   822
in 
389
Christian Urban <urbanc@in.tum.de>
parents: 388
diff changeset
   823
  pretty_helper (print o get_typs) tyenv
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   824
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   825
text_raw \<open>
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   826
  \end{isabelle}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   827
  \end{minipage}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   828
  \caption{A pretty printing function for type environments, which are 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   829
  produced by unification and matching.\label{fig:prettyenv}}
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   830
  \end{figure}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   831
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   832
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   833
text \<open>
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   834
  The first components in this list stand for the schematic type variables and
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   835
  the second are the associated sorts and types. In this example the sort is
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   836
  the default sort \<open>HOL.type\<close>. Instead of @{ML \<open>Vartab.dest\<close>}, we will
386
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   837
  use in what follows our own pretty-printing function from
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   838
  Figure~\ref{fig:prettyenv} for @{ML_type "Type.tyenv"}s. For the type
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   839
  environment in the example this function prints out the more legible:
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   840
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   841
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   842
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   843
  \<open>pretty_tyenv @{context} tyenv_unif\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   844
  \<open>[?'a := ?'b list, ?'b := nat]\<close>}
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   845
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   846
  The way the unification function @{ML typ_unify in Sign} is implemented 
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   847
  using an initial type environment and initial index makes it easy to
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   848
  unify more than two terms. For example 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   849
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   850
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   851
ML %linenosgray\<open>val (tyenvs, _) = let
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   852
  val tys1 = (@{typ_pat "?'a"}, @{typ_pat "?'b list"})
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   853
  val tys2 = (@{typ_pat "?'b"}, @{typ_pat "nat"})
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   854
in
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   855
  fold (Sign.typ_unify @{theory}) [tys1, tys2] (Vartab.empty, 0) 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   856
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   857
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   858
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   859
  The index \<open>0\<close> in Line 5 is the maximal index of the schematic type
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   860
  variables occurring in \<open>tys1\<close> and \<open>tys2\<close>. This index will be
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   861
  increased whenever a new schematic type variable is introduced during
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   862
  unification.  This is for example the case when two schematic type variables
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   863
  have different, incomparable sorts. Then a new schematic type variable is
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   864
  introduced with the combined sorts. To show this let us assume two sorts,
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   865
  say \<open>s1\<close> and \<open>s2\<close>, which we attach to the schematic type
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   866
  variables \<open>?'a\<close> and \<open>?'b\<close>. Since we do not make any
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   867
  assumption about the sorts, they are incomparable.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   868
\<close>
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   869
418
1d1e4cda8c54 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
   870
class s1
1d1e4cda8c54 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
   871
class s2 
1d1e4cda8c54 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
   872
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   873
ML %grayML\<open>val (tyenv, index) = let
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   874
  val ty1 = @{typ_pat "?'a::s1"}
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   875
  val ty2 = @{typ_pat "?'b::s2"}
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   876
in
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   877
  Sign.typ_unify @{theory} (ty1, ty2) (Vartab.empty, 0) 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   878
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   879
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   880
text \<open>
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   881
  To print out the result type environment we switch on the printing 
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   882
  of sort information by setting @{ML_ind show_sorts in Syntax} to 
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   883
  true. This allows us to inspect the typing environment.
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   884
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   885
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   886
  \<open>pretty_tyenv @{context} tyenv\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   887
  \<open>[?'a::s1 := ?'a1::{s1, s2}, ?'b::s2 := ?'a1::{s1, s2}]\<close>}
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   888
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   889
  As can be seen, the type variables \<open>?'a\<close> and \<open>?'b\<close> are instantiated
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   890
  with a new type variable \<open>?'a1\<close> with sort \<open>{s1, s2}\<close>. Since a new
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   891
  type variable has been introduced the @{ML index}, originally being \<open>0\<close>, 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   892
  has been increased to \<open>1\<close>.
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   893
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   894
  @{ML_matchresult [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   895
  \<open>index\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   896
  \<open>1\<close>}
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   897
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   898
  Let us now return to the unification problem \<open>?'a * ?'b\<close> and 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   899
  \<open>?'b list * nat\<close> from the beginning of this section, and the 
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   900
  calculated type environment @{ML tyenv_unif}:
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   901
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   902
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   903
  \<open>pretty_tyenv @{context} tyenv_unif\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   904
  \<open>[?'a := ?'b list, ?'b := nat]\<close>}
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   905
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   906
  Observe that the type environment which the function @{ML typ_unify in
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   907
  Sign} returns is \emph{not} an instantiation in fully solved form: while \<open>?'b\<close> is instantiated to @{typ nat}, this is not propagated to the
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   908
  instantiation for \<open>?'a\<close>.  In unification theory, this is often
386
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   909
  called an instantiation in \emph{triangular form}. These triangular 
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   910
  instantiations, or triangular type environments, are used because of 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   911
  performance reasons. To apply such a type environment to a type, say \<open>?'a *
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   912
  ?'b\<close>, you should use the function @{ML_ind norm_type in Envir}:
377
272ba2cceeb2 added a section about unification and matching
Christian Urban <urbanc@in.tum.de>
parents: 375
diff changeset
   913
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   914
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   915
  \<open>Envir.norm_type tyenv_unif @{typ_pat "?'a * ?'b"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   916
  \<open>nat list * nat\<close>}
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   917
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   918
  Matching of types can be done with the function @{ML_ind typ_match in Sign}
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   919
  also from the structure @{ML_structure Sign}. This function returns a @{ML_type
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   920
  Type.tyenv} as well, but might raise the exception \<open>TYPE_MATCH\<close> in case
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   921
  of failure. For example
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   922
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   923
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   924
ML %grayML\<open>val tyenv_match = let
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   925
  val pat = @{typ_pat "?'a * ?'b"}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   926
  and ty  = @{typ_pat "bool list * nat"}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   927
in
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   928
  Sign.typ_match @{theory} (pat, ty) Vartab.empty 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   929
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   930
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   931
text \<open>
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   932
  Printing out the calculated matcher gives
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   933
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   934
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   935
  \<open>pretty_tyenv @{context} tyenv_match\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   936
  \<open>[?'a := bool list, ?'b := nat]\<close>}
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   937
  
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   938
  Unlike unification, which uses the function @{ML norm_type in Envir}, 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   939
  applying the matcher to a type needs to be done with the function
386
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   940
  @{ML_ind subst_type in Envir}. For example
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   941
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   942
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   943
  \<open>Envir.subst_type tyenv_match @{typ_pat "?'a * ?'b"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   944
  \<open>bool list * nat\<close>}
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   945
399
Christian Urban <urbanc@in.tum.de>
parents: 398
diff changeset
   946
  Be careful to observe the difference: always use
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   947
  @{ML subst_type in Envir} for matchers and @{ML norm_type in Envir} 
386
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   948
  for unifiers. To show the difference, let us calculate the 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   949
  following matcher:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   950
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   951
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   952
ML %grayML\<open>val tyenv_match' = let
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   953
  val pat = @{typ_pat "?'a * ?'b"}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   954
  and ty  = @{typ_pat "?'b list * nat"}
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   955
in
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   956
  Sign.typ_match @{theory} (pat, ty) Vartab.empty 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   957
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   958
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   959
text \<open>
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   960
  Now @{ML tyenv_unif} is equal to @{ML tyenv_match'}. If we apply 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   961
  @{ML norm_type in Envir} to the type \<open>?'a * ?'b\<close> we obtain
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   962
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   963
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   964
  \<open>Envir.norm_type tyenv_match' @{typ_pat "?'a * ?'b"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   965
  \<open>nat list * nat\<close>}
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   966
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   967
  which does not solve the matching problem, and if
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   968
  we apply @{ML subst_type in Envir} to the same type we obtain
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   969
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
   970
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   971
  \<open>Envir.subst_type tyenv_unif @{typ_pat "?'a * ?'b"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
   972
  \<open>?'b list * nat\<close>}
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   973
  
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   974
  which does not solve the unification problem.
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   975
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   976
  \begin{readmore}
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   977
  Unification and matching for types is implemented
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   978
  in @{ML_file "Pure/type.ML"}. The ``interface'' functions for them
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   979
  are in @{ML_file "Pure/sign.ML"}. Matching and unification produce type environments
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   980
  as results. These are implemented in @{ML_file "Pure/envir.ML"}.
379
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
   981
  This file also includes the substitution and normalisation functions,
386
Christian Urban <urbanc@in.tum.de>
parents: 385
diff changeset
   982
  which apply a type environment to a type. Type environments are lookup 
379
Christian Urban <urbanc@in.tum.de>
parents: 378
diff changeset
   983
  tables which are implemented in @{ML_file "Pure/term_ord.ML"}.
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
   984
  \end{readmore}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   985
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   986
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   987
text \<open>
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   988
  Unification and matching of terms is substantially more complicated than the
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   989
  type-case. The reason is that terms have abstractions and, in this context,  
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   990
  unification or matching modulo plain equality is often not meaningful. 
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
   991
  Nevertheless, Isabelle implements the function @{ML_ind
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   992
  first_order_match in Pattern} for terms.  This matching function returns a
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   993
  type environment and a term environment.  To pretty print the latter we use
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   994
  the function \<open>pretty_env\<close>:
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   995
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   996
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
   997
ML %grayML\<open>fun pretty_env ctxt env =
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
   998
let
389
Christian Urban <urbanc@in.tum.de>
parents: 388
diff changeset
   999
  fun get_trms (v, (T, t)) = (Var (v, T), t) 
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1000
  val print = apply2 (pretty_term ctxt)
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1001
in
389
Christian Urban <urbanc@in.tum.de>
parents: 388
diff changeset
  1002
  pretty_helper (print o get_trms) env 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1003
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1004
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1005
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1006
  As can be seen from the \<open>get_trms\<close>-function, a term environment associates 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1007
  a schematic term variable with a type and a term.  An example of a first-order 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1008
  matching problem is the term @{term "P (\<lambda>a b. Q b a)"} and the pattern 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1009
  \<open>?X ?Y\<close>.
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1010
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1011
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1012
ML %grayML\<open>val (_, fo_env) = let
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1013
  val fo_pat = @{term_pat "(?X::(nat\<Rightarrow>nat\<Rightarrow>nat)\<Rightarrow>bool) ?Y"}
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1014
  val trm_a = @{term "P::(nat\<Rightarrow>nat\<Rightarrow>nat)\<Rightarrow>bool"} 
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1015
  val trm_b = @{term "\<lambda>a b. (Q::nat\<Rightarrow>nat\<Rightarrow>nat) b a"}
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1016
  val init = (Vartab.empty, Vartab.empty) 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1017
in
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1018
  Pattern.first_order_match @{theory} (fo_pat, trm_a $ trm_b) init
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1019
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1020
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1021
text \<open>
399
Christian Urban <urbanc@in.tum.de>
parents: 398
diff changeset
  1022
  In this example we annotated types explicitly because then 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1023
  the type environment is empty and can be ignored. The 
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
  1024
  resulting term environment is
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1025
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1026
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1027
  \<open>pretty_env @{context} fo_env\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1028
  \<open>[?X := P, ?Y := \<lambda>a b. Q b a]\<close>}
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1029
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1030
  The matcher can be applied to a term using the function @{ML_ind subst_term
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1031
  in Envir} (remember the same convention for types applies to terms: @{ML
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
  1032
  subst_term in Envir} is for matchers and @{ML norm_term in Envir} for
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
  1033
  unifiers). The function @{ML subst_term in Envir} expects a type environment,
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1034
  which is set to empty in the example below, and a term environment.
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1035
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1036
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1037
  \<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1038
  val trm = @{term_pat "(?X::(nat\<Rightarrow>nat\<Rightarrow>nat)\<Rightarrow>bool) ?Y"}
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1039
in
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1040
  Envir.subst_term (Vartab.empty, fo_env) trm
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
  1041
  |> pretty_term @{context}
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
  1042
  |> pwriteln
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1043
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1044
  \<open>P (\<lambda>a b. Q b a)\<close>}
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1045
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1046
  First-order matching is useful for matching against applications and
399
Christian Urban <urbanc@in.tum.de>
parents: 398
diff changeset
  1047
  variables. It can also deal with abstractions and a limited form of
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1048
  alpha-equivalence, but this kind of matching should be used with care, since
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
  1049
  it is not clear whether the result is meaningful. A meaningful example is
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1050
  matching \<open>\<lambda>x. P x\<close> against the pattern \<open>\<lambda>y. ?X y\<close>. In this
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1051
  case, first-order matching produces \<open>[?X := P]\<close>.
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1052
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1053
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1054
  \<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1055
  val fo_pat = @{term_pat "\<lambda>y. (?X::nat\<Rightarrow>bool) y"}
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1056
  val trm = @{term "\<lambda>x. (P::nat\<Rightarrow>bool) x"} 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1057
  val init = (Vartab.empty, Vartab.empty) 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1058
in
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1059
  Pattern.first_order_match @{theory} (fo_pat, trm) init
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1060
  |> snd 
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1061
  |> pretty_env @{context} 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1062
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1063
  \<open>[?X := P]\<close>}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1064
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1065
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1066
text \<open>
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1067
  Unification of abstractions is more thoroughly studied in the context of
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1068
  higher-order pattern unification and higher-order pattern matching.  A
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1069
  \emph{\index*{pattern}} is a well-formed term in which the arguments to
429
d04d1cd0e058 corrected def of ho-pat-unif, some extra hints about patterns
schropp <schropp@in.tum.de>
parents: 423
diff changeset
  1070
  every schematic variable are distinct bounds.
d04d1cd0e058 corrected def of ho-pat-unif, some extra hints about patterns
schropp <schropp@in.tum.de>
parents: 423
diff changeset
  1071
  In particular this excludes terms where a
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1072
  schematic variable is an argument of another one and where a schematic
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1073
  variable is applied twice with the same bound variable. The function
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1074
  @{ML_ind pattern in Pattern} in the structure @{ML_structure Pattern} tests
429
d04d1cd0e058 corrected def of ho-pat-unif, some extra hints about patterns
schropp <schropp@in.tum.de>
parents: 423
diff changeset
  1075
  whether a term satisfies these restrictions under the assumptions
d04d1cd0e058 corrected def of ho-pat-unif, some extra hints about patterns
schropp <schropp@in.tum.de>
parents: 423
diff changeset
  1076
  that it is beta-normal, well-typed and has no loose bound variables.
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1077
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1078
  @{ML_matchresult [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1079
  \<open>let
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1080
  val trm_list = 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1081
        [@{term_pat "?X"}, @{term_pat "a"},
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1082
         @{term_pat "f (\<lambda>a b. ?X a b) c"},
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1083
         @{term_pat "\<lambda>a b. (+) a b"},
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1084
         @{term_pat "\<lambda>a. (+) a ?Y"}, @{term_pat "?X ?Y"},
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1085
         @{term_pat "\<lambda>a b. ?X a b ?Y"}, @{term_pat "\<lambda>a. ?X a a"},
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1086
         @{term_pat "?X a"}] 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1087
in
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1088
  map Pattern.pattern trm_list
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1089
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1090
  \<open>[true, true, true, true, true, false, false, false, false]\<close>}
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1091
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
  1092
  The point of the restriction to patterns is that unification and matching 
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
  1093
  are decidable and produce most general unifiers, respectively matchers. 
429
d04d1cd0e058 corrected def of ho-pat-unif, some extra hints about patterns
schropp <schropp@in.tum.de>
parents: 423
diff changeset
  1094
  Note that \emph{both} terms to be unified have to be higher-order patterns
d04d1cd0e058 corrected def of ho-pat-unif, some extra hints about patterns
schropp <schropp@in.tum.de>
parents: 423
diff changeset
  1095
  for this to work. The exception @{ML_ind Pattern in Pattern} indicates failure
d04d1cd0e058 corrected def of ho-pat-unif, some extra hints about patterns
schropp <schropp@in.tum.de>
parents: 423
diff changeset
  1096
  in this regard.
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
  1097
  In this way, matching and unification can be implemented as functions that 
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
  1098
  produce a type and term environment (unification actually returns a 
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1099
  record of type @{ML_type Envir.env} containing a max-index, a type environment 
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1100
  and a term environment). The corresponding functions are @{ML_ind match in Pattern}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1101
  and @{ML_ind unify in Pattern}, both implemented in the structure
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1102
  @{ML_structure Pattern}. An example for higher-order pattern unification is
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1103
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1104
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1105
  \<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1106
  val trm1 = @{term_pat "\<lambda>x y. g (?X y x) (f (?Y x))"}
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1107
  val trm2 = @{term_pat "\<lambda>u v. g u (f u)"}
384
Christian Urban <urbanc@in.tum.de>
parents: 383
diff changeset
  1108
  val init = Envir.empty 0
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1109
  val env = Pattern.unify (Context.Proof @{context}) (trm1, trm2) init (* FIXME: Reference to generic context *)
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
  1110
in
384
Christian Urban <urbanc@in.tum.de>
parents: 383
diff changeset
  1111
  pretty_env @{context} (Envir.term_env env)
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1112
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1113
  \<open>[?X := \<lambda>y x. x, ?Y := \<lambda>x. x]\<close>}
384
Christian Urban <urbanc@in.tum.de>
parents: 383
diff changeset
  1114
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1115
  The function @{ML_ind "Envir.empty"} generates a record with a specified
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1116
  max-index for the schematic variables (in the example the index is \<open>0\<close>) and empty type and term environments. The function @{ML_ind
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1117
  "Envir.term_env"} pulls out the term environment from the result record. The
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1118
  corresponding function for type environment is @{ML_ind
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1119
  "Envir.type_env"}. An assumption of this function is that the terms to be
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1120
  unified have already the same type. In case of failure, the exceptions that
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1121
  are raised are either \<open>Pattern\<close>, \<open>MATCH\<close> or \<open>Unif\<close>.
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1122
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1123
  As mentioned before, unrestricted higher-order unification, respectively
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1124
  unrestricted higher-order matching, is in general undecidable and might also
559
ffa5c4ec9611 improvements by Piotr Trojanek
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 557
diff changeset
  1125
  not possess a single most general solution. Therefore Isabelle implements the
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1126
  unification function @{ML_ind unifiers in Unify} so that it returns a lazy
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1127
  list of potentially infinite unifiers.  An example is as follows
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1128
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1129
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1130
ML %grayML\<open>val uni_seq =
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1131
let 
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1132
  val trm1 = @{term_pat "?X ?Y"}
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1133
  val trm2 = @{term "f a"}
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1134
  val init = Envir.empty 0
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1135
in
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1136
  Unify.unifiers (Context.Proof @{context}, init, [(trm1, trm2)])
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1137
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1138
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1139
text \<open>
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1140
  The unifiers can be extracted from the lazy sequence using the 
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1141
  function @{ML_ind "Seq.pull"}. In the example we obtain three 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1142
  unifiers \<open>un1\<dots>un3\<close>.
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1143
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1144
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1145
ML %grayML\<open>val SOME ((un1, _), next1) = Seq.pull uni_seq;
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1146
val SOME ((un2, _), next2) = Seq.pull next1;
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1147
val SOME ((un3, _), next3) = Seq.pull next2;
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1148
val NONE = Seq.pull next3\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1149
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1150
text \<open>
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1151
  \footnote{\bf FIXME: what is the list of term pairs in the unifier: flex-flex pairs?}
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1152
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1153
  We can print them out as follows.
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1154
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1155
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1156
  \<open>pretty_env @{context} (Envir.term_env un1);
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1157
pretty_env @{context} (Envir.term_env un2);
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1158
pretty_env @{context} (Envir.term_env un3)\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1159
  \<open>[?X := \<lambda>a. a, ?Y := f a]
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1160
[?X := f, ?Y := a]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1161
[?X := \<lambda>b. f a]\<close>}
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1162
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1163
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1164
  In case of failure the function @{ML_ind unifiers in Unify} does not raise
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1165
  an exception, rather returns the empty sequence. For example
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1166
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1167
  @{ML_matchresult [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1168
\<open>let 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1169
  val trm1 = @{term "a"}
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1170
  val trm2 = @{term "b"}
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1171
  val init = Envir.empty 0
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1172
in
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1173
  Unify.unifiers (Context.Proof @{context}, init, [(trm1, trm2)])
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1174
  |> Seq.pull
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1175
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1176
\<open>NONE\<close>}
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1177
408
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
  1178
  In order to find a reasonable solution for a unification problem, Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
  1179
  also tries first to solve the problem by higher-order pattern
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
  1180
  unification. Only in case of failure full higher-order unification is
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
  1181
  called. This function has a built-in bound, which can be accessed and
451
fc074e669f9f disabled foobar_prove; updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
  1182
  manipulated as a configuration value. For example
408
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
  1183
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1184
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1185
  \<open>Config.get_global @{theory} (Unify.search_bound)\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1186
  \<open>Int 60\<close>}
408
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
  1187
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
  1188
  If this bound is reached during unification, Isabelle prints out the
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
  1189
  warning message @{text [quotes] "Unification bound exceeded"} and
409
Christian Urban <urbanc@in.tum.de>
parents: 408
diff changeset
  1190
  plenty of diagnostic information (sometimes annoyingly plenty of 
Christian Urban <urbanc@in.tum.de>
parents: 408
diff changeset
  1191
  information). 
408
Christian Urban <urbanc@in.tum.de>
parents: 405
diff changeset
  1192
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
  1193
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1194
  For higher-order matching the function is called @{ML_ind matchers in Unify}
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1195
  implemented in the structure @{ML_structure Unify}. Also this function returns
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1196
  sequences with possibly more than one matcher.  Like @{ML unifiers in
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1197
  Unify}, this function does not raise an exception in case of failure, but
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1198
  returns an empty sequence. It also first tries out whether the matching
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1199
  problem can be solved by first-order matching.
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1200
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1201
  Higher-order matching might be necessary for instantiating a theorem
559
ffa5c4ec9611 improvements by Piotr Trojanek
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 557
diff changeset
  1202
  appropriately. More on this will be given in Section~\ref{sec:theorems}. 
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1203
  Here we only have a look at a simple case, namely the theorem 
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1204
  @{thm [source] spec}:
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1205
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1206
  \begin{isabelle}
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1207
  \isacommand{thm}~@{thm [source] spec}\\
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1208
  \<open>> \<close>~@{thm spec}
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1209
  \end{isabelle}
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1210
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1211
  as an introduction rule. Applying it directly can lead to unexpected
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 412
diff changeset
  1212
  behaviour since the unification has more than one solution. One way round
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1213
  this problem is to instantiate the schematic variables \<open>?P\<close> and
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1214
  \<open>?x\<close>.  Instantiation function for theorems is 
465
886a7c614ced updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 458
diff changeset
  1215
  @{ML_ind instantiate_normalize in Drule} from the structure 
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1216
  @{ML_structure Drule}. One problem, however, is
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1217
  that this function expects the instantiations as lists of @{ML_type "((indexname * sort) * ctyp)"}
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1218
  respective @{ML_type "(indexname * typ) * cterm"}:
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1219
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1220
  \begin{isabelle}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1221
  @{ML instantiate_normalize in Drule}\<open>:\<close>
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1222
  @{ML_type "((indexname * sort) * ctyp) list * ((indexname * typ) * cterm) list -> thm -> thm"}
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1223
  \end{isabelle}
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1224
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1225
  This means we have to transform the environment the higher-order matching 
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1226
  function returns into such an instantiation. For this we use the functions
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1227
  @{ML_ind term_env in Envir} and @{ML_ind type_env in Envir}, which extract
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1228
  from an environment the corresponding variable mappings for schematic type 
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1229
  and term variables. These mappings can be turned into proper 
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1230
  @{ML_type ctyp}-pairs with the function
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1231
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1232
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1233
ML %grayML\<open>fun prep_trm ctxt (x, (T, t)) = 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1234
  ((x, T), Thm.cterm_of ctxt t)\<close> 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1235
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1236
text \<open>
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1237
  and into proper @{ML_type cterm}-pairs with
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1238
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1239
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1240
ML %grayML\<open>fun prep_ty ctxt (x, (S, ty)) = 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1241
  ((x, S), Thm.ctyp_of ctxt ty)\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1242
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1243
text \<open>
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1244
  We can now calculate the instantiations from the matching function. 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1245
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1246
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1247
ML %linenosgray\<open>fun matcher_inst ctxt pat trm i = 
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1248
let
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1249
  val univ = Unify.matchers (Context.Proof ctxt) [(pat, trm)] 
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1250
  val env = nth (Seq.list_of univ) i
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1251
  val tenv = Vartab.dest (Envir.term_env env)
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1252
  val tyenv = Vartab.dest (Envir.type_env env)
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1253
in
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1254
  (map (prep_ty ctxt) tyenv, map (prep_trm ctxt) tenv)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1255
end\<close>
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1256
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1257
ML \<open>Context.get_generic_context\<close>
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1258
text \<open>
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1259
  In Line 3 we obtain the higher-order matcher. We assume there is a finite
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1260
  number of them and select the one we are interested in via the parameter 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1261
  \<open>i\<close> in the next line. In Lines 5 and 6 we destruct the resulting 
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1262
  environments using the function @{ML_ind dest in Vartab}. Finally, we need 
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1263
  to map the functions @{ML prep_trm} and @{ML prep_ty} over the respective 
415
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1264
  environments (Line 8). As a simple example we instantiate the
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1265
  @{thm [source] spec} rule so that its conclusion is of the form 
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1266
  @{term "Q True"}. 
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1267
 
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1268
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1269
  @{ML_matchresult_fake [gray,display,linenos] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1270
  \<open>let  
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1271
  val pat = Logic.strip_imp_concl (Thm.prop_of @{thm spec})
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1272
  val trm = @{term "Trueprop (Q True)"}
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1273
  val inst = matcher_inst @{context} pat trm 1
403
444bc9f17cfc added something about unifiacation and instantiations
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
  1274
in
465
886a7c614ced updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 458
diff changeset
  1275
  Drule.instantiate_normalize inst @{thm spec}
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1276
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1277
  \<open>\<forall>x. Q x \<Longrightarrow> Q True\<close>}
415
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1278
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1279
  Note that we had to insert a \<open>Trueprop\<close>-coercion in Line 3 since the 
415
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1280
  conclusion of @{thm [source] spec} contains one.
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1281
 
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
  1282
  \begin{readmore}
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
  1283
  Unification and matching of higher-order patterns is implemented in 
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1284
  @{ML_file "Pure/pattern.ML"}. This file also contains a first-order matcher
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
  1285
  for terms. Full higher-order unification is implemented
383
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
  1286
  in @{ML_file "Pure/unify.ML"}. It uses lazy sequences which are implemented
Christian Urban <urbanc@in.tum.de>
parents: 382
diff changeset
  1287
  in @{ML_file "Pure/General/seq.ML"}.
378
8d160d79b48c section about matching and unification of types
Christian Urban <urbanc@in.tum.de>
parents: 377
diff changeset
  1288
  \end{readmore}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1289
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1290
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1291
section \<open>Sorts (TBD)\label{sec:sorts}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1292
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1293
text \<open>
432
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1294
  Type classes are formal names in the type system which are linked to
433
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1295
  predicates of one type variable (via the axclass mechanism) and thereby
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1296
  express extra properties on types, to be propagated by the type system.
432
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1297
  The type-in-class judgement is defined
433
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1298
  via a simple logic over types, with inferences solely based on
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1299
  modus ponens, instantiation and axiom use.
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1300
  The declared axioms of this logic are called an order-sorted algebra (see Schmidt-Schauss).
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1301
  It consists of an acyclic subclass relation and a set of image containment
432
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1302
  declarations for type constructors, called arities.
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1303
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1304
  A well-behaved high-level view on type classes has long been established
433
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1305
  (cite Haftmann-Wenzel): the predicate behind a type class is the foundation
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1306
  of a locale (for context-management reasons)
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1307
  and may use so-called type class parameters. These are type-indexed constants
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1308
  dependent on the sole type variable and are implemented via overloading.
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1309
  Overloading a constant means specifying its value on a type based on
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1310
  a well-founded reduction towards other values of constants on types.
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1311
  When instantiating type classes
559
ffa5c4ec9611 improvements by Piotr Trojanek
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 557
diff changeset
  1312
  (i.e.\ proving arities) you are specifying overloading via primitive recursion.
432
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1313
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1314
  Sorts are finite intersections of type classes and are implemented as lists
559
ffa5c4ec9611 improvements by Piotr Trojanek
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 557
diff changeset
  1315
  of type class names. The empty intersection, i.e.\ the empty list, is therefore
432
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1316
  inhabited by all types and is called the topsort.
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1317
433
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1318
  Free and schematic type variables are always annotated with sorts, thereby restricting
432
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1319
  the domain of types they quantify over and corresponding to an implicit hypothesis
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1320
  about the type variable.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1321
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1322
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1323
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1324
ML \<open>Sign.classes_of @{theory}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1325
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1326
ML \<open>Sign.of_sort @{theory}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1327
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1328
text \<open>
398
7f7080ce7c2b started something about sorts
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
  1329
  \begin{readmore}
7f7080ce7c2b started something about sorts
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
  1330
  Classes, sorts and arities are defined in @{ML_file "Pure/term.ML"}.
7f7080ce7c2b started something about sorts
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
  1331
  
432
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1332
  @{ML_file "Pure/sorts.ML"} contains comparison and normalization functionality for sorts,
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1333
    manages the order sorted algebra and offers an interface for reinterpreting
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1334
    derivations of type in class judgements
433
schropp <schropp@in.tum.de>
parents: 432
diff changeset
  1335
  @{ML_file "Pure/defs.ML"} manages the constant dependency graph and keeps it well-founded
432
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1336
    (its define function doesn't terminate for complex non-well-founded dependencies)
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1337
  @{ML_file "Pure/axclass.ML"} manages the theorems that back up subclass and arity relations
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1338
    and provides basic infrastructure for establishing the high-level view on type classes
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1339
  @{ML_file "Pure/sign.ML"} is a common interface to all the type-theory-like declarations
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1340
    (especially names, constants, paths, type classes) a
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1341
    theory acquires by theory extension mechanisms and manages associated certification
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1342
    functionality.
087dc1726a99 some blabla about type classes, sorts, overloading, high level view on type classes
schropp <schropp@in.tum.de>
parents: 431
diff changeset
  1343
    It also provides the most needed functionality from individual underlying modules.
398
7f7080ce7c2b started something about sorts
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
  1344
  \end{readmore}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1345
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1346
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1347
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1348
section \<open>Type-Checking\label{sec:typechecking}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1349
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1350
text \<open>
559
ffa5c4ec9611 improvements by Piotr Trojanek
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 557
diff changeset
  1351
  Remember Isabelle follows the Church-style typing for terms, i.e.\ a term contains 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1352
  enough typing information (constants, free variables and abstractions all have typing 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1353
  information) so that it is always clear what the type of a term is. 
369
74ba778b09c9 tuned index
Christian Urban <urbanc@in.tum.de>
parents: 368
diff changeset
  1354
  Given a well-typed term, the function @{ML_ind type_of in Term} returns the 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1355
  type of a term. Consider for example:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1356
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1357
  @{ML_matchresult [display,gray] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1358
  \<open>type_of (@{term "f::nat \<Rightarrow> bool"} $ @{term "x::nat"})\<close> \<open>bool\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1359
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1360
  To calculate the type, this function traverses the whole term and will
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1361
  detect any typing inconsistency. For example changing the type of the variable 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1362
  @{term "x"} from @{typ "nat"} to @{typ "int"} will result in the error message: 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1363
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1364
  @{ML_matchresult_fake [display,gray] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1365
  \<open>type_of (@{term "f::nat \<Rightarrow> bool"} $ @{term "x::int"})\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1366
  \<open>*** Exception- TYPE ("type_of: type mismatch in application" \<dots>\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1367
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1368
  Since the complete traversal might sometimes be too costly and
369
74ba778b09c9 tuned index
Christian Urban <urbanc@in.tum.de>
parents: 368
diff changeset
  1369
  not necessary, there is the function @{ML_ind fastype_of in Term}, which 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1370
  also returns the type of a term.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1371
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1372
  @{ML_matchresult [display,gray] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1373
  \<open>fastype_of (@{term "f::nat \<Rightarrow> bool"} $ @{term "x::nat"})\<close> \<open>bool\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1374
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1375
  However, efficiency is gained on the expense of skipping some tests. You 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1376
  can see this in the following example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1377
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1378
   @{ML_matchresult [display,gray] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1379
  \<open>fastype_of (@{term "f::nat \<Rightarrow> bool"} $ @{term "x::int"})\<close> \<open>bool\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1380
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1381
  where no error is detected.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1382
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1383
  Sometimes it is a bit inconvenient to construct a term with 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1384
  complete typing annotations, especially in cases where the typing 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1385
  information is redundant. A short-cut is to use the ``place-holder'' 
345
4c54ef4dc84d polished
Christian Urban <urbanc@in.tum.de>
parents: 343
diff changeset
  1386
  type @{ML_ind dummyT in Term} and then let type-inference figure out the 
400
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
  1387
  complete type. The type inference can be invoked with the function
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
  1388
  @{ML_ind check_term in Syntax}. An example is as follows:
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1389
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1390
  @{ML_matchresult_fake [display,gray] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1391
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1392
  val c = Const (@{const_name "plus"}, dummyT) 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1393
  val o = @{term "1::nat"} 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1394
  val v = Free ("x", dummyT)
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1395
in   
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1396
  Syntax.check_term @{context} (c $ o $ v)
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1397
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1398
\<open>Const ("HOL.plus_class.plus", "nat \<Rightarrow> nat \<Rightarrow> nat") $
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1399
  Const ("HOL.one_class.one", "nat") $ Free ("x", "nat")\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1400
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1401
  Instead of giving explicitly the type for the constant \<open>plus\<close> and the free 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1402
  variable \<open>x\<close>, type-inference fills in the missing information.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1403
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1404
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1405
  See @{ML_file "Pure/Syntax/syntax.ML"} where more functions about reading,
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1406
  checking and pretty-printing of terms are defined. Functions related to
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1407
  type-inference are implemented in @{ML_file "Pure/type.ML"} and 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1408
  @{ML_file "Pure/type_infer.ML"}. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1409
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1410
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1411
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1412
  \begin{exercise}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1413
  Check that the function defined in Exercise~\ref{fun:revsum} returns a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1414
  result that type-checks. See what happens to the  solutions of this 
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1415
  exercise given in Appendix \ref{ch:solutions} when they receive an 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  1416
  ill-typed term as input.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1417
  \end{exercise}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1418
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1419
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1420
section \<open>Certified Terms and Certified Types\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1421
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1422
text \<open>
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1423
  You can freely construct and manipulate @{ML_type "term"}s and @{ML_type
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1424
  typ}es, since they are just arbitrary unchecked trees. However, you
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1425
  eventually want to see if a term is well-formed, or type-checks, relative to
369
74ba778b09c9 tuned index
Christian Urban <urbanc@in.tum.de>
parents: 368
diff changeset
  1426
  a theory.  Type-checking is done via the function @{ML_ind cterm_of in Thm}, which
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1427
  converts a @{ML_type term} into a @{ML_type cterm}, a \emph{certified}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1428
  term. Unlike @{ML_type term}s, which are just trees, @{ML_type "cterm"}s are
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1429
  abstract objects that are guaranteed to be type-correct, and they can only
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1430
  be constructed via ``official interfaces''.
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1431
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1432
  Certification is always relative to a context. For example you can 
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1433
  write:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1434
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1435
  @{ML_matchresult_fake [display,gray] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1436
  \<open>Thm.cterm_of @{context} @{term "(a::nat) + b = c"}\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1437
  \<open>a + b = c\<close>}
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1438
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1439
  This can also be written with an antiquotation:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1440
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1441
  @{ML_matchresult_fake [display,gray] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1442
  \<open>@{cterm "(a::nat) + b = c"}\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1443
  \<open>a + b = c\<close>}
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1444
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1445
  Attempting to obtain the certified term for
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1446
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1447
  @{ML_matchresult_fake_both [display,gray] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1448
  \<open>@{cterm "1 + True"}\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1449
  \<open>Type unification failed \<dots>\<close>}
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1450
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1451
  yields an error (since the term is not typable). A slightly more elaborate
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1452
  example that type-checks is:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1453
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1454
@{ML_matchresult_fake [display,gray] 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1455
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1456
  val natT = @{typ "nat"}
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1457
  val zero = @{term "0::nat"}
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  1458
  val plus = Const (@{const_name plus}, [natT, natT] ---> natT)
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1459
in
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1460
  Thm.cterm_of @{context} (plus $ zero $ zero)
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1461
end\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1462
  \<open>0 + 0\<close>}
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1463
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1464
  In Isabelle not just terms need to be certified, but also types. For example, 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1465
  you obtain the certified type for the Isabelle type @{typ "nat \<Rightarrow> bool"} on 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1466
  the ML-level as follows:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1467
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1468
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1469
  \<open>Thm.ctyp_of @{context} (@{typ nat} --> @{typ bool})\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1470
  \<open>nat \<Rightarrow> bool\<close>}
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1471
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1472
  or with the antiquotation:
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1473
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1474
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1475
  \<open>@{ctyp "nat \<Rightarrow> bool"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1476
  \<open>nat \<Rightarrow> bool\<close>}
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1477
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1478
  Since certified terms are, unlike terms, abstract objects, we cannot
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1479
  pattern-match against them. However, we can construct them. For example
513
f223f8223d4a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
  1480
  the function @{ML_ind apply in Thm} produces a certified application.
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1481
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1482
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1483
  \<open>Thm.apply @{cterm "P::nat \<Rightarrow> bool"} @{cterm "3::nat"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1484
  \<open>P 3\<close>}
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1485
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1486
  Similarly the function @{ML_ind list_comb in Drule} from the structure @{ML_structure Drule}
351
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 350
diff changeset
  1487
  applies a list of @{ML_type cterm}s.
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1488
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1489
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1490
  \<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1491
  val chead = @{cterm "P::unit \<Rightarrow> nat \<Rightarrow> bool"}
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1492
  val cargs = [@{cterm "()"}, @{cterm "3::nat"}]
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1493
in
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1494
  Drule.list_comb (chead, cargs)
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1495
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1496
  \<open>P () 3\<close>}
335
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1497
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1498
  \begin{readmore}
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1499
  For functions related to @{ML_type cterm}s and @{ML_type ctyp}s see 
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1500
  the files @{ML_file "Pure/thm.ML"}, @{ML_file "Pure/more_thm.ML"} and
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1501
  @{ML_file "Pure/drule.ML"}.
163ac0662211 reorganised the certified terms section; tuned
Christian Urban <urbanc@in.tum.de>
parents: 329
diff changeset
  1502
  \end{readmore}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1503
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1504
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1505
section \<open>Theorems\label{sec:theorems}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1506
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1507
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1508
  Just like @{ML_type cterm}s, theorems are abstract objects of type @{ML_type thm} 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1509
  that can only be built by going through interfaces. As a consequence, every proof 
388
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
  1510
  in Isabelle is correct by construction. This follows the tradition of the LCF-approach.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1511
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1512
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1513
  To see theorems in ``action'', let us give a proof on the ML-level for the following 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1514
  statement:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1515
\<close>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1516
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1517
  lemma 
551
be361e980acf updated subscripts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 544
diff changeset
  1518
    assumes assm\<^sub>1: "\<And>(x::nat). P x \<Longrightarrow> Q x" 
be361e980acf updated subscripts
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 544
diff changeset
  1519
    and     assm\<^sub>2: "P t"
351
f118240ab44a updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 350
diff changeset
  1520
    shows "Q t"(*<*)oops(*>*) 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1521
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1522
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1523
  The corresponding ML-code is as follows:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1524
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1525
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1526
ML %linenosgray\<open>val my_thm = 
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1527
let
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1528
  val assm1 = @{cprop "\<And>(x::nat). P x \<Longrightarrow> Q x"}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1529
  val assm2 = @{cprop "(P::nat \<Rightarrow> bool) t"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1530
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1531
  val Pt_implies_Qt = 
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  1532
    Thm.assume assm1
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  1533
    |> Thm.forall_elim @{cterm "t::nat"}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1534
  
449
f952f2679a11 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 448
diff changeset
  1535
  val Qt = Thm.implies_elim Pt_implies_Qt (Thm.assume assm2)
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1536
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1537
  Qt 
449
f952f2679a11 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 448
diff changeset
  1538
  |> Thm.implies_intr assm2
f952f2679a11 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 448
diff changeset
  1539
  |> Thm.implies_intr assm1
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1540
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1541
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1542
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1543
  Note that in Line 3 and 4 we use the antiquotation \<open>@{cprop \<dots>}\<close>, which
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1544
  inserts necessary \<open>Trueprop\<close>s.
415
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1545
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1546
  If we print out the value of @{ML my_thm} then we see only the 
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1547
  final statement of the theorem.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1548
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1549
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1550
  \<open>pwriteln (pretty_thm @{context} my_thm)\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1551
  \<open>\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t\<close>}
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1552
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1553
  However, internally the code-snippet constructs the following 
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1554
  proof.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1555
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1556
  \[
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1557
  \infer[(\<open>\<Longrightarrow>\<close>$-$intro)]{\vdash @{prop "(\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> P t \<Longrightarrow> Q t"}}
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1558
    {\infer[(\<open>\<Longrightarrow>\<close>$-$intro)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1559
       {\infer[(\<open>\<Longrightarrow>\<close>$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"}, @{prop "P t"} \vdash @{prop "Q t"}}
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1560
          {\infer[(\<open>\<And>\<close>$-$elim)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "P t \<Longrightarrow> Q t"}}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1561
                 {\infer[(assume)]{@{prop "\<And>x. P x \<Longrightarrow> Q x"} \vdash @{prop "\<And>x. P x \<Longrightarrow> Q x"}}{}}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1562
                 &
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1563
           \infer[(assume)]{@{prop "P t"} \vdash @{prop "P t"}}{}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1564
          }
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1565
       }
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1566
    }
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1567
  \]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1568
339
c588e8422737 used a better implementation of \index in Latex; added more to the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 338
diff changeset
  1569
  While we obtained a theorem as result, this theorem is not
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1570
  yet stored in Isabelle's theorem database. Consequently, it cannot be 
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1571
  referenced on the user level. One way to store it in the theorem database is
502
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
  1572
  by using the function @{ML_ind note in Local_Theory} from the structure 
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1573
  @{ML_structure Local_Theory} (the Isabelle command
502
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
  1574
  \isacommand{local\_setup} will be explained in more detail in 
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
  1575
  Section~\ref{sec:local}).
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1576
\<close>
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1577
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1578
(*FIXME: add forward reference to Proof_Context.export *)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1579
ML %linenosgray\<open>val my_thm_vars =
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1580
let
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1581
  val ctxt0 = @{context}
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1582
  val (_, ctxt1) = Variable.add_fixes ["P", "Q", "t"] ctxt0
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1583
in
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1584
  singleton (Proof_Context.export ctxt1 ctxt0) my_thm
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1585
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1586
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1587
local_setup %gray \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1588
  Local_Theory.note ((@{binding "my_thm"}, []), [my_thm_vars]) #> snd\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1589
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1590
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1591
text \<open>
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  1592
  The third argument of @{ML note in Local_Theory} is the list of theorems we
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1593
  want to store under a name. We can store more than one under a single name. 
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  1594
  The first argument of @{ML note in Local_Theory} is the name under
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  1595
  which we store the theorem or theorems. The second argument can contain a
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1596
  list of theorem attributes, which we will explain in detail in
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  1597
  Section~\ref{sec:attributes}. Below we just use one such attribute,
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  1598
  @{ML_ind simp_add in Simplifier}, for adding the theorem to the simpset:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1599
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1600
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1601
local_setup %gray \<open>
394
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
  1602
  Local_Theory.note ((@{binding "my_thm_simp"}, 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1603
       [Attrib.internal (K Simplifier.simp_add)]), [my_thm_vars]) #> snd\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1604
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1605
text \<open>
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1606
  Note that we have to use another name under which the theorem is stored,
394
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
  1607
  since Isabelle does not allow us to call  @{ML_ind note in Local_Theory} twice
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1608
  with the same name. The attribute needs to be wrapped inside the function @{ML_ind
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1609
  internal in Attrib} from the structure @{ML_structure Attrib}. If we use the function 
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1610
  @{ML get_thm_names_from_ss} from
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1611
  the previous chapter, we can check whether the theorem has actually been
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1612
  added.
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1613
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1614
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1615
  @{ML_matchresult [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1616
  \<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1617
  fun pred s = match_string "my_thm_simp" s
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1618
in
544
501491d56798 updated to simplifier change
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 535
diff changeset
  1619
  exists pred (get_thm_names_from_ss @{context})
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1620
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1621
  \<open>true\<close>}
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1622
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1623
  The main point of storing the theorems @{thm [source] my_thm} and @{thm
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1624
  [source] my_thm_simp} is that they can now also be referenced with the
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1625
  \isacommand{thm}-command on the user-level of Isabelle
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1626
502
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
  1627
    
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1628
  \begin{isabelle}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1629
  \isacommand{thm}~\<open>my_thm my_thm_simp\<close>\isanewline
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1630
  \<open>>\<close>~@{prop "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}\isanewline
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1631
  \<open>>\<close>~@{prop "\<lbrakk>\<And>x. P x \<Longrightarrow> Q x; P t\<rbrakk> \<Longrightarrow> Q t"}
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1632
  \end{isabelle}
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1633
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1634
  or with the \<open>@{thm \<dots>}\<close>-antiquotation on the ML-level. Otherwise the 
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1635
  user has no access to these theorems. 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1636
394
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
  1637
  Recall that Isabelle does not let you call @{ML note in Local_Theory} twice
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1638
  with the same theorem name. In effect, once a theorem is stored under a name, 
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1639
  this association is fixed. While this is a ``safety-net'' to make sure a
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1640
  theorem name refers to a particular theorem or collection of theorems, it is 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1641
  also a bit too restrictive in cases where a theorem name should refer to a 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1642
  dynamically expanding list of theorems (like a simpset). Therefore Isabelle 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1643
  also implements a mechanism where a theorem name can refer to a custom theorem 
451
fc074e669f9f disabled foobar_prove; updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
  1644
  list. For this you can use the function @{ML_ind add_thms_dynamic in Global_Theory}. 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1645
  To see how it works let us assume we defined our own theorem list \<open>MyThmList\<close>.
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1646
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1647
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1648
ML %grayML\<open>structure MyThmList = Generic_Data
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1649
  (type T = thm list
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1650
   val empty = []
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1651
   val extend = I
394
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
  1652
   val merge = merge Thm.eq_thm_prop)
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
  1653
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1654
fun update thm = Context.theory_map (MyThmList.map (Thm.add_thm thm))\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1655
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1656
text \<open>
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1657
  The function @{ML update} allows us to update the theorem list, for example
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1658
  by adding the theorem @{thm [source] TrueI}.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1659
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1660
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1661
setup %gray \<open>update @{thm TrueI}\<close>
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1662
 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1663
text \<open>
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1664
  We can now install the theorem list so that it is visible to the user and 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1665
  can be refered to by a theorem name. For this need to call 
451
fc074e669f9f disabled foobar_prove; updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
  1666
  @{ML_ind add_thms_dynamic in Global_Theory}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1667
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1668
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1669
setup %gray \<open>
451
fc074e669f9f disabled foobar_prove; updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 449
diff changeset
  1670
  Global_Theory.add_thms_dynamic (@{binding "mythmlist"}, MyThmList.get) 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1671
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1672
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1673
text \<open>
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1674
  with a name and a function that accesses the theorem list. Now if the
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1675
  user issues the command
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1676
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1677
  \begin{isabelle}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1678
  \isacommand{thm}~\<open>mythmlist\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1679
  \<open>> True\<close>
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1680
  \end{isabelle}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1681
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1682
  the current content of the theorem list is displayed. If more theorems are stored in 
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1683
  the list, say
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1684
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1685
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1686
setup %gray \<open>update @{thm FalseE}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1687
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1688
text \<open>
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1689
  then the same command produces
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1690
  
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1691
  \begin{isabelle}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1692
  \isacommand{thm}~\<open>mythmlist\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1693
  \<open>> False \<Longrightarrow> ?P\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1694
  \<open>> True\<close>
355
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1695
  \end{isabelle}
42a1c230daff added something about add_thms_dynamic
Christian Urban <urbanc@in.tum.de>
parents: 354
diff changeset
  1696
400
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
  1697
  Note that if we add the theorem @{thm [source] FalseE} again to the list
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1698
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1699
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1700
setup %gray \<open>update @{thm FalseE}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1701
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1702
text \<open>
400
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
  1703
  we still obtain the same list. The reason is that we used the function @{ML_ind
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
  1704
  add_thm in Thm} in our update function. This is a dedicated function which
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
  1705
  tests whether the theorem is already in the list.  This test is done
415
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1706
  according to alpha-equivalence of the proposition of the theorem. The
400
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
  1707
  corresponding testing function is @{ML_ind eq_thm_prop in Thm}.
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
  1708
  Suppose you proved the following three theorems.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1709
\<close>
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1710
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1711
lemma
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1712
  shows thm1: "\<forall>x. P x" 
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1713
  and   thm2: "\<forall>y. P y" 
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1714
  and   thm3: "\<forall>y. Q y" sorry
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1715
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1716
text \<open>
400
Christian Urban <urbanc@in.tum.de>
parents: 399
diff changeset
  1717
  Testing them for alpha equality produces:
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1718
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1719
  @{ML_matchresult [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1720
\<open>(Thm.eq_thm_prop (@{thm thm1}, @{thm thm2}),
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1721
 Thm.eq_thm_prop (@{thm thm2}, @{thm thm3}))\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1722
\<open>(true, false)\<close>}
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1723
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1724
  Many functions destruct theorems into @{ML_type cterm}s. For example
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1725
  the functions @{ML_ind lhs_of in Thm} and @{ML_ind rhs_of in Thm} return 
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1726
  the left and right-hand side, respectively, of a meta-equality.
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1727
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1728
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1729
  \<open>let
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1730
  val eq = @{thm True_def}
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1731
in
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1732
  (Thm.lhs_of eq, Thm.rhs_of eq) 
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1733
  |> apply2 (Pretty.string_of o (pretty_cterm @{context}))
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1734
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1735
  \<open>(True, (\<lambda>x. x) = (\<lambda>x. x))\<close>}
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1736
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1737
  Other function produce terms that can be pattern-matched. 
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1738
  Suppose the following two theorems.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1739
\<close>
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1740
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1741
lemma  
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1742
  shows foo_test1: "A \<Longrightarrow> B \<Longrightarrow> C" 
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1743
  and   foo_test2: "A \<longrightarrow> B \<longrightarrow> C" sorry
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1744
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1745
text \<open>
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
  1746
  We can destruct them into premises and conclusions as follows. 
340
4ddcf4980950 more work
Christian Urban <urbanc@in.tum.de>
parents: 339
diff changeset
  1747
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1748
 @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1749
\<open>let
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1750
  val ctxt = @{context}
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1751
  fun prems_and_concl thm =
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1752
     [[Pretty.str "Premises:", pretty_terms ctxt (Thm.prems_of thm)], 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1753
      [Pretty.str "Conclusion:", pretty_term ctxt (Thm.concl_of thm)]]
441
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
  1754
     |> map Pretty.block
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
  1755
     |> Pretty.chunks
Christian Urban <urbanc@in.tum.de>
parents: 440
diff changeset
  1756
     |> pwriteln
338
3bc732c9f7ff more on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 337
diff changeset
  1757
in
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1758
  prems_and_concl @{thm foo_test1};
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1759
  prems_and_concl @{thm foo_test2}
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1760
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1761
\<open>Premises: ?A, ?B
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1762
Conclusion: ?C
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1763
Premises: 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1764
Conclusion: ?A \<longrightarrow> ?B \<longrightarrow> ?C\<close>}
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1765
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1766
  Note that in the second case, there is no premise. The reason is that \<open>\<Longrightarrow>\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1767
  separates premises and conclusion, while \<open>\<longrightarrow>\<close> is the object implication
415
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1768
  from HOL, which just constructs a formula.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1769
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1770
  \begin{readmore}
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
  1771
  The basic functions for theorems are defined in
337
a456a21f608a a bit more work on the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 336
diff changeset
  1772
  @{ML_file "Pure/thm.ML"}, @{ML_file "Pure/more_thm.ML"} and @{ML_file "Pure/drule.ML"}. 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1773
  \end{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  1774
388
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
  1775
  Although we will explain the simplifier in more detail as tactic in 
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
  1776
  Section~\ref{sec:simplifier}, the simplifier 
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
  1777
  can be used to work directly over theorems, for example to unfold definitions. To show
382
Christian Urban <urbanc@in.tum.de>
parents: 381
diff changeset
  1778
  this, we build the theorem @{term "True \<equiv> True"} (Line 1) and then 
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1779
  unfold the constant @{term "True"} according to its definition (Line 2).
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1780
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1781
  @{ML_matchresult_fake [display,gray,linenos]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1782
  \<open>Thm.reflexive @{cterm "True"}
552
82c482467d75 updated to latest isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 551
diff changeset
  1783
  |> Simplifier.rewrite_rule @{context} [@{thm True_def}]
440
a0b280dd4bc7 partially moved from string_of_term to pretty_term
Christian Urban <urbanc@in.tum.de>
parents: 439
diff changeset
  1784
  |> pretty_thm @{context}
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1785
  |> pwriteln\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1786
  \<open>(\<lambda>x. x) = (\<lambda>x. x) \<equiv> (\<lambda>x. x) = (\<lambda>x. x)\<close>}
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1787
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1788
  Often it is necessary to transform theorems to and from the object 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1789
  logic, that is replacing all \<open>\<longrightarrow>\<close> and \<open>\<forall>\<close> by \<open>\<Longrightarrow>\<close> 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1790
  and \<open>\<And>\<close>, or the other way around.  A reason for such a transformation 
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1791
  might be stating a definition. The reason is that definitions can only be 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1792
  stated using object logic connectives, while theorems using the connectives 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1793
  from the meta logic are more convenient for reasoning. Therefore there are
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1794
  some build in functions which help with these transformations. The function 
418
1d1e4cda8c54 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
  1795
  @{ML_ind rulify in Object_Logic} 
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1796
  replaces all object connectives by equivalents in the meta logic. For example
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1797
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1798
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1799
  \<open>Object_Logic.rulify @{context} @{thm foo_test2}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1800
  \<open>\<lbrakk>?A; ?B\<rbrakk> \<Longrightarrow> ?C\<close>}
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1801
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1802
  The transformation in the other direction can be achieved with function
418
1d1e4cda8c54 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
  1803
  @{ML_ind atomize in Object_Logic} and the following code.
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1804
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1805
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1806
  \<open>let
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1807
  val thm = @{thm foo_test1}
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1808
  val meta_eq = Object_Logic.atomize @{context} (Thm.cprop_of thm)
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1809
in
552
82c482467d75 updated to latest isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 551
diff changeset
  1810
  Raw_Simplifier.rewrite_rule @{context} [meta_eq] thm
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1811
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1812
  \<open>?A \<longrightarrow> ?B \<longrightarrow> ?C\<close>}
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1813
418
1d1e4cda8c54 updated to new isabelle
Christian Urban <urbanc@in.tum.de>
parents: 416
diff changeset
  1814
  In this code the function @{ML atomize in Object_Logic} produces 
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1815
  a meta-equation between the given theorem and the theorem transformed
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1816
  into the object logic. The result is the theorem with object logic 
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1817
  connectives. However, in order to completely transform a theorem
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1818
  involving meta variables, such as @{thm [source] list.induct}, which 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1819
  is of the form 
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1820
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1821
  @{thm [display] list.induct}
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1822
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1823
  we have to first abstract over the meta variables \<open>?P\<close> and 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1824
  \<open>?list\<close>. For this we can use the function 
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1825
  @{ML_ind forall_intr_vars in Drule}. This allows us to implement the
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1826
  following function for atomizing a theorem.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1827
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1828
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1829
ML %grayML\<open>fun atomize_thm ctxt thm =
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1830
let
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1831
  val thm' = forall_intr_vars thm
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1832
  val thm'' = Object_Logic.atomize ctxt (Thm.cprop_of thm')
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1833
in
552
82c482467d75 updated to latest isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 551
diff changeset
  1834
  Raw_Simplifier.rewrite_rule ctxt [thm''] thm'
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1835
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1836
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1837
text \<open>
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1838
  This function produces for the theorem @{thm [source] list.induct}
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1839
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1840
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1841
  \<open>atomize_thm @{context} @{thm list.induct}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1842
  \<open>\<forall>P list. P [] \<longrightarrow> (\<forall>a list. P list \<longrightarrow> P (a # list)) \<longrightarrow> P list\<close>}
347
01e71cddf6a3 slightly tuned
Christian Urban <urbanc@in.tum.de>
parents: 346
diff changeset
  1843
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1844
  Theorems can also be produced from terms by giving an explicit proof. 
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1845
  One way to achieve this is by using the function @{ML_ind prove in Goal}
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1846
  in the structure @{ML_structure Goal}. For example below we use this function
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1847
  to prove the term @{term "P \<Longrightarrow> P"}.
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1848
  
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1849
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1850
  \<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1851
  val trm = @{term "P \<Longrightarrow> P::bool"}
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1852
  val tac = K (assume_tac @{context} 1)
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1853
in
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1854
  Goal.prove @{context} ["P"] [] trm tac
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1855
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1856
  \<open>?P \<Longrightarrow> ?P\<close>}
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1857
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1858
  This function takes first a context and second a list of strings. This list
359
be6538c7b41d polished
Christian Urban <urbanc@in.tum.de>
parents: 358
diff changeset
  1859
  specifies which variables should be turned into schematic variables once the term
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1860
  is proved (in this case only \<open>"P"\<close>).  The fourth argument is the term to be 
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1861
  proved. The fifth is a corresponding proof given in form of a tactic (we explain 
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1862
  tactics in Chapter~\ref{chp:tactical}). In the code above, the tactic proves the 
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1863
  theorem by assumption. 
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1864
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1865
  There is also the possibility of proving multiple goals at the same time
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1866
  using the function @{ML_ind prove_common in Goal}. For this let us define the
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1867
  following three helper functions.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1868
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1869
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1870
ML %grayML\<open>fun rep_goals i = replicate i @{prop "f x = f x"}
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1871
fun rep_tacs i = replicate i (resolve_tac @{context} [@{thm refl}])
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1872
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1873
fun multi_test ctxt i =
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1874
  Goal.prove_common ctxt NONE ["f", "x"] [] (rep_goals i) 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1875
    (K ((Goal.conjunction_tac THEN' RANGE (rep_tacs i)) 1))\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1876
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1877
text \<open>
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1878
  With them we can now produce three theorem instances of the 
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1879
  proposition.
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1880
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1881
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1882
  \<open>multi_test @{context} 3\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1883
  \<open>["?f ?x = ?f ?x", "?f ?x = ?f ?x", "?f ?x = ?f ?x"]\<close>}
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1884
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1885
  However you should be careful with @{ML prove_common in Goal} and very
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1886
  large goals. If you increase the counter in the code above to \<open>3000\<close>, 
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1887
  you will notice that takes approximately ten(!) times longer than
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1888
  using @{ML map} and @{ML prove in Goal}.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1889
\<close>
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1890
  
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1891
ML %grayML\<open>let 
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1892
  fun test_prove ctxt thm =
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  1893
    Goal.prove ctxt ["P", "x"] [] thm (K (resolve_tac  @{context} [@{thm refl}] 1))
448
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1894
in
957f69b9b7df added something about Goal.prove_multi
Christian Urban <urbanc@in.tum.de>
parents: 446
diff changeset
  1895
  map (test_prove @{context}) (rep_goals 3000)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1896
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1897
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1898
text \<open>
388
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
  1899
  While the LCF-approach of going through interfaces ensures soundness in Isabelle, there
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
  1900
  is the function @{ML_ind make_thm in Skip_Proof} in the structure 
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1901
  @{ML_structure Skip_Proof} that allows us to turn any proposition into a theorem.
388
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
  1902
  Potentially making the system unsound.  This is sometimes useful for developing 
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
  1903
  purposes, or when explicit proof construction should be omitted due to performace 
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
  1904
  reasons. An example of this function is as follows:
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
  1905
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1906
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1907
  \<open>Skip_Proof.make_thm @{theory} @{prop "True = False"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1908
  \<open>True = False\<close>}
388
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
  1909
415
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1910
  \begin{readmore}
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1911
  Functions that setup goal states and prove theorems are implemented in 
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1912
  @{ML_file "Pure/goal.ML"}. A function and a tactic that allow one to
544
501491d56798 updated to simplifier change
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 535
diff changeset
  1913
  skip proofs of theorems are implemented in @{ML_file "Pure/skip_proof.ML"}.
415
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1914
  \end{readmore}
dc76ba24e81b a bit more tuning
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
  1915
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1916
  Theorems also contain auxiliary data, such as the name of the theorem, its
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1917
  kind, the names for cases and so on. This data is stored in a string-string
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1918
  list and can be retrieved with the function @{ML_ind get_tags in
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1919
  Thm}. Assume you prove the following lemma.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1920
\<close>
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1921
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1922
lemma foo_data: 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1923
  shows "P \<Longrightarrow> P \<Longrightarrow> P" by assumption
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1924
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1925
text \<open>
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1926
  The auxiliary data of this lemma can be retrieved using the function 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1927
  @{ML_ind get_tags in Thm}. So far the the auxiliary data of this lemma is 
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1928
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1929
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1930
  \<open>Thm.get_tags @{thm foo_data}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1931
  \<open>[("name", "General.foo_data"), ("kind", "lemma")]\<close>}
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1932
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1933
  consisting of a name and a kind.  When we store lemmas in the theorem database, 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1934
  we might want to explicitly extend this data by attaching case names to the 
375
92f7328dc5cc added type work and updated to Isabelle and poly 5.3
Christian Urban <urbanc@in.tum.de>
parents: 374
diff changeset
  1935
  two premises of the lemma.  This can be done with the function @{ML_ind name in Rule_Cases}
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1936
  from the structure @{ML_structure Rule_Cases}.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1937
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1938
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1939
local_setup %gray \<open>
394
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
  1940
  Local_Theory.note ((@{binding "foo_data'"}, []), 
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
  1941
    [(Rule_Cases.name ["foo_case_one", "foo_case_two"] 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1942
       @{thm foo_data})]) #> snd\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1943
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1944
text \<open>
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1945
  The data of the theorem @{thm [source] foo_data'} is then as follows:
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1946
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1947
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1948
  \<open>Thm.get_tags @{thm foo_data'}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1949
  \<open>[("name", "General.foo_data'"), ("kind", "lemma"), 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  1950
 ("case_names", "foo_case_one;foo_case_two")]\<close>}
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1951
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  1952
  You can observe the case names of this lemma on the user level when using 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1953
  the proof methods \<open>cases\<close> and \<open>induct\<close>. In the proof below
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1954
\<close>
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1955
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  1956
lemma
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  1957
shows "Q \<Longrightarrow> Q \<Longrightarrow> Q"
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1958
proof (cases rule: foo_data')
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1959
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1960
(*<*)oops(*>*)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1961
text_raw\<open>
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1962
\begin{tabular}{@ {}l}
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1963
\isacommand{print\_cases}\\
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1964
\<open>> cases:\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1965
\<open>>   foo_case_one:\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1966
\<open>>     let "?case" = "?P"\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1967
\<open>>   foo_case_two:\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1968
\<open>>     let "?case" = "?P"\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1969
\end{tabular}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1970
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1971
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1972
  we can proceed by analysing the cases \<open>foo_case_one\<close> and 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1973
  \<open>foo_case_two\<close>. While if the theorem has no names, then
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1974
  the cases have standard names \<open>1\<close>, \<open>2\<close> and so 
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1975
  on. This can be seen in the proof below.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1976
\<close>
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1977
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1978
lemma
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  1979
shows "Q \<Longrightarrow> Q \<Longrightarrow> Q"
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  1980
proof (cases rule: foo_data)
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1981
342
930b1308fd96 fixed glitch with tocibind
Christian Urban <urbanc@in.tum.de>
parents: 341
diff changeset
  1982
(*<*)oops(*>*)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1983
text_raw\<open>
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1984
\begin{tabular}{@ {}l}
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1985
\isacommand{print\_cases}\\
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1986
\<open>> cases:\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1987
\<open>>   1:\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1988
\<open>>     let "?case" = "?P"\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1989
\<open>>   2:\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1990
\<open>>     let "?case" = "?P"\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1991
\end{tabular}\<close>
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  1992
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  1993
 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1994
text \<open>
533
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
  1995
  Sometimes one wants to know the theorems that are involved in
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  1996
  proving a theorem, especially when the proof is by \<open>auto\<close>. These theorems can be obtained by introspecting the proved theorem.
533
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
  1997
  To introspect a theorem, let us define the following three functions
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
  1998
  that analyse the @{ML_type_ind proof_body} data-structure from the
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  1999
  structure @{ML_structure Proofterm}.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2000
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2001
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2002
ML %grayML\<open>fun pthms_of (PBody {thms, ...}) = map #2 thms 
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  2003
val get_names = (map Proofterm.thm_node_name) o pthms_of 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2004
val get_pbodies = map (Future.join o Proofterm.thm_node_body) o pthms_of\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2005
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2006
text \<open>
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2007
  To see what their purpose is, consider the following three short proofs.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2008
\<close>
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2009
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2010
lemma my_conjIa:
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2011
shows "A \<and> B \<Longrightarrow> A \<and> B"
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2012
apply(rule conjI)
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2013
apply(drule conjunct1)
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2014
apply(assumption)
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2015
apply(drule conjunct2)
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2016
apply(assumption)
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2017
done
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2018
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2019
lemma my_conjIb:
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2020
shows "A \<and> B \<Longrightarrow> A \<and> B"
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2021
apply(assumption)
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2022
done
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2023
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2024
lemma my_conjIc:
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2025
shows "A \<and> B \<Longrightarrow> A \<and> B"
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2026
apply(auto)
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2027
done
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
  2028
535
5734ab5dd86d adapted to new build framework
Christian Urban <urbanc@in.tum.de>
parents: 534
diff changeset
  2029
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2030
text \<open>
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2031
  While the information about which theorems are used is obvious in
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2032
  the first two proofs, it is not obvious in the third, because of the
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2033
  \<open>auto\<close>-step.  Fortunately, ``behind'' every proved theorem is
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2034
  a proof-tree that records all theorems that are employed for
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2035
  establishing this theorem.  We can traverse this proof-tree
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2036
  extracting this information.  Let us first extract the name of the
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2037
  established theorem. This can be done with
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2038
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2039
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2040
  \<open>@{thm my_conjIa}
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2041
  |> Thm.proof_body_of
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2042
  |> get_names\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2043
  \<open>["Essential.my_conjIa"]\<close>}
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2044
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2045
  whereby \<open>Essential\<close> refers to the theory name in which
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2046
  we established the theorem @{thm [source] my_conjIa}. The function @{ML_ind
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2047
  proof_body_of in Thm} returns a part of the data that is stored in a
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2048
  theorem.  Notice that the first proof above references
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2049
  three theorems, namely @{thm [source] conjI}, @{thm [source] conjunct1} 
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2050
  and @{thm [source] conjunct2}. We can obtain them by descending into the
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2051
  first level of the proof-tree, as follows.
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2052
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2053
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2054
  \<open>@{thm my_conjIa}
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2055
  |> Thm.proof_body_of
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2056
  |> get_pbodies
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2057
  |> map get_names
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2058
  |> List.concat\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2059
  \<open>["HOL.conjunct2", "HOL.conjunct1", "HOL.conjI", "Pure.protectD", 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2060
  "Pure.protectI"]\<close>}
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2061
533
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
  2062
  The theorems @{thm [source] Pure.protectD} and @{thm [source]
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
  2063
  Pure.protectI} that are internal theorems are always part of a
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2064
  proof in Isabelle. Note also that applications of \<open>assumption\<close> do not
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2065
  count as a separate theorem, as you can see in the following code.
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2066
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2067
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2068
  \<open>@{thm my_conjIb}
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2069
  |> Thm.proof_body_of
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2070
  |> get_pbodies
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2071
  |> map get_names
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2072
  |> List.concat\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2073
  \<open>["Pure.protectD", "Pure.protectI"]\<close>}
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2074
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2075
  Interestingly, but not surprisingly, the proof of @{thm [source]
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2076
  my_conjIc} procceeds quite differently from @{thm [source] my_conjIa}
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2077
  and @{thm [source] my_conjIb}, as can be seen by the theorems that
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2078
  are returned for @{thm [source] my_conjIc}.
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2079
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2080
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2081
  \<open>@{thm my_conjIc}
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2082
  |> Thm.proof_body_of
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2083
  |> get_pbodies
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2084
  |> map get_names
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2085
  |> List.concat\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2086
  \<open>["HOL.Eq_TrueI", "HOL.simp_thms_25", "HOL.eq_reflection",
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2087
  "HOL.conjunct2", "HOL.conjunct1", "HOL.TrueI", "Pure.protectD",
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2088
  "Pure.protectI"]\<close>}
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2089
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2090
  Of course we can also descend into the second level of the tree 
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2091
  ``behind'' @{thm [source] my_conjIa} say, which
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2092
  means we obtain the theorems that are used in order to prove
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2093
  @{thm [source] conjunct1}, @{thm [source] conjunct2} and @{thm [source] conjI}.
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2094
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2095
  @{ML_matchresult_fake [display, gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2096
  \<open>@{thm my_conjIa}
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2097
  |> Thm.proof_body_of
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2098
  |> get_pbodies
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2099
  |> map get_pbodies
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2100
  |> (map o map) get_names
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2101
  |> List.concat
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2102
  |> List.concat
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2103
  |> duplicates (op=)\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2104
  \<open>["HOL.spec", "HOL.and_def", "HOL.mp", "HOL.impI", "Pure.protectD",
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2105
  "Pure.protectI"]\<close>}
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2106
533
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
  2107
  \begin{exercise}
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
  2108
  Have a look at the theorems that are used when a lemma is ``proved''
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
  2109
  by \isacommand{sorry}? 
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
  2110
  \end{exercise}
Christian Urban <urbanc@in.tum.de>
parents: 530
diff changeset
  2111
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2112
  \begin{readmore} 
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2113
  The data-structure @{ML_type proof_body} is implemented
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2114
  in the file @{ML_file "Pure/proofterm.ML"}. The implementation 
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2115
  of theorems and related functions are in @{ML_file "Pure/thm.ML"}.  
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2116
  \end{readmore}
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2117
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  2118
  One great feature of Isabelle is its document preparation system, where
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2119
  proved theorems can be quoted in documents referencing directly their
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2120
  formalisation. This helps tremendously to minimise cut-and-paste errors. However,
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2121
  sometimes the verbatim quoting is not what is wanted or what can be shown to
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2122
  readers. For such situations Isabelle allows the installation of \emph{\index*{theorem 
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2123
  styles}}. These are, roughly speaking, functions from terms to terms. The input 
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2124
  term stands for the theorem to be presented; the output can be constructed to
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2125
  ones wishes.  Let us, for example, assume we want to quote theorems without
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2126
  leading \<open>\<forall>\<close>-quantifiers. For this we can implement the following function 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2127
  that strips off \<open>\<forall>\<close>s.
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2128
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2129
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2130
ML %linenosgray\<open>fun strip_allq (Const (@{const_name "All"}, _) $ Abs body) = 
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2131
      Term.dest_abs body |> snd |> strip_allq
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2132
  | strip_allq (Const (@{const_name "Trueprop"}, _) $ t) = 
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2133
      strip_allq t
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2134
  | strip_allq t = t\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2135
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2136
text \<open>
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2137
  We use in Line 2 the function @{ML_ind dest_abs in Term} for deconstructing abstractions,
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2138
  since this function deals correctly with potential name clashes. This function produces
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2139
  a pair consisting of the variable and the body of the abstraction. We are only interested
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2140
  in the body, which we feed into the recursive call. In Line 3 and 4, we also
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2141
  have to explicitly strip of the outermost @{term Trueprop}-coercion. Now we can 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2142
  install this function as the theorem style named \<open>my_strip_allq\<close>. 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2143
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2144
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2145
setup %gray\<open>
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 552
diff changeset
  2146
  Term_Style.setup @{binding "my_strip_allq"} (Scan.succeed (K strip_allq)) 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2147
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2148
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2149
text \<open>
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2150
  We can test this theorem style with the following theorem
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2151
\<close>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2152
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  2153
theorem style_test:
529
13d7ea419c5f moved the introspection part into the theorem section
Christian Urban <urbanc@in.tum.de>
parents: 517
diff changeset
  2154
shows "\<forall>x y z. (x, x) = (y, z)" sorry
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  2155
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2156
text \<open>
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2157
  Now printing out in a document the theorem @{thm [source] style_test} normally
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2158
  using \<open>@{thm \<dots>}\<close> produces
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  2159
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2160
  \begin{isabelle}
502
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
  2161
  \begin{graybox}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2162
  \<open>@{thm style_test}\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2163
  \<open>>\<close>~@{thm style_test}
502
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
  2164
  \end{graybox}
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2165
  \end{isabelle}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2166
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2167
  as expected. But with the theorem style \<open>@{thm (my_strip_allq) \<dots>}\<close> 
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  2168
  we obtain
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  2169
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2170
  \begin{isabelle}
502
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
  2171
  \begin{graybox}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2172
  \<open>@{thm (my_strip_allq) style_test}\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2173
  \<open>>\<close>~@{thm (my_strip_allq) style_test}
502
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
  2174
  \end{graybox}
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2175
  \end{isabelle}
352
9f12e53eb121 polished theorem section
Christian Urban <urbanc@in.tum.de>
parents: 351
diff changeset
  2176
  
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2177
  without the leading quantifiers. We can improve this theorem style by explicitly 
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2178
  giving a list of strings that should be used for the replacement of the
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2179
  variables. For this we implement the function which takes a list of strings
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2180
  and uses them as name in the outermost abstractions.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2181
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2182
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2183
ML %grayML\<open>fun rename_allq [] t = t
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2184
  | rename_allq (x::xs) (Const (@{const_name "All"}, U) $ Abs (_, T, t)) = 
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2185
      Const (@{const_name "All"}, U) $ Abs (x, T, rename_allq xs t)
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2186
  | rename_allq xs (Const (@{const_name "Trueprop"}, U) $ t) =
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2187
      rename_allq xs t
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2188
  | rename_allq _ t = t\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2189
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2190
text \<open>
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2191
  We can now install a the modified theorem style as follows
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2192
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2193
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2194
setup %gray \<open>let
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2195
  val parser = Scan.repeat Args.name
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2196
  fun action xs = K (rename_allq xs #> strip_allq)
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2197
in
553
c53d74b34123 updated to changes in Isabelle
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 552
diff changeset
  2198
  Term_Style.setup @{binding "my_strip_allq2"} (parser >> action)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2199
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2200
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2201
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2202
  The parser reads a list of names. In the function \<open>action\<close> we first
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2203
  call @{ML rename_allq} with the parsed list, then we call @{ML strip_allq}
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2204
  on the resulting term. We can now suggest, for example, two variables for
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2205
  stripping off the first two \<open>\<forall>\<close>-quantifiers.
354
544c149005cf some slight polishing
Christian Urban <urbanc@in.tum.de>
parents: 353
diff changeset
  2206
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2207
  \begin{isabelle}
502
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
  2208
  \begin{graybox}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2209
  \<open>@{thm (my_strip_allq2 x' x'') style_test}\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2210
  \<open>>\<close>~@{thm (my_strip_allq2 x' x'') style_test}
502
Christian Urban <urbanc@in.tum.de>
parents: 482
diff changeset
  2211
  \end{graybox}
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2212
  \end{isabelle}
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2213
404
3d27d77c351f added RANGE
Christian Urban <urbanc@in.tum.de>
parents: 403
diff changeset
  2214
  Such styles allow one to print out theorems in documents formatted to 
3d27d77c351f added RANGE
Christian Urban <urbanc@in.tum.de>
parents: 403
diff changeset
  2215
  ones heart content. The styles can also be used in the document 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2216
  antiquotations \<open>@{prop ...}\<close>, \<open>@{term_type ...}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2217
  and \<open>@{typeof ...}\<close>.
404
3d27d77c351f added RANGE
Christian Urban <urbanc@in.tum.de>
parents: 403
diff changeset
  2218
3d27d77c351f added RANGE
Christian Urban <urbanc@in.tum.de>
parents: 403
diff changeset
  2219
  Next we explain theorem attributes, which is another
353
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2220
  mechanism for dealing with theorems.
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2221
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2222
  \begin{readmore}
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2223
  Theorem styles are implemented in @{ML_file "Pure/Thy/term_style.ML"}.
e73ccbed776e completed section on theorems
Christian Urban <urbanc@in.tum.de>
parents: 352
diff changeset
  2224
  \end{readmore}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2225
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2226
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2227
section \<open>Theorem Attributes\label{sec:attributes}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2228
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2229
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2230
  Theorem attributes are \<open>[symmetric]\<close>, \<open>[THEN \<dots>]\<close>, \<open>[simp]\<close> and so on. Such attributes are \emph{neither} tags \emph{nor} flags
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  2231
  annotated to theorems, but functions that do further processing of 
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  2232
  theorems. In particular, it is not possible to find out
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2233
  what are all theorems that have a given attribute in common, unless of course
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2234
  the function behind the attribute stores the theorems in a retrievable 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2235
  data structure. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2236
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2237
  If you want to print out all currently known attributes a theorem can have, 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2238
  you can use the Isabelle command
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2239
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2240
  \begin{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2241
  \isacommand{print\_attributes}\\
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2242
  \<open>> COMP:  direct composition with rules (no lifting)\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2243
  \<open>> HOL.dest:  declaration of Classical destruction rule\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2244
  \<open>> HOL.elim:  declaration of Classical elimination rule\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2245
  \<open>> \<dots>\<close>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2246
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2247
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2248
  The theorem attributes fall roughly into two categories: the first category manipulates
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2249
  theorems (for example \<open>[symmetric]\<close> and \<open>[THEN \<dots>]\<close>), and the second
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2250
  stores theorems somewhere as data (for example \<open>[simp]\<close>, which adds
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  2251
  theorems to the current simpset).
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2252
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2253
  To explain how to write your own attribute, let us start with an extremely simple 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2254
  version of the attribute \<open>[symmetric]\<close>. The purpose of this attribute is
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2255
  to produce the ``symmetric'' version of an equation. The main function behind 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2256
  this attribute is
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2257
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2258
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2259
ML %grayML\<open>val my_symmetric = Thm.rule_attribute [] (fn _ => fn thm => thm RS @{thm sym})\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2260
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2261
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2262
  where the function @{ML_ind  rule_attribute in Thm} expects a function taking a 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2263
  context (which we ignore in the code above) and a theorem (\<open>thm\<close>), and 
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2264
  returns another theorem (namely \<open>thm\<close> resolved with the theorem 
363
Christian Urban <urbanc@in.tum.de>
parents: 361
diff changeset
  2265
  @{thm [source] sym}: @{thm sym[no_vars]}; the function @{ML_ind RS in Drule} 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2266
  is explained in Section~\ref{sec:simpletacs}). The function 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2267
  @{ML rule_attribute in Thm} then returns  an attribute.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2268
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2269
  Before we can use the attribute, we need to set it up. This can be done
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2270
  using the Isabelle command \isacommand{attribute\_setup} as follows:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2271
\<close>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2272
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  2273
attribute_setup %gray my_sym = 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2274
  \<open>Scan.succeed my_symmetric\<close> "applying the sym rule"
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2275
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2276
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2277
  Inside the \<open>\<verbopen> \<dots> \<verbclose>\<close>, we have to specify a parser
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2278
  for the theorem attribute. Since the attribute does not expect any further 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2279
  arguments (unlike \<open>[THEN \<dots>]\<close>, for instance), we use the parser @{ML
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2280
  Scan.succeed}. An example for the attribute \<open>[my_sym]\<close> is the proof
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2281
\<close> 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2282
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2283
lemma test[my_sym]: "2 = Suc (Suc 0)" by simp
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2284
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2285
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2286
  which stores the theorem @{thm test} under the name @{thm [source] test}. You
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2287
  can see this, if you query the lemma: 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2288
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2289
  \begin{isabelle}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2290
  \isacommand{thm}~\<open>test\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2291
  \<open>> \<close>~@{thm test}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2292
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2293
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2294
  We can also use the attribute when referring to this theorem:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2295
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2296
  \begin{isabelle}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2297
  \isacommand{thm}~\<open>test[my_sym]\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2298
  \<open>> \<close>~@{thm test[my_sym]}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2299
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2300
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2301
  An alternative for setting up an attribute is the function @{ML_ind  setup in Attrib}.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2302
  So instead of using \isacommand{attribute\_setup}, you can also set up the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2303
  attribute as follows:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2304
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2305
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2306
ML %grayML\<open>Attrib.setup @{binding "my_sym"} (Scan.succeed my_symmetric)
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2307
  "applying the sym rule"\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2308
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2309
text \<open>
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  2310
  This gives a function from @{ML_type "theory -> theory"}, which
361
Christian Urban <urbanc@in.tum.de>
parents: 360
diff changeset
  2311
  can be used for example with \isacommand{setup} or with
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2312
  @{ML \<open>Context.>> o Context.map_theory\<close>}.\footnote{\bf FIXME: explain what happens here.}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2313
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2314
  As an example of a slightly more complicated theorem attribute, we implement 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2315
  our own version of \<open>[THEN \<dots>]\<close>. This attribute will take a list of theorems
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  2316
  as argument and resolve the theorem with this list (one theorem 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2317
  after another). The code for this attribute is
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2318
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2319
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2320
ML %grayML\<open>fun MY_THEN thms = 
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2321
let
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2322
  fun RS_rev thm1 thm2 = thm2 RS thm1
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2323
in
562
daf404920ab9 Accomodate to Isabelle 2018
Norbert Schirmer <norbert.schirmer@web.de>
parents: 559
diff changeset
  2324
  Thm.rule_attribute [] (fn _ => fn thm => fold RS_rev thms thm)
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2325
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2326
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2327
text \<open>
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2328
  where for convenience we define the reverse and curried version of @{ML RS}. 
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2329
  The setup of this theorem attribute uses the parser @{ML thms in Attrib}, 
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2330
  which parses a list of theorems. 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2331
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2332
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2333
attribute_setup %gray MY_THEN = \<open>Attrib.thms >> MY_THEN\<close> 
356
43df2d59fb98 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 355
diff changeset
  2334
  "resolving the list of theorems with the theorem"
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2335
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2336
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2337
  You can, for example, use this theorem attribute to turn an equation into a 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2338
  meta-equation:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2339
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2340
  \begin{isabelle}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2341
  \isacommand{thm}~\<open>test[MY_THEN eq_reflection]\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2342
  \<open>> \<close>~@{thm test[MY_THEN eq_reflection]}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2343
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2344
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2345
  If you need the symmetric version as a meta-equation, you can write
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2346
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2347
  \begin{isabelle}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2348
  \isacommand{thm}~\<open>test[MY_THEN sym eq_reflection]\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2349
  \<open>> \<close>~@{thm test[MY_THEN sym eq_reflection]}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2350
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2351
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2352
  It is also possible to combine different theorem attributes, as in:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2353
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2354
  \begin{isabelle}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2355
  \isacommand{thm}~\<open>test[my_sym, MY_THEN eq_reflection]\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2356
  \<open>> \<close>~@{thm test[my_sym, MY_THEN eq_reflection]}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2357
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2358
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2359
  However, here also a weakness of the concept
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2360
  of theorem attributes shows through: since theorem attributes can be
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2361
  arbitrary functions, they do not commute in general. If you try
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2362
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2363
  \begin{isabelle}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2364
  \isacommand{thm}~\<open>test[MY_THEN eq_reflection, my_sym]\<close>\\
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2365
  \<open>> \<close>~\<open>exception THM 1 raised: RSN: no unifiers\<close>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2366
  \end{isabelle}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2367
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2368
  you get an exception indicating that the theorem @{thm [source] sym}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2369
  does not resolve with meta-equations. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2370
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2371
  The purpose of @{ML_ind rule_attribute in Thm} is to directly manipulate
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2372
  theorems.  Another usage of theorem attributes is to add and delete theorems
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2373
  from stored data.  For example the theorem attribute \<open>[simp]\<close> adds
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2374
  or deletes a theorem from the current simpset. For these applications, you
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2375
  can use @{ML_ind declaration_attribute in Thm}. To illustrate this function,
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2376
  let us introduce a theorem list.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2377
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2378
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2379
ML %grayML\<open>structure MyThms = Named_Thms
481
32323727af96 updated
Christian Urban <urbanc@in.tum.de>
parents: 469
diff changeset
  2380
  (val name = @{binding "attr_thms"} 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2381
   val description = "Theorems for an Attribute")\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2382
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2383
text \<open>
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2384
  We are going to modify this list by adding and deleting theorems.
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2385
  For this we use the two functions @{ML MyThms.add_thm} and
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2386
  @{ML MyThms.del_thm}. You can turn them into attributes 
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2387
  with the code
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2388
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2389
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2390
ML %grayML\<open>val my_add = Thm.declaration_attribute MyThms.add_thm
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2391
val my_del = Thm.declaration_attribute MyThms.del_thm\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2392
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2393
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2394
  and set up the attributes as follows
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2395
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2396
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2397
attribute_setup %gray my_thms = \<open>Attrib.add_del my_add my_del\<close> 
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2398
  "maintaining a list of my_thms" 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2399
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2400
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2401
  The parser @{ML_ind  add_del in Attrib} is a predefined parser for 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2402
  adding and deleting lemmas. Now if you prove the next lemma 
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2403
  and attach to it the attribute \<open>[my_thms]\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2404
\<close>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2405
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2406
lemma trueI_2[my_thms]: "True" by simp
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2407
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2408
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2409
  then you can see it is added to the initially empty list.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2410
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2411
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2412
  \<open>MyThms.get @{context}\<close> 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2413
  \<open>["True"]\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2414
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2415
  You can also add theorems using the command \isacommand{declare}.
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2416
\<close>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2417
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2418
declare test[my_thms] trueI_2[my_thms add]
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2419
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2420
text \<open>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2421
  With this attribute, the \<open>add\<close> operation is the default and does 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2422
  not need to be explicitly given. These three declarations will cause the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2423
  theorem list to be updated as:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2424
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2425
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2426
  \<open>MyThms.get @{context}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2427
  \<open>["True", "Suc (Suc 0) = 2"]\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2428
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2429
  The theorem @{thm [source] trueI_2} only appears once, since the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2430
  function @{ML_ind  add_thm in Thm} tests for duplicates, before extending
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2431
  the list. Deletion from the list works as follows:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2432
\<close>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2433
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2434
declare test[my_thms del]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2435
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2436
text \<open>After this, the theorem list is again: 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2437
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2438
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2439
  \<open>MyThms.get @{context}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2440
  \<open>["True"]\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2441
329
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2442
  We used in this example two functions declared as @{ML_ind
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2443
  declaration_attribute in Thm}, but there can be any number of them. We just
5dffcab68680 more work
Christian Urban <urbanc@in.tum.de>
parents: 328
diff changeset
  2444
  have to change the parser for reading the arguments accordingly.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2445
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2446
  \footnote{\bf FIXME What are: \<open>theory_attributes\<close>, \<open>proof_attributes\<close>?}
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2447
  \footnote{\bf FIXME readmore}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2448
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2449
  \begin{readmore}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2450
  FIXME: @{ML_file "Pure/more_thm.ML"}; parsers for attributes is in 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2451
  @{ML_file "Pure/Isar/attrib.ML"}...also explained in the chapter about
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2452
  parsing.
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2453
  \end{readmore}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2454
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2455
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2456
section \<open>Pretty-Printing\label{sec:pretty}\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2457
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2458
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2459
  So far we printed out only plain strings without any formatting except for
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2460
  occasional explicit line breaks using @{text [quotes] "\\n"}. This is
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2461
  sufficient for ``quick-and-dirty'' printouts. For something more
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2462
  sophisticated, Isabelle includes an infrastructure for properly formatting
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2463
  text. Most of its functions do not operate on @{ML_type string}s, but on
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2464
  instances of the pretty type:
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2465
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2466
  @{ML_type_ind [display, gray] "Pretty.T"}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2467
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2468
  The function @{ML str in Pretty} transforms a (plain) string into such a pretty 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2469
  type. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2470
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2471
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2472
  \<open>Pretty.str "test"\<close> \<open>String ("test", 4)\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2473
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2474
  where the result indicates that we transformed a string with length 4. Once
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2475
  you have a pretty type, you can, for example, control where linebreaks may
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2476
  occur in case the text wraps over a line, or with how much indentation a
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2477
  text should be printed.  However, if you want to actually output the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2478
  formatted text, you have to transform the pretty type back into a @{ML_type
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2479
  string}. This can be done with the function @{ML_ind  string_of in Pretty}. In what
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2480
  follows we will use the following wrapper function for printing a pretty
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2481
  type:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2482
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2483
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2484
ML %grayML\<open>fun pprint prt = tracing (Pretty.string_of prt)\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2485
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2486
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2487
  The point of the pretty-printing infrastructure is to give hints about how to
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2488
  layout text and let Isabelle do the actual layout. Let us first explain
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2489
  how you can insert places where a line break can occur. For this assume the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2490
  following function that replicates a string n times:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2491
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2492
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2493
ML %grayML\<open>fun rep n str = implode (replicate n str)\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2494
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2495
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2496
  and suppose we want to print out the string:
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2497
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2498
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2499
ML %grayML\<open>val test_str = rep 8 "fooooooooooooooobaaaaaaaaaaaar "\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2500
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2501
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2502
  We deliberately chose a large string so that it spans over more than one line. 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2503
  If we print out the string using the usual ``quick-and-dirty'' method, then
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2504
  we obtain the ugly output:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2505
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2506
@{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2507
\<open>tracing test_str\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2508
\<open>fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar foooooooooo
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2509
ooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaa
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2510
aaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fo
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2511
oooooooooooooobaaaaaaaaaaaar\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2512
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2513
  We obtain the same if we just use the function @{ML pprint}.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2514
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2515
@{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2516
\<open>pprint (Pretty.str test_str)\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2517
\<open>fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar foooooooooo
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2518
ooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaa
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2519
aaaaaaar fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar fo
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2520
oooooooooooooobaaaaaaaaaaaar\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2521
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2522
  However by using pretty types you have the ability to indicate possible
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2523
  linebreaks for example at each whitespace. You can achieve this with the
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2524
  function @{ML_ind breaks in Pretty}, which expects a list of pretty types
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2525
  and inserts a possible line break in between every two elements in this
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2526
  list. To print this list of pretty types as a single string, we concatenate
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2527
  them with the function @{ML_ind blk in Pretty} as follows:
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2528
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2529
@{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2530
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2531
  val ptrs = map Pretty.str (space_explode " " test_str)
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2532
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2533
  pprint (Pretty.blk (0, Pretty.breaks ptrs))
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2534
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2535
\<open>fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2536
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2537
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2538
fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2539
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2540
  Here the layout of @{ML test_str} is much more pleasing to the 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2541
  eye. The @{ML \<open>0\<close>} in @{ML_ind  blk in Pretty} stands for no hanging 
360
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
  2542
  indentation of the printed string. You can increase the indentation 
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
  2543
  and obtain
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2544
  
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2545
@{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2546
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2547
  val ptrs = map Pretty.str (space_explode " " test_str)
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2548
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2549
  pprint (Pretty.blk (3, Pretty.breaks ptrs))
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2550
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2551
\<open>fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2552
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2553
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2554
   fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2555
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2556
  where starting from the second line the indent is 3. If you want
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2557
  that every line starts with the same indent, you can use the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2558
  function @{ML_ind  indent in Pretty} as follows:
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2559
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2560
@{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2561
\<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2562
  val ptrs = map Pretty.str (space_explode " " test_str)
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2563
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2564
  pprint (Pretty.indent 10 (Pretty.blk (0, Pretty.breaks ptrs)))
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2565
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2566
\<open>          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2567
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2568
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2569
          fooooooooooooooobaaaaaaaaaaaar fooooooooooooooobaaaaaaaaaaaar\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2570
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2571
  If you want to print out a list of items separated by commas and 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2572
  have the linebreaks handled properly, you can use the function 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2573
  @{ML_ind  commas in Pretty}. For example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2574
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2575
@{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2576
\<open>let
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2577
  val ptrs = map (Pretty.str o string_of_int) (99998 upto 100020)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2578
in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2579
  pprint (Pretty.blk (0, Pretty.commas ptrs))
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2580
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2581
\<open>99998, 99999, 100000, 100001, 100002, 100003, 100004, 100005, 100006, 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2582
100007, 100008, 100009, 100010, 100011, 100012, 100013, 100014, 100015, 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2583
100016, 100017, 100018, 100019, 100020\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2584
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2585
  where @{ML upto} generates a list of integers. You can print out this
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2586
  list as a ``set'', that means enclosed inside @{text [quotes] "{"} and
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2587
  @{text [quotes] "}"}, and separated by commas using the function
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2588
  @{ML_ind  enum in Pretty}. For example
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2589
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2590
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2591
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2592
  
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2593
@{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2594
\<open>let
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2595
  val ptrs = map (Pretty.str o string_of_int) (99998 upto 100020)
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2596
in
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2597
  pprint (Pretty.enum "," "{" "}" ptrs)
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2598
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2599
\<open>{99998, 99999, 100000, 100001, 100002, 100003, 100004, 100005, 100006, 
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2600
  100007, 100008, 100009, 100010, 100011, 100012, 100013, 100014, 100015, 
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2601
  100016, 100017, 100018, 100019, 100020}\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2602
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2603
  As can be seen, this function prints out the ``set'' so that starting 
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2604
  from the second, each new line has an indentation of 2.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2605
  
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2606
  If you print out something that goes beyond the capabilities of the
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2607
  standard functions, you can do relatively easily the formatting
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2608
  yourself. Assume you want to print out a list of items where like in ``English''
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2609
  the last two items are separated by @{text [quotes] "and"}. For this you can
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2610
  write the function
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2611
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2612
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2613
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2614
ML %linenosgray\<open>fun and_list [] = []
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2615
  | and_list [x] = [x]
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2616
  | and_list xs = 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2617
      let 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2618
        val (front, last) = split_last xs
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2619
      in
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2620
        (Pretty.commas front) @ 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2621
        [Pretty.brk 1, Pretty.str "and", Pretty.brk 1, last]
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2622
      end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2623
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2624
text \<open>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2625
  where Line 7 prints the beginning of the list and Line
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2626
  8 the last item. We have to use @{ML \<open>Pretty.brk 1\<close>} in order
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2627
  to insert a space (of length 1) before the 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2628
  @{text [quotes] "and"}. This space is also a place where a line break 
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2629
  can occur. We do the same after the @{text [quotes] "and"}. This gives you
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2630
  for example
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2631
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2632
@{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2633
\<open>let
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2634
  val ptrs1 = map (Pretty.str o string_of_int) (1 upto 22)
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2635
  val ptrs2 = map (Pretty.str o string_of_int) (10 upto 28)
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2636
in
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2637
  pprint (Pretty.blk (0, and_list ptrs1));
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2638
  pprint (Pretty.blk (0, and_list ptrs2))
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2639
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2640
\<open>1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2641
and 22
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2642
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27 and
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2643
28\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2644
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2645
  Like @{ML blk in Pretty}, the function @{ML_ind chunks in Pretty} prints out 
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2646
  a list of items, but automatically inserts forced breaks between each item.
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2647
  Compare
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2648
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2649
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2650
  \<open>let
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2651
  val a_and_b = [Pretty.str "a", Pretty.str "b"]
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2652
in
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2653
  pprint (Pretty.blk (0, a_and_b));
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2654
  pprint (Pretty.chunks a_and_b)
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2655
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2656
\<open>ab
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2657
a
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2658
b\<close>}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2659
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2660
  The function @{ML_ind big_list in Pretty} helps with printing long lists.
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2661
  It is used for example in the command \isacommand{print\_theorems}. An
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2662
  example is as follows.
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2663
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2664
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2665
  \<open>let
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2666
  val pstrs = map (Pretty.str o string_of_int) (4 upto 10)
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2667
in
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2668
  pprint (Pretty.big_list "header" pstrs)
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2669
end\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2670
  \<open>header
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2671
  4
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2672
  5
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2673
  6
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2674
  7
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2675
  8
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2676
  9
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2677
  10\<close>}
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2678
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2679
  The point of the pretty-printing functions is to conveninetly obtain 
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2680
  a lay-out of terms and types that is pleasing to the eye. If we print
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2681
  out the the terms produced by the the function @{ML de_bruijn} from 
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2682
  exercise~\ref{ex:debruijn} we obtain the following: 
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2683
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2684
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2685
  \<open>pprint (Syntax.pretty_term  @{context} (de_bruijn 4))\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2686
  \<open>(P 3 = P 4 \<longrightarrow> P 4 \<and> P 3 \<and> P 2 \<and> P 1) \<and>
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2687
(P 2 = P 3 \<longrightarrow> P 4 \<and> P 3 \<and> P 2 \<and> P 1) \<and>
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2688
(P 1 = P 2 \<longrightarrow> P 4 \<and> P 3 \<and> P 2 \<and> P 1) \<and> 
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2689
(P 1 = P 4 \<longrightarrow> P 4 \<and> P 3 \<and> P 2 \<and> P 1) \<longrightarrow>
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2690
P 4 \<and> P 3 \<and> P 2 \<and> P 1\<close>}
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2691
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2692
  We use the function @{ML_ind pretty_term in Syntax} for pretty-printing
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2693
  terms. Next we like to pretty-print a term and its type. For this we use the
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2694
  function \<open>tell_type\<close>.
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2695
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2696
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2697
ML %linenosgray\<open>fun tell_type ctxt trm = 
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2698
let
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2699
  fun pstr s = Pretty.breaks (map Pretty.str (space_explode " " s))
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2700
  val ptrm = Pretty.quote (Syntax.pretty_term ctxt trm)
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2701
  val pty  = Pretty.quote (Syntax.pretty_typ ctxt (fastype_of trm))
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2702
in
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2703
  pprint (Pretty.blk (0, 
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2704
    (pstr "The term " @ [ptrm] @ pstr " has type " 
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2705
      @ [pty, Pretty.str "."])))
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2706
end\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2707
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2708
text \<open>
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2709
  In Line 3 we define a function that inserts possible linebreaks in places
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2710
  where a space is. In Lines 4 and 5 we pretty-print the term and its type
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2711
  using the functions @{ML pretty_term in Syntax} and @{ML_ind 
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2712
  pretty_typ in Syntax}. We also use the function @{ML_ind quote in
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2713
  Pretty} in order to enclose the term and type inside quotation marks. In
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2714
  Line 9 we add a period right after the type without the possibility of a
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2715
  line break, because we do not want that a line break occurs there.
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2716
  Now you can write
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2717
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2718
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2719
  \<open>tell_type @{context} @{term "min (Suc 0)"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2720
  \<open>The term "min (Suc 0)" has type "nat \<Rightarrow> nat".\<close>}
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2721
  
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2722
  To see the proper line breaking, you can try out the function on a bigger term 
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
  2723
  and type. For example:
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2724
567
f7c97e64cc2a tuned ML-antiquotations; added intro portions.
Norbert Schirmer <norbert.schirmer@web.de>
parents: 566
diff changeset
  2725
  @{ML_matchresult_fake [display,gray]
569
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2726
  \<open>tell_type @{context} @{term "(=) ((=) ((=) ((=) ((=) (=)))))"}\<close>
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2727
  \<open>The term "(=) ((=) ((=) ((=) ((=) (=)))))" has type 
f875a25aa72d prefer cartouches over " in ML antiquotations
Norbert Schirmer <norbert.schirmer@web.de>
parents: 567
diff changeset
  2728
"((((('a \<Rightarrow> 'a \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool) \<Rightarrow> bool".\<close>}
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2729
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2730
  \begin{readmore}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2731
  The general infrastructure for pretty-printing is implemented in the file
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2732
  @{ML_file "Pure/General/pretty.ML"}. The file @{ML_file "Pure/Syntax/syntax.ML"}
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2733
  contains pretty-printing functions for terms, types, theorems and so on.
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2734
  
505
2862dacb04aa updated to Isabelle 30 November
Christian Urban <urbanc@in.tum.de>
parents: 502
diff changeset
  2735
  @{ML_file "Pure/PIDE/markup.ML"}
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
  2736
  \end{readmore}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2737
\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2738
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2739
section \<open>Summary\<close>
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2740
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2741
text \<open>
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2742
  \begin{conventions}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2743
  \begin{itemize}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2744
  \item Start with a proper context and then pass it around 
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2745
  through all your functions.
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2746
  \end{itemize}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
  2747
  \end{conventions}
565
cecd7a941885 isabelle update_cartouches -t
Norbert Schirmer <norbert.schirmer@web.de>
parents: 562
diff changeset
  2748
\<close>
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2749
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
  2750
end