Encodings. Using the minimal λ-calculus language we get. functions. local binding. booleans. numbers... and recursive functions?

Similar documents
Chapter 1. Fundamentals of Higher Order Programming

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

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

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

CMSC 330: Organization of Programming Languages. Lambda Calculus

An Introduction to Scheme

Fundamentals and lambda calculus

Formal Systems and their Applications

CMSC 330: Organization of Programming Languages. Lambda Calculus

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

CS1 Recitation. Week 1

9/23/2014. Why study? Lambda calculus. Church Rosser theorem Completeness of Lambda Calculus: Turing Complete

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

ENVIRONMENT DIAGRAMS AND RECURSION 2

CSE 341 Section 7. Eric Mullen Spring Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Environment Diagrams and Recursion Fall 2017 Discussion 2: September 6, 2017 Solutions. 1 More Environment Diagrams

ENVIRONMENT DIAGRAMS AND RECURSION 2

Tail Recursion. ;; a recursive program for factorial (define fact (lambda (m) ;; m is non-negative (if (= m 0) 1 (* m (fact (- m 1))))))

Recursive Definitions, Fixed Points and the Combinator

CMSC 330: Organization of Programming Languages

CSC324 Functional Programming Efficiency Issues, Parameter Lists

Deferred operations. Continuations Structure and Interpretation of Computer Programs. Tail recursion in action.

Implementing Recursion

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, Higher Order Functions. HOFs in Environment Diagrams

LECTURE 16. Functional Programming

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

CS 314 Principles of Programming Languages

CS 61A HKN Review Session. Nathan Zhang, Caleb Wyllie

CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

Types and evaluation

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CSE 341 Section 7. Ethan Shea Autumn Adapted from slides by Nicholas Shahan, Dan Grossman, Tam Dang, and Eric Mullen

Organization of Programming Languages CS3200/5200N. Lecture 11

CSE 505: Concepts of Programming Languages

CSE 341: Programming Languages

Homework 3 COSE212, Fall 2018

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Func+on applica+ons (calls, invoca+ons)

CS 61A Higher Order Functions and Recursion Fall 2018 Discussion 2: June 26, 2018 Solutions. 1 Higher Order Functions. HOFs in Environment Diagrams

Overview. CS301 Session 3. Problem set 1. Thinking recursively

C311 Lab #3 Representation Independence: Representation Independent Interpreters

CS422 - Programming Language Design

Denotational Semantics. Domain Theory

TAIL RECURSION, SCOPE, AND PROJECT 4 11

CS115 INTRODUCTION TO COMPUTER SCIENCE 1. Additional Notes Module 5

Lambda Calculus. CS 550 Programming Languages Jeremy Johnson

CMSC 330: Organization of Programming Languages

Project 1: Scheme Pretty-Printer

Lambda Calculus LC-1

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

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

Continuations and Continuation-Passing Style

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Principles of Programming Languages 2017W, Functional Programming

1.3. Conditional expressions To express case distinctions like

Typed Scheme: Scheme with Static Types

Don t Believe the Hype. CS152: Programming Languages. Lecture 21 Object-Oriented Programming. Class-based OOP. So what is OOP?

Programming Languages Lecture 14: Sum, Product, Recursive Types

Lambda calculus. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 6

Typing Data. Chapter Recursive Types Declaring Recursive Types

CMSC330. Objects, Functional Programming, and lambda calculus

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

CS 11 Haskell track: lecture 1

INTRODUCTION TO SCHEME

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages. Lambda calculus

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Mixin Up the ML Module System

Scheme as implemented by Racket

Functions as data. Massimo Merro. 9 November Massimo Merro The Lambda language 1 / 21

6.821 Jeopardy: The Home Version The game that turns into 6.82fun!

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Higher-Order Functions (Part I)

Functions & First Class Function Values

Functions and Recursion. Dr. Philip Cannata 1

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Recursion. Tjark Weber. Functional Programming 1. Based on notes by Sven-Olof Nyström. Tjark Weber (UU) Recursion 1 / 37

Programming Languages

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Special Directions for this Test

CS 275 Name Final Exam Solutions December 16, 2016

Exercise 1 ( = 24 points)

CS 209 Functional Programming

CSCC24 Functional Programming Scheme Part 2

CMSC 330: Organization of Programming Languages. Operational Semantics

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

CSE-321 Programming Languages 2012 Midterm

Transcription:

Part

Encodings Using the minimal λ-calculus language we get functions local binding booleans numbers... and recursive functions?

Factorial in Plait (local [(define fac (lambda (n) (if (zero? n) (* n (fac (- n ))))))] (fac 0)) local binds both in the body expression and in the binding expression

Factorial in Plait (letrec ([fac (lambda (n) (if (zero? n) (* n (fac (- n )))))]) (fac 0)) letrec has the shape of let but the binding structure of local

Factorial in Plait (let ([fac (lambda (n) (if (zero? n) (* n (fac (- n )))))]) (fac 0)) Doesn t work, because let binds fac only in the body

Factorial Overall goal: Implement letrec as syntactic sugar for Curly {letrec {[name rhs]} name} Step : Implement fac in Plait without letrec Step 2: Isolate the rhs... {lambda {n} {if {zero? n} {* n {fac {- n }}}}}... Step 3: Surrounding as a parse transformation for Curly

This is Diffcult...

Part 2

Factorial Overall goal: Implement letrec as syntactic sugar for Curly {letrec {[name rhs]} name} Step : Implement fac in Plait without letrec Step 2: Isolate the rhs... {lambda {n} {if {zero? n} {* n {fac {- n }}}}}... Step 3: Surrounding as a parse transformation for Curly

Factorial (let ([fac (lambda (n) (if (zero? n) (* n (fac (- n )))))]) (fac 0)) At the point that we call fac, obviously we have a binding for fac...... so pass it as an argument!

Factorial (let ([facx (lambda (facx n) (if (zero? n) (* n (fac (- n )))))]) (facx facx 0))

Factorial (let ([facx (lambda (facx n) (if (zero? n) (* n (facx facx (- n )))))]) (facx facx 0)) Wrap this to get fac back...

Factorial (let ([fac (lambda (n) (let ([facx (lambda (facx n) (if (zero? n) (* n (facx facx (- n )))))]) (facx facx n)))]) (fac 0))

Part 3

Factorial Overall goal: Implement letrec as syntactic sugar for Curly {letrec {[name rhs]} name} Step : Implement fac in Plait without letrec Step 2: Isolate the rhs... {lambda {n} {if {zero? n} {* n {fac {- n }}}}}... Step 3: Surrounding as a parse transformation for Curly

Factorial (let ([fac (lambda (n) (let ([facx (lambda (facx n) (if (zero? n) (* n (facx facx (- n )))))]) (facx facx n)))]) (fac 0)) But Curly has only single-argument functions...

Factorial (let ([fac (lambda (n) (let ([facx (lambda (facx) (lambda (n) (if (zero? n) (* n ((facx facx) (- n ))))))]) ((facx facx) n)))]) (fac 0)) Simplify: (lambda (n) (let ([f...]) ((f f) n))) (let ([f...]) (f f))...

Factorial (let ([fac (let ([facx (lambda (facx) (lambda (n) (if (zero? n) (* n ((facx facx) (- n ))))))]) (facx facx))]) (fac 0))

Factorial (let ([fac (let ([facx (lambda (facx) ; Almost looks like original fac: (lambda (n) (if (zero? n) (* n ((facx facx) (- n ))))))]) (facx facx))]) (fac 0)) More like original: introduce a local binding for (facx facx)...

Factorial (let ([fac (let ([facx (lambda (facx) (let ([fac (facx facx)]) ; Exactly like original fac: (lambda (n) (if (zero? n) (* n (fac (- n )))))))]) (facx facx))]) (fac 0)) Oops! this is an infnite loop We used to evaluate (facx facx) only when n is non-zero Delay (facx facx)...

Factorial (let ([fac (let ([facx (lambda (facx) (let ([fac (lambda (x) ((facx facx) x))]) ; Exactly like original fac: (lambda (n) (if (zero? n) (* n (fac (- n )))))))]) (facx facx))]) (fac 0))

Factorial (let ([fac (let ([facx (lambda (facx) (let ([fac (lambda (x) ((facx facx) x))]) ; Exactly like original fac: (lambda (n) (if (zero? n) (* n (fac (- n )))))))]) (facx facx))]) (fac 0))

Factorial (let ([fac (let ([facx (lambda (facx) (let ([fac (lambda (x) ((facx facx) x))]) ((lambda (fac) ; Exactly like original fac: (lambda (n) (if (zero? n) (* n (fac (- n )))))) fac)))]) (facx facx))]) (fac 0))

Factorial (let ([fac (let ([fx (lambda (fx) (let ([f (lambda (x) ((fx fx) x))]) ((lambda (fac) ; Exactly like original fac: (lambda (n) (if (zero? n) (* n (fac (- n )))))) f)))]) (fx fx)) ]) (fac 0))

Factorial (define mk-rec (lambda (body-proc) (let ([fx (lambda (fx) (let ([f (lambda (x) ((fx fx) x))]) (body-proc f)))]) (fx fx)) )) (let ([fac (mk-rec (lambda (fac) ; Exactly like original fac: (lambda (n) (if (zero? n) (* n (fac (- n )))))) )]) (fac 0))

Factorial (let ([fac (mk-rec (lambda (fac) ; Exactly like original fac: (lambda (n) (if (zero? n) (* n (fac (- n )))))))]) (fac 0))

Fibonnaci (let ([fib (mk-rec (lambda (fib) ; Usual fib: (lambda (n) (if (or (= n 0) (= n )) (+ (fib (- n )) (fib (- n 2)))))))]) (fib 5))

Sum (let ([sum (mk-rec (lambda (sum) ; Usual sum: (lambda (l) (if (empty? l) 0 (+ (fst l) (sum (rest l)))))))]) (sum `( 2 3 4)))

Part 4

Factorial Overall goal: Implement letrec as syntactic sugar for Curly {letrec {[name rhs]} name} Step : Implement fac in Plait without letrec Step 2: Isolate the rhs... {lambda {n} {if {zero? n} {* n {fac {- n }}}}}... Step 3: Surrounding as a parse transformation for Curly

Implementing Recursion {letrec {[fac {lambda {n} {if0 n {* n {fac {- n }}}}}]} {fac 0}} could be parsed the same as {let {[fac {mk-rec {lambda {fac} {lambda {n} {if0 n {* n {fac {- n }}}}}}}]} {fac 0}}

Implementing Recursion {letrec {[fac {lambda {n} {if0 n {* n {fac {- n }}}}}]} {fac 0}} could be parsed the same as mk-rec = {lambda {body-proc} {let {[fx {let {[fac {lambda {fx} {mk-rec {let {[f {lambda {x} {{fx fx} x}}]} {lambda {fac} {body-proc f}}}]} {lambda {n} {fx fx}}} {if0 n {* n {fac {- n }}}}}}}]} {fac 0}}

Implementing Recursion {letrec {[fac {lambda {n} {if0 n {* n {fac {- n }}}}}]} {fac 0}} could be parsed the same as mk-rec = {lambda {body-proc} {{lambda {fx} {fx fx}} {let {[fac {lambda {fx} {mk-rec {{lambda {f} {body-proc f}} {lambda {x} {lambda {fac} {{fx fx} x}}}}}} {lambda {n} {if0 n {* n {fac {- n }}}}}}}]} {fac 0}}

{letrec {[name rhs]} body} could be parsed the same as Implementing Recursion {let {[name {mk-rec {lambda {name} rhs}}]} body} which is really {{lambda {name} body} {mk-rec {lambda {name} rhs}}} which, writing out mk-rec, is really {{lambda {name} body} {{lambda {body-proc} {let {[fx {fun {fx} {let {[f {lambda {x} {{fx fx} x}}]} {body-proc f}}}]} {fx fx}}} {lambda {name} rhs}}}

Part 5

The Big Picture {letrec {[name rhs]} body} {{lambda {name} body} {{lambda {body-proc} {let {[fx {fun {fx} {let {[f {lambda {x} {{fx fx} x}}]} {body-proc f}}}]} {fx fx}}} {lambda {name} rhs}}}

Y Combinator mk-rec is better known as the Y combinator {lambda {body-proc} {{lambda {fx} {fx fx}} {lambda {fx} {{lambda {f} {body-proc f}} {lambda {x} {{fx fx} x}}}}}}

Y Combinator mk-rec is better known as the Y combinator Y = def (λ (g) {(λ (fx) {fx fx}) (λ (fx) {(λ (f) {g f}) (λ (x) {{fx fx} x})})}) a.k.a. the fxpoint operator {Y (lambda (f in ) f out )}

Y Combinator mk-rec is better known as the Y combinator Y = def (λ (g) {(λ (fx) {fx fx}) (λ (fx) {(λ (f) {g f}) (λ (x) {{fx fx} x})})}) See also The Why of Y (Gabriel) or The Little Schemer (Friedman & Felleisen)

Part 6

Example with Quasiquote Escapes (define (parse [s : S-Exp]) : Exp... [(s-exp-match? `{let {[SYMBOL ANY]} ANY} s) (let ([bs (s-exp->list (first (s-exp->list (second (s-exp->list s)))))]) (let ([name (first bs)] [rhs (second bs)] [body (third (s-exp->list s))]) (parse `{{lambda {,name},body},rhs})))]...)