Inductively Defined Data Concrete and Abstract syntax Karl Lieberherr.

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 (-
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Cs7100(Prasad)L8Interp1 Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications)
3.6 Interpreter: Recursion Recall Scheme's letrec (recursive let ): > (letrec ((fact (lambda (x) (if (zero? x) 1 (* x (fact (- x 1))))) (fact 6)) 720 Question:
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 Main motivation: include recursion (see McCarthy biographical excerpt on web site).
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
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.
PrasadL145OOL1 Managing Environments An Exercise in the Design, Analysis, Specification, and Implementation of the Core of an OOP Language. Object-Oriented.
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:
Chapter 2: Data Abstraction 2.2 An Abstraction for Inductive Data Types Recall inductive BNF definition of binary trees: ::= | ( ) To write programs using.
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.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
Lab 11 Lab 1: CSG 711: Programming to Structure Karl Lieberherr.
Compiler (javac) vs. Interpreter (drscheme): Chapter 3: Environment-Passing Interpreters Front End Interpreter program textsyntax tree answer > ((lambda.
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.
CS314 – Section 5 Recitation 10
A Simple Syntax-Directed Translator
Introduction to Scheme
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
Corky Cartwright January 18, 2017
Programming Language Concepts
Implementing Recursion
Original material by Eric Grimson
Case Study: Undefined Variables
5.4.1: Implementation Method Invocation
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:
Lecture #9 מבוא מורחב.
Abstract Syntax cs7100 (Prasad) L7AST.
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Chapter 3: Environment-Passing Interpreters
6.001 SICP Interpretation Parts of an interpreter
Recursive Procedures and Scopes
Inductively Defined Data Concrete and Abstract syntax
Lecture 25: The Metacircular Evaluator Eval Apply
CMPU-145: Foundations of Computer Science Spring, 2019
Presentation transcript:

Inductively Defined Data Concrete and Abstract syntax Karl Lieberherr

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

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)

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)

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)

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 … ))))))

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

Exercise: Test data type Contains in first a Lambda expression lambda- exp and in second an Application app-exp. Would like something like: (define-struct Test ( first ;; type lambda-exp second ;; type app-exp ) but with the dynamic checking benefit of define- datatype.

Better Way: variants are first class 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 Exp “)”. AppExp = “(“ Exp Exp “)”. Test = LambdaExp AppExp. Each non-terminal defines a data type.

Concern analysis (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

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)