text\<open>Did you ever dream about records with multiple inheritance? Then you should definitely have a look at statespaces. They may be
what you are dreaming of. Or at least almost \dots\<close>
text\<open>Isabelle allows to add new top-level commands to the
system. Building on the locale infrastructure, we provide a command \<^theory_text>\<open>statespace\<close> like this:\<close>
statespace vars =
n::nat
b::bool
print_locale vars_namespace print_locale vars_valuetypes print_locale vars
text\<open>\noindent This resembles a \<^theory_text>\<open>record\<close> definition,
but introduces sophisticated locale
infrastructure instead of HOL type schemes. The resulting context
postulates two distinct names \<^term>\<open>n\<close> and \<^term>\<open>b\<close> and
projection~/ injection functions that convert from abstract values to \<^typ>\<open>nat\<close> and \<open>bool\<close>. The logical content of the locale is:\<close>
locale vars' = fixes n::'name and b::'name assumes"distinct [n, b]"
fixes project_bool::"'value \ bool" and inject_bool::"bool \ 'value" assumes"\b. project_bool (inject_bool b) = b"
text\<open>\noindent The HOL predicate \<^const>\<open>distinct\<close> describes
distinctness of all names in the context. Locale\<open>vars'\<close> defines the raw logical content that is defined in the state space locale. We also maintain non-logical context information to support
the user:
\<^item> Syntax for state lookup and updates that automatically inserts
the corresponding projection and injection functions. \<^item> Setup for the proof tools that exploit the distinctness
information and the cancellation of projections and injections in
deductions and simplifications.
This extra-logical information is added to the localein form of
declarations, which associate the name of a variable to the
corresponding projection and injection functions to handle the syntax
transformations, and a link from the variable name to the
corresponding distinctness theorem. As state spaces are merged or
extended there are multiple distinctness theoremsin the context. Our
declarations take care that the link always points to the strongest
distinctness assumption. With these declarations in place, a lookup
can be written as \<open>s\<cdot>n\<close>, which is translated to \<open>project_nat (s n)\<close>, and an
update as \<open>s\<langle>n := 2\<rangle>\<close>, which is translated to \<open>s(n := inject_nat 2)\<close>.
We can now establish the
following lemma:\<close>
lemma (in vars) foo: "s\b = s\b" by simp
text\<open>\noindent Here the simplifier was able to refer to
distinctness of \<^term>\<open>b\<close> and \<^term>\<open>n\<close> to solve the equation.
The resulting lemmaisalso recorded inlocale\<open>vars\<close> for
later useandis automatically propagated to all its interpretations.
Here is another example:\<close>
statespace'a varsX = NB: vars [n=N, b=B] + vars + x::'a
text\<open>\noindent The state space \<open>varsX\<close> imports two copies
of the state space \<open>vars\<close>, where one has the variables renamed to upper-case letters, and adds another variable \<^term>\<open>x\<close> of type \<^typ>\<open>'a\<close>. This type is fixed inside the state space but may get
instantiated later on, analogous to type parameters of an ML-functor.
The distinctness assumption is now \<open>distinct [N, B, n, b, x]\<close>, from this we can derive both \<^term>\<open>distinct [N,B]\<close> and \<^term>\<open>distinct [n,b]\<close>,
the distinction assumptions for the two versions of locale\<open>vars\<close> above. Moreover we have all necessary
projection and injection assumptions available. These assumptions
together allow us to establish state space \<^term>\<open>varsX\<close> as an interpretation of both instances of locale\<^term>\<open>vars\<close>. Hence we
inherit both variants of theorem\<open>foo\<close>: \<open>s\<langle>N := 2\<rangle>\<cdot>B =
s\<cdot>B\<close> as well as \<open>s\<langle>n := 2\<rangle>\<cdot>b = s\<cdot>b\<close>. These are immediate
consequences of the localeinterpretation action.
The declarations forsyntaxand the distinctness theoremsalso observe
the morphisms generated by the locale package due to the renaming \<^term>\<open>n = N\<close>:\<close>
lemma (in varsX) foo: "s\N := 2\\x = s\x" by simp
text\<open>To assure scalability towards many distinct names, the
distinctness predicate is refined to operate on balanced trees. Thus
we get logarithmic certificates for the distinctness of two names by
the distinctness of the paths in the tree. Asked for the distinctness
of two names, our tool produces the paths of the variables in the tree
(this is implemented in Isabelle/ML, outside the logic) and returns a
certificate corresponding to the different paths. Merging state
spaces requires to prove that the combined distinctness assumption
implies the distinctness assumptions of the components. Such a proof is of the order $m \cdot \log n$, where $n$ and $m$ are the number of
nodes in the larger and smaller tree, respectively.\<close>
text\<open>We continue with more examples.\<close>
lemma (in foo) foo1: shows"s\a := i\\a = i" by simp
lemma (in foo) foo2: shows"(s\a:=i\)\a = i" by simp
lemma (in foo) foo3: shows"(s\a:=i\)\b = s\b" by simp
lemma (in foo) foo4: shows"(s\a:=i,b:=j,c:=k,a:=x\) = (s\b:=j,c:=k,a:=x\)" by simp
statespace bar =
b::bool
c::string
lemma (in bar) bar1: shows"(s\b:=True\)\c = s\c" by simp
text\<open>You can define a derived state space by inheriting existing state spaces, renaming
of components if you like, andby declaring new components. \<close>
statespace ('a,'b) loo = 'a foo + bar [b=B,c=C] +
X::'b
lemma (in loo) loo1: shows"s\a:=i\\B = s\B" proof - thm foo1 txt\<open>The Lemma @{thm [source] foo1} from the parent state space isalso available here: \begin{center}@{thm foo1}\end{center}\<close> have"s\a = i" by (rule foo1) thm bar1 txt\<open>Note the renaming of the parameters in Lemma @{thm [source] bar1}: \begin{center}@{thm bar1}\end{center}\<close> have"s\C = s\C" by (rule bar1) show ?thesis by simp qed
statespace'a dup = FA: 'a foo [f=F, a=A] + 'a foo +
x::int
text\<open>There were known problems with syntax-declarations. They only
worked when the contextis already completely built. This is now overcome. e.g.:\<close>
locale fooX = foo + assumes"s\b = k"
text\<open>
We can also put statespaces side-by-side byusing ordinary @{command locale} expressions
(instead of the @{command statespace}). \<close>
locale side_by_side = foo + bar where b="B::'a"and c=C for B C
context side_by_side begin text\<open>Simplification within one of the statespaces works as expected.\<close> lemma"s\C = s\C" by simp
text\<open>In contrast to the statespace @{locale loo} there is no 'inter' statespace distinctness
between the names of @{locale foo} and @{locale bar}.\<close> end
text\<open>Sharing of names in side-by-side statespaces is also possible as long as they are mapped to the same type.\<close>
text\<open>Note that the distinctness theorem for @{locale vars1} is selected here to do the proof.\<close> lemma"s\m = s\m" by simp
text\<open>Note that the distinctness theorem for @{locale vars2} is selected here to do the proof.\<close> lemma"s\k = s\k" by simp
text\<open>Still there is no inter-statespace distinctness.\<close> lemma"s\m = s\m" (* apply simp *) oops end
statespace merge_vars1_vars2 = vars1 + vars2
context merge_vars1_vars2 begin text\<open>When defining a statespace instead of a side-by-side locale we get the distinctness of
all variables.\<close> lemma"s\m = s\m" by simp end
subsection \<open>Benchmarks\<close>
text\<open>Here are some bigger examples for benchmarking.\<close>
ML \<open> fun make_benchmark n =
writeln (Active.sendback_markup_command
("statespace benchmark" ^ string_of_int n ^ " =\n" ^
(cat_lines (map (fn i => "A" ^ string_of_int i ^ "::nat") (1 upto n))))); \<close>
lemma (in benchmark100) test: "s\A100 = s\A100" by simp lemma (in benchmark500) test: "s\A100 = s\A100" by simp lemma (in benchmark1000) test: "s\A100 = s\A100" by simp
end
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.12Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.