24
A Brief Survey of Abstract Machines for Functional Programming Languages Prashant Kumar University of Calgary [email protected] June 6, 2014 Prashant Kumar (UofC) Abstract Machines June 6, 2014 1 / 24

A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

A Brief Survey of Abstract Machines for FunctionalProgramming Languages

Prashant Kumar

University of Calgary

[email protected]

June 6, 2014

Prashant Kumar (UofC) Abstract Machines June 6, 2014 1 / 24

Page 2: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Abstract Machines for Functional Programming languages

Some abstract machines for functional languages are as follows:-

Modern SECD

Krivine Machine

Prashant Kumar (UofC) Abstract Machines June 6, 2014 2 / 24

Page 3: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Modern SECD Machine

1 An improvement over the SECD machine .

2 Doesn’t have a dump . Stack doubles up as a dump.

Prashant Kumar (UofC) Abstract Machines June 6, 2014 3 / 24

Page 4: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Features of Modern SECD

1 a code pointer c (the instructions yet to be executed)

2 a stack s (holding intermediate result and pending function calls )

3 an environment e (giving values to the variables )

4 Modern SECD uses De-Bruijn’s Indices to look into the environment.

Prashant Kumar (UofC) Abstract Machines June 6, 2014 4 / 24

Page 5: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

λ-expression to De Bruijn’s notation

Example

Lets consider (λx .xx) (λx .x) and (λx .λy .(xy)) (λx .x) (λy .y)

De Bruijn’s notation for the above lambda expression are as below(λx .xx) (λx .x) ⇒ (λ.(#1 #1))(λ. #1)

(λx .λy .(xy)) (λx .x) (λy .y) ⇒ (λ. λ.(#2 #1))(λ.#1)(λ.#1)

The De Bruijn Indices have been assumed to start from 1 here.

Prashant Kumar (UofC) Abstract Machines June 6, 2014 5 / 24

Page 6: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Data structures for Lambda and De Bruijn Lambdaexpressions

Prashant Kumar (UofC) Abstract Machines June 6, 2014 6 / 24

Page 7: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Haskell code for converting Lambda expressions toDe-Bruijn Lambda expressions

Prashant Kumar (UofC) Abstract Machines June 6, 2014 7 / 24

Page 8: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Instruction Set in Modern SECD Machine

Instruction SetConst - push the constant on the stack

Clo(c) - push closure of code c with current environment

Ret - terminate current function, jump back to caller

App - pop function closure and argument, perform application

Add - pop two arguments from the top of the stack and add

Access(n) - push n-th field of the environment

Prashant Kumar (UofC) Abstract Machines June 6, 2014 8 / 24

Page 9: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Compilation Scheme of Modern SECD Machine

Compilation Scheme

Lambda Terms CompilationJkK Const (k)JλaK Clo(JaK : Ret)JMNK JNK : JMK : AppJa + bK JbK : JaK : AddJ#nK Access (n)

Prashant Kumar (UofC) Abstract Machines June 6, 2014 9 / 24

Page 10: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Example of Compilation

Compilation

Lets have a look at the compilation of source term:

(λ x .x + 1) 2 ⇒ (λ .#1 + 1) 2

Compiled Code:

Const(2) : Clo[Const(1) : Access(1) : Add : Ret] : App

Prashant Kumar (UofC) Abstract Machines June 6, 2014 10 / 24

Page 11: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Data structures for Code , Environment and Stack inHaskell

Prashant Kumar (UofC) Abstract Machines June 6, 2014 11 / 24

Page 12: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Haskell Code for Compilation to Modern SECDCommands

Prashant Kumar (UofC) Abstract Machines June 6, 2014 12 / 24

Page 13: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Machine Transitions in Modern SECD Machine

Before After

Code Env Stack Code Env Stack

Const k : c e s c e k : s

Clo(c ′) : c e s c e Clos(c ′, e) : s

App : c e Clos(c ′, e′) : v : s c ′ v : e′ Clos(c, e) : s

Ret : c e v : Clos(c ′, e′) : s c ′ e′ v : s

Add : c e n : m : s c e (n +m) : s

Access(n); c e s c e e(n) : s

Clos(c , e) denotes closure of code “c“ with environment “e“

Prashant Kumar (UofC) Abstract Machines June 6, 2014 13 / 24

Page 14: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Start and End conditions in Modern SECD Machine

Initial State

Code = c ; Environment = Nil ; Stack = Nil

Final State

Code = Nil ; Environment = Nil ; Stack = v

Prashant Kumar (UofC) Abstract Machines June 6, 2014 14 / 24

Page 15: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Implementation of Transition Function in Haskell

Prashant Kumar (UofC) Abstract Machines June 6, 2014 15 / 24

Page 16: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Example of evaluation in Modern SECD Machine

Lets try evaluating Const(2) : Clo[Const(1) : Access(1) : Add : Ret] : Appwhich is compilation of (λ x . x + 1) 2

Let c = Const(1) : Access(1) : Add : Ret

Code Env StackConst(2) : Clo(c) : App Nil NilClo(c) : App Nil Const 2App Nil Clos(c ,Nil) : 2Const(1) : Access(1) : Add : Ret 2 Clos(Nil ,Nil)Access(1) : Add : Ret 2 1 : Clos(Nil ,Nil)Add : Ret 2 2 : 1 : Clos(Nil ,Nil)Ret 2 3 : Clos(Nil ,Nil)Nil Nil 3

Prashant Kumar (UofC) Abstract Machines June 6, 2014 16 / 24

Page 17: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Instruction Sets in Krivine Machine

1 Constant

2 Grab

3 Access(n)

4 Push(c)

Compilation Scheme

Lambda Terms Compilation

JkK Constant k

JλaK Grab : JaKJ#nK Access(n)

JMNK Push( JNK) : JMK

Prashant Kumar (UofC) Abstract Machines June 6, 2014 17 / 24

Page 18: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

krivine Data Structures in Haskell

Prashant Kumar (UofC) Abstract Machines June 6, 2014 18 / 24

Page 19: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Haskell Code for Compilation to krivine Commands

Prashant Kumar (UofC) Abstract Machines June 6, 2014 19 / 24

Page 20: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Example of Compilation in Krivine Machine

Lets consider (λx .xx)(λx .x)

De Bruijn’s notation for the above lambda expression is(λ.(#1#1))(λ.#1)

J(λ.(#1#1))(λ.#1)K

Push(Jλ.#1K) : Jλ.(#1#1)K

Push(Grab, J#1K) : Grab : J(#1#1)K

Push(Grab,Access(1)) : Grab : Push(J#1K) : J#1K

Push(Grab,Access(1)) : Grab : Push(Access(1)) : Access(1)

Prashant Kumar (UofC) Abstract Machines June 6, 2014 20 / 24

Page 21: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Machine Transitions in Krivine Machine

Before AfterCode Env Stack Code Env StackAccess(1) : c Clos(c0, e0) : e s c0 e0 sAccess(n + 1) : c Clos(c0, e0) : e s Access(n) e s

Push(c′) : c e s c e Clos(c′, e)Grab : c e Clos(c′, e′) : s c Clos(c′, e′) : e s

Prashant Kumar (UofC) Abstract Machines June 6, 2014 21 / 24

Page 22: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Start and End conditions in Krivine Machine

Initial State

Code = c ;Environment = Nil ;Stack = Nil

Final State

Code = Grab : c ;Environment = Nil ;Stack = Nil

The code is decompiled to get the result.

Prashant Kumar (UofC) Abstract Machines June 6, 2014 22 / 24

Page 23: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Haskell Code for Implementation of the Transitionfunction of krivine Machine

Prashant Kumar (UofC) Abstract Machines June 6, 2014 23 / 24

Page 24: A Brief Survey of Abstract Machines for Functional ...pages.cpsc.ucalgary.ca/~robin/FMCS/FMCS2014/Prashant_talk.pdf · A Brief Survey of Abstract Machines for Functional Programming

Example of evaluation in Krivine Machine

Lets consider the λ-expression (λx .xx)(λx .x).

This translates to the following.Push(Grab,Access(1)) : Grab : Push(Access(1)) : Access(1)

Let P,G and A stand for Push,Grab and Access respectively.

Code Env StackP(G : A(1)) : G : P(A(1)) : A(1) Nil NilG : P(A(1)) : A(1) Nil Clos((G : A(1)),Nil)P(A(1)) : A(1) Clos((G : A(1)),Nil) NilA(1) Clos((G : A(1)),Nil) Clos(A(1), Clos((G : A(1)),Nil)G : A(1) Nil Clos(A(1), Clos((G : A(1)),Nil)A(1) Clos(A(1), Clos((G : A(1)),Nil) NilA(1) Clos((G : A(1)),Nil) NilG : A(1) Nil Nil

G ;A(1) decompiles to λ .1 which is λx .x

Prashant Kumar (UofC) Abstract Machines June 6, 2014 24 / 24