ProgTutorial/Advanced.thy
author Christian Urban <urbanc@in.tum.de>
Sat, 21 Jan 2012 15:18:38 +0000
changeset 508 633d3f013be2
parent 506 caa733190454
child 514 7e25716c3744
permissions -rw-r--r--
merged
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 Advanced
441
Christian Urban <urbanc@in.tum.de>
parents: 414
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 
395
2c392f61f400 spilt the Essential's chapter
Christian Urban <urbanc@in.tum.de>
parents: 394
diff changeset
     8
  "Advanced_Code.thy"
441
Christian Urban <urbanc@in.tum.de>
parents: 414
diff changeset
     9
  ["theory Advanced", "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
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
    14
chapter {* Advanced Isabelle\label{chp:advanced} *}
381
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
    15
97518188ef0e added more to the unification section
Christian Urban <urbanc@in.tum.de>
parents: 380
diff changeset
    16
text {*
410
Christian Urban <urbanc@in.tum.de>
parents: 408
diff changeset
    17
   \begin{flushright}
Christian Urban <urbanc@in.tum.de>
parents: 408
diff changeset
    18
  {\em All things are difficult before they are easy.} \\[1ex]
Christian Urban <urbanc@in.tum.de>
parents: 408
diff changeset
    19
  proverb
Christian Urban <urbanc@in.tum.de>
parents: 408
diff changeset
    20
  \end{flushright}
Christian Urban <urbanc@in.tum.de>
parents: 408
diff changeset
    21
Christian Urban <urbanc@in.tum.de>
parents: 408
diff changeset
    22
  \medskip
400
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
    23
  While terms, types and theorems are the most basic data structures in
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
    24
  Isabelle, there are a number of layers built on top of them. Most of these
408
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    25
  layers are concerned with storing and manipulating data. Handling them
487
1c4250bc6258 more on theories
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    26
  properly is an essential skill for programming on the ML-level of Isabelle. 
1c4250bc6258 more on theories
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    27
  The most basic layer are theories. They contain global data and
408
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    28
  can be seen as the ``long-term memory'' of Isabelle. There is usually only
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    29
  one theory active at each moment. Proof contexts and local theories, on the
400
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
    30
  other hand, store local data for a task at hand. They act like the
408
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    31
  ``short-term memory'' and there can be many of them that are active in
Christian Urban <urbanc@in.tum.de>
parents: 401
diff changeset
    32
  parallel.
318
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
506
Christian Urban <urbanc@in.tum.de>
parents: 502
diff changeset
    35
section {* Theories\label{sec:theories} *}
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    36
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    37
text {*
492
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
    38
  Theories, as said above, are the most basic layer of abstraction in
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
    39
  Isabelle. They record information about definitions, syntax declarations, axioms,
488
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    40
  theorems and much more.  For example, if a definition is made, it
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    41
  must be stored in a theory in order to be usable later on. Similar
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    42
  with proofs: once a proof is finished, the proved theorem needs to
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    43
  be stored in the theorem database of the theory in order to be
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
    44
  usable. All relevant data of a theory can be queried with the
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
    45
  Isabelle command \isacommand{print\_theory}.
358
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    46
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    47
  \begin{isabelle}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    48
  \isacommand{print\_theory}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    49
  @{text "> names: Pure Code_Generator HOL \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    50
  @{text "> classes: Inf < type \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    51
  @{text "> default sort: type"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    52
  @{text "> syntactic types: #prop \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    53
  @{text "> logical types: 'a \<times> 'b \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    54
  @{text "> type arities: * :: (random, random) random \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    55
  @{text "> logical constants: == :: 'a \<Rightarrow> 'a \<Rightarrow> prop \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    56
  @{text "> abbreviations: \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    57
  @{text "> axioms: \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    58
  @{text "> oracles: \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    59
  @{text "> definitions: \<dots>"}\\
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    60
  @{text "> theorems: \<dots>"}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    61
  \end{isabelle}
9cf3bc448210 some polishing
Christian Urban <urbanc@in.tum.de>
parents: 356
diff changeset
    62
487
1c4250bc6258 more on theories
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    63
  Functions acting on theories often end with the suffix @{text "_global"},
1c4250bc6258 more on theories
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    64
  for example the function @{ML read_term_global in Syntax} in the structure
1c4250bc6258 more on theories
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    65
  @{ML_struct Syntax}. The reason is to set them syntactically apart from
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
    66
  functions acting on contexts or local theories, which will be discussed in
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
    67
  the next sections. There is a tendency amongst Isabelle developers to prefer
487
1c4250bc6258 more on theories
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    68
  ``non-global'' operations, because they have some advantages, as we will also
490
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
    69
  discuss later. However, some basic understanding of theories is still necessary
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
    70
  for effective Isabelle programming.
487
1c4250bc6258 more on theories
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
    71
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
    72
  An important Isabelle command with theories is \isacommand{setup}. In the
492
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
    73
  previous chapters we used it already to make a theorem attribute known
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
    74
  to Isabelle and to register a theorem under a name. What happens behind the
490
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
    75
  scenes is that \isacommand{setup} expects a function of type @{ML_type
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
    76
  "theory -> theory"}: the input theory is the current theory and the output
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
    77
  the theory where the attribute has been registered or the theorem has been
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
    78
  stored.  This is a fundamental principle in Isabelle. A similar situation
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
    79
  arises with declaring a constant, which can be done on the ML-level with 
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
    80
  function @{ML_ind declare_const in Sign} from the structure @{ML_struct
490
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
    81
  Sign}. To see how \isacommand{setup} works, consider the following code:
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
    82
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
    83
*}  
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
    84
485
f3536f0b47a9 added section about testboard
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
    85
ML{*let
f3536f0b47a9 added section about testboard
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
    86
  val thy = @{theory}
f3536f0b47a9 added section about testboard
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
    87
  val bar_const = ((@{binding "BAR"}, @{typ "nat"}), NoSyn)
f3536f0b47a9 added section about testboard
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
    88
in 
f3536f0b47a9 added section about testboard
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
    89
  Sign.declare_const @{context} bar_const thy  
f3536f0b47a9 added section about testboard
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
    90
end*}
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
    91
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
    92
text {*
488
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    93
  If you simply run this code\footnote{Recall that ML-code needs to be enclosed in
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    94
  \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}.} with the
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
    95
  intention of declaring a constant @{text "BAR"} having type @{typ nat}, then 
488
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
    96
  indeed you obtain a theory as result. But if you query the
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
    97
  constant on the Isabelle level using the command \isacommand{term}
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
    98
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
    99
  \begin{isabelle}
485
f3536f0b47a9 added section about testboard
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
   100
  \isacommand{term}~@{text BAR}\\
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   101
  @{text "> \"BAR\" :: \"'a\""}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   102
  \end{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   103
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   104
  you can see that you do \emph{not} obtain the expected constant of type @{typ nat}, but a free 
484
490fe9483c0d more material
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   105
  variable (printed in blue) of polymorphic type. The problem is that the 
490fe9483c0d more material
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   106
  ML-expression above did not ``register'' the declaration with the current theory. 
490fe9483c0d more material
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   107
  This is what the command \isacommand{setup} is for. The constant is properly 
490fe9483c0d more material
Christian Urban <urbanc@in.tum.de>
parents: 481
diff changeset
   108
  declared with
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   109
*}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   110
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   111
setup %gray {* fn thy => 
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   112
let
485
f3536f0b47a9 added section about testboard
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
   113
  val bar_const = ((@{binding "BAR"}, @{typ "nat"}), NoSyn)
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   114
  val (_, thy') = Sign.declare_const @{context} bar_const thy
485
f3536f0b47a9 added section about testboard
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
   115
in 
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   116
  thy'
485
f3536f0b47a9 added section about testboard
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
   117
end *}
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   118
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   119
text {* 
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   120
  where the declaration is actually applied to the current theory and
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   121
  
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   122
  \begin{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   123
  \isacommand{term}~@{text [quotes] "BAR"}\\
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   124
  @{text "> \"BAR\" :: \"nat\""}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   125
  \end{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   126
492
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
   127
  now returns a (black) constant with the type @{typ nat}, as expected.
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   128
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   129
  In a sense, \isacommand{setup} can be seen as a transaction that
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   130
  takes the current theory @{text thy}, applies an operation, and
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   131
  produces a new current theory @{text thy'}. This means that we have
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   132
  to be careful to apply operations always to the most current theory,
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   133
  not to a \emph{stale} one. Consider again the function inside the
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   134
  \isacommand{setup}-command:
485
f3536f0b47a9 added section about testboard
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
   135
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   136
  \begin{isabelle}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   137
  \begin{graybox}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   138
  \isacommand{setup}~@{text "\<verbopen>"} @{ML
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   139
"fn thy => 
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   140
let
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   141
  val bar_const = ((@{binding \"BAR\"}, @{typ \"nat\"}), NoSyn)
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   142
  val (_, thy') = Sign.declare_const @{context} bar_const thy
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   143
in
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   144
  thy
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   145
end"}~@{text "\<verbclose>"}\isanewline
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   146
  @{text "> ERROR: \"Stale theory encountered\""}
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   147
  \end{graybox}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   148
  \end{isabelle}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   149
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   150
  This time we erroneously return the original theory @{text thy}, instead of
488
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   151
  the modified one @{text thy'}. Such buggy code will always result into 
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   152
  a runtime error message about stale theories.
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   153
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   154
  However, sometimes it does make sense to work with two theories at the same
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   155
  time, especially in the context of parsing and typing. In the code below we
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   156
  use in Line 3 the function @{ML_ind copy in Theory} from the structure
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   157
  @{ML_struct Theory} for obtaining a new theory that contains the same
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   158
  data, but is unrelated to the existing theory.
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   159
*}
485
f3536f0b47a9 added section about testboard
Christian Urban <urbanc@in.tum.de>
parents: 484
diff changeset
   160
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   161
setup %graylinenos {* fn thy => 
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   162
let
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   163
  val tmp_thy = Theory.copy thy
498
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   164
  val foo_const = ((@{binding "FOO"}, @{typ "nat \<Rightarrow> nat"}), NoSyn)
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   165
  val (_, tmp_thy') = Sign.declare_const @{context} foo_const tmp_thy
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   166
  val trm1 = Syntax.read_term_global tmp_thy' "FOO baz"
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   167
  val trm2 = Syntax.read_term_global thy "FOO baz"
502
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   168
  val _ = pwriteln 
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   169
    (Pretty.str (@{make_string} trm1 ^ "\n" ^ @{make_string} trm2))
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   170
in
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   171
  thy
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   172
end *}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   173
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   174
text {*
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   175
  That means we can make changes to the theory @{text tmp_thy} without
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   176
  affecting the current theory @{text thy}. In this case we declare in @{text
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   177
  "tmp_thy"} the constant @{text FOO} (Lines 4 and 5). The point of this code
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   178
  is that we next, in Lines 6 and 7, parse a string to become a term (both
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   179
  times the string is @{text [quotes] "FOO baz"}). But since we parse the string
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   180
  once in the context of the theory @{text tmp_thy'} in which @{text FOO} is
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   181
  declared to be a constant of type @{typ "nat \<Rightarrow>nat"} and once in the context 
488
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   182
  of @{text thy} where it is not, we obtain two different terms, namely 
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   183
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   184
  \begin{isabelle}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   185
  \begin{graybox}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   186
  @{text "> Const (\"Advanced.FOO\", \"nat \<Rightarrow> nat\") $ Free (\"baz\", \"nat\")"}\isanewline
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   187
  @{text "> Free (\"FOO\", \"'a \<Rightarrow> 'b\") $ Free (\"baz\", \"'a\")"}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   188
  \end{graybox}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   189
  \end{isabelle}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   190
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   191
  There are two reasons for parsing a term in a temporary theory. One is to
488
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   192
  obtain fully qualified names for constants and the other is appropriate type 
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   193
  inference. This is relevant in situations where definitions are made later, 
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   194
  but parsing and type inference has to take already proceed as if the definitions 
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   195
  were already made.
502
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   196
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   197
  \begin{readmore}
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   198
  Most of the functions about theories are implemented in
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   199
  @{ML_file "Pure/theory.ML"} and @{ML_file "Pure/global_theory.ML"}.
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   200
  \end{readmore}
348
Christian Urban <urbanc@in.tum.de>
parents: 347
diff changeset
   201
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   202
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   203
section {* Contexts *}
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   204
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   205
text {*
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   206
  Contexts are arguably more important than theories, even though they only 
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   207
  contain ``short-term memory data''. The reason is that a vast number of
490
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   208
  functions in Isabelle depend in one way or another on contexts. Even such
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   209
  mundane operations like printing out a term make essential use of contexts.
494
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   210
  For this consider the following contrived proof-snippet whose purpose is to 
490
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   211
  fix two variables:
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   212
*}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   213
488
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   214
lemma "True"
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   215
proof -
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   216
  txt_raw {*\mbox{}\\[-7mm]*} 
492
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
   217
  ML_prf {* Variable.dest_fixes @{context} *} 
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   218
  txt_raw {*\mbox{}\\[-7mm]\mbox{}*}
492
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
   219
 fix x y  
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   220
  txt_raw {*\mbox{}\\[-7mm]*}
488
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   221
  ML_prf {* Variable.dest_fixes @{context} *} 
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   222
  txt_raw {*\mbox{}\\[-7mm] \ldots*}(*<*)oops(*>*)
488
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   223
490
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   224
text {*
496
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   225
  The interesting point is that we injected ML-code before and after
490
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   226
  the variables are fixed. For this remember that ML-code inside a proof
494
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   227
  needs to be enclosed inside \isacommand{ML\_prf}~@{text "\<verbopen> \<dots> \<verbclose>"},
490
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   228
  not \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}. The function 
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   229
  @{ML_ind dest_fixes in Variable} from the structure @{ML_struct Variable} takes 
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   230
  a context and returns all its currently fixed variable (names). That 
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   231
  means a context has a dataslot containing information about fixed variables.
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   232
  The ML-antiquotation @{text "@{context}"} points to the context that is
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   233
  active at that point of the theory. Consequently, in the first call to 
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   234
  @{ML dest_fixes in Variable} this dataslot is  empty; in the second it is 
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   235
  filled with @{text x} and @{text y}. What is interesting is that contexts
494
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   236
  can be stacked. For this consider the following proof fragment:
490
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   237
*}
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   238
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   239
lemma "True"
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   240
proof -
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   241
  fix x y
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   242
  { fix z w
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   243
  txt_raw {*\mbox{}\\[-7mm]*}
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   244
  ML_prf {* Variable.dest_fixes @{context} *} 
492
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
   245
  txt_raw {*\mbox{}\\[-7mm]\mbox{}*}
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
   246
 }
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   247
  txt_raw {*\mbox{}\\[-7mm]*}
490
Christian Urban <urbanc@in.tum.de>
parents: 488
diff changeset
   248
  ML_prf {* Variable.dest_fixes @{context} *} 
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   249
  txt_raw {*\mbox{}\\[-7mm] \ldots*}(*<*)oops(*>*)
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   250
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   251
text {*
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   252
  Here the first time we call @{ML dest_fixes in Variable} we have four fixes variables;
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   253
  the second time we get only the fixes variables @{text x} and @{text y} as answer, since
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   254
  @{text z} and @{text w} are not anymore in the scope of the context. 
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   255
  This means the curly-braces act on the Isabelle level as opening and closing statements 
496
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   256
  for a context. The above proof fragment corresponds roughly to the following ML-code
491
Christian Urban <urbanc@in.tum.de>
parents: 490
diff changeset
   257
*}
488
780100cd4060 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 487
diff changeset
   258
492
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
   259
ML{*val ctxt0 = @{context};
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
   260
val ([x, y], ctxt1) = Variable.add_fixes ["x", "y"] ctxt0;
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
   261
val ([z, w], ctxt2) = Variable.add_fixes ["z", "w"] ctxt1*}
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
   262
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
   263
text {*
494
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   264
  where the function @{ML_ind add_fixes in Variable} fixes a list of variables
Christian Urban <urbanc@in.tum.de>
parents: 493
diff changeset
   265
  specified by strings. Let us come back to the point about printing terms. Consider
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   266
  printing out the term \mbox{@{text "(x, y, z, w)"}} using our function @{ML_ind pretty_term}.
496
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   267
  This function takes a term and a context as argument. Notice how the printing
498
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   268
  of the term changes according to which context is used.
492
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
   269
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   270
  \begin{isabelle}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   271
  \begin{graybox}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   272
  @{ML "let
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   273
  val trm = @{term \"(x, y, z, w)\"}
493
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   274
in
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   275
  pwriteln (Pretty.chunks 
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   276
    [ pretty_term ctxt0 trm,
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   277
      pretty_term ctxt1 trm,
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   278
      pretty_term ctxt2 trm ])
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   279
end"}\\
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   280
  \setlength{\fboxsep}{0mm}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   281
  @{text ">"}~@{text "("}\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text x}}}@{text ","}~%
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   282
  \colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text y}}}@{text ","}~%
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   283
  \colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text z}}}@{text ","}~%
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   284
  \colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text w}}}@{text ")"}\\
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   285
  @{text ">"}~@{text "("}\colorbox{gray!20}{\raisebox{0mm}[3mm][1mm]{@{text x}}}@{text ","}~%
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   286
  \colorbox{gray!20}{\raisebox{0mm}[3mm][1mm]{@{text y}}}@{text ","}~%
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   287
  \colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text z}}}@{text ","}~%
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   288
  \colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text w}}}@{text ")"}\\
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   289
  @{text ">"}~@{text "("}\colorbox{gray!20}{\raisebox{0mm}[3mm][1mm]{@{text x}}}@{text ","}~%
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   290
  \colorbox{gray!20}{\raisebox{0mm}[3mm][1mm]{@{text y}}}@{text ","}~%
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   291
  \colorbox{gray!20}{\raisebox{0mm}[3mm][1mm]{@{text z}}}@{text ","}~%
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   292
  \colorbox{gray!20}{\raisebox{0mm}[3mm][1mm]{@{text w}}}@{text ")"}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   293
  \end{graybox}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   294
  \end{isabelle}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   295
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   296
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   297
  The term we are printing is in all three cases the same---a tuple containing
496
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   298
  four free variables. As you can see, however, in case of @{ML "ctxt0"} all
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   299
  variables are highlighted indicating a problem, while in case of @{ML
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   300
  "ctxt1"} @{text x} and @{text y} are printed as normal (blue) free
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   301
  variables, but not @{text z} and @{text w}. In the last case all variables
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   302
  are printed as expected. The point of this example is that the context
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   303
  contains the information which variables are fixed, and designates all other
497
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   304
  free variables as being alien or faulty.  Therefore the highlighting. 
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   305
  While this seems like a minor detail, the concept of making the context aware 
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   306
  of fixed variables is actually quite useful. For example it prevents us from 
501
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   307
  fixing a variable twice
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   308
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   309
  @{ML_response_fake [gray, display]
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   310
  "@{context}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   311
|> Variable.add_fixes [\"x\", \"x\"]" 
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   312
  "ERROR: Duplicate fixed variable(s): \"x\""}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   313
501
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   314
  More importantly it also allows us to easily create fresh names for
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   315
  fixed variables.  For this you have to use the function @{ML_ind
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   316
  variant_fixes in Variable} from the structure @{ML_struct Variable}.
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   317
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   318
  @{ML_response_fake [gray, display]
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   319
  "@{context}
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   320
|> Variable.variant_fixes [\"y\", \"y\", \"z\"]" 
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   321
  "([\"y\", \"ya\", \"z\"], ...)"}
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   322
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   323
  Now a fresh variant for the second occurence of @{text y} is created
502
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   324
  avoiding any clash. In this way we can also create fresh free variables
501
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   325
  that avoid any clashes with fixed variables. In Line~3 below we fix
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   326
  the variable @{text x} in the context @{text ctxt1}. Next we want to
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   327
  create two fresh variables of type @{typ nat} as variants of the
f56fc3305a08 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 500
diff changeset
   328
  string @{text [quotes] "x"} (Lines 6 and 7).
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   329
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   330
  @{ML_response_fake [display, gray, linenos]
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   331
  "let
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   332
  val ctxt0 = @{context}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   333
  val (_, ctxt1) = Variable.add_fixes [\"x\"] ctxt0
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   334
  val frees = replicate 2 (\"x\", @{typ nat})
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   335
in
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   336
  (Variable.variant_frees ctxt0 [] frees,
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   337
   Variable.variant_frees ctxt1 [] frees)
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   338
end"
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   339
  "([(\"x\", \"nat\"), (\"xa\", \"nat\")], 
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   340
 [(\"xa\", \"nat\"), (\"xb\", \"nat\")])"}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   341
496
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   342
  As you can see, if we create the fresh variables with the context @{text ctxt0},
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   343
  then we obtain @{text "x"} and @{text "xa"}; but in @{text ctxt1} we obtain @{text "xa"}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   344
  and @{text "xb"} avoiding @{text x}, which was fixed in this context.
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   345
496
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   346
  Often one has the problem that given some terms, create fresh variables
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   347
  avoiding any variable occurring in those terms. For this you can use the
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   348
  function @{ML_ind declare_term in Variable} from the structure @{ML_struct Variable}.
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   349
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   350
  @{ML_response_fake [gray, display]
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   351
  "let
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   352
  val ctxt1 = Variable.declare_term @{term \"(x, xa)\"} @{context}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   353
  val frees = replicate 2 (\"x\", @{typ nat})
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   354
in
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   355
  Variable.variant_frees ctxt1 [] frees
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   356
end"
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   357
  "[(\"xb\", \"nat\"), (\"xc\", \"nat\")]"}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   358
496
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   359
  The result is @{text xb} and @{text xc} for the names of the fresh
498
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   360
  variables, since @{text x} and @{text xa} occur in the term we declared. 
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   361
  Note that @{ML_ind declare_term in Variable} does not fix the
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   362
  variables; it just makes them ``known'' to the context. You can see
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   363
  that if you print out a declared term.
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   364
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   365
  \begin{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   366
  \begin{graybox}
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   367
  @{ML "let
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   368
  val trm = @{term \"P x y z\"}
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   369
  val ctxt1 = Variable.declare_term trm @{context}
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   370
in
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   371
  pwriteln (pretty_term ctxt1 trm)
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   372
end"}\\
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   373
  \setlength{\fboxsep}{0mm}
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   374
  @{text ">"}~\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text P}}}~%
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   375
  \colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text x}}}~%
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   376
  \colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text y}}}~%
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   377
  \colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text z}}}
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   378
  \end{graybox}
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   379
  \end{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   380
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   381
  All variables are highligted, indicating that they are not
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   382
  fixed. However, declaring a term is helpful when parsing terms using
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   383
  the function @{ML_ind read_term in Syntax} from the structure
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   384
  @{ML_struct Syntax}. Consider the following code:
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   385
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   386
  @{ML_response_fake [gray, display]
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   387
  "let
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   388
  val ctxt0 = @{context}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   389
  val ctxt1 = Variable.declare_term @{term \"x::nat\"} ctxt0
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   390
in
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   391
  (Syntax.read_term ctxt0 \"x\", 
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   392
   Syntax.read_term ctxt1 \"x\") 
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   393
end"
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   394
  "(Free (\"x\", \"'a\"), Free (\"x\", \"nat\"))"}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   395
  
496
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   396
  Parsing the string in the context @{text "ctxt0"} results in a free variable 
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   397
  with a default polymorphic type, but in case of @{text "ctxt1"} we obtain a
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   398
  free variable of type @{typ nat} as previously declared. Which
496
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   399
  type the parsed term receives depends upon the last declaration that
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   400
  is made, as the next example illustrates.
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   401
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   402
  @{ML_response_fake [gray, display]
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   403
  "let
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   404
  val ctxt1 = Variable.declare_term @{term \"x::nat\"} @{context}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   405
  val ctxt2 = Variable.declare_term @{term \"x::int\"} ctxt1
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   406
in
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   407
  (Syntax.read_term ctxt1 \"x\", 
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   408
   Syntax.read_term ctxt2 \"x\") 
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   409
end"
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   410
  "(Free (\"x\", \"nat\"), Free (\"x\", \"int\"))"}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   411
499
42bac8b16951 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 498
diff changeset
   412
  The most useful feature of contexts is that one can export, or transfer, 
42bac8b16951 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 498
diff changeset
   413
  terms and theorems between them. We show this first for terms. 
497
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   414
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   415
  \begin{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   416
  \begin{graybox}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   417
  \begin{linenos}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   418
  @{ML "let
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   419
  val ctxt0 = @{context}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   420
  val (_, ctxt1) = Variable.add_fixes [\"x\", \"y\", \"z\"] ctxt0 
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   421
  val foo_trm = @{term \"P x y z\"}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   422
in
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   423
  singleton (Variable.export_terms ctxt1 ctxt0) foo_trm
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   424
  |> pretty_term ctxt0
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   425
  |> pwriteln
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   426
end"}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   427
  \end{linenos}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   428
  \setlength{\fboxsep}{0mm}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   429
  @{text ">"}~\colorbox{gray!5}{\raisebox{0mm}[3mm][1mm]{@{text P}}}~%
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   430
  @{text "?x ?y ?z"}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   431
  \end{graybox}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   432
  \end{isabelle}
Christian Urban <urbanc@in.tum.de>
parents: 496
diff changeset
   433
498
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   434
  In Line 3 we fix the variables @{term x}, @{term y} and @{term z} in
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   435
  context @{text ctxt1}.  The function @{ML_ind export_terms in
500
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   436
  Variable} from the structure @{ML_struct Variable} can be used to transfer
498
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   437
  terms between contexts. Transferring means to turn all (free)
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   438
  variables that are fixed in one context, but not in the other, into
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   439
  schematic variables. In our example, we are transferring the term
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   440
  @{text "P x y z"} from context @{text "ctxt1"} to @{text "ctxt0"},
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   441
  which means @{term x}, @{term y} and @{term z} become schematic
500
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   442
  variables (as can be seen by the leading question marks in the result). 
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   443
  Note that the variable @{text P} stays a free variable, since it not fixed in
498
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   444
  @{text ctxt1}; it is even highlighed, because @{text "ctxt0"} does
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   445
  not know about it. Note also that in Line 6 we had to use the
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   446
  function @{ML_ind singleton}, because the function @{ML_ind
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   447
  export_terms in Variable} normally works over lists of terms.
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   448
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   449
  The case of transferring theorems is even more useful. The reason is
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   450
  that the generalisation of fixed variables to schematic variables is
499
42bac8b16951 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 498
diff changeset
   451
  not trivial if done manually. For illustration purposes we use in the 
42bac8b16951 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 498
diff changeset
   452
  following code the function @{ML_ind make_thm in Skip_Proof} from the 
500
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   453
  structure @{ML_struct Skip_Proof}. This function will turn an arbitray 
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   454
  term, in our case @{term "P x y z x y z"}, into a theorem (disregarding 
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   455
  whether it is actually provable).
498
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   456
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   457
  @{ML_response_fake [display, gray]
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   458
  "let
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   459
  val thy = @{theory}
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   460
  val ctxt0 = @{context}
499
42bac8b16951 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 498
diff changeset
   461
  val (_, ctxt1) = Variable.add_fixes [\"P\", \"x\", \"y\", \"z\"] ctxt0 
498
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   462
  val foo_thm = Skip_Proof.make_thm thy @{prop \"P x y z x y z\"}
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   463
in
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   464
  singleton (Proof_Context.export ctxt1 ctxt0) foo_thm
Christian Urban <urbanc@in.tum.de>
parents: 497
diff changeset
   465
end"
499
42bac8b16951 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 498
diff changeset
   466
  "?P ?x ?y ?z ?x ?y ?z"}
42bac8b16951 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 498
diff changeset
   467
502
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   468
  Since we fixed all variables in @{text ctxt1}, in the exported
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   469
  result all of them are schematic. The great point of contexts is
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   470
  that exporting from one to another is not just restricted to
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   471
  variables, but also works with assumptions. For this we can use the
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   472
  function @{ML_ind export in Assumption} from the structure
500
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   473
  @{ML_struct Assumption}. Consider the following code.
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   474
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   475
  @{ML_response_fake [display, gray, linenos]
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   476
  "let
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   477
  val ctxt0 = @{context}
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   478
  val ([eq], ctxt1) = Assumption.add_assumes [@{cprop \"x \<equiv> y\"}] ctxt0
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   479
  val eq' = Thm.symmetric eq
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   480
in
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   481
  Assumption.export false ctxt1 ctxt0 eq'
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   482
end"
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   483
  "x \<equiv> y \<Longrightarrow> y \<equiv> x"}
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   484
  
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   485
  The function @{ML_ind add_assumes in Assumption} from the structure
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   486
  @{ML_struct Assumption} adds the assumption \mbox{@{text "x \<equiv> y"}}
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   487
  to the context @{text ctxt1} (Line 3). This function expects a list
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   488
  of @{ML_type cterm}s and returns them as theorems, together with the
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   489
  new context in which they are ``assumed''. In Line 4 we use the
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   490
  function @{ML_ind symmetric in Thm} from the structure @{ML_struct
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   491
  Thm} in order to obtain the symmetric version of the assumed
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   492
  meta-equality. Now exporting the theorem @{text "eq'"} from @{text
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   493
  ctxt1} to @{text ctxt0} means @{term "y \<equiv> x"} will be prefixed with
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   494
  the assumed theorem. The boolean flag in @{ML_ind export in
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   495
  Assumption} indicates whether the assumptions should be marked with
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   496
  the goal marker (see Section~\ref{sec:basictactics}). In normal
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   497
  circumstances this is not necessary and so should be set to @{ML
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   498
  false}.  The result of the export is then the theorem \mbox{@{term
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   499
  "x \<equiv> y \<Longrightarrow> y \<equiv> x"}}.  As can be seen this is an easy way for obtaing
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   500
  simple theorems. We will explain this in more detail in
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   501
  Section~\ref{sec:structured}.
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   502
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   503
  The function @{ML_ind export in Proof_Context} from the structure 
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   504
  @{ML_struct Proof_Context} combines both export functions from 
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   505
  @{ML_struct Variable} and @{ML_struct Assumption}. This can be seen
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   506
  in the following example.
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   507
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   508
  @{ML_response_fake [display, gray]
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   509
  "let
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   510
  val ctxt0 = @{context}
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   511
  val ((fvs, [eq]), ctxt1) = ctxt0
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   512
    |> Variable.add_fixes [\"x\", \"y\"]
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   513
    ||>> Assumption.add_assumes [@{cprop \"x \<equiv> y\"}]
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   514
  val eq' = Thm.symmetric eq
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   515
in
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   516
  Proof_Context.export ctxt1 ctxt0 [eq']
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   517
end"
6cfde4ff13e3 more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
   518
  "[?x \<equiv> ?y \<Longrightarrow> ?y \<equiv> ?x]"}
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   519
*}
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   520
496
Christian Urban <urbanc@in.tum.de>
parents: 495
diff changeset
   521
493
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   522
495
f3f24874e8be more on contexts
Christian Urban <urbanc@in.tum.de>
parents: 494
diff changeset
   523
text {*
493
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   524
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   525
*}
Christian Urban <urbanc@in.tum.de>
parents: 492
diff changeset
   526
492
Christian Urban <urbanc@in.tum.de>
parents: 491
diff changeset
   527
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   528
(*
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   529
ML{*Proof_Context.debug := true*}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   530
ML{*Proof_Context.verbose := true*}
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   531
*)
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   532
487
1c4250bc6258 more on theories
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   533
(*
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   534
lemma "True"
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   535
proof -
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   536
  { -- "\<And>x. _"
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   537
    fix x
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   538
    have "B x" sorry
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   539
    thm this
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   540
  }
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   541
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   542
  thm this
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   543
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   544
  { -- "A \<Longrightarrow> _"
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   545
    assume A
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   546
    have B sorry
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   547
    thm this
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   548
  }
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   549
   
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   550
  thm this
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   551
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   552
  { -- "\<And>x. x = _ \<Longrightarrow> _"
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   553
    def x \<equiv> a
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   554
    have "B x" sorry
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   555
  }
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   556
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   557
  thm this
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   558
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   559
oops
487
1c4250bc6258 more on theories
Christian Urban <urbanc@in.tum.de>
parents: 486
diff changeset
   560
*)
413
95461cf6fd07 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 410
diff changeset
   561
502
Christian Urban <urbanc@in.tum.de>
parents: 501
diff changeset
   562
section {* Local Theories and Local Setups\label{sec:local} (TBD) *}
341
62dea749d5ed more work on theorem section
Christian Urban <urbanc@in.tum.de>
parents: 340
diff changeset
   563
394
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   564
text {*
400
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
   565
  In contrast to an ordinary theory, which simply consists of a type
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
   566
  signature, as well as tables for constants, axioms and theorems, a local
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
   567
  theory contains additional context information, such as locally fixed
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
   568
  variables and local assumptions that may be used by the package. The type
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
   569
  @{ML_type local_theory} is identical to the type of \emph{proof contexts}
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
   570
  @{ML_type "Proof.context"}, although not every proof context constitutes a
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
   571
  valid local theory.
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
   572
Christian Urban <urbanc@in.tum.de>
parents: 396
diff changeset
   573
  @{ML "Context.>> o Context.map_theory"}
394
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   574
  @{ML_ind "Local_Theory.declaration"}
486
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   575
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   576
   A similar command is \isacommand{local\_setup}, which expects a function
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   577
  of type @{ML_type "local_theory -> local_theory"}. Later on we will also
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   578
  use the commands \isacommand{method\_setup} for installing methods in the
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   579
  current theory and \isacommand{simproc\_setup} for adding new simprocs to
45cfd2ece7bd a section about theories and setups
Christian Urban <urbanc@in.tum.de>
parents: 485
diff changeset
   580
  the current simpset.
394
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   581
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   582
336
a12bb28fe2bd polished on the pretty printing section
Christian Urban <urbanc@in.tum.de>
parents: 335
diff changeset
   583
394
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   584
section {* Morphisms (TBD) *}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   585
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   586
text {*
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   587
  Morphisms are arbitrary transformations over terms, types, theorems and bindings.
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   588
  They can be constructed using the function @{ML_ind morphism in Morphism},
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   589
  which expects a record with functions of type
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   590
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   591
  \begin{isabelle}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   592
  \begin{tabular}{rl}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   593
  @{text "binding:"} & @{text "binding -> binding"}\\
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   594
  @{text "typ:"}     & @{text "typ -> typ"}\\
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   595
  @{text "term:"}    & @{text "term -> term"}\\
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   596
  @{text "fact:"}    & @{text "thm list -> thm list"}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   597
  \end{tabular}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   598
  \end{isabelle}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   599
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   600
  The simplest morphism is the  @{ML_ind identity in Morphism}-morphism defined as
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   601
*}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   602
481
32323727af96 updated
Christian Urban <urbanc@in.tum.de>
parents: 475
diff changeset
   603
ML{*val identity = Morphism.morphism {binding = [], typ = [], term = [], fact = []}*}
394
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   604
  
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   605
text {*
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   606
  Morphisms can be composed with the function @{ML_ind "$>" in Morphism}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   607
*}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   608
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   609
ML{*fun trm_phi (Free (x, T)) = Var ((x, 0), T) 
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   610
  | trm_phi (Abs (x, T, t)) = Abs (x, T, trm_phi t)
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   611
  | trm_phi (t $ s) = (trm_phi t) $ (trm_phi s)
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   612
  | trm_phi t = t*}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   613
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   614
ML{*val phi = Morphism.term_morphism trm_phi*}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   615
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   616
ML{*Morphism.term phi @{term "P x y"}*}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   617
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   618
text {*
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   619
  @{ML_ind term_morphism in Morphism}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   620
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   621
  @{ML_ind term in Morphism},
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   622
  @{ML_ind thm in Morphism}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   623
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   624
  \begin{readmore}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   625
  Morphisms are implemented in the file @{ML_file "Pure/morphism.ML"}.
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   626
  \end{readmore}
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   627
*}
318
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
section {* Misc (TBD) *}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   630
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   631
ML {*Datatype.get_info @{theory} "List.list"*}
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   632
319
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   633
text {* 
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   634
FIXME: association lists:
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   635
@{ML_file "Pure/General/alist.ML"}
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   636
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   637
FIXME: calling the ML-compiler
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   638
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   639
*}
6bce4acf7f2a added file for producing a keyword file
Christian Urban <urbanc@in.tum.de>
parents: 318
diff changeset
   640
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   641
section {* What Is In an Isabelle Name? (TBD) *}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   642
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   643
text {*
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   644
  On the ML-level of Isabelle, you often have to work with qualified names.
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   645
  These are strings with some additional information, such as positional
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   646
  information and qualifiers. Such qualified names can be generated with the
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   647
  antiquotation @{text "@{binding \<dots>}"}. For example
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   648
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   649
  @{ML_response [display,gray]
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   650
  "@{binding \"name\"}"
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   651
  "name"}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   652
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   653
  An example where a qualified name is needed is the function 
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   654
  @{ML_ind define in Local_Theory}.  This function is used below to define 
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   655
  the constant @{term "TrueConj"} as the conjunction @{term "True \<and> True"}.
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   656
*}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   657
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   658
local_setup %gray {* 
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   659
  Local_Theory.define ((@{binding "TrueConj"}, NoSyn), 
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   660
      (Attrib.empty_binding, @{term "True \<and> True"})) #> snd *}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   661
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   662
text {* 
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   663
  Now querying the definition you obtain:
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   664
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   665
  \begin{isabelle}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   666
  \isacommand{thm}~@{text "TrueConj_def"}\\
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   667
  @{text "> "}~@{thm TrueConj_def}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   668
  \end{isabelle}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   669
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   670
  \begin{readmore}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   671
  The basic operations on bindings are implemented in 
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   672
  @{ML_file "Pure/General/binding.ML"}.
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   673
  \end{readmore}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   674
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   675
  \footnote{\bf FIXME give a better example why bindings are important}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   676
  \footnote{\bf FIXME give a pointer to \isacommand{local\_setup}; if not, then explain
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   677
  why @{ML snd} is needed.}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   678
  \footnote{\bf FIXME: There should probably a separate section on binding, long-names
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   679
  and sign.}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   680
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   681
*}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   682
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   683
360
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
   684
ML {* Sign.intern_type @{theory} "list" *}
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
   685
ML {* Sign.intern_const @{theory} "prod_fun" *}
Christian Urban <urbanc@in.tum.de>
parents: 359
diff changeset
   686
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   687
text {*
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   688
  \footnote{\bf FIXME: Explain the following better; maybe put in a separate
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   689
  section and link with the comment in the antiquotation section.}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   690
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   691
  Occasionally you have to calculate what the ``base'' name of a given
462
1d1e795bc3ad updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   692
  constant is. For this you can use the function @{ML_ind  Long_Name.base_name}. For example:
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   693
462
1d1e795bc3ad updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 441
diff changeset
   694
  @{ML_response [display,gray] "Long_Name.base_name \"List.list.Nil\"" "\"Nil\""}
414
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   695
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   696
  \begin{readmore}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   697
  Functions about naming are implemented in @{ML_file "Pure/General/name_space.ML"};
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   698
  functions about signatures in @{ML_file "Pure/sign.ML"}.
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   699
  \end{readmore}
5fc2fb34c323 polished
Christian Urban <urbanc@in.tum.de>
parents: 413
diff changeset
   700
*}
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
   701
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
   702
text {* 
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
   703
  @{ML_ind "Binding.name_of"} returns the string without markup
394
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   704
0019ebf76e10 updated to new Isabelle
Christian Urban <urbanc@in.tum.de>
parents: 393
diff changeset
   705
  @{ML_ind "Binding.conceal"} 
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
   706
*}
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
   707
388
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
   708
section {* Concurrency (TBD) *}
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
   709
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
   710
text {*
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
   711
  @{ML_ind prove_future in Goal}
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
   712
  @{ML_ind future_result in Goal}
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
   713
  @{ML_ind fork_pri in Future}
0b337dedc306 added Skip_Proof.mk_thm and some pointers about concurrency
Christian Urban <urbanc@in.tum.de>
parents: 387
diff changeset
   714
*}
387
5dcee4d751ad completed the unification section
Christian Urban <urbanc@in.tum.de>
parents: 386
diff changeset
   715
396
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
   716
section {* Parse and Print Translations (TBD) *}
Christian Urban <urbanc@in.tum.de>
parents: 395
diff changeset
   717
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
   718
section {* Summary *}
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
   719
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
   720
text {*
395
2c392f61f400 spilt the Essential's chapter
Christian Urban <urbanc@in.tum.de>
parents: 394
diff changeset
   721
  TBD
349
9e374cd891e1 updated to Isabelle changes
Christian Urban <urbanc@in.tum.de>
parents: 348
diff changeset
   722
*}
318
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   723
efb5fff99c96 split up the first-steps section into two chapters
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   724
end