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..ee980f0 --- /dev/null +++ b/src/ChurchNum.hs @@ -0,0 +1,53 @@ +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 +import Combinators + +-- 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 (cause s-combinator is not) +-- two x y = x $ x y +two = s Base.compose identity + +-- inc :: Num a => a -> a +inc = (+1) + +-- dec :: Num a => a -> a +-- dec x = x - 1 +dec = Base.flip (-) 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 diff --git a/src/Combinators.hs b/src/Combinators.hs new file mode 100644 index 0000000..88490ff --- /dev/null +++ b/src/Combinators.hs @@ -0,0 +1,5 @@ +module Combinators where + +s f g x = f x (g x) -- S-combinator +identity x = x -- I-combinator +constant x y = y -- K -combinator