-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collisions.agda
31 lines (24 loc) · 970 Bytes
/
Collisions.agda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{-# OPTIONS --without-K --exact-split --safe --no-unicode #-}
{-
Based on Software Foundations Vol 3 : Verified Functional Algoriths
by Andrew W. Appel
Chapter 12 Number Representations and Efficient Lookup Tables (Trie)
https://softwarefoundations.cis.upenn.edu/current/vfa-current/Trie.html
-}
module VerifiedAlgos.Collisions where
open import Data.Bool
open import Data.Nat using (ℕ; suc)
open import Data.List
open import VerifiedAlgos.Maps using (TotalMap; emptyMap; update)
open import Relation.Binary.PropositionalEquality using (_≡_; refl)
loop : List ℕ → ℕ → TotalMap Bool → ℕ
loop [] c table = c
loop (x ∷ xs) c table =
if (table x)
then loop xs (suc c) table
else loop xs c (update table x true)
collisions : List ℕ → ℕ
collisions [] = 0
collisions input @ (x ∷ xs) = loop input 0 (emptyMap false)
testCollisions : collisions (3 ∷ 1 ∷ 4 ∷ 1 ∷ 5 ∷ 9 ∷ 2 ∷ 6 ∷ []) ≡ 1
testCollisions = refl