Skip to content

Instantly share code, notes, and snippets.

@cronokirby
Created August 8, 2020 20:14
Show Gist options
  • Select an option

  • Save cronokirby/106bbba1ad8a82f34cab87020215cbcb to your computer and use it in GitHub Desktop.

Select an option

Save cronokirby/106bbba1ad8a82f34cab87020215cbcb to your computer and use it in GitHub Desktop.
{-# LANGUAGE RankNTypes #-}
newtype Nat = Nat { unNat :: forall m. Monoid m => m -> m }
instance Num Nat where
Nat f + Nat g = Nat (\m -> f m <> g m)
Nat f * Nat g = Nat (\m -> f (g m))
abs = id
negate _ = error "negate @Nat"
signum (Nat f) = if f [()] == [] then 0 else 1
fromInteger 0 = Nat (const mempty)
fromInteger x = Nat (\m -> m <> unNat (fromInteger (x - 1)) m)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment