Skip to content

Commit

Permalink
Add curhnum
Browse files Browse the repository at this point in the history
import churnNum

import churnNum
  • Loading branch information
vvscode committed May 8, 2018
1 parent 1cc5805 commit b816b9d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Main where

import Base
import ChurchNum
import Lib

main :: IO ()
Expand Down
1 change: 1 addition & 0 deletions src/Base.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE NoImplicitPrelude #-}
module Base (
identity,
constant,
Expand Down
50 changes: 50 additions & 0 deletions src/ChurchNum.hs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b816b9d

Please sign in to comment.