1***************************
2Interactive Theorem Proving
3***************************
5Idris also supports interactive theorem proving via tactics. This
6is generally not recommended to be used directly, but rather used as a
7mechanism for building proof automation which is beyond the scope of this
8tutorial. In this section, we briefly discus tactics.
10One way to write proofs interactively is to write the general *structure* of
11the proof, and use the interactive mode to complete the details.
12Consider the following definition, proved in :ref:`sect-theorems`:
16 plusReduces : (n:Nat) -> plus Z n = n
18We’ll be constructing the proof by *induction*, so we write the cases for ``Z``
19and ``S``, with a recursive call in the ``S`` case giving the inductive
20hypothesis, and insert *holes* for the rest of the definition:
24 plusReducesZ' : (n:Nat) -> n = plus n Z
25 plusReducesZ' Z = ?plusredZ_Z
26 plusReducesZ' (S k) = let ih = plusReducesZ' k in
29On running , two global names are created, ``plusredZ_Z`` and
30``plusredZ_S``, with no definition. We can use the ``:m`` command at the
31prompt to find out which holes are still to be solved (or, more
32precisely, which functions exist but have no definitions), then the
33``:t`` command to see their types:
39 [plusredZ_S,plusredZ_Z]
43 *theorems> :t plusredZ_Z
44 plusredZ_Z : Z = plus Z Z
46 *theorems> :t plusredZ_S
47 plusredZ_S : (k : Nat) -> (k = plus k Z) -> S k = plus (S k) Z
49The ``:p`` command enters interactive proof mode, which can be used to
50complete the missing definitions.
54 *theorems> :p plusredZ_Z
56 ---------------------------------- (plusredZ_Z) --------
57 {hole0} : Z = plus Z Z
59This gives us a list of premises (above the line; there are none here)
60and the current goal (below the line; named ``{hole0}`` here). At the
61prompt we can enter tactics to direct the construction of the proof. In
62this case, we can normalise the goal with the ``compute`` tactic:
68 ---------------------------------- (plusredZ_Z) --------
71Now we have to prove that ``Z`` equals ``Z``, which is easy to prove by
72``Refl``. To apply a function, such as ``Refl``, we use ``refine`` which
73introduces subgoals for each of the function’s explicit arguments
78 -plusredZ_Z> refine Refl
79 plusredZ_Z: no more goals
81Here, we could also have used the ``trivial`` tactic, which tries to
82refine by ``Refl``, and if that fails, tries to refine by each name in
83the local context. When a proof is complete, we use the ``qed`` tactic
84to add the proof to the global context, and remove the hole from the
85unsolved holes list. This also outputs a trace of the proof:
100The ``:addproof`` command, at the interactive prompt, will add the proof
101to the source file (effectively in an appendix). Let us now prove the
102other required lemma, ``plusredZ_S``:
106 *theorems> :p plusredZ_S
108 ---------------------------------- (plusredZ_S) --------
109 {hole0} : (k : Nat) -> (k = plus k Z) -> S k = plus (S k) Z
111In this case, the goal is a function type, using ``k`` (the argument
112accessible by pattern matching) and ``ih`` — the local variable
113containing the result of the recursive call. We can introduce these as
114premisses using the ``intro`` tactic twice (or ``intros``, which
115introduces all arguments as premisses). This gives:
121 ---------------------------------- (plusredZ_S) --------
122 {hole2} : S k = plus (S k) Z
124Since plus is defined by recursion on its first argument, the term
125``plus (S k) Z`` in the goal can be simplified, so we use ``compute``.
131 ---------------------------------- (plusredZ_S) --------
132 {hole2} : S k = S (plus k Z)
134We know, from the type of ``ih``, that ``k = plus k Z``, so we would
135like to use this knowledge to replace ``plus k Z`` in the goal with
136``k``. We can achieve this with the ``rewrite`` tactic:
140 -plusredZ_S> rewrite ih
144 ---------------------------------- (plusredZ_S) --------
149The ``rewrite`` tactic takes an equality proof as an argument, and tries
150to rewrite the goal using that proof. Here, it results in an equality
151which is trivially provable:
156 plusredZ_S: no more goals
164Again, we can add this proof to the end of our source file using the
165``:addproof`` command at the interactive prompt.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list