Created
January 12, 2018 23:26
-
-
Save martinsson/a4ff8af307fd23104232fa9321707ac6 to your computer and use it in GitHub Desktop.
Strongly typed coin change kata in idris
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Change : (amount: Nat) -> Type where | |
NoChange : Change Z | |
SucChange : (prev: Change amount) -> Change (S amount) | |
Composite : (ch1: Change amount1) -> (ch2: Change amount2) -> | |
{auto prf: amount1 + amount2 = amount'}-> Change amount' | |
-- Simple : (value: Nat) -> (quantity: Nat) -> | |
-- {auto prf: value * quantity = amount'} -> Change amount' | |
extractAmount : (Change amount) -> Nat | |
extractAmount {amount} _ = amount | |
Eq (Change amount) where | |
(==) x y = (extractAmount x) == (extractAmount y) | |
change : (amount: Nat) -> Change amount | |
change Z = NoChange | |
change (S k) = rewrite (sym $ plusZeroRightNeutral k) in | |
Composite (SucChange (change k)) NoChange | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment