ArticleslgStudy

mathematics

KeY

KeY is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand KeY rather than just read about it. In short: KeY is a formal verification tool for Java programs. It accepts specifications written in the Java Modeling Language to Java source files.

KeY — main illustration
KeY — illustration

Key takeaways

  • KeY belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect KeY to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of KeY from memory before moving on to harder problems.

Reference excerpt

KeY is a formal verification tool for Java programs. It accepts specifications written in the Java Modeling Language to Java source files. These are transformed into theorems of dynamic logic and then compared against program semantics that are likewise defined in terms of dynamic logic. KeY is significantly powerful in that it supports both interactive (i.e. by hand) and fully automated correctness proofs. Failed proof attempts can be used for a more efficient debugging or verification-based testing. There have been several extensions to KeY in order to apply it to the verification of C programs or hybrid systems. KeY is jointly developed by Karlsruhe Institute of Technology, Germany; Technische Universität Darmstadt, Germany; and Chalmers University of Technology in Gothenburg, Sweden and is licensed under the GPL.

Overview The usual user input to KeY consists of a Java source file with annotations in JML. Both are translated to KeY's internal representation, dynamic logic. From the given specifications, several proof obligations arise which are to be discharged, i.e. a proof has to be found. To this ends, the program is symbolically executed with the resulting changes to program variables stored in so-called updates. Once the program has been processed completely, there remains a first-order logic proof obligation. At the heart of the KeY system lies a first-order theorem prover based on sequent calculus, which is used to close the proof. Interference rules are captured in so called taclets which consist of an own simple language to describe changes to a sequent.

Java Card DL

The theoretical foundation of KeY is a formal logic called Java Card DL. DL stands for Dynamic Logic. It is a version of a first-order dynamic logic tailored to Java Card programs. As such, it for example allows statements (formulas) like ϕ→[α]ψ, which intuitively says that the post-condition ψ must hold in all program states reachable by executing the Java Card program α in any state that satisfies the pre-condition ϕ. This is equivalent to {ϕ}α{ψ} in Hoare calculus if ϕ and ψ are purely first order. Dynamic logic, however, extends Hoare logic in that formulas may contain nested program modalities such as [α], or that quantification over formulas which contain modalities is possible. There is also a dual modality ⟨α⟩ which includes termination. This dynamic logic can be seen as a special multi-modal logic (with an infinite number of modalities) where for each Java block α there are modalities [α] and ⟨α⟩.

Deduction component At the heart of the KeY system lies a first-order theorem prover based on a sequent calculus. A sequent is of the form Γ⊢Δ where Γ (assumptions) and Δ (propositions) are sets of formulas with the intuitive meaning that ⋀γ∈Γγ→⋁δ∈Δδ holds true. By means of deduction, an initial sequent representing the proof obligation is shown to be constructible from just fundamental first-order axioms (such as equality e =˙ e).

Symbolic execution of Java code During that, program modalities are eliminated by symbolic execution. For instance, the formula x =˙ 0→[x++;]x =˙ 1 is logically equivalent to x =˙ 0→x =˙ 0. As this example shows, symbolic execution in dynamic logic is very similar to calculating weakest preconditions. Both [α]ψ and wp(α,ψ) essentially denote the same thing – with two exceptions: Firstly, wp is a function of some meta-calculus while [α]ψ really is a formula of the given calculus. Secondly, symbolic execution runs through the program forward just as an actual execution would. To save intermediate results of assignments, KeY introduces a concept called updates, which are similar to substitutions but are only applied once the program modality has been eliminated. Syntactically, updates are consist of parallel (side-effect free) assignments written in curly braces in front of a modality. An example of symbolic execution with updates: [x=3;x=x+1;]x =˙ 4 is transformed to {x:=3}[x=x+1;]x =˙ 4 in the first step and to {x:=4}[]x =˙ 4 in the second step. The modality then is empty and "backwards application" of the update to the postcondition yields a precondition where x could take any value.

Example Suppose one wants to prove that the following method calculates the product of some non-negative integers x and y.

One thus starts the proof with the premise x≥0∧y≥0 and the to-be-shown conclusion z =˙ x⋅y. Note that tableaux of sequent calculi are usually written "upside-down", i.e., the starting sequent appears at the bottom and deduction steps go upwards. The proof can be seen in the figure on the right.

Additional features

Symbolic Execution Debugger The Symbolic Execution Debugger visualizes the control flow of a program as a symbolic execution tree that contains all feasible execution paths through the program up to a certain point. It is provided as a plugin to the Eclipse development platform.

Test Case Generator KeY is usable as a model-based testing tool that can generate unit tests for Java programs. The model from which test data and the test case are derived consists of a formal specification (provided in JML) and a symbolic execution tree of the implementation under test which is computed by the KeY system.

Distribution and Variants of the KeY System KeY is free software written in Java and licensed under the GNU General Public License, version 2. It can be downloaded from the project website in source or as pre-compiled JAR.

KeY-Hoare KeY-Hoare is built on top of KeY and features a Hoare calculus with state updates. State updates are a means of describing state transitions in a Kripke structure. This calculus can be seen as a subset to the one that is used in the main branch of KeY. Due to the simplicity of the Hoare calculus, this implementation is essentially meant to exemplify formal methods in undergraduate classes.

… excerpt ends here. Continue reading the full article.

Illustrations

KeY illustration
KeY illustration

Worked examples

Example 1 — a first encounter with KeY

Start with the simplest possible case. Write down what KeY claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to KeY before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about KeY ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of KeY

In research
KeY appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses KeY in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
KeY is common in secondary-school and first-year university syllabi. It links to neighbouring topics Formal methods tools, Free theorem provers, Proof assistants, so understanding it makes those chapters shorter.
In everyday life
Look for KeY outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “KeY” →

How to study KeY in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what KeY means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain KeY out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is KeY in simple terms?

KeY is a formal verification tool for Java programs. It accepts specifications written in the Java Modeling Language to Java source files.

Why does KeY matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study KeY?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on KeY.

Tags

  • Formal methods tools
  • Free theorem provers
  • Proof assistants

Keep exploring