Inductively Defined Data Concrete and Abstract syntax

Slides:



Advertisements
Similar presentations
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Advertisements

Cs784(Prasad)L10Rec1 Implementing Recursion. cs784(Prasad)L10Rec2 let insufficient for recursion ( (let((fact(lambda (n) (if (zero? n) 1 (* n (fact (-
Letrec fact(n) = if zero?(n) then 1 else *(n, (fact sub1(n))) 4.4 Type Inference Type declarations aren't always necessary. In our toy typed language,
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
Cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
EOPL3: Section 3.3 PROC and App B: SLLGEN
Cs784(tk)1 Implementing Recursion. Recap: Goals Experimenting with PL design alternatives Scoping, parameter passing, arrays,... Runnable prototype of.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
3.5 Procedures Recall procedures (functions) in Scheme: (let ((f (lambda(y z) (+ y (- z 5))) (f 2 28)) We would like something similar in our toy language:
4.2 Type Checking (type-of-expression > tenv ) = bool (type-of-expression > tenv ) = x ( type-of-expression if > tenv ) = x Recall type of if statement:
1 Objects and types Typed languages = define a set of types in the language and assign a type to each expression in the program Type checking = how can.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
Plt /19/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.1 A Simple Interpreter.
CSG 7111 Structure and Interpretation of an Aspect Language for Datatype Karl Lieberherr.
12/2/20151 GC16/3011 Functional Programming Lecture 2 The Lambda Calculus: A Simple Introduction.
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda.
Inductively Defined Data Concrete and Abstract syntax Karl Lieberherr.
3.3 Conditional Evaluation --> if 1 then 2 else 3 2 Want to support if/then/else We'll represent false as 0, true as 1: Syntax (BNF): ::= if then else.
Objects and Classes Gul Agha CS 421 Spring /11/2001CS 322 Fall Characteristics of OOP Object-based  encapsulate state  provide interface.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Chapter 2: Data Abstraction 2
CS 326 Programming Languages, Concepts and Implementation
Structure and Interpretation of an Aspect Language for Datatype
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Corky Cartwright January 18, 2017
Programming Language Concepts
Implementing Recursion
Original material by Eric Grimson
Lesson 4 Typed Arithmetic Typed Lambda Calculus
Case Study: Undefined Variables
Presentation by Julie Betlach 7/02/2009
5.4.1: Implementation Method Invocation
Is everyone signed up on piazza?
The Metacircular Evaluator
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Procedures App B: SLLGEN 1.
Programming Language Principles
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
Chapter 1 Review: BNF Grammar for lists:
Abstract Syntax cs7100 (Prasad) L7AST.
Lecture 12: Message passing The Environment Model
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Review for Final Exam.
3.6 Interpreter: Recursion
SYNTAX DIRECTED DEFINITION
6.001 SICP Variations on a Scheme
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Lecture 11: Multiple representations of abstract data Message passing Overloading Section 2.4, pages ,2.5.2 pages מבוא.
Chapter 3: Environment-Passing Interpreters
6.001 SICP Interpretation Parts of an interpreter
Recursive Procedures and Scopes
Presentation transcript:

Inductively Defined Data Concrete and Abstract syntax Karl Lieberherr CSG 111

Don’t believe the words Concrete syntax may be more abstract than abstract syntax!!! CSG 111

Both Abstract and Concrete Exp ::= Identifier var-exp (id) ::= “(lambda” “(“Identifier”)” Exp”)” lambda-exp (id body) ::= “(“ Exp “(“ Exp “)” “)” app-exp (rator rand) page 49 of EOPL 2: (replace Exp by Expression, capitalize instead of angle) CSG 111

Both Abstract and Concrete Exp ::= Id var-exp (id) ::= “(lambda” “(“ Id ”)” Exp ”)” lambda-exp (id body) ::= “(“ Exp “(“ Exp “)” “)” app-exp (rator rand) (define-datatype Exp Exp? (var-exp (id Id?)) (lambda-exp (id Id?) (body Exp?)) (app-exp (rator Exp?) (rand Exp?))) page 49 of EOPL 2: (Exp by Expression, Id by Identifier, capitalize instead of angle) CSG 111

Represent Id as symbol Exp ::= Id var-exp (id) ::= “(lambda” “(“ Id ”)” Exp ”)” lambda-exp (id body) ::= “(“ Exp “(“ Exp “)” “)” app-exp (rator rand) (define-datatype Exp Exp? (var-exp (id symbol?)) (lambda-exp (id symbol?) (body Exp?)) (app-exp (rator Exp?) (rand Exp?))) page 49 of EOPL 2: (Exp by Expression, Id by Identifier, capitalize instead of angle) CSG 111

cases (define occurs-free? (lambda (var exp) (cases Exp e (var-exp (id) (eqv? id var)) (lambda-exp (id body) and (not (eqv? id var)) (occurs-free? var body))) (app-exp (rator rand) (or … )))))) CSG 111

Exercises for define-datatype Arithmetic expressions ( * (+ 3 5) 7) two arguments only include evaluator Nested containers CSG 111

Variants are also data types Exp ::= Id var-exp (id) ::= “(lambda” “(“ Id ”)” Exp ”)” lambda-exp (id body) ::= “(“ Exp “(“ Exp “)” “)” app-exp (rator rand) (define-datatype Exp Exp? (var-exp (id symbol?)) (lambda-exp (id symbol?) (body Exp?)) (app-exp (rator Exp?) (rand Exp?))) Better way: Exp : VarExp | LambdaExp | AppExp. VarExp = Id. LambdaExp = “(lambda” “(“ <id> Id <body> Exp “)”. AppExp = “(“ <rator> Exp <rand> Exp “)”. Test = <first> LambdaExp <second> AppExp. Each non-terminal defines a data type. CSG 111

Concern analysis Concerns: traversal summing weights (define (check ac) (local (;; Container -> Number ;; the weight of a container ;; effect: the number of capacity violations in a container (define (weight-container ac) (local ([define witems (weight-loi (Container-contents ac))]) (when (> witems (Container-capacity ac)) (set! violations (+ 1 violations))) witems)) ;; (Listof Item) -> Number ;; the weight of a list of items (define (weight-loi l) (foldr + 0 (map weight-item l))) ;; Item -> Number ;; the weight of an item (define (weight-item l) (cond [(Simple? l) (Simple-weight l)] [(Container? l) (weight-container l)])) (define violations 0)) ;; the number of violations detected (weight-container ac) violations)) Concerns: traversal summing weights summing violations CSG 111

Concrete syntax more Abstract than Abstract Syntax: example Exp ::= Identifier var-exp (id) ::= “(lambda” “(“Identifier”)” List(Exp)”)” lambda-exp (id body) ::= “(“ Exp “(“ List(Exp) “)” “)” app-exp (rator rand) page 49 of EOPL 2: (replace Exp by Expression, capitalize instead of angle) CSG 111