Abstract machines & interpreters

Slides:



Advertisements
Similar presentations
Substitution & Evaluation Order cos 441 David Walker.
Advertisements

Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Elements of Lambda Calculus Functional Programming Academic Year Alessandro Cimatti
David Evans CS150: Computer Science University of Virginia Computer Science Lecture 39: Lambda Calculus.
6. Introduction to the Lambda Calculus. © O. Nierstrasz PS — Introduction to the Lambda Calculus 6.2 Roadmap  What is Computability? — Church’s Thesis.
Tim Sheard Oregon Graduate Institute Lecture 11: A Reduction Semantics for MetaML CS510 Section FSC Winter 2005 Winter 2005.
Reflexive -- First sentence of proof is: (1) Let x  Z (2) Let (x,x)  R. (3) Let (x,x)  I (4) Let x  R.
David Evans CS200: Computer Science University of Virginia Computer Science Class 31: Universal Turing Machines.
Lecture 20: λ Calculus λ Calculus & Computability Yan Huang Dept. of Comp. Sci. University of Virginia.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
9.1 To evaluate square roots Objective Part I Evaluating Square Roots
Closure Operators, Equivalences and Models a Readers’ Digest Stefan Kahrs.
CSE 230 The -Calculus. Background Developed in 1930’s by Alonzo Church Studied in logic and computer science Test bed for procedural and functional PLs.
1 Formal Semantics. 2 Why formalize? ML is tricky, particularly in corner cases generalizable type variables? polymorphic references? exceptions? Some.
Principles of programming languages 9: Lambda calculus Isao Sasano Department of Information Science and Engineering.
Lambda Calculus Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 11.
-Calculus Kangwon National University 임현승 Programming Languages These slides are based on the slides by Prof. Sungwoo Park at POSTECH.
PART 10 Pattern Recognition 1. Fuzzy clustering 2. Fuzzy pattern recognition 3. Fuzzy image processing FUZZY SETS AND FUZZY LOGIC Theory and Applications.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L04Ext-1 September 21, 2006http:// Some more thoughts.
CSE-321 Programming Languages -Calculus (II) POSTECH March 26, 2007 박성우.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L03-1 September 14, 2006http:// -calculus: A Basis for.
A LISP interepreter for the Lambda Calculus Functional Programming
1.3 Properties of Numbers 8/24/16. Common Core State Standards Interpret complicated expressions by viewing one or more of their parts as a single entity.
Operational Semantics of Scheme
CS5205: Foundations in Programming Languages
Evaluating Expressions and Combining Like Terms
Evaluating Expressions and Combining Like Terms
Introduction to the λ-Calculus and Functional Programming Languages
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Lambda Calculus CSE 340 – Principles of Programming Languages
Formal Methods in software development
CS510 Compiler Lecture 4.
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
Deterministic FA/ PDA Sequential Machine Theory Prof. K. J. Hintz
More on Lambda Calculus
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
SSA in Scheme Static single assignment (SSA) :
Equivalence, DFA, NDFA Sequential Machine Theory Prof. K. J. Hintz
First-class continuations
CSCE 355 Foundations of Computation
Lesson2 Lambda Calculus Basics
Non-Deterministic Finite Automata
Carlos Varela Rennselaer Polytechnic Institute September 4, 2015
Programming Languages and Compilers (CS 421)
Evaluating Expressions and Combining Like Terms
Programming Languages and Compilers (CS 421)
Class 36: The Meaning of Truth CS200: Computer Science
Programming Languages and Compilers (CS 421)
Equivalent Ratio In the ratio a : b if the terms ‘a’ and ‘b’ are multiplied by the same non zero number, we get equivalent ratios.
Extending our interpreted language with Scheme’s set!
Organization of Programming Languages
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Formal Methods in software development
Announcements Exam 2 on Friday, November 2nd Topics
L Calculus.
CS21 Decidability and Tractability
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Class 31: Universal Turing Machines CS200: Computer Science
Programming Languages and Compilers (CS 421)
Bell Ringer (NWEA) 1. Katie completed the following problem and got it incorrect. Can you tell Katie what you did wrong?  
CSE S. Tanimoto Lambda Calculus
OBJ first-order functional language based on equational logic
CS 611: Lecture 10 More Lambda Calculus September 20, 1999
Evaluating Expressions and Combining Like Terms
Factorization by identity (x + a)(x + b).
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Presentation transcript:

Abstract machines & interpreters

(Term rewriting semantics) recap

(→*) (→) = (→β) ∪ (→α) ∪ (→η) (→) = (→β) ∪ (→α) ∪ (→η) (→*) A non-deterministic pre-order (i.e., reflexive, transitive)

α α α α α e0 e1 e2 e3 e4 e5

α α α α α e0 e1 e2 e3 e4 e5 β η e6

α α α α α e0 e1 e2 e3 e4 e5 β η e6 β β β

α α α α α e0 e1 e2 e3 e4 e5 β β e10 e7 β β e11 e8 β e9

* * α α α α α e0 e1 e2 e3 e4 e5 β β e10 e7 β β e11 e8 β e9 e12 Confluence (i.e., Church-Rosser Theorem) α α α α α e0 e1 e2 e3 e4 e5 β β e10 e7 β β e11 e8 β * e9 * e12

{ { ((λ (x) E0) E1) →β E0[x ← E1] redex

(E0 E1)[x ← E] = (E0[x ← E] E1[x ← E]) x[x ← E] = E y[x ← E] = y where y ≠ x (E0 E1)[x ← E] = (E0[x ← E] E1[x ← E]) (λ (x) E0)[x ← E] = (λ (x) E0) (λ (y) E0)[x ← E] = (λ (y) E0[x ← E]) where y ≠ x and y ∉ FV(E) β-reduction cannot occur when y ∈ FV(E)

(... (λ (x) ((λ (a) (λ (x) a)) (λ (b) x))) ...) ... (λ (x) (λ (x) (λ (b) x)))

(λ (y) E0)[x ← E] = (λ (y) E0[x ← E]) (... (λ (x) ((λ (a) (λ (x) a)) (λ (b) x))) ...) (λ (y) E0)[x ← E] = (λ (y) E0[x ← E]) where y ≠ x and y ∉ FV(E)

Evaluation contexts ℰ ::= (ℰ e) | (v ℰ) | □ v ::= (λ (x) e) e ::= (λ (x) e) | (e e) | x ℰ ::= (ℰ e) | (v ℰ) | □

(((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w)) Context and redex { { r ℰ[(v v)] = (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w)) ℰ = (□ (λ (w) w)) r = ((λ (x) ((λ (y) y) x)) (λ (z) z))

Abstract Machines

(e0, env) ⇓ ((λ (x) e2), env’) (e1, env) ⇓ v1 (e2, env’[x ↦ v1]) ⇓ v2 ((e0 e1), env) ⇓ v2 ((λ (x) e), env) ⇓ ((λ (x) e), env) (x, env) ⇓ env(x)

Big-step (Natural) Semantics

(e0 e1), env e’, env’

e0 e1 (e0 e1), env e’, env’

(define (interp e env) (match e [(? symbol? x) (hash-ref env x)] [`(λ (,x) ,e0) `(clo (λ (,x) ,e0) ,env)] [`(,e0 ,e1) (define v0 (interp e0 env)) (define v1 (interp e1 env)) (match v0 [`(clo (λ (,x) ,e2) ,env) (interp e2 (hash-set env x v1))])]))

(define (interp e env) (match e [(? symbol? x) (hash-ref env x)] [`(λ (,x) ,e0) `(clo (λ (,x) ,e0) ,env)] [`(,e0 ,e1) (define v0 (interp e0 env)) (define v1 (interp e1 env)) (match v0 [`(clo (λ (,x) ,e2) ,env) (interp e2 (hash-set env x v1))])]))

(e0, env) ⇓ ((λ (x) e2), env’) (e1, env) ⇓ v1 (e2, env’[x ↦ v1]) ⇓ v2 ((e0 e1), env) ⇓ v2 ((λ (x) e), env) ⇓ ((λ (x) e), env) (x, env) ⇓ env(x)

(((λ (x) …) (λ (z) z)), ∅) ⇓ … ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))

((λ (x) …), ∅) ⇓ ((λ (x) …), ∅) (((λ (x) …) (λ (z) z)), ∅) ⇓ … ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))

((λ (z) z), ∅) ⇓ ((λ (z) z), ∅) … ⇓ ((λ (x) …), ∅) ((λ (z) z), ∅) ⇓ ((λ (z) z), ∅) (((λ (x) …) (λ (z) z)), ∅) ⇓ … ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))

(e0, env) ⇓ ((λ (x) e2), env’) (e1, env) ⇓ v1 (e2, env’[x ↦ v1]) ⇓ v2 ((e0 e1), env) ⇓ v2 ((λ (x) e), env) ⇓ ((λ (x) e), env) (x, env) ⇓ env(x)

(((λ (y) y) x)), [x ↦ ((λ (z) z), ∅)]) ⇓ … ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))

… ⇓ ((λ (y) y), [x ↦ ((λ (z) z), ∅)]) (((λ (y) y) x)), [x ↦ ((λ (z) z), ∅)]) ⇓ … (((λ (x) …) (λ (z) z)), ∅) ⇓ … ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))

(x, [x ↦ ((λ (z) z), ∅)]) ⇓ ((λ (z) z), ∅) … ⇓ ((λ (y) y), [x ↦ ((λ (z) z), ∅)]) … ⇓ ((λ (z) z), ∅) (((λ (y) y) x)), [x ↦ ((λ (z) z), ∅)]) ⇓ … (((λ (x) …) (λ (z) z)), ∅) ⇓ … ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))

(y, [y ↦ ((λ (z) z), ∅), x ↦ ((λ (z) z), ∅)]) ⇓ ((λ (z) z), ∅) (x, [x ↦ ((λ (z) z), ∅)]) ⇓ ((λ (z) z), ∅) … ⇓ ((λ (y) y), [x ↦ ((λ (z) z), ∅)]) … ⇓ ((λ (z) z), ∅) (((λ (y) y) x)), [x ↦ ((λ (z) z), ∅)]) ⇓ … (((λ (x) …) (λ (z) z)), ∅) ⇓ … ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))

(((λ (x) …) (λ (z) z)), ∅) ⇓ ((λ (z) z), ∅) ((λ (w) w), ∅) ⇓ ((λ (w) w), ∅) (((λ (x) …) (λ (z) z)), ∅) ⇓ ((λ (z) z), ∅) ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))

(((λ (x) …) (λ (z) z)), ∅) ⇓ ((λ (z) z), ∅) (z, [z ↦ ((λ (w) w), ∅)]) ⇓ ((λ (w) w), ∅) ((λ (w) w), ∅) ⇓ ((λ (w) w), ∅) (((λ (x) …) (λ (z) z)), ∅) ⇓ ((λ (z) z), ∅) ( , ∅) ⇓ … (((λ (x) ((λ (y) y) x)) (λ (z) z)) (λ (w) w))

Review Meta-circular interpreters (reuse all features from host) Term-rewriting (non-deterministic but consistent) outermost and innermost beta-reductions => Lazy (CBN) and Eager (CBV) when stop at λ Using a grammar for evaluation contexts, we force CBV Substitution environments and big-step reductions Equivalent to a closure-creating interpreter

Try evaluating using context&redex and then big-step Exercises Try evaluating using context&redex and then big-step (((λ (z) z) (λ (u) (u u))) (λ (x) x)) (((λ (f) (λ (g) ((f g) f))) (λ (z) z)) (λ (u) (u u)))