diff --git a/app/Main.hs b/app/Main.hs index a2e2765..7338a82 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,6 +1,7 @@ module Main where import Base +import ChurchNum import Lib main :: IO () diff --git a/src/Base.hs b/src/Base.hs index a64fc92..4f0bbc5 100644 --- a/src/Base.hs +++ b/src/Base.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE NoImplicitPrelude #-} module Base ( identity, constant, diff --git a/src/ChurchNum.hs b/src/ChurchNum.hs new file mode 100644 index 0000000..0cc662a --- /dev/null +++ b/src/ChurchNum.hs @@ -0,0 +1,50 @@ +module ChurchNum ( + zero, -- point free + one, -- point free + two, -- point free + inc, -- point free + dec , + add, + sub, + mul, -- point free + church, + unchurch,-- point free + isZero +) where + +import Base + +-- zero :: p2 -> t3 -> t3 +zero = Base.flip constant + +-- one :: (t1 -> t2) -> t1 -> t2 +one = apply + +-- two :: (t -> t) -> t -> t +-- @help: can't figure out point free version +two x y = x $ x y + +-- inc :: Num a => a -> a +inc = (+1) + +-- dec :: Num a => a -> a +dec x = x - 1 + +-- add :: Num a => a -> a -> a +add = (+) + +-- sub :: Num a => a -> a -> a +sub = (-) + +-- mult :: Num a => a -> a -> a +mul a b = church a (+b) 0 + +-- church :: (Eq t1, Num t1) => t1 -> (t2 -> t2) -> t2 -> t2 +church 0 = zero +church n = \f x -> f $ church (n -1 ) f x + +-- unchurch :: ((Integer -> Integer) -> Integer -> t3) -> t3 +unchurch = Base.flip ($ (1 +)) 0 + +-- isZero :: (Eq a, Num a) => a -> Bool +isZero = (==) 0