Skip to content

Instantly share code, notes, and snippets.

@donovancrichton
Created October 25, 2022 01:23
Show Gist options
  • Save donovancrichton/061baeb88d7c73160ff35c3833da7b35 to your computer and use it in GitHub Desktop.
Save donovancrichton/061baeb88d7c73160ff35c3833da7b35 to your computer and use it in GitHub Desktop.
Pigeonhole Principle in Idris
module PigeonHole
import Data.Nat
import Data.Vect
%default total
data Elem : a -> Vect k a -> Type where
Here : Elem x (x :: xs)
There : Elem x xs -> Elem x (y :: xs)
sum' : Vect k Nat -> Nat
sum' [] = 0
sum' (x :: xs) = S (sum' xs)
pigeonHole : (xs : Vect k Nat) -> (prf : LTE (S k) (PigeonHole.sum' xs)) -> (n : Nat ** (Elem n xs, LTE 2 n))
pigeonHole [] prf = absurd prf
pigeonHole (x :: xs) prf =
case isLTE 2 x of
No contra =>
let (m ** (prf', prf2')) = pigeonHole xs (fromLteSucc prf)
in (m ** (There prf', prf2'))
Yes p => (x ** (Here, p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment