Skip to content

Commit

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

import churnNum

Add combinators

Add combinators
  • Loading branch information
vvscode committed May 8, 2018
1 parent 1cc5805 commit 53d8eca
Show file tree
Hide file tree
Showing 4 changed files with 60 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
53 changes: 53 additions & 0 deletions src/ChurchNum.hs
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions src/Combinators.hs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 53d8eca

Please sign in to comment.