<?xml version="1.0" ?><?xml-stylesheet type="text/xsl" href="moviola.xsl"?><?xml-stylesheet type="text/xsl" href="moviola-coqdoc.xsl"?><movie><film><frame frameNumber="0"><command>(** *** Exercise coq_tree_01 *)</command><response/><command-coqdoc><div class="doc" divNumber="0">
<a name="lab1"/><h3 class="section">Exercise coq_tree_01</h3>

</div></command-coqdoc></frame><frame frameNumber="1"><command>

(** Let us define binary tree of natural numbers.
   Such a tree is either a leaf, or an internal
   node with some natural number and two subtrees. *)</command><response/><command-coqdoc><div class="doc" divNumber="0">
Let us define binary tree of natural numbers.
   Such a tree is either a leaf, or an internal
   node with some natural number and two subtrees. 
</div></command-coqdoc></frame><frame frameNumber="2"><command>

Inductive nat_tree : Set :=
  | leaf : nat_tree
  | node : nat_tree -&gt; nat -&gt; nat_tree -&gt; nat_tree.</command><response>nat_tree is defined
nat_tree_rect is defined
nat_tree_ind is defined
nat_tree_rec is defined
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="keyword">Inductive</span> <span class="id" type="var">nat_tree</span> : <span class="id" type="keyword">Set</span> :=<br/>
  | <span class="id" type="var">leaf</span> : <span class="id" type="var">nat_tree</span><br/>
  | <span class="id" type="var">node</span> : <span class="id" type="var">nat_tree</span> -&gt; <span class="id" type="var">nat</span> -&gt; <span class="id" type="var">nat_tree</span> -&gt; <span class="id" type="var">nat_tree</span>.
</div></command-coqdoc></frame><frame frameNumber="3"><command>

(** Now let us define the 'In n T' predicate that returns
   a proposition indicating whether a tree 'T' contains 
   a natural number 'n' *)</command><response/><command-coqdoc><div class="doc" divNumber="0">
Now let us define the 'In n T' predicate that returns
   a proposition indicating whether a tree 'T' contains 
   a natural number 'n' 
</div></command-coqdoc></frame><frame frameNumber="4"><command>

Fixpoint In (n : nat) (T : nat_tree) {struct T} : Prop :=
  match T with
  | leaf =&gt; False
  | node l v r =&gt; In n l \/ v = n \/ In n r
  end.</command><response>In is recursively defined (decreasing on 2nd argument)
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="keyword">Fixpoint</span> <span class="id" type="var">In</span> (<span class="id" type="var">n</span> : <span class="id" type="var">nat</span>) (<span class="id" type="var">T</span> : <span class="id" type="var">nat_tree</span>) {<span class="id" type="keyword">struct</span> <span class="id" type="var">T</span>} : <span class="id" type="keyword">Prop</span> :=<br/>
  <span class="id" type="keyword">match</span> <span class="id" type="var">T</span> <span class="id" type="keyword">with</span><br/>
  | <span class="id" type="var">leaf</span> =&gt; <span class="id" type="var">False</span><br/>
  | <span class="id" type="var">node</span> <span class="id" type="var">l</span> <span class="id" type="var">v</span> <span class="id" type="var">r</span> =&gt; <span class="id" type="var">In</span> <span class="id" type="var">n</span> <span class="id" type="var">l</span> \/ <span class="id" type="var">v</span> = <span class="id" type="var">n</span> \/ <span class="id" type="var">In</span> <span class="id" type="var">n</span> <span class="id" type="var">r</span><br/>
  <span class="id" type="keyword">end</span>.
</div></command-coqdoc></frame><frame frameNumber="5"><command>
  
(** Now let us prove that if an element 'n' belongs to
   the tree with &quot;root&quot; b and &quot;leaf&quot; a
   then 'n' either equals 'a' or 'b'. *)</command><response/><command-coqdoc><div class="code" divNumber="0">

<br/>


</div><div class="doc" divNumber="1">
Now let us prove that if an element 'n' belongs to
   the tree with &quot;root&quot; b and &quot;leaf&quot; a
   then 'n' either equals 'a' or 'b'. 
</div></command-coqdoc></frame><frame frameNumber="6"><command>

Lemma tree2_In : forall n a b, 
  In n (node (node leaf a leaf) b leaf) -&gt;
  n = a \/ n = b.</command><response>1 subgoal
  
  ============================
   forall n a b : nat,
   In n (node (node leaf a leaf) b leaf) -&gt; n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">tree2_In</span> : <span class="id" type="keyword">forall</span> <span class="id" type="var">n</span> <span class="id" type="var">a</span> <span class="id" type="var">b</span>, <br/>
  <span class="id" type="var">In</span> <span class="id" type="var">n</span> (<span class="id" type="var">node</span> (<span class="id" type="var">node</span> <span class="id" type="var">leaf</span> <span class="id" type="var">a</span> <span class="id" type="var">leaf</span>) <span class="id" type="var">b</span> <span class="id" type="var">leaf</span>) -&gt;<br/>
  <span class="id" type="var">n</span> = <span class="id" type="var">a</span> \/ <span class="id" type="var">n</span> = <span class="id" type="var">b</span>.
</div></command-coqdoc></frame><frame frameNumber="7"><command>
  
Proof.</command><response/><command-coqdoc><div class="code" divNumber="0">

<br/>

<br/>
<span class="id" type="keyword">Proof</span>.
</div></command-coqdoc></frame><frame frameNumber="8"><command>

(** Here is the ultra short proof script: [Note that these script are
   often constructed after having first constructed a longer one!] *)</command><response/><command-coqdoc><div class="doc" divNumber="0">
Here is the ultra short proof script: <span class="inlinecode"><span class="id" type="var">Note</span> <span class="id" type="var">that</span> <span class="id" type="var">these</span> <span class="id" type="var">script</span> <span class="id" type="var">are</span>
   <span class="id" type="var">often</span> <span class="id" type="var">constructed</span> <span class="id" type="keyword">after</span> <span class="id" type="var">having</span> <span class="id" type="var">first</span> <span class="id" type="var">constructed</span> <span class="id" type="var">a</span> <span class="id" type="var">longer</span> <span class="id" type="var">one</span>!</span> 
</div></command-coqdoc></frame><frame frameNumber="9"><command>

intros n a b [[H1 | [H2 | H3]] | [H4 |H5]]; try contradiction;auto.</command><response>Proof completed.
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">a</span> <span class="id" type="var">b</span> [[<span class="id" type="var">H1</span> | [<span class="id" type="var">H2</span> | <span class="id" type="var">H3</span>]] | [<span class="id" type="var">H4</span> |H5]]; <span class="id" type="tactic">try</span> <span class="id" type="var">contradiction</span>;<span class="id" type="tactic">auto</span>.
</div></command-coqdoc></frame><frame frameNumber="10"><command>

(** Some comments:
   - ; composes two tactics  
       T1;T2 means that T2 is applied to _all_ subgoals that are generated by T1
   - &quot;try T&quot;  tries tactic &quot;T1&quot; on the goal and does nothing if &quot;T1&quot; fails
     this is different from just &quot;T1&quot;, because T1 may fail, but &quot;try T1&quot; never fails
   - &quot;auto&quot; solves quite a few goals by itself. 
   - &quot;intros [[H1 | [H2 | H3]] | [H4 |H5]]&quot; does an intro _and_ a destruct
     Basically: if the goal is forall &quot;h:A, B&quot; and &quot;A&quot; is an inductive type with two constructors, 
     -- you can type &quot;intros h; elim h; intro H1 &quot; etc (later: &quot;intro H2&quot;)
     -- you can type &quot;intros h; destruct h as [H1 | H2]&quot; etc
     -- you can type &quot;intros [H1 | H2]&quot;
     The latter is what I am doing here, taking full advantage of the unfolded shape of 
     &quot;In n (node (node leaf a leaf) b leaf)&quot;.
   - The clue of the exercise is a bit hidden in my script, because &quot;intros&quot; does the unfolding
     of the definition of In (and the computation) by itself
*)</command><response/><command-coqdoc><div class="doc" divNumber="0">
Some comments:

<ul>
<li> ; composes two tactics  
       T1;T2 means that T2 is applied to _all_ subgoals that are generated by T1

</li>
<li> &quot;try T&quot;  tries tactic &quot;T1&quot; on the goal and does nothing if &quot;T1&quot; fails
     this is different from just &quot;T1&quot;, because T1 may fail, but &quot;try T1&quot; never fails

</li>
<li> &quot;auto&quot; solves quite a few goals by itself. 

</li>
<li> &quot;intros <span class="inlinecode">[<span class="id" type="var">H1</span> | [<span class="id" type="var">H2</span> | <span class="id" type="var">H3</span>]] | [<span class="id" type="var">H4</span> |H5]</span>&quot; does an intro _and_ a destruct
     Basically: if the goal is forall &quot;h:A, B&quot; and &quot;A&quot; is an inductive type with two constructors, 

<ul>
<li> you can type &quot;intros h; elim h; intro H1 &quot; etc (later: &quot;intro H2&quot;)

</li>
<li> you can type &quot;intros h; destruct h as <span class="inlinecode"><span class="id" type="var">H1</span> | <span class="id" type="var">H2</span></span>&quot; etc

</li>
<li> you can type &quot;intros <span class="inlinecode"><span class="id" type="var">H1</span> | <span class="id" type="var">H2</span></span>&quot;
     The latter is what I am doing here, taking full advantage of the unfolded shape of 
     &quot;In n (node (node leaf a leaf) b leaf)&quot;.

</li>
</ul>

</li>
<li> The clue of the exercise is a bit hidden in my script, because &quot;intros&quot; does the unfolding
     of the definition of In (and the computation) by itself

</li>
</ul>

</div></command-coqdoc></frame><frame frameNumber="11"><command>
Qed.</command><response>tree2_In is defined
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="keyword">Qed</span>.
</div></command-coqdoc></frame><frame frameNumber="12"><command>

Lemma tree2_In' : forall n a b, 
  In n (node (node leaf a leaf) b leaf) -&gt;
  n = a \/ n = b.</command><response>1 subgoal
  
  ============================
   forall n a b : nat,
   In n (node (node leaf a leaf) b leaf) -&gt; n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="keyword">Lemma</span> <span class="id" type="var">tree2_In'</span> : <span class="id" type="keyword">forall</span> <span class="id" type="var">n</span> <span class="id" type="var">a</span> <span class="id" type="var">b</span>, <br/>
  <span class="id" type="var">In</span> <span class="id" type="var">n</span> (<span class="id" type="var">node</span> (<span class="id" type="var">node</span> <span class="id" type="var">leaf</span> <span class="id" type="var">a</span> <span class="id" type="var">leaf</span>) <span class="id" type="var">b</span> <span class="id" type="var">leaf</span>) -&gt;<br/>
  <span class="id" type="var">n</span> = <span class="id" type="var">a</span> \/ <span class="id" type="var">n</span> = <span class="id" type="var">b</span>.
</div></command-coqdoc></frame><frame frameNumber="13"><command>
  
Proof.</command><response/><command-coqdoc><div class="code" divNumber="0">

<br/>

<br/>
<span class="id" type="keyword">Proof</span>.
</div></command-coqdoc></frame><frame frameNumber="14"><command>
(** Here is the longer proof script that I had first created *)</command><response/><command-coqdoc><div class="doc" divNumber="0">
Here is the longer proof script that I had first created 
</div></command-coqdoc></frame><frame frameNumber="15"><command>
intros n a b H.</command><response>1 subgoal
  
  n : nat
  a : nat
  b : nat
  H : In n (node (node leaf a leaf) b leaf)
  ============================
   n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="tactic">intros</span> <span class="id" type="var">n</span> <span class="id" type="var">a</span> <span class="id" type="var">b</span> <span class="id" type="var">H</span>.
</div></command-coqdoc></frame><frame frameNumber="16"><command>
simpl in H.</command><response>1 subgoal
  
  n : nat
  a : nat
  b : nat
  H : (False \/ a = n \/ False) \/ b = n \/ False
  ============================
   n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="tactic">simpl</span> <span class="id" type="keyword">in</span> <span class="id" type="var">H</span>.
</div></command-coqdoc></frame><frame frameNumber="17"><command>
  (** This unfolds the definition of In and therefore computes the normal form of the type of H *)</command><response/><command-coqdoc><div class="doc" divNumber="0">
This unfolds the definition of In and therefore computes the normal form of the type of H 
</div></command-coqdoc></frame><frame frameNumber="18"><command>
elim H.</command><response>2 subgoals
  
  n : nat
  a : nat
  b : nat
  H : (False \/ a = n \/ False) \/ b = n \/ False
  ============================
   False \/ a = n \/ False -&gt; n = a \/ n = b

subgoal 2 is:
 b = n \/ False -&gt; n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="tactic">elim</span> <span class="id" type="var">H</span>.
</div></command-coqdoc></frame><frame frameNumber="19"><command>
intro H1.</command><response>2 subgoals
  
  n : nat
  a : nat
  b : nat
  H : (False \/ a = n \/ False) \/ b = n \/ False
  H1 : False \/ a = n \/ False
  ============================
   n = a \/ n = b

subgoal 2 is:
 b = n \/ False -&gt; n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="tactic">intro</span> <span class="id" type="var">H1</span>.
</div></command-coqdoc></frame><frame frameNumber="20"><command>
elim H1.</command><response>3 subgoals
  
  n : nat
  a : nat
  b : nat
  H : (False \/ a = n \/ False) \/ b = n \/ False
  H1 : False \/ a = n \/ False
  ============================
   False -&gt; n = a \/ n = b

subgoal 2 is:
 a = n \/ False -&gt; n = a \/ n = b
subgoal 3 is:
 b = n \/ False -&gt; n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="tactic">elim</span> <span class="id" type="var">H1</span>.
</div></command-coqdoc></frame><frame frameNumber="21"><command>
intro H2.</command><response>3 subgoals
  
  n : nat
  a : nat
  b : nat
  H : (False \/ a = n \/ False) \/ b = n \/ False
  H1 : False \/ a = n \/ False
  H2 : False
  ============================
   n = a \/ n = b

subgoal 2 is:
 a = n \/ False -&gt; n = a \/ n = b
subgoal 3 is:
 b = n \/ False -&gt; n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="tactic">intro</span> <span class="id" type="var">H2</span>.
</div></command-coqdoc></frame><frame frameNumber="22"><command>
contradiction.</command><response>2 subgoals
  
  n : nat
  a : nat
  b : nat
  H : (False \/ a = n \/ False) \/ b = n \/ False
  H1 : False \/ a = n \/ False
  ============================
   a = n \/ False -&gt; n = a \/ n = b

subgoal 2 is:
 b = n \/ False -&gt; n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="var">contradiction</span>.
</div></command-coqdoc></frame><frame frameNumber="23"><command>
intro H3.</command><response>2 subgoals
  
  n : nat
  a : nat
  b : nat
  H : (False \/ a = n \/ False) \/ b = n \/ False
  H1 : False \/ a = n \/ False
  H3 : a = n \/ False
  ============================
   n = a \/ n = b

subgoal 2 is:
 b = n \/ False -&gt; n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="tactic">intro</span> <span class="id" type="var">H3</span>.
</div></command-coqdoc></frame><frame frameNumber="24"><command>
elim H3.</command><response>3 subgoals
  
  n : nat
  a : nat
  b : nat
  H : (False \/ a = n \/ False) \/ b = n \/ False
  H1 : False \/ a = n \/ False
  H3 : a = n \/ False
  ============================
   a = n -&gt; n = a \/ n = b

subgoal 2 is:
 False -&gt; n = a \/ n = b
subgoal 3 is:
 b = n \/ False -&gt; n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="tactic">elim</span> <span class="id" type="var">H3</span>.
</div></command-coqdoc></frame><frame frameNumber="25"><command>
intro H4; left; symmetry; assumption.</command><response>2 subgoals
  
  n : nat
  a : nat
  b : nat
  H : (False \/ a = n \/ False) \/ b = n \/ False
  H1 : False \/ a = n \/ False
  H3 : a = n \/ False
  ============================
   False -&gt; n = a \/ n = b

subgoal 2 is:
 b = n \/ False -&gt; n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="tactic">intro</span> <span class="id" type="var">H4</span>; <span class="id" type="var">left</span>; <span class="id" type="tactic">symmetry</span>; <span class="id" type="tactic">assumption</span>.
</div></command-coqdoc></frame><frame frameNumber="26"><command>
intro H5;contradiction.</command><response>1 subgoal
  
  n : nat
  a : nat
  b : nat
  H : (False \/ a = n \/ False) \/ b = n \/ False
  ============================
   b = n \/ False -&gt; n = a \/ n = b
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="tactic">intro</span> <span class="id" type="var">H5</span>;<span class="id" type="var">contradiction</span>.
</div></command-coqdoc></frame><frame frameNumber="27"><command>
  (** Now I am glueing things together a bit: *)</command><response/><command-coqdoc><div class="doc" divNumber="0">
Now I am glueing things together a bit: 
</div></command-coqdoc></frame><frame frameNumber="28"><command>
intro H6; elim H6; try contradiction; try (right; symmetry; assumption).</command><response>Proof completed.
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="tactic">intro</span> <span class="id" type="var">H6</span>; <span class="id" type="tactic">elim</span> <span class="id" type="var">H6</span>; <span class="id" type="tactic">try</span> <span class="id" type="var">contradiction</span>; <span class="id" type="tactic">try</span> (<span class="id" type="var">right</span>; <span class="id" type="tactic">symmetry</span>; <span class="id" type="tactic">assumption</span>).
</div></command-coqdoc></frame><frame frameNumber="29"><command>
Qed.</command><response>tree2_In' is defined
</response><command-coqdoc><div class="code" divNumber="0">

<br/>
<span class="id" type="keyword">Qed</span>.
</div></command-coqdoc></frame><frame frameNumber="30"><command>



</command><response/><command-coqdoc/></frame></film><scenes><scene class="doc" sceneNumber="0"><div-reference div="0" frame="0"/><div-reference div="0" frame="1"/></scene><scene class="code" sceneNumber="1"><div-reference div="0" frame="2"/></scene><scene class="doc" sceneNumber="2"><div-reference div="0" frame="3"/></scene><scene class="code" sceneNumber="3"><div-reference div="0" frame="4"/><div-reference div="0" frame="5"/></scene><scene class="doc" sceneNumber="4"><div-reference div="1" frame="5"/></scene><scene class="code" sceneNumber="5"><div-reference div="0" frame="6"/><div-reference div="0" frame="7"/></scene><scene class="doc" sceneNumber="6"><div-reference div="0" frame="8"/></scene><scene class="code" sceneNumber="7"><div-reference div="0" frame="9"/></scene><scene class="doc" sceneNumber="8"><div-reference div="0" frame="10"/></scene><scene class="code" sceneNumber="9"><div-reference div="0" frame="11"/><div-reference div="0" frame="12"/><div-reference div="0" frame="13"/></scene><scene class="doc" sceneNumber="10"><div-reference div="0" frame="14"/></scene><scene class="code" sceneNumber="11"><div-reference div="0" frame="15"/><div-reference div="0" frame="16"/></scene><scene class="doc" sceneNumber="12"><div-reference div="0" frame="17"/></scene><scene class="code" sceneNumber="13"><div-reference div="0" frame="18"/><div-reference div="0" frame="19"/><div-reference div="0" frame="20"/><div-reference div="0" frame="21"/><div-reference div="0" frame="22"/><div-reference div="0" frame="23"/><div-reference div="0" frame="24"/><div-reference div="0" frame="25"/><div-reference div="0" frame="26"/></scene><scene class="doc" sceneNumber="14"><div-reference div="0" frame="27"/></scene><scene class="code" sceneNumber="15"><div-reference div="0" frame="28"/><div-reference div="0" frame="29"/></scene></scenes></movie>