TFAE Type Checker: Understanding the Grammar and Data Types - Prof. Matt Flatt, Study notes of Programming Languages

An in-depth look into the tfae (tree-folding algebraic effects) type checker. It covers the grammar and data types used in the system, including te (type environment), type, and tfae expressions. The document also includes the definition of the typecheck function, which is responsible for type checking tfae expressions.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-4gy
koofers-user-4gy 🇺🇸

7 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
TFAE Grammar
<TFAE> ::= <num>
|{+ <TFAE> <TFAE>}
|{- <TFAE> <TFAE>}
|<id>
|{fun {<id> : <TE>} <TFAE>}
|{<TFAE> <TFAE>}
<TE> ::= num
|bool
|(<TE> -> <TE>)
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download TFAE Type Checker: Understanding the Grammar and Data Types - Prof. Matt Flatt and more Study notes Programming Languages in PDF only on Docsity!

TFAE Grammar

::=^

| {+^ } | {-^ } | | {fun { :

} }

|^ { }

^

::=^ num^ |^ bool^ |^ ( -> )

TFAE Expressions and Types^ (define-type

TE

[numTE][boolTE][arrowTE

(arg

: TE)(result^ : TE)])

(define-type

Type

[numT][boolT][arrowT

(arg

: Type)(result^ : Type)])

(define-type

TypeEnv

[mtEnv][aBind

(name

: symbol)

(type

: Type)

(rest

: TypeEnv)])

TFAE Type Checker

typecheck

: (FAE

TypeEnv

-> Type)

(lambda

(fae^

env) (type-case

FAE^ fae [num^ (n) ...])))

Γ^ :

num

TFAE Type Checker

typecheck

: (FAE

TypeEnv

-> Type)

(lambda

(fae^

env) (type-case

FAE^ fae [num^ (n) (numT)])))

Γ^ :

num

TFAE Type Checker

typecheck

: (FAE

TypeEnv

-> Type)

(lambda

(fae^

env) (type-case

FAE^ fae [add^ (l r)(type-case

Type^

(typecheck

l env)

[numT^

()...^ (typecheck

r env) ...]

[else^

(type-error

l "num")])])))

Γ^ e

:^ num 1

Γ^ e

:^ num 2

Γ^ {+

ee^1

}^ :^2

num

TFAE Type Checker

typecheck

: (FAE

TypeEnv

-> Type)

(lambda

(fae^

env) (type-case

FAE^ fae [add^ (l r)(type-case

Type^

(typecheck

l env)

[numT^

()(type-case

Type^

(typecheck r env) [numT^

()^ (numT)] [else^

(type-error

r "num")])]

[else^

(type-error

l "num")])])))

Γ^ e

:^ num 1

Γ^ e

:^ num 2

Γ^ {+

ee^1

}^ :^2

num

TFAE Type Checker

typecheck

: (FAE

TypeEnv

-> Type)

(lambda

(fae^

env) (type-case

FAE^ fae [id^ (name) (get-type

name^

env)])))

[ ...^

←τ^ ... ]

:

TFAE Type Checker

typecheck

: (FAE

TypeEnv

-> Type)

(lambda

(fae^

env) (type-case

FAE^ fae [fun^ (name

te^ body)...])))^ Γ

[^

←τ]^1

e^ :^ τ

2

Γ^ {fun { :

τ}^ e^1

}^ :^ (

τ→^ τ^1

TFAE Type Checker

typecheck

: (FAE

TypeEnv

-> Type)

(lambda

(fae^

env) (type-case

FAE^ fae [fun^ (name

te^ body)(local^ [(define

arg-type

(parse-type

te))]

...^ (typecheck

body^

(aBind

namearg-typeenv))

...)])))

Γ[^

←τ]^1

e^ :^ τ

2

Γ^ {fun { :

τ}^ e^1

}^ :^ (

τ→^ τ^1

TFAE Type Checker

typecheck

: (FAE

TypeEnv

-> Type)

(lambda

(fae^

env) (type-case

FAE^ fae [fun^ (name

te^ body)(local^ [(define

arg-type

(parse-type

te))]

(arrowT

arg-type(typecheck

body^

(aBind name

arg-typeenv))))])))

Γ[^

←τ]^1

e^ :^ τ

2

Γ^ {fun { :

τ}^ e^1

}^ :^ (

τ→^ τ^1

TFAE Type Checker

typecheck

: (FAE

TypeEnv

-> Type)

(lambda

(fae^

env) (type-case

FAE^ fae [app^ (fn

arg)... (typecheck

fn env) ... ...^ (typecheck

arg env) ...])))

Γ^ e

:^ (τ 1

→^ τ) 2 3

Γ^ e

:^ τ 2 2

Γ^ {

ee }^1

:^ τ^3

TFAE Type Checker

typecheck

: (FAE

TypeEnv

-> Type)

(lambda

(fae^

env) (type-case

FAE^ fae [app^ (fn

arg)(type-case

Type^

(typecheck

fn env)

[arrowT

(arg-type

result-type) ...^ (typecheck

arg^ env) ...]

[else^

(type-error

fn "function")])])))

Γ^ e

:^ (τ 1

→^ τ) 2 3

Γ^ e

:^ τ 2 2

Γ^ {

ee }^1

:^ τ^3

Pairs

{with {pair

: (num

->^ (num

->^ (num

-> num)))

{fun^ {x : num}{fun

{y : num}{fun^

{s : num}{if0^ s x y}}}}}

{with {fst

: ((num

->^ num) ->

num)

{fun^ {p : (num

->^ num)} {p 0}}} {with {snd

: ((num

->^ num) -> num) {fun^ {p : (num

->^ num)} {p 1}}} {snd^ {{pair

1} 2}}}}}

Pairs

{with {pair : (bool

->^ (bool

->^ (num

-> bool)))

{fun^ {x : bool}{fun

{y : bool}{fun^

{s : num}{if0^ s x y}}}}}

{with {fst : ((num

->^ bool) ->

bool)

{fun^ {p : (num

->^ bool)} {p 0}}} {with {snd

: ((num

->^ bool) ->

bool)

{fun^ {p : (num

->^ bool)} {p 1}}} {snd {{pair

true} false}}}}}