Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong.

Size: px
Start display at page:

Download "Data Abstraction. An Abstraction for Inductive Data Types. Philip W. L. Fong."

Transcription

1 Data Abstraction An Abstraction for Inductive Data Types Philip W. L. Fong Department of Computer Science University of Regina Regina, Saskatchewan, Canada

2 Introduction This lecture covers [EOPL2] Sect We skip Sect. 2.1 for now. An Abstraction for Inductive Data Types p.1/43

3 Variant Records An Abstraction for Inductive Data Types p.2/43

4 Variant Records Aggregate data types Array element selection by indexing Record element (i.e., field) selection by field name Union Discriminated union (i.e., variant records) variant discrimination via a tag field An Abstraction for Inductive Data Types p.3/43

5 Example: Variant Records struct BinTree { enum { T_LEAF, T_INTERIOR } tag; union { struct { int datum; } leaf; struct { const char *symbol; const BinTree *left; const BinTree *right; } interior; } node; } An Abstraction for Inductive Data Types p.4/43

6 Variant Records for Scheme [EOPL2] provides a facility, datatype, for supporting the use of variant records in Scheme. Note: datatype is not a standard feature in Scheme. An Abstraction for Inductive Data Types p.5/43

7 Example: Binary Trees Grammar:!bintree" ::=!number" ::= (!symbol"!bintree"!bintree") An Abstraction for Inductive Data Types p.6/43

8 Design Goals 1. constructors that allow us to build variants of binary trees 2. a predicate to test if a given value is a binary tree 3. some way of determining, given a binary tree, whether it is a leaf or interior node 4. some way of extracting the components of each variant An Abstraction for Inductive Data Types p.7/43

9 Using datatype A datatype for binary trees: (define-datatype bintree bintree? (leaf-node (datum number?)) (interior-node (key symbol?) (left bintree?) (right bintree?))) An Abstraction for Inductive Data Types p.8/43

10 Using datatype A datatype for binary trees: (define-datatype bintree bintree? (leaf-node (datum number?)) (interior-node (key symbol?) (left bintree?) (right bintree?))) This declares a new datatype called bintree. An Abstraction for Inductive Data Types p.9/43

11 Using datatype A datatype for binary trees: (define-datatype bintree bintree? (leaf-node (datum number?)) (interior-node (key symbol?) (left bintree?) (right bintree?))) A predicate (bintree? x) is defined to test if x is a bintree. An Abstraction for Inductive Data Types p.10/43

12 Using datatype A datatype for binary trees: (define-datatype bintree bintree? (leaf-node (datum number?)) (interior-node (key symbol?) (left bintree?) (right bintree?))) A bintree can be either a leaf-node or an interior-node. An Abstraction for Inductive Data Types p.11/43

13 Using datatype A datatype for binary trees: (define-datatype bintree bintree? (leaf-node (datum number?)) (interior-node (key symbol?) (left bintree?) (right bintree?))) A leaf-node has only one field, called datum, which stores a number. The type of a field is specified by naming a predicate, such as number? in the case of datum. An Abstraction for Inductive Data Types p.12/43

14 Using datatype A datatype for binary trees: (define-datatype bintree bintree? (leaf-node (datum number?)) (interior-node (key symbol?) (left bintree?) (right bintree?))) A constructor procedure (leaf-node n) is defined so that one can construct a leaf-node out of a number n. An Abstraction for Inductive Data Types p.13/43

15 Using datatype A datatype for binary trees: (define-datatype bintree bintree? (leaf-node (datum number?)) (interior-node (key symbol?) (left bintree?) (right bintree?))) An interior-node consists of 3 fields: key, left, and right. Their types are respectively symbol, bintree, and bintree. An Abstraction for Inductive Data Types p.14/43

16 Using datatype A datatype for binary trees: (define-datatype bintree bintree? (leaf-node (datum number?)) (interior-node (key symbol?) (left bintree?) (right bintree?))) A constructor (interior-node S B1 B2 ) is defined so that one can create an interior-node out of a symbol S, and two bintrees B1 and B2. An Abstraction for Inductive Data Types p.15/43

17 Example > (define BT (interior-node a a (leaf-node 3) b 3 (interior-node b 5 0 (leaf-node 5) (leaf-node 0)))) > (bintree? BT) #t An Abstraction for Inductive Data Types p.16/43

18 Design Goals 1. constructors that allow us to build variants of binary trees 2. a predicate to test if a given value is a binary tree 3. some way of determining, given a binary tree, whether it is a leaf or interior node 4. some way of extracting the components of each variant An Abstraction for Inductive Data Types p.17/43

19 Variant Discrimination & Component Extraction: 1st Attempt Need a way of determining if a given bintree is a leaf-node. (leaf-node? tree) Need a way of extracting the datum field of a given leaf-node. (leaf-node-datum leaf ) Similar facilities for interior-node... (interior-node? tree) (interior-node-key node) (interior-node-left node) (interior-node-right node) An Abstraction for Inductive Data Types p.18/43

20 Pattern Matching The designer of the datatype facility did not go that path. That approach would have led to clumsy code. Instead, they have adopted a variant discrimination and component extraction mechanism known as pattern matching, which was first introduced in the functional programming language ML. Pattern matching for datatypes is provided via the cases syntactic form. An Abstraction for Inductive Data Types p.19/43

21 Example: leaf-sum (leaf-sum tree) Argument(s): tree: a bintree Return: sum of all data at the leaf nodes of tree An Abstraction for Inductive Data Types p.20/43

22 Example: leaf-sum (define leaf-sum (lambda (tree) (cases bintree tree (leaf-node (N) N) (interior-node (S L R) (+ (leaf-sum L) (leaf-sum R)))))) Write a (cases...) construct to discriminate variants. Identify the type of variant record (i.e., bintree) to be discriminated. An Abstraction for Inductive Data Types p.21/43

23 Example: leaf-sum (define leaf-sum (lambda (tree) (cases bintree tree (leaf-node (N) N) (interior-node (S L R) (+ (leaf-sum L) (leaf-sum R)))))) Apply the cases construct to an instance of a datatype (i.e., tree). An Abstraction for Inductive Data Types p.22/43

24 Example: leaf-sum (define leaf-sum (lambda (tree) (cases bintree tree (leaf-node (N) N) (interior-node (S L R) (+ (leaf-sum L) (leaf-sum R)))))) Identify all the variants (i.e., leaf-node & interior-node). For each variant, identify also a list of local variables to be bound to the components of that variant (i.e., (N) in the case of leaf-node, and (S L R) in the case of interior-node). An Abstraction for Inductive Data Types p.23/43

25 Example: leaf-sum (define leaf-sum (lambda (tree) (cases bintree tree (leaf-node (N) N) (interior-node (S L R) (+ (leaf-sum L) (leaf-sum R)))))) If tree is a leaf-node, then a list of local variables, (N), will be created, with bindings equal the corresponding components of the leaf-node. With these local bindings effective, the expression N will be evaluated, and its value is returned as the value of the cases construct. An Abstraction for Inductive Data Types p.24/43

26 Example: leaf-sum (define leaf-sum (lambda (tree) (cases bintree tree (leaf-node (N) N) (interior-node (S L R) (+ (leaf-sum L) (leaf-sum R)))))) If tree is an interior-node, then a list of local variables, (S L R), will be created, with bindings equal the corresponding components of the interior-node. With these local bindings effective, the expression (+ (leaf-sum L) (leaf-sum R)) will be evaluated. An Abstraction for Inductive Data Types p.25/43

27 Example: flip-bintree (flip-bintree tree) Argument(s): tree: a bintree Return: Return a bintree obtained by swapping the left and right subtrees of every interior node in tree. Example: a a 3 b 5 0 b An Abstraction for Inductive Data Types p.26/43

28 Example: flip-bintree (define flip-bintree (lambda (tree) (cases bintree tree (leaf-node (N) (leaf-node N)) (interior-node (S L R) (interior-node S (flip-bintree R) (flip-bintree L)))))) An Abstraction for Inductive Data Types p.27/43

29 Abstract Syntax An Abstraction for Inductive Data Types p.28/43

30 Concrete Syntax Grammar for Lambda Calculus:!expression" ::=!identifier" ::= (lambda (!identifier")!expression") ::= (!expression"!expression") The brackets, the list representation, and key words such as lambda are introduced to make the language easy to write by humans. Inconvenient to work with for language processors (e.g., interpreters). An Abstraction for Inductive Data Types p.29/43

31 Example: Revisiting occurs-free? A variable x occurs free in a lambda calculus expression E iff 1. E is a variable reference such that E is the same as x; or 2. E is of the form (lambda (y ) E1 ), where y is different from x and x occurs free in E1 ; or 3. E is of the form (E1 E2 ) such that x occurs free in either E1 or E2. An Abstraction for Inductive Data Types p.30/43

32 Example: Revisiting occurs-free? An implementation for concrete syntax: (define occurs-free? (lambda (x E) (cond ((symbol? E) (eqv? x E)) ((eqv? (car E) lambda) (let ((y (caadr E)) (E1 (caddr E))) (and (not (eqv? (y x))) (occurs-free? x E1)))) (else (let ((E1 (car E)) (E2 (cadr E))) (or (occurs-free? x E1) (occurs-free? x E2))))))) An Abstraction for Inductive Data Types p.31/43

33 Abstract Syntax Rather than working with clumsy concrete syntax, a language processor (e.g., an interpreter) usually works with a more convenient internal representation called abstract syntax trees. The idea is that most of the concrete syntax information is discarded, leaving only the essential information that reflects the abstract syntactic structure of a program. An Abstraction for Inductive Data Types p.32/43

34 Abstract Syntax Trees Concrete syntax: (lambda (x) (f (f x))) Abstract syntax tree: lambda-exp id body x app-exp rtor rand var-exp app-exp id rtor f var-exp id rand var-exp id f f An Abstraction for Inductive Data Types p.33/43

35 Parsing A parser transforms a source program (in concrete syntax) to an abstract syntax tree (AST): program AST Parser Processor text An Abstraction for Inductive Data Types p.34/43

36 Defining Abstract Syntax Trees The art and science of building a parser belong to a course on compiler construction (CS410). A shallow coverage of this topic is given in Chapter 3 of [EOPL2]. In this lecture, we are concerned with with following question: Given a concrete syntax, how does one define a data structure for representing the corresponding abstract syntax trees? An Abstraction for Inductive Data Types p.35/43

37 Example: Lambda Calculus!expression" ::=!identifier" ::= (lambda (!identifier")!expression") ::= (!expression"!expression") An Abstraction for Inductive Data Types p.36/43

38 Example: Lambda Calculus!expression" ::=!identifier" var-exp (id) ::= (lambda (!identifier")!expression") lambda-exp (id body) ::= (!expression"!expression") app-exp (rator rand) One datatype for each nonterminal being defined. One variant for each production. One field for each occurrence of a nonterminal in a production. An Abstraction for Inductive Data Types p.37/43

39 Example: Lambda Calculus (define-datatype expression expression? (var-exp (id symbol?)) (lambda-exp (id symbol?) (body expression?)) (app-exp (rator expression?) (rand expression?))) An Abstraction for Inductive Data Types p.38/43

40 Example: Revisiting occurs-free? A variable x occurs free in a lambda calculus expression E iff 1. E is a variable reference such that E is the same as x; or 2. E is of the form (lambda (y ) E1 ), where y is different from x and x occurs free in E1 ; or 3. E is of the form (E1 E2 ) such that x occurs free in either E1 or E2. An Abstraction for Inductive Data Types p.39/43

41 Example: Revisiting occurs-free? An implementation based on processing abstract syntax trees: (define occurs-free? (lambda (x E) (cases expression E (var-exp (y) (eqv? y x)) (lambda-exp (y E1) (and (not (eqv? y x)) (occurs-free? x E1))) (app-exp (E1 E2) (or (occurs-free? x E1) (occurs-free? x E2)))))) An Abstraction for Inductive Data Types p.40/43

42 Example: Revisiting occurs-free? An implementation for concrete syntax: (define occurs-free? (lambda (x E) (cond ((symbol? E) (eqv? x E)) ((eqv? (car E) lambda) (let ((y (caadr E)) (E1 (caddr E))) (and (not (eqv? (y x))) (occurs-free? x E1)))) (else (let ((E1 (car E)) (E2 (cadr E))) (or (occurs-free? x E1) (occurs-free? x E2))))))) An Abstraction for Inductive Data Types p.41/43

43 Working with Abstract Syntax The language processors developed in this course maniputate abstract syntax trees rather than concrete syntax. Throughout this course, whenever a language grammar (i.e., concrete syntax) is given, we always define a corresponding set of datatypes to represent its abstract syntax trees. An Abstraction for Inductive Data Types p.42/43

44 Lecture Summary This lecture covers [EOPL2] Sect Variant records via datatype Pattern matching via cases Concrete syntax vs abstract syntax Parsing conversion of concrete syntax to abstract syntax trees Defining abstract syntax trees using datatype Manipulating abstract syntax trees using cases An Abstraction for Inductive Data Types p.43/43

A Brief Introduction to Scheme (II)

A Brief Introduction to Scheme (II) A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data

More information

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan 1 Com S 342 so far So far we have studied: Language design goals, Basic functional programming, Flat recursion over lists, Notion of Scheme procedures

More information

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

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)

More information

Lecture08: Scope and Lexical Address

Lecture08: Scope and Lexical Address Lecture08: Scope and Lexical Address Free and Bound Variables (EOPL 1.3.1) Given an expression E, does a particular variable reference x appear free or bound in that expression? Definition: A variable

More information

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018 Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters Corky Cartwright January 26, 2018 Denotational Semantics The primary alternative to syntactic semantics is denotational semantics.

More information

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

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

More information

Lecture 12: Conditional Expressions and Local Binding

Lecture 12: Conditional Expressions and Local Binding Lecture 12: Conditional Expressions and Local Binding Introduction Corresponds to EOPL 3.3-3.4 Please review Version-1 interpreter to make sure that you understand how it works Now we will extend the basic

More information

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

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 6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,

More information

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) CSE 413 Languages & Implementation Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1 Goals Representing programs as data Racket structs as a better way to represent

More information

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

Procedures. EOPL3: Section 3.3 PROC and App B: SLLGEN Procedures EOPL3: Section 3.3 PROC and App B: SLLGEN The PROC language Expression ::= proc (Identifier) Expression AST: proc-exp (var body) Expression ::= (Expression Expression) AST: call-exp (rator rand)

More information

Essentials of Programming Languages Language

Essentials of Programming Languages Language Essentials of Programming Languages Language Version 5.3 August 6, 2012 The Essentials of Programming Languages language in DrRacket provides a subset of functions and syntactic forms of racket mostly

More information

Essentials of Programming Languages Language

Essentials of Programming Languages Language Essentials of Programming Languages Language Version 6.90.0.26 April 20, 2018 The Essentials of Programming Languages language in DrRacket provides a subset of functions and syntactic forms of racket mostly

More information

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Programming Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

More information

C311 Lab #3 Representation Independence: Representation Independent Interpreters

C311 Lab #3 Representation Independence: Representation Independent Interpreters C311 Lab #3 Representation Independence: Representation Independent Interpreters Will Byrd webyrd@indiana.edu February 5, 2005 (based on Professor Friedman s lecture on January 29, 2004) 1 Introduction

More information

CS152: Programming Languages. Lecture 2 Syntax. Dan Grossman Spring 2011

CS152: Programming Languages. Lecture 2 Syntax. Dan Grossman Spring 2011 CS152: Programming Languages Lecture 2 Syntax Dan Grossman Spring 2011 Finally, some formal PL content For our first formal language, let s leave out functions, objects, records, threads, exceptions,...

More information

Lecture 3: Expressions

Lecture 3: Expressions CS 7400 September 23 30, 2009 Dr. Wand Readings: EOPL3, Secs. 3.1 3.5 Lecture 3: Expressions Key Concepts: syntax and semantics expressed and denoted values expressions environment specifying the behavior

More information

Trees. CS 5010 Program Design Paradigms Bootcamp Lesson 5.1

Trees. CS 5010 Program Design Paradigms Bootcamp Lesson 5.1 Trees CS 5010 Program Design Paradigms Bootcamp Lesson 5.1 Mitchell Wand, 2012-2017 This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. 1 Module 05 Basic

More information

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

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator. .00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do

More information

Type Inference and Type Habitation (5/10/2004)

Type Inference and Type Habitation (5/10/2004) 1 Type Inference and Type Habitation (5/10/2004) Daniel P. Friedman, William E. Byrd, David W. Mack Computer Science Department, Indiana University Bloomington, IN 47405, USA Oleg Kiselyov Fleet Numerical

More information

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018 CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018 Typical workflow concrete syntax (string) "(fn x => x + x) 4" Parsing Possible errors / warnings

More information

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add Using Symbols in Expressions (1) (define z y) z Symbol y z ==> y prints as (+ x 3) evaluate sub-expressions... PrimProc Number Number ( ) machine code to add 23 3 Number 26 apply... ==> 26 prints as 1

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

More information

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation

More information

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want Computer Science 21b (Spring Term, 2017) Structure and Interpretation of Computer Programs Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want Here is one of the big ideas

More information

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan 1 Reading SICP, page 11-19, Section 1.1.6 Little Schemer, Chapter 2 2 The Idea of Syntax Abstraction Problem. Often programming tasks are repetitive,

More information

Comp 311: Sample Midterm Examination

Comp 311: Sample Midterm Examination Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief

More information

Introduction to Scheme

Introduction to Scheme How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free

More information

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

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011 CS152: Programming Languages Lecture 11 STLC Extensions and Related Topics Dan Grossman Spring 2011 Review e ::= λx. e x e e c v ::= λx. e c τ ::= int τ τ Γ ::= Γ, x : τ (λx. e) v e[v/x] e 1 e 1 e 1 e

More information

Lecture 2: Data Types and their Representations; Syntax: Scanning and Parsing

Lecture 2: Data Types and their Representations; Syntax: Scanning and Parsing CS 7400 September 16-21, 2009 Dr. Wand Readings: EOPL, Chap. 2 Lecture 2: Data Types and their Representations; Syntax: Scanning and Parsing Key Concepts: Data types The Data Abstraction Principle Interface,

More information

A Brief Introduction to Scheme (I)

A Brief Introduction to Scheme (I) A Brief Introduction to Scheme (I) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Scheme Scheme I p.1/44 Scheme: Feature Set A

More information

Lexical and Syntax Analysis. Top-Down Parsing

Lexical and Syntax Analysis. Top-Down Parsing Lexical and Syntax Analysis Top-Down Parsing Easy for humans to write and understand String of characters Lexemes identified String of tokens Easy for programs to transform Data structure Syntax A syntax

More information

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams 6.037 Lecture 7B Scheme Variants Normal Order Lazy Evaluation Streams Edited by Mike Phillips & Ben Vandiver Original Material by Eric Grimson & Duane Boning Further Variations on a Scheme Beyond Scheme

More information

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures Dan Grossman Fall 2014 Hi! I m not Hal J I love this stuff and have taught

More information

Programming Languages

Programming Languages Programming Languages Tevfik Koşar Lecture - XIII March 2 nd, 2006 1 Roadmap Functional Languages Lambda Calculus Intro to Scheme Basics Functions Bindings Equality Testing Searching 2 1 Functional Languages

More information

LECTURE 3. Compiler Phases

LECTURE 3. Compiler Phases LECTURE 3 Compiler Phases COMPILER PHASES Compilation of a program proceeds through a fixed series of phases. Each phase uses an (intermediate) form of the program produced by an earlier phase. Subsequent

More information

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages CSc 520 Principles of Programming Languages 9: Scheme Metacircular Interpretation Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian

More information

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1

Static Semantics. Winter /3/ Hal Perkins & UW CSE I-1 CSE 401 Compilers Static Semantics Hal Perkins Winter 2009 2/3/2009 2002-09 Hal Perkins & UW CSE I-1 Agenda Static semantics Types Symbol tables General ideas for now; details later for MiniJava project

More information

CS61A Midterm 2 Review (v1.1)

CS61A Midterm 2 Review (v1.1) Spring 2006 1 CS61A Midterm 2 Review (v1.1) Basic Info Your login: Your section number: Your TA s name: Midterm 2 is going to be held on Tuesday 7-9p, at 1 Pimentel. What will Scheme print? What will the

More information

CS 440: Programming Languages and Translators, Spring 2019 Mon

CS 440: Programming Languages and Translators, Spring 2019 Mon Haskell, Part 4 CS 440: Programming Languages and Translators, Spring 2019 Mon 2019-01-28 More Haskell Review definition by cases Chapter 6: Higher-order functions Revisit currying map, filter Unnamed

More information

Contents. Chapter 1 SPECIFYING SYNTAX 1

Contents. Chapter 1 SPECIFYING SYNTAX 1 Contents Chapter 1 SPECIFYING SYNTAX 1 1.1 GRAMMARS AND BNF 2 Context-Free Grammars 4 Context-Sensitive Grammars 8 Exercises 8 1.2 THE PROGRAMMING LANGUAGE WREN 10 Ambiguity 12 Context Constraints in Wren

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

CS 275 Name Final Exam Solutions December 16, 2016

CS 275 Name Final Exam Solutions December 16, 2016 CS 275 Name Final Exam Solutions December 16, 2016 You may assume that atom? is a primitive procedure; you don t need to define it. Other helper functions that aren t a standard part of Scheme you need

More information

Typed Scheme: Scheme with Static Types

Typed Scheme: Scheme with Static Types Typed Scheme: Scheme with Static Types Version 4.1.1 Sam Tobin-Hochstadt October 5, 2008 Typed Scheme is a Scheme-like language, with a type system that supports common Scheme programming idioms. Explicit

More information

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics National Institute of Informatics May 31, June 7, June 14, 2010 All Right Reserved. Outline I 1 Parser Type 2 Monad Parser Monad 3 Derived Primitives 4 5 6 Outline Parser Type 1 Parser Type 2 3 4 5 6 What

More information

CS 242. Fundamentals. Reading: See last slide

CS 242. Fundamentals. Reading: See last slide CS 242 Fundamentals Reading: See last slide Syntax and Semantics of Programs Syntax The symbols used to write a program Semantics The actions that occur when a program is executed Programming language

More information

Last class. CS Principles of Programming Languages. Introduction. Outline

Last class. CS Principles of Programming Languages. Introduction. Outline Last class CS6848 - Principles of Programming Languages Principles of Programming Languages V. Krishna Nandivada IIT Madras Interpreters A Environment B Cells C Closures D Recursive environments E Interpreting

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,

More information

A Simple Syntax-Directed Translator

A Simple Syntax-Directed Translator Chapter 2 A Simple Syntax-Directed Translator 1-1 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called

More information

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

SEMANTIC ANALYSIS TYPES AND DECLARATIONS SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether

More information

An Explicit-Continuation Metacircular Evaluator

An Explicit-Continuation Metacircular Evaluator Computer Science (1)21b (Spring Term, 2018) Structure and Interpretation of Computer Programs An Explicit-Continuation Metacircular Evaluator The vanilla metacircular evaluator gives a lot of information

More information

Types and Programming Languages. Lecture 5. Extensions of simple types

Types and Programming Languages. Lecture 5. Extensions of simple types Types and Programming Languages Lecture 5. Extensions of simple types Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao Tong University Fall, 2016 Coming soon Simply typed λ-calculus has enough structure

More information

Chapter 4. Abstract Syntax

Chapter 4. Abstract Syntax Chapter 4 Abstract Syntax Outline compiler must do more than recognize whether a sentence belongs to the language of a grammar it must do something useful with that sentence. The semantic actions of a

More information

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited CMSC 330: Organization of Programming Languages Type Systems, Names & Binding Topics Covered Thus Far Programming languages Syntax specification Regular expressions Context free grammars Implementation

More information

Lexical and Syntax Analysis

Lexical and Syntax Analysis Lexical and Syntax Analysis (of Programming Languages) Top-Down Parsing Lexical and Syntax Analysis (of Programming Languages) Top-Down Parsing Easy for humans to write and understand String of characters

More information

Lexical vs. Dynamic Scope

Lexical vs. Dynamic Scope Intro Lexical vs. Dynamic Scope where do I point to? Remember in Scheme whenever we call a procedure we pop a frame and point it to where the procedure points to (its defining environment). This is called

More information

Intermediate Code Generation

Intermediate Code Generation Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

More information

University of Massachusetts Lowell

University of Massachusetts Lowell University of Massachusetts Lowell 91.301: Organization of Programming Languages Fall 2002 Quiz 1 Solutions to Sample Problems 2 91.301 Problem 1 What will Scheme print in response to the following statements?

More information

The Typed Racket Guide

The Typed Racket Guide The Typed Racket Guide Version 5.3.6 Sam Tobin-Hochstadt and Vincent St-Amour August 9, 2013 Typed Racket is a family of languages, each of which enforce

More information

Typed Racket: Racket with Static Types

Typed Racket: Racket with Static Types Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type

More information

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 6 Robert Grimm, New York University 1 Review Last week Function Languages Lambda Calculus SCHEME review 2 Outline Promises, promises, promises Types,

More information

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators

CITS3211 FUNCTIONAL PROGRAMMING. 6. Folding operators CITS3211 FUNCTIONAL PROGRAMMING 6. Folding operators Summary: This lecture discusses an important group of higher order functions known as the folding operators. Almost any recursive function over lists

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Type Systems, Names and Binding CMSC 330 - Spring 2013 1 Topics Covered Thus Far! Programming languages Ruby OCaml! Syntax specification Regular expressions

More information

CSc 520. Principles of Programming Languages 7: Scheme List Processing

CSc 520. Principles of Programming Languages 7: Scheme List Processing CSc 520 Principles of Programming Languages 7: Scheme List Processing Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg

More information

Types and Static Type Checking (Introducing Micro-Haskell)

Types and Static Type Checking (Introducing Micro-Haskell) Types and Static (Introducing Micro-Haskell) Informatics 2A: Lecture 13 Alex Simpson School of Informatics University of Edinburgh als@inf.ed.ac.uk 16 October, 2012 1 / 21 1 Types 2 3 4 2 / 21 Thus far

More information

CS 360 Programming Languages Interpreters

CS 360 Programming Languages Interpreters CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like

More information

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

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am. The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper

More information

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated Typical workflow concrete synta (string) "(fn => + ) 4" Parsing CSE341: Programming Languages abstract synta (tree) Lecture 17 Implementing Languages Including Closures Function Constant + 4 Var Var Type

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

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

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals Review CS152: Programming Languages Lecture 11 STLC Extensions and Related Topics e ::= λx. e x ee c v ::= λx. e c (λx. e) v e[v/x] e 1 e 2 e 1 e 2 τ ::= int τ τ Γ ::= Γ,x : τ e 2 e 2 ve 2 ve 2 e[e /x]:

More information

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing

CSc 520 Principles of Programming Languages. Examining Lists. Constructing Lists... 7: Scheme List Processing Constructing Lists CSc 520 Principles of Programming Languages 7: Scheme List Processing Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona The most important

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 15: Review and Functional Programming Zheng (Eddy) Zhang Rutgers University March 19, 2018 Class Information Midterm exam forum open in Sakai. HW4 and

More information

Compiler Theory. (Semantic Analysis and Run-Time Environments)

Compiler Theory. (Semantic Analysis and Run-Time Environments) Compiler Theory (Semantic Analysis and Run-Time Environments) 005 Semantic Actions A compiler must do more than recognise whether a sentence belongs to the language of a grammar it must do something useful

More information

B The SLLGEN Parsing System

B The SLLGEN Parsing System B The SLLGEN Parsing System Programs are just strings of characters. In order to process a program, we need to group these characters into meaningful units. This grouping is usually divided into two stages:

More information

Note first midterm date: Wed. evening, Feb. 23 Today's class: Types in OCaml and abstract syntax

Note first midterm date: Wed. evening, Feb. 23 Today's class: Types in OCaml and abstract syntax CS 421 Lecture 3 Note first midterm date: Wed. evening, Feb. 23 Today's class: Types in OCaml and abstract syntax Type declarations in OCaml Trees Polymorphic types Abstract syntax CS 421 lecture 3 page

More information

CSCE 314 Programming Languages. Type System

CSCE 314 Programming Languages. Type System CSCE 314 Programming Languages Type System Dr. Hyunyoung Lee 1 Names Names refer to different kinds of entities in programs, such as variables, functions, classes, templates, modules,.... Names can be

More information

Functional Programming - 2. Higher Order Functions

Functional Programming - 2. Higher Order Functions Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Lesson 6 - Defining a Programming Language Bottom Up Collaboration and Management - Elements of Programming Dana Fisman 1 What we accomplished

More information

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m. CS 6110 S18 Lecture 8 Structural Operational Semantics and IMP Today we introduce a very simple imperative language, IMP, along with two systems of rules for evaluation called small-step and big-step semantics.

More information

CMSC 330: Organization of Programming Languages. Lambda Calculus

CMSC 330: Organization of Programming Languages. Lambda Calculus CMSC 330: Organization of Programming Languages Lambda Calculus 1 Turing Completeness Turing machines are the most powerful description of computation possible They define the Turing-computable functions

More information

Lexical and Syntax Analysis

Lexical and Syntax Analysis Lexical and Syntax Analysis (of Programming Languages) Bison, a Parser Generator Lexical and Syntax Analysis (of Programming Languages) Bison, a Parser Generator Bison: a parser generator Bison Specification

More information

CSE505, Fall 2012, Midterm Examination October 30, 2012

CSE505, Fall 2012, Midterm Examination October 30, 2012 CSE505, Fall 2012, Midterm Examination October 30, 2012 Rules: The exam is closed-book, closed-notes, except for one side of one 8.5x11in piece of paper. Please stop promptly at Noon. You can rip apart

More information

Lecture 16: Object Programming Languages

Lecture 16: Object Programming Languages Lecture 16: Object Programming Languages Introduction Corresponds to EOPL 5.1 and 5.2 Goal: to introduce Object Oriented Programming Language (OOPL) concepts using the EOPL extensible language framework

More information

COP4020 Programming Assignment 1 - Spring 2011

COP4020 Programming Assignment 1 - Spring 2011 COP4020 Programming Assignment 1 - Spring 2011 In this programming assignment we design and implement a small imperative programming language Micro-PL. To execute Mirco-PL code we translate the code to

More information

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions CVO103: Programming Languages Lecture 5 Design and Implementation of PLs (1) Expressions Hakjoo Oh 2018 Spring Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 1 / 23 Plan Part 1 (Preliminaries):

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Operational Semantics CMSC 330 Summer 2018 1 Formal Semantics of a Prog. Lang. Mathematical description of the meaning of programs written in that language

More information

From Syntactic Sugar to the Syntactic Meth Lab:

From Syntactic Sugar to the Syntactic Meth Lab: From Syntactic Sugar to the Syntactic Meth Lab: Using Macros to Cook the Language You Want a V E N E TRUT H Brandeis S PA R T U N T O I T S I N N E R M OS T COMPUTER SCIENCE 21B: Structure and Interpretation

More information

Parsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau

Parsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau Parsing a primer Ralf Lämmel Software Languages Team University of Koblenz-Landau http://www.softlang.org/ Mappings (edges) between different representations (nodes) of language elements. For instance,

More information

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP) Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP) Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology

More information

CA Compiler Construction

CA Compiler Construction CA4003 - Compiler Construction Semantic Analysis David Sinclair Semantic Actions A compiler has to do more than just recognise if a sequence of characters forms a valid sentence in the language. It must

More information

5. Semantic Analysis!

5. Semantic Analysis! 5. Semantic Analysis! Prof. O. Nierstrasz! Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.! http://www.cs.ucla.edu/~palsberg/! http://www.cs.purdue.edu/homes/hosking/!

More information

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Scheme in Scheme: The Metacircular Evaluator Eval and Apply Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015 The metacircular evaluator is A rendition of Scheme,

More information

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking Agenda COMP 181 Type checking October 21, 2009 Next week OOPSLA: Object-oriented Programming Systems Languages and Applications One of the top PL conferences Monday (Oct 26 th ) In-class midterm Review

More information

Project 2: Scheme Interpreter

Project 2: Scheme Interpreter Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same

More information

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

n   n Try tutorial on front page to get started! n   spring13/ n Stack Overflow! Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

More information

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017 CSE341 Spring 2017, Final Examination June 8, 2017 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

Scheme: Strings Scheme: I/O

Scheme: Strings Scheme: I/O Scheme: Strings Scheme: I/O CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Wednesday, April 5, 2017 Glenn G. Chappell Department of Computer Science University of

More information

More Assigned Reading and Exercises on Syntax (for Exam 2)

More Assigned Reading and Exercises on Syntax (for Exam 2) More Assigned Reading and Exercises on Syntax (for Exam 2) 1. Read sections 2.3 (Lexical Syntax) and 2.4 (Context-Free Grammars) on pp. 33 41 of Sethi. 2. Read section 2.6 (Variants of Grammars) on pp.

More information

1. For every evaluation of a variable var, the variable is bound.

1. For every evaluation of a variable var, the variable is bound. 7 Types We ve seen how we can use interpreters to model the run-time behavior of programs. Now we d like to use the same technology to analyze or predict the behavior of programs without running them.

More information

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Type checking Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Summary of parsing Parsing A solid foundation: context-free grammars A simple parser: LL(1) A more powerful parser:

More information

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

Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation? Normal Order (Lazy) Evaluation Alternative models for computation: Normal (Lazy) Order Evaluation Memoization Streams Applicative Order: evaluate all arguments, then apply operator Normal Order: pass unevaluated

More information