33 *} |
33 *} |
34 |
34 |
35 section {* Theories and Setups\label{sec:theories} *} |
35 section {* Theories and Setups\label{sec:theories} *} |
36 |
36 |
37 text {* |
37 text {* |
38 Theories, as said above, are a basic layer of abstraction in Isabelle. They |
38 Theories, as said above, are a basic layer of abstraction in |
39 contain definitions, syntax declarations, axioms, theorems and much more. If a |
39 Isabelle. They contain definitions, syntax declarations, axioms, |
40 definition is made, it must be stored in a theory in order to be usable |
40 theorems and much more. For example, if a definition is made, it |
41 later on. Similar with proofs: once a proof is finished, the proved theorem |
41 must be stored in a theory in order to be usable later on. Similar |
42 needs to be stored in the theorem database of the theory in order to be |
42 with proofs: once a proof is finished, the proved theorem needs to |
|
43 be stored in the theorem database of the theory in order to be |
43 usable. All relevant data of a theory can be queried as follows. |
44 usable. All relevant data of a theory can be queried as follows. |
44 |
45 |
45 \begin{isabelle} |
46 \begin{isabelle} |
46 \isacommand{print\_theory}\\ |
47 \isacommand{print\_theory}\\ |
47 @{text "> names: Pure Code_Generator HOL \<dots>"}\\ |
48 @{text "> names: Pure Code_Generator HOL \<dots>"}\\ |
60 |
61 |
61 Functions acting on theories often end with the suffix @{text "_global"}, |
62 Functions acting on theories often end with the suffix @{text "_global"}, |
62 for example the function @{ML read_term_global in Syntax} in the structure |
63 for example the function @{ML read_term_global in Syntax} in the structure |
63 @{ML_struct Syntax}. The reason is to set them syntactically apart from |
64 @{ML_struct Syntax}. The reason is to set them syntactically apart from |
64 functions acting on contexts or local theories (which will be discussed in |
65 functions acting on contexts or local theories (which will be discussed in |
65 next sections). There is a tendency in the Isabelle development to prefer |
66 the next sections). There is a tendency in the Isabelle development to prefer |
66 ``non-global'' operations, because they have some advantages, as we will also |
67 ``non-global'' operations, because they have some advantages, as we will also |
67 discuss later. However, some basic management of theories will very likely |
68 discuss later. However, some basic management of theories will very likely |
68 never go away. |
69 never go away. |
69 |
70 |
70 In the context ML-programming, the most important command with theories |
71 In the context of ML-programming, the most important command with theories |
71 is \isacommand{setup}. In the previous chapters we used it, for |
72 is \isacommand{setup}. In the previous chapters we used it, for |
72 example, to make a theorem attribute known to Isabelle or to register a theorem |
73 example, to make a theorem attribute known to Isabelle or to register a theorem |
73 under a name. What happens behind the scenes is that \isacommand{setup} |
74 under a name. What happens behind the scenes is that \isacommand{setup} |
74 expects a function of type @{ML_type "theory -> theory"}: the input theory |
75 expects a function of type @{ML_type "theory -> theory"}: the input theory |
75 is the current theory and the output the theory where the attribute has been |
76 is the current theory and the output the theory where the attribute has been |
86 in |
87 in |
87 Sign.declare_const @{context} bar_const thy |
88 Sign.declare_const @{context} bar_const thy |
88 end*} |
89 end*} |
89 |
90 |
90 text {* |
91 text {* |
91 If you simply write this code\footnote{Recall that ML-code needs to be enclosed in |
92 If you simply run this code\footnote{Recall that ML-code needs to be enclosed in |
92 \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}.} with the |
93 \isacommand{ML}~@{text "\<verbopen> \<dots> \<verbclose>"}.} with the |
93 intention of declaring a constant @{text "BAR"} with type @{typ nat} and run |
94 intention of declaring a constant @{text "BAR"} with type @{typ nat}, then |
94 the code, then indeed you obtain a theory as result. But if you query the |
95 indeed you obtain a theory as result. But if you query the |
95 constant on the Isabelle level using the command \isacommand{term} |
96 constant on the Isabelle level using the command \isacommand{term} |
96 |
97 |
97 \begin{isabelle} |
98 \begin{isabelle} |
98 \isacommand{term}~@{text BAR}\\ |
99 \isacommand{term}~@{text BAR}\\ |
99 @{text "> \"BAR\" :: \"'a\""} |
100 @{text "> \"BAR\" :: \"'a\""} |
143 @{text "> ERROR \"Stale theory encountered\""} |
144 @{text "> ERROR \"Stale theory encountered\""} |
144 \end{graybox} |
145 \end{graybox} |
145 \end{isabelle} |
146 \end{isabelle} |
146 |
147 |
147 This time we erroneously return the original theory @{text thy}, instead of |
148 This time we erroneously return the original theory @{text thy}, instead of |
148 the modified one @{text thy'}. Such programming errors with handling the |
149 the modified one @{text thy'}. Such buggy code will always result into |
149 most current theory will always result into stale theory error messages. |
150 a runtime error message about stale theories. |
150 |
151 |
151 However, sometimes it does make sense to work with two theories at the same |
152 However, sometimes it does make sense to work with two theories at the same |
152 time, especially in the context of parsing and typing. In the code below we |
153 time, especially in the context of parsing and typing. In the code below we |
153 use in Line 3 the function @{ML_ind copy in Theory} from the structure |
154 use in Line 3 the function @{ML_ind copy in Theory} from the structure |
154 @{ML_struct Theory} for obtaining a new theory that contains the same |
155 @{ML_struct Theory} for obtaining a new theory that contains the same |
173 "tmp_thy"} the constant @{text FOO} (Lines 4 and 5). The point of this code |
174 "tmp_thy"} the constant @{text FOO} (Lines 4 and 5). The point of this code |
174 is that we next, in Lines 6 and 7, parse a string to become a term (both |
175 is that we next, in Lines 6 and 7, parse a string to become a term (both |
175 times the string is @{text [quotes] "FOO baz"}). But since we parse the string |
176 times the string is @{text [quotes] "FOO baz"}). But since we parse the string |
176 once in the context of the theory @{text tmp_thy'} in which @{text FOO} is |
177 once in the context of the theory @{text tmp_thy'} in which @{text FOO} is |
177 declared to be a constant of type @{typ "nat \<Rightarrow>nat"} and once in the context |
178 declared to be a constant of type @{typ "nat \<Rightarrow>nat"} and once in the context |
178 of @{text thy}, we obtain two different terms, namely |
179 of @{text thy} where it is not, we obtain two different terms, namely |
179 |
180 |
180 \begin{isabelle} |
181 \begin{isabelle} |
181 \begin{graybox} |
182 \begin{graybox} |
182 @{text "> Const (\"Advanced.FOO\", \"nat \<Rightarrow> nat\") $ Free (\"baz\", \"nat\")"}\isanewline |
183 @{text "> Const (\"Advanced.FOO\", \"nat \<Rightarrow> nat\") $ Free (\"baz\", \"nat\")"}\isanewline |
183 @{text "> Free (\"FOO\", \"'a \<Rightarrow> 'b\") $ Free (\"baz\", \"'a\")"} |
184 @{text "> Free (\"FOO\", \"'a \<Rightarrow> 'b\") $ Free (\"baz\", \"'a\")"} |
184 \end{graybox} |
185 \end{graybox} |
185 \end{isabelle} |
186 \end{isabelle} |
186 |
187 |
187 There are two reasons for parsing a term in a temporary theory. One is to |
188 There are two reasons for parsing a term in a temporary theory. One is to |
188 obtain fully qualified names and the other is appropriate type inference. |
189 obtain fully qualified names for constants and the other is appropriate type |
189 This is relevant in situations where definitions are made later, but parsing |
190 inference. This is relevant in situations where definitions are made later, |
190 and type inference has to take already proceed as if the definitions were |
191 but parsing and type inference has to take already proceed as if the definitions |
191 already made. |
192 were already made. |
192 *} |
193 *} |
193 |
194 |
194 section {* Contexts (TBD) *} |
195 section {* Contexts (TBD) *} |
195 |
196 |
196 text {* |
197 text {* |
197 Contexts are arguably more important than theories, even though they only |
198 Contexts are arguably more important than theories, even though they only |
198 contain ``short-term memory data''. The reason is that a vast number of |
199 contain ``short-term memory data''. The reason is that a vast number of |
199 functions in Isabelle depend one way or the other on contexts. Even such |
200 functions in Isabelle depend one way or the other on contexts. Even such |
200 mundane operation like printing out a term make essential use of contexts. |
201 mundane operation like printing out a term make essential use of contexts. |
201 *} |
202 For this consider the following contrived proof which only purpose is to |
|
203 fix two variables |
|
204 *} |
|
205 |
|
206 lemma "True" |
|
207 proof - |
|
208 txt_raw {*\mbox{}\\[-6mm]*} |
|
209 ML_prf {* Variable.dest_fixes @{context} *} |
|
210 txt_raw {*\mbox{}\\[-6mm]*} |
|
211 fix x y |
|
212 txt_raw {*\mbox{}\\[-6mm]*} |
|
213 ML_prf {* Variable.dest_fixes @{context} *} |
|
214 txt_raw {*\mbox{}\\[-6mm]*} |
|
215 oops |
|
216 |
202 |
217 |
203 (* |
218 (* |
204 ML{*Proof_Context.debug := true*} |
219 ML{*Proof_Context.debug := true*} |
205 ML{*Proof_Context.verbose := true*} |
220 ML{*Proof_Context.verbose := true*} |
206 *) |
221 *) |