Running FAE Programs Natively

Similar documents
Higher-Order Functions

Non-Functional Procedures

Higher-Order Functions (Part I)

Typed Recursion {with {mk-rec : (((num -> num) -> (num -> num)) -> (num -> num)) {fun {body : ((num -> num) -> (num -> num))} {{fun {fx :

Threads. {seqn {spawn EXPR 1 } {spawn EXPR 2 }} Runs EXPR 1 and EXPR 2 in any order, even interleaved with each other

State. Substitution relies on an identifer having a fxed value

Implementing Recursion

cs173: Programming Languages Final Exam

Implementing Continuations

From FP to OOP. Start with data: class Posn { int x; int y; Posn(int x, int y) { this.x = x; this.y = y; } }

Functions and Recursion. Dr. Philip Cannata 1

cs173: Programming Languages Midterm Exam

CPSC 311, 2011W1 Midterm Exam #2 2011/11/09

CS 314 Principles of Programming Languages

Environments

CPSC 311, 2010W1 Midterm Exam #2

Functions & First Class Function Values

Parsing Scheme (+ (* 2 3) 1) * 1

Programming Languages: Application and Interpretation

"Good" vs. "Bad" Expressions. ; interp-expr FAE... -> FAE-Value

Mid-Term 2 Grades

CS 314 Principles of Programming Languages. Lecture 16

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

Type Declarations. [... <id> τ... ] <id> : τ. Γ <num> : number. Γ true : boolean. Γ false : boolean. Γ e 1 : number.

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

A LISP Interpreter in ML

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Types and evaluation

Cost of Substitution

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

An Introduction to Functions

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

6.001, Spring Semester, 1998, Final Exam Solutions Your Name: 2 Part c: The procedure eval-until: (define (eval-until exp env) (let ((return (eval-seq

Control in Sequential Languages

Programming Languages: Application and Interpretation

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Procedures. EOPL3: Section 3.3 PROC and App B: SLLGEN

Quiz. Question #1: What is the value of the following expression? {+ 1 2} Wrong answer: 0 Wrong answer: 42. Answer: 3 1-4

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

CS115 - Module 10 - General Trees

Random Testing in 321

User-defined Functions. Conditional Expressions in Scheme

Lecture 3: Expressions

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

How to Design Programs

Lisp. Versions of LISP

The Environment Model. Nate Foster Spring 2018

OCaml Language Choices CMSC 330: Organization of Programming Languages

;;; Determines if e is a primitive by looking it up in the primitive environment. ;;; Define indentation and output routines for the output for

Advanced Systems Programming

Typed Scheme: Scheme with Static Types

Programming Languages and Compilers (CS 421)

Creating Heap Data Structures

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

An introduction to Scheme

CS 61A Discussion 8: Scheme. March 23, 2017

n What is its running time? 9/18/17 2 n poor_rev [1,2,3] = n (poor_rev [1] = n ((poor_rev [1] =

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

CMSC330. Objects, Functional Programming, and lambda calculus

Typing Control. Chapter Conditionals

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

6.001: Structure and Interpretation of Computer Programs

Principles of Programming Languages

Konzepte von Programmiersprachen

Compiler Construction Lecture 05 A Simple Stack Machine. Lent Term, Lecturer: Timothy G. Griffin. Computer Laboratory University of Cambridge

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

An Explicit-Continuation Metacircular Evaluator

The Environment Model

CS153: Compilers Lecture 15: Local Optimization

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

CS558 Programming Languages

Principles of Programming Languages 2017W, Functional Programming

Racket: Modules, Contracts, Languages

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Review of Activation Frames. FP of caller Y X Return value A B C

Type Checking and Type Inference

Design Issues. Subroutines and Control Abstraction. Subroutines and Control Abstraction. CSC 4101: Programming Languages 1. Textbook, Chapter 8

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

C#.Net. Course Contents. Course contents VT BizTalk. No exam, but laborations

M. Snyder, George Mason University LAMBDA CALCULUS. (untyped)

Subroutines. Subroutines. The Basics. aka: user-defined functions, methods, procdures, sub-procedures, etc etc etc.

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

System Functions and their Types in Shen

CS 222: Pointers and Manual Memory Management

Type Soundness. Type soundness is a theorem of the form If e : τ, then running e never produces an error

CMSC 330: Organization of Programming Languages. Operational Semantics

Programming Languages Assignment #7

Principles of Programming Languages

June 14, Contents 1 Introduction 3 2 Proling in the ML Kit 4 3 Region inference Region annotated lambda program : : : : : : : : : : : : :

CMSC 430 Introduction to Compilers. Fall Everything (else) you always wanted to know about OCaml (but were afraid to ask)

Code example: sfact SICP Explicit-control evaluator. Example register machine: instructions. Goal: a tail-recursive evaluator.

CSC324 Functional Programming Efficiency Issues, Parameter Lists

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

Transcription:

Running FAE Programs Natively So far: explained various language constructs by using Scheme Explaining Scheme? Certain parts explained using simpler parts of Scheme Leftover parts explained by reduction rules Today: leftover parts of Scheme to even simpler parts bottoming out in something like assembly language 1

Step 1: Simpler Representation of Continuations Old build continuations out of procedures ; FAE SubCache (FAE-Value -> alpha) -> alpha (define (interp a-fae sc k) [add (l r) (interp l sc (lambda (v1) (interp r sc (lambda (v2) (k (num+ v1 v2))))))] ) 2

Step 1: Simpler Representation of Continuations New build continuations as records ; FAE SubCache FAE-Cont -> FAE-Value (define (interp a-fae sc k) [add (l r) (interp l sc (addsecondk r sc k))] ) 3

Step 1: Simpler Representation of Continuations Old apply continuations as procedures ; FAE SubCache (FAE-Value -> alpha) -> alpha (define (interp a-fae sc k) [num (n) (k (numv n))] ) 4

Step 1: Simpler Representation of Continuations New apply continuations as records ; FAE SubCache FAE-Cont -> FAE-Value (define (interp a-fae sc k) [num (n) (continue k (numv n))] [add (l r) (interp l sc (addsecondk r sc k))] ) ; continue : FAE-Cont FAE-Value -> FAE-Value (define (continue k v) (type-case FAE-Cont k [mtk () v] [addsecondk (r sc k) (interp r sc (doaddk v k))] )) 5

Step 1: Simpler Representation of Continuations (define-type FAE-Cont [mtk] [addsecondk (r FAE?) (sc SubCache?) (k FAE-Cont?)] ) Count every lambda used to generate a continuation and add a corresponding variant to FAE-Cont One field for each free variable in the lambda 6

Step 2: Replace Symbols with Numbers We ve done this step before: compile converts a FAE to a CFAE (define-type FAE [id (name symbol?)] ) (define-type CFAE [cid (pos number?)] ) 7

Step 2: Replace Symbols with Numbers We ve done this step before: pre-compute substitution positions ; compile : FAE CSubCache -> CFae (define (compile a-fae sc) (type-case FAE a-fae [id (name) (cid (locate name sc))] [fun (param body-expr) (cfun (compile body-expr (acsub param sc)))] )) 8

Step 2: Replace Symbols with Numbers We ve done this step before: use simple list for substitutions at run-time ; interp : FAE SubCache FAE-Cont -> FAE-Value (define (interp a-fae sc k) [cid (pos) (continue k (list-ref sc pos))] ) ; continue : FAE-Cont FAE-Value -> FAE-Value (define (continue k v) [doappk (fun-val k) (interp (closurev-body fun-val) (cons v (closurev-sc fun-val)) k)]) 9

Step 3: Replace Function Calls with Gotos Aside from building records and using primitives like +, all function calls are in tail position (define (interp a-fae sc k) (type-case CFAE a-fae [cnum (n) (continue )] [cadd (l r) (interp )] [csub (l r) (interp )] [cid (pos) (continue )] [cfun (body-expr) (continue )] [capp (fun-expr arg-expr) (interp )] [cif0 (test-expr then-expr else-expr) (interp )])) (define (continue k v) (type-case CFAE-Cont k [mtk () v] [addsecondk (r sc k) (interp )] [doaddk (v1 k) (continue )] [subsecondk (r sc k) (interp )] [dosubk (v1 k) (continue )] [appargk (arg-expr sc k) (interp )] [doappk (fun-val k) (interp )] [doifk (then-expr else-expr sc k) (if (numzero? v) (interp ) (interp ))])) 10

Step 3: Replace Function Calls with Gotos Aside from building records and using primitives like +, all function calls are in tail position Change each to set! plus a 0-argument call Old: (define (interp a-fae sc k) (type-case CFAE a-fae [cadd (l r) (interp l sc (addsecondk r sc k))] )) New: (define fae-reg (cnum 0)) (define sc-reg empty) ; interp : -> void (define (interp) (type-case CFAE fae-reg [cadd (l r) (begin (set! fae-reg l) (set! k-reg (addsecondk r sc-reg k-reg)) (interp))] )) 11

Step 4: Replace Datatypes with Kons Eliminate define-datatype and type-case by using a single datatype and case (define (any? x) true) (define-type Pair [kons (first any?) (rest any?)]) (define fst kons-first) (define rst kons-rest) 12

Step 4: Replace Datatypes with Kons Eliminate define-datatype and type-case by using a single datatype and case Old: (type-case CFAE fae-reg [cadd (l r) (set! k-reg (addsecondk r sc-reg k-reg)) ]) New: (case (fst fae-reg) [(9) (set! k-reg (kons 1 (kons (rst (rst fae-reg)) (kons sc-reg k-reg)))) ]) 13

Step 4: Replace Datatypes with Kons Eliminate define-datatype and type-case by using a single datatype and case mtk 0 addsecondk 1 doaddk 2 cnum 8 cadd 9 numv 15 closurev 16 14

Step 4: Replace Datatypes with Kons Eliminate define-datatype and type-case by using a single datatype and case Use kons for substitutions, too (define (interp) (case (fst fae-reg) [(11) (begin ; id (set! sc2-reg sc-reg) (set! v-reg (rst fae-reg)) (sc-ref))] )) (define sc2-reg 0) (define (sc-ref) (if (zero? v-reg) (begin (set! v-reg (fst sc2-reg)) (continue)) (begin (set! sc2-reg (rst sc2-reg)) (set! v-reg (- v-reg 1)) (sc-ref)))) 15

Step 5: Replace Pair Datatype with Malloc Simulate malloc using a vector: (define memory (make-vector 2048)) (define ptr 0) ; kons : number number -> number (define (kons a b) (begin (vector-set! memory ptr a) (vector-set! memory (+ ptr 1) b) (set! ptr (+ ptr 2)) (- ptr 2))) ; fst : number -> number (define (fst n) (vector-ref memory n)) ; rst : number -> number (define (rst n) (vector-ref memory (+ n 1))) 16

Step 6: Deallocation Next time 17