-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
import churnNum import churnNum Add combinators Add combinators
- Loading branch information
Showing
4 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{-# LANGUAGE NoImplicitPrelude #-} | ||
module Base ( | ||
identity, | ||
constant, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |