lambda-phi / lambda / Lambda

A simple Lambda calculus implementation with type inference.


type Type
    = IntType
    | NumType
    | Type String
    | AbsType Type Type

A Lambda calculus type.


type Expr
    = Int Basics.Int
    | Num Basics.Float
    | Var String
    | Abs String Expr
    | App Expr Expr

A Lambda calculus expression.


type Error
    = SyntaxError Parser.Error
    | VariableNotFound String
    | TypeMismatch Type Type

An error from parsing or evaluation.

evaluate : Expr -> Result Error ( Expr, Type )

Evaluates an expression and returns either an (Expr, Type) pair, or an Error.

import Lambda

-- Builtin values
evaluate (Int 42) --> Ok (Int 42, IntType)
evaluate (Num 3.14) --> Ok (Num 3.14, NumType)

-- Variable: x
evaluate (Var "x") --> Err (VariableNotFound "x")

-- Abstraction: λx.x
evaluate (Abs "x" (Var "x")) --> Ok (Abs "x" (Var "x"), AbsType (Type "a") (Type "a"))

-- Application: (λx.x) 42
evaluate (App (Abs "x" (Var "x")) (Int 42)) --> Ok (Int 42, IntType)