Today Translating our parsing rules to Haskell Lambda Calculus - - PowerPoint PPT Presentation

today
SMART_READER_LITE
LIVE PREVIEW

Today Translating our parsing rules to Haskell Lambda Calculus - - PowerPoint PPT Presentation

Today Translating our parsing rules to Haskell Lambda Calculus Higher-Order abstract syntax A more formal definition of substitution What is an embedded language? Exercises & more Haskell (on demand) s L s 1 N s 2 L


slide-1
SLIDE 1

Today

  • Translating our parsing rules to Haskell
  • Lambda Calculus
  • Higher-Order abstract syntax
  • A more formal definition of substitution
  • What is an embedded language?
  • Exercises & more Haskell (on demand)
slide-2
SLIDE 2

ε L s1 N s2 L s1s2 L s L (s) N

(L-1) (L-2) (N-1)

* these rules don’t reflect associativity correctly *

i ∈ Int i FExpr e1 PExpr e2 SExpr e1 + e2 SExpr e PExpr e SExpr e1 FExpr ↔ e1’ expr e2 PExpr ↔ e2’ expr e1 * e2 PExpr ↔ (Times e1’ e2’) expr e FExpr e PExpr e SExpr (e) FExpr e1 Pxpr ↔ e1’ expr e2 xpr ↔ e2’ expr e1 + e2 SExpr ↔ (Plus e1’ e2’) expr e1 PExpr ↔ e1’ expr e2 FExpr ↔ e2’ expr e1 * e2 PExpr ↔ (Times e1’ e2’) expr ↔ (Num i) expr ↔ e’ expr ↔ e’ expr ↔ e’ expr ↔ e’ expr ↔ e’ expr ↔ e’ expr

slide-3
SLIDE 3

λ-Abstractions

map even [1,2,3,4] = [False, True, False, True] map (3+) [1,2,3,4] = [4,5,6,7] map (/2) [2,4,6,8] = [1,2,3,4]

  • Example: mapping a function over lists

let f x = x * x + 5 in map f [1,2,3,4] = [6,9,14,21] map (\x -> x * x + 5) [1,2,3,4] (\x -> x * 2) 10 = 20 let f = \x -> x * x + 5 in map f [1,2,3,4]

slide-4
SLIDE 4

λ-Abstractions

map (\(x,y) -> (x + y)/2) [(4,6), (2,4), (1,7)] = [5,3,4]

  • ‘\’ is read as λ (lambda)

  • λ-expressions are supported by now in many non-functional languages

zipWith (\x -> \y -> (x + y)/2) [4,2,1] [6,4,7] = [5,3,4] zipWith (\x y -> (x + y)/2) [4,2,1] [6,4,7] = [5,3,4]

slide-5
SLIDE 5

The untyped λ-Calculus

  • Introduced in the 1936 by mathematician Alonzo Church
  • Very simple, Turing-complete formalism for computations
  • λ-terms:

id λ-term λ id.t λ-term t λ-term (t s) λ-term t λ-term s λ-term id ∈ Ident

slide-6
SLIDE 6

The untyped λ-Calculus

  • The calculus has three rules:
  • α-conversion
  • if t ≡α s, then the two terms are equivalent in the calculus
  • λ x. λ y. y ≡α λ y. λ x. x ≡α λ a λ b. b
  • β-reduction
  • (λ x.t)s can be reduced to t[x:=s]
  • (λ x. λ y. y) (λ a. a) →β λ y. y
  • (λ x. λ y. x) (λ a. a) →β λ y. λ a. a
  • (λ x. x + 1) 5 →β 5 + 1
  • η-conversion
  • λ x. (f x) is equivalent to f if x is not free in f
  • λ x. ((λ z. z + 1) x) ≡η (λ z. z + 1)
slide-7
SLIDE 7

The untyped λ-Calculus

  • How can boolean values be encoded in the calculus?
  • True: λx.λy.x
  • False: λx.λy.y
  • If: λc.λt.λe.c t e
slide-8
SLIDE 8

The untyped λ-Calculus

  • Natural numbers, arithmetic, logic operations
  • 0 := λf.λx.x
  • 1 := λf.λx.(f x)
  • 2 := λf.λx.(f (f x))
  • 3 := λf.λx.(f (f (f x)))
  • successor (+1): λn.λf.λx.(f ((n f) x))
  • addition (+): λm.λn.λf.λx.(m f (n f x))
  • multiplication(+): λm.λn.λf.λx. (m (n f ) x)
  • predecessor :

λn.λf.λx.(n (λg.λh.h (g f))(λu. x) (λu.u))

slide-9
SLIDE 9

The untyped λ-Calculus

λn.λf.λx.(n (λg.λh.h (g f))(λu.x)(λu.u))(λf.λx. f x) λf.λx.((λf.f) (λg.λh.h (g f))(λu.x)(λu.u)) λn.λf.λx.(n (λg.λh.h (g f))(λu.x)(λu.u)) (λf.f) λf.λx.((λg.λh.h (g f))(λu.x)(λu.u)) λf.λx.((λh.h ((λu.x) f))(λu.u)) λf.λx.((λh.h x)(λu.u)) λf.λx.((λu.u) x) λf.λx.x

→β →β →β →β →β →β

  • predecessor is complicated - here is the computation for “pred 1”:

≡η

(the argument of each beta-reduction step is coloured green, the binding and usage occurrences of the variable substituted is red)

slide-10
SLIDE 10

Higher-order abstract syntax

  • A problem with (first-order) abstract syntax
  • Defining and usage occurrence of variables are treated the same
  • abstract syntax doesn’t differentiate between binding and using occurrence
  • f a variable
  • it’s difficult to identify α-equivalent expressions
  • variables are just terms, like numbers

(Num i) expr t1 expr t2 expr (Times t1 t2 ) expr id Ident (Var id) expr (Var id) expr t1 expr t2 expr (Let id t1 t2 ) expr

slide-11
SLIDE 11

Higher-order abstract syntax

  • Higher-order abstract syntax has variables and abstraction as special

constructs

  • A term of the form x.t is called an abstraction
  • Structure of a higher-order term: a higher-order term can have one of four

forms: (1) a constant (e.g., int, string) (2) a variable x (3) (Operator t1 ... tn)

  • Num 4
  • Plus x (Num 4)

(4) x.t (i.e., the variable x is bound in term t)

  • x. Plus x (Num 1)
  • x.y. Plus x y
slide-12
SLIDE 12

Higher-order abstract syntax

  • Higher-order abstract syntax for let-expressions

id Ident (Var id) expr var(id) expr t1 expr t2 expr (Let id t1 t2 ) expr id Ident id expr t1 expr t2 expr (Let t1 id.t2 ) expr

first-order higher-order

id FExpr ↔ id expr id Ident e1 SExpr ↔ t1 expr e2 SExpr ↔ t2 expr let id = e1 in e2 end SExpr ↔ (Let t1 id.t2) expr

  • Mapping of concrete to higher-order syntax
  • Example:

let x = 5 in x+y SExpr ↔ (Let (Num 5) (x.Plus x y)) expr

slide-13
SLIDE 13

Substitution

Definition: A notation for substitution We write t[x:=t’] to denote a term t where all the free occurrences of x have been replaced by the term t’.

  • Example:

(Plus x y) [x :=(Num 1)] = (Plus (Num 1) y) Definition: Renaming If we replace a variable in the binding and the body of an abstraction, it is called renaming, and the resulting term is α-equivalent to the original term: x.t ≡α y.t [x:= y] if y doesn’t occur free in t (or y ∉ FV (t))

slide-14
SLIDE 14

Substitution

  • A inductive definition of FV(t):

FV (x) = {x} FV (Op t1 ... tn) = FV(t1) ∪ .... ∪ FV(tn) FV (x.t) = FV(t) \ {x}

  • Substituting one variable by another:

x[x:=y] = y z[x:=y] = z, if z≠x (Op t1 ... tn) [x:=y] = Op (t1 [x:=y]) ... (tn [x:=y]) x.t [x:=y] = x.t z.t [x:=y] = z. (t [x:=y]), if x≠z, y≠z y.t [x:=y] = undefined, if x≠y

slide-15
SLIDE 15

Substitution

  • Substituting a variable by a term u:

x [x:=u] = u z [x:=u] = z, if z≠x (Op t1 ... tn) [x:=u] = Op (t1 [x:=u]) ... (tn [x:=u]) x.t [x:=u] = x.t z.t [x:=u] = z. (t [x:=u]), if x≠z, z∉FV(u) y.t [x:=u] = undefined, if y∈FV(u)

slide-16
SLIDE 16

Domain Specific Embedded Languages

  • Instead of designing and developing language from scratch, add domain

specific instructions via library to host language

  • There are two ways to embed a language
  • shallow embedding
  • deep embedding
slide-17
SLIDE 17

Shallow Embedding

  • Shallow embedding provides fixed interpretation
  • Semantics captured in the type
  • Example: arithmetic expression language

type Expr = Float

add :: Expr -> Expr -> Expr add e1 e2 = e1 + e2 mult :: Expr -> Expr -> Expr mult e1 e2 = e1 * e2 neg :: Expr -> Expr neg e = -1 * e sampleExpr :: Expr sampleExpr = neg (mult (add 2 3) 5)

slide-18
SLIDE 18

DEEP EMBEDDING

  • Captures DSL expression as abstract syntax tree (AST), allowing multiple

interpretations

data Expr = Num Int | Var String | Plus Expr Expr | Times Expr Expr | LetBnd Ident Expr Expr

slide-19
SLIDE 19

Project

  • Deeply embedded language
  • Concrete syntax is not of concern for us
  • in real EDSLs, constructors usually wrapped in smart constructors
  • Possibilities:
  • graphics description language
  • text layout, html
  • scripting language
  • can either generate code (of your choice), or interpret it in Haskell