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