Skip to content

Instantly share code, notes, and snippets.

@domandlj
Created March 3, 2025 06:07
Show Gist options
  • Select an option

  • Save domandlj/c2836578ea713397f052d84b0a53966c to your computer and use it in GitHub Desktop.

Select an option

Save domandlj/c2836578ea713397f052d84b0a53966c to your computer and use it in GitHub Desktop.
Filter
module FilterProof where
open import Data.List hiding (filter; concat)
open import Data.Bool
import Relation.Binary.PropositionalEquality as Eq
open Eq using (_≡_; refl; trans; sym; cong; cong-app; subst)
open Eq.≡-Reasoning using (begin_; _≡⟨⟩_; step-≡; _∎)
private variable
A : Set
B : Set
filter : (A → Bool) → List A → List A
filter f [] = []
filter f (x ∷ xs) =
if f x then x ∷ filter f xs else filter f xs
concat : List (List A) → List A
concat [] = []
concat (x ∷ xs) = x ++ (concat xs)
return : A → List A
return x = [ x ]
_>>=_ : List A → (A → List B) → List B
xs >>= f = concat (map f xs)
mfilter : (A → Bool) → List A → List A
mfilter f xs = xs >>= (λ x → if f x then return x else [])
filter-eq : ∀ f (xs : List A) → filter f xs ≡ mfilter f xs
filter-eq f [] = refl
filter-eq f (x ∷ xs) with f x
... | true =
begin
x ∷ filter f xs
≡⟨ cong (λ t → x ∷ t) hip ⟩
x ∷ mfilter f xs
where
hip = filter-eq f xs
... | false =
begin
filter f xs
≡⟨ hip ⟩
mfilter f xs
where
hip = filter-eq f xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment