Code Monkey home page Code Monkey logo

lambda-calculus-haskell's Introduction

Lambda Calculus with Church Encodings

An implementation of lambda calculus and Church encodings in Haskell, with a focus on understanding the basic concepts and applications of these concepts.

Lambda Calculus

Lambda calculus is a formal system for expressing computation. It consists of three types of expressions:

  • Variables, denoted by $x$.
  • Abstraction, denoted by $\lambda x. M$, which represents a function that takes an argument $x$ and returns the expression $M$.
  • Application, denoted by $e_1$ $e_2$, which applies the expression $e_1$ to the argument $e_2$.

In this implementation, the datatype to represent the abstract syntax of the λ-calculus is the following:

data LambdaTerm
  = Variable String
  | Application LambdaTerm LambdaTerm
  | Lambda String LambdaTerm

For instance, the term: (λx.λy.x y) y is represented as follows:

(Lambda "x" (Lambda "y" (Application (Variable "x") (Variable "y")))) (Variable "y")

In lambda calculus, a term is said to be closed if it has no free variables, and open if it does. The process of simplifying an expression by applying substitutions is called beta-reduction, and is defined by the rule $(\lambda x. e_1) e_2 \rightarrow e_1[e_2/x]$. This implementation utilizes these concepts to implement two algorithms:

  • Capture-avoiding substitution, a method for replacing variables in a lambda term without altering the meaning of the term.
  • Beta reduction, a process for simplifying expressions by applying a single step of beta-reduction.

Church Encodings

Church encodings are a way of representing data and operators in lambda calculus. This implementation includes encodings for several common data structures, including:

  • Booleans, represented by λx.λy.x for true and λx.λy.y for false.

    newtype ChurchBoolean = ChurchBoolean (forall r. r -> r -> r)
    
    -- false = λx.λy.y
    churchFalse :: ChurchBoolean
    churchFalse = ChurchBoolean $ \_ y -> y
    
    -- true = λx.λy.x
    churchTrue :: ChurchBoolean
    churchTrue = ChurchBoolean $ \x \_ -> x
    
    -- Convert Church Boolean to boolean
    boolUnchurch :: ChurchBoolean -> Bool
    boolUnchurch (ChurchBoolean b) = b True False
    
    -- Convert boolean to Church Boolean
    boolChurch :: Bool -> ChurchBoolean
    boolChurch b = if b then churchTrue else churchFalse
    
  • Numerals, represented by λf.λx.x for 0, λf.λx.f x for 1, and so on.

    newtype ChurchNum = ChurchNum (forall r. (r -> r) -> r -> r)
    
    -- 0 = λf.λx.x
    churchZero :: ChurchNum
    churchZero = ChurchNum $ \_ x -> x
    
    -- Represent any numeral as Church numeral
    -- num n = λf.λx.f (numChurch (n-1) f x)
    numChurch :: Integer -> ChurchNum
    numChurch 0 = ChurchNum $ \_ x -> x
    numChurch n = successor $ numChurch (n - 1)
    
  • Pairs, represented by λp.p true for the first element, and λp.p false for the second element.

Run examples

Install dependencies:

cabal install

Run the tests:

cabal test --test-show-details=direct

Acknowledgment

Assignment from "Theory of Programming Languages" at Universidad Complutense de Madrid and Universidad Politécnica de Madrid. Prof. Julio Mariño.

lambda-calculus-haskell's People

Contributors

flandrade avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.