Tries
Due: Thursday, February 13, 00:00AM
Your task is to implement a trie in Coq. The type of tries should be parameterized by the type of the element, but type of keys should simply be binary strings. You need to implement the following functions:
Parameter insert : forall (A : Set), bitString -> A -> trie A -> trie A
Parameter search : forall (A : Set), bitString -> trie A -> option A
And prove the following theorem:
Axiom insert_then_search :
forall (A : Set) (k : bitString) (v : A) (t : trie A),
search k (insert k v t) = Some v.
Hints
Type Definitions
You can use a list of booleans to represent the bit-string. On the trie, I suggest storing values at internal nodes and letting the leaves be empty. You probably want the value to be optional.
You may use this definition:
Inductive bits : Set :=
| EmptyBits : bits
| Bit : bool -> bits -> bits.
Inductive trie : Set :=
| Leaf : trie
| Node : trie -> bits -> option A -> trie -> trie.
Function Definitions
In your definitions for search
and insert
, I strongly recommend recurring on the key and not on the trie.
Theorem Proving
There is a lot of case-analysis required in the proof of insert_then_search
. I recommend breaking up the cases in the following order:
- Induction on the key, then
- Case analysis on the trie, then
- Case analysis on the head of the two bit strings
The proof is quite long, and will be easier to do if you first prove these lemmas.
Axiom search_insert_top :
forall k v lhs rhs, search' k (Node lhs k (Some v) rhs) = Some v.
Axiom insert_produces_node :
forall k v t, exists lhs rhs k' v',
insert k v t = Node lhs k' v' rhs.
Axiom search_rec : forall k b v w t1 t2 k',
search (Bit b k) (insert (Bit b k) v (Node t1 (Bit b k') w t2)) =
search k (insert k v (Node t1 k' w t2)).
Axiom correct_even : forall b k k' v w t1 t2,
(forall (v : A) (t : trie), search' k (insert' k v t) = Some v) ->
search' (Bit b k) (insert' (Bit b k) v (Node t1 (Bit b k') w t2)) = Some v.
Axiom correct_odd : forall b b' k k' v w t1 t2,
(forall (v : A) (t : trie), search' k (insert' k v t) = Some v) ->
b <> b' ->
search' (Bit b k) (insert' (Bit b k) v (Node t1 (Bit b' k') w t2)) = Some v.