Page 1

Implementing a Small Functional Language

Page 2

Why implement a functional language in a functional language?

Page 3

Symbolic Expressions

Remember this from last week?

Page 4

Adding a boolean operator

Page 5

Adding error values

Page 6

Adding functions

The λ-calculus (lambda calculus)

Page 7

Adding functions to our small language

data Expr = Num Integer
          | Var Name
          | Add Expr Expr
          | Mul Expr Expr
          | Equal Expr Expr
          | App Expr Expr      -- new
          | Lambda Name Expr   -- new
Page 8

Adding functions to our Value types?

How do we represent the value of an anonymous function?

Page 9

Adding functions to our Value types

Page 10

Computing with functions

eval env (App fun arg)   = apply (eval env fun) (eval env arg)
eval env (Lambda x body) = F (\ v -> eval ((x:v):env) body)

apply :: Value -> Value -> Value
apply (F f) v = f v
apply _     _ = E "non-function applied to an argument"
Page 11

Testing our small functional language

Page 12

Adding some predefined values in our environment

initial_env = [("false",B False),
               ("true",B True),
Page 13

Recursive functions

Page 14

Testing recursive functions

Page 15

Possible improvements