Abstract Syntax cs7100 (Prasad) L7AST.

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 (-
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
Metacircular Evaluation SICP Chapter 4 Mark Boady.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
6.001 SICP SICP – Evaluation I Recitation 11/19/2004 Eval review Evaluation examples define lambda apply New language elements.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
EOPL3: Section 3.3 PROC and App B: SLLGEN
Environment: Data type associating symbols with values { a: 4s:'foof: x.x } Interface: (empty-env) = [ Ø ] (apply-env [f] s ) = f(s) (extend-env '( s 1...
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.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Plt /17/ Environment-Passing Interpreters Programming Language Essentials 2nd edition Chapter 3.8 Parameter-Passing Variations.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
SICP Interpretation part 2 Store operators in the environment Environment as explicit parameter Defining new procedures.
Operational Semantics of Scheme
Edited by Original material by Eric Grimson
CS 326 Programming Languages, Concepts and Implementation
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
Introduction to Scheme
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Scheme : variant of LISP
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Implementing Recursion
Env. Model Implementation
Original material by Eric Grimson
5.4.1: Implementation Method Invocation
2.3 Representation Strategies for Data Types
Semantics of PLs via Interpreters: Getting Started
The Metacircular Evaluator
Lecture 15: Tables and OOP
FP Foundations, Scheme In Text: Chapter 14.
Abstract Syntax Prabhaker Mateti 1.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Procedures App B: SLLGEN 1.
3.7 Variable Assignment Recall instance variables in Python:
Lecture 16: Tables and OOP
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Chapter 1 Review: BNF Grammar for lists:
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture #9 מבוא מורחב.
Abstract Syntax cs7100 (Prasad) L7AST.
Data Mutation Primitive and compound data mutators set! for names
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
Mutators for compound data Stack Queue
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Lecture 14: The environment model (cont
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
Assignments and Procs w/Params
Recursive Procedures and Scopes
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Abstract Syntax cs7100 (Prasad) L7AST

Language of l-expressions <exp> ::= <identifier> | (lambda ( <identifier> )<exp>) | (<exp> <exp>) E.g., concrete syntax Scheme S-expressions ( lambda (x) ( f ( f x ) ) ) cs7100 (Prasad) L7AST

Abstract Syntax (vs Concrete Syntax) lambda-exp id body app-exp rand rator var-exp app-exp rator rand id var-exp var-exp id id cs7100 (Prasad) L7AST

Overview Concrete Syntax Abstract Syntax Interpreter Results Parse-expression Concrete Syntax Abstract Syntax Unparse-expression Interpreter Potentially one can have several concrete syntax. Specifically, the text uses two concrete syntaxes in examples: Scheme Symbolic Expressions Scheme Character Strings Results cs7100 (Prasad) L7AST

Representing Abstract Syntax with Records (define-datatype expression expression? (var-exp (id symbol?)) (lambda-exp (id symbol?) (body expression?)) (app-exp (rator expression?) (rand expression?))) Resembles signature declarations var-exp : symbol? -> expression? lambda-exp : symbol? x expression? -> expression? app-exp : expression? x expression? -> expression? (caadr X) = (car (car (cdr X))) caadr = first of second of caddr = third cs7100 (Prasad) L7AST

Parse: Concrete to Abstract Syntax (define parse-expression (lambda (datum) (cond ((symbol? datum) (var-exp datum)) ((pair? datum) (if (eqv? (car datum) 'lambda) (lambda-exp (caadr datum) (parse-expression (caddr datum))) (app-exp (parse-expression (car datum)) (parse-expression (cadr datum))) ) (else (eopl:error 'parse-expression "Invalid concrete syntax ~s" datum)) ))) > (current-directory "J:\\tkprasad\\cs784\\EOPL-CODE\\interps") > (load "chez-init.scm") > (load "2-2-2.scm") 2-2-2.scm 2000-12-11 16:51 > (parse-expression 'x) (var-exp x) > (parse-expression '(lambda (x) (f x))) (lambda-exp x (app-exp (var-exp f) (var-exp x))) > (parse-expression 45) Error reported by parse-expression: Invalid concrete syntax 45 debug> From the trace, it looks like (define (var-exp datum) (list ’var-exp datum)) Observe Recursive call cs7100 (Prasad) L7AST

Example (Petite Scheme) > (current-directory “I:\\tkprasad\\cs784\\EOPL-CODE\\interps") > (load "chez-init.scm") > (load "2-2-2.scm") > (parse-expression 'x) (var-exp x) > (parse-expression '(lambda (x) (f x))) (lambda-exp x (app-exp (var-exp f) (var-exp x))) > (parse-expression 45) Error reported by parse-expression: Invalid concrete syntax 45 debug>e >(unparse-expression (lambda-exp 'x (app-exp (var-exp 'f) (var-exp 'x)))) (lambda (x) (f x)) cs7100 (Prasad) L7AST

Example (PLT Scheme) cs7100 (Prasad) L7AST

Unparse: Abstract to Concrete Syntax (define unparse-expression (lambda (exp) (cases expression exp (var-exp (id) id) (lambda-exp (id body) (list 'lambda (list id) (unparse-expression body)) ) (app-exp (rator rand) (list (unparse-expression rator) (unparse-expression rand)) ) ))) > (unparse-expression '(lambda-exp x (app-exp (var-exp f) (var-exp x)))) (lambda (x) (f x)) > (unparse-expression 45) (cases expression exp (var-exp (id) id) (lambda-exp (id body) (list 'lambda (list id) (unparse-expression body))) (app-exp (rator rand) (list (unparse-expression rator) (unparse-expression rand)))) Error reported by cases: Not a expression variant: 45. Cases-construct is tied to define-datatype. (rator rand) = positional and local names Observe Recursive call cs7100 (Prasad) L7AST

Role of Induction and Recursion Define data structures (infinite values) by induction. Seed elements. Closure operations. Define functions (operations) by recursion. Boundary/Basis case. Composite/Recursive case. Prove properties using structural induction. Basis case. Inductive step. Example: Natural number Constructors zero, succ Operations add,mul Properties identity, commutativity Chapter 1 + Scoping cs7100 (Prasad) L7AST

Representing Environment Constructors (empty-env, extend-env)+ Lookup (apply-env) together capture table behavior. They apportion the overall workload differently in the different implementations. Extend-env introduces a collection of bindings simultaneously. cs7100 (Prasad) L7AST

Alternative 1 (define empty-env (lambda () '())) (define extend-env (lambda (syms vals env) (cons (list syms vals) env) )) (define apply-env (lambda (env sym) (if (null? env) (eopl:error 'apply-env "No binding for ~s" sym) (let ((syms (car (car env))) (vals (cadr (car env))) (env (cdr env))) (let ((pos (rib-find-position sym syms))) (if (number? pos) (list-ref vals pos) (apply-env env sym))))) )) Ribcage = list of list of pairs impl. Functional style: All data together + all search code together cs7100 (Prasad) L7AST

Alternative 2 (define empty-env (lambda () (lambda (sym) (eopl:error 'apply-env "No binding for ~s" sym)) ) ) (define extend-env (lambda (syms vals env) (let ((pos (list-find-position sym syms))) (if (number? pos) (list-ref vals pos) (apply-env env sym)))) ) (define apply-env (lambda (env sym) (env sym) ) Env value – unary function – (lambda (sym) …) OOP Style: Incremental block and the corresponding search procedure together cs7100 (Prasad) L7AST

Alternative 3 (define-datatype environment environment? (empty-env-record) (extended-env-record (syms (list-of symbol?)) (vals (list-of scheme-value?)) (env environment?))) (define scheme-value? (lambda (v) #t)) cs7100 (Prasad) L7AST

(cont’d) (define empty-env (lambda () (empty-env-record) )) (define extend-env (lambda (syms vals env) (extended-env-record syms vals env))) (define apply-env (lambda (env sym) (cases environment env (empty-env-record () (eopl:error 'apply-env "No binding for ~s" sym)) (extended-env-record (syms vals env) (let ((pos (list-find-position sym syms))) (if (number? pos) (list-ref vals pos) (apply-env env sym)))) ) )) Ordinary tagged record implementation cs7100 (Prasad) L7AST

Queue (define reset (lambda (q) (vector-ref q 0))) (define empty? (lambda (q) (vector-ref q 1))) (define enqueue (lambda (q) (vector-ref q 2))) (define dequeue (lambda (q) (vector-ref q 3))) (define Q (create-queue)) ((enqueue Q) 55) ((empty? Q)) ((dequeue Q)) ((reset Q)) Queue represented as a pair of list with the second list in reverse order to enable constant time access to both ends in practice. When the first list is empty, the second list is reversed --- “inefficient” dequeue. (Cf. CS776 Queue) (Data invariant – normal form) OBJECTs in Scheme = Assignment operation + encapsulation (“limited” let construct + static scoping) + (infinite lifetime). cs7100 (Prasad) L7AST

(let ((q-in '()) (q-out '())) (letrec ((reset-queue (define create-queue (lambda () (let ((q-in '()) (q-out '())) (letrec ((reset-queue (set! q-in '()) (set! q-out '())) ) (empty-queue? (and (null? q-in) (null? q-out))) ) (enqueue (lambda (x) (set! q-in (cons x q-in))) ) (dequeue (if (empty-queue?) (eopl:error 'dequeue "On an empty queue") (begin (if (null? q-out) (set! q-out (reverse q-in)) (set! q-in '())) (begin '())) (let ((ans (car q-out))) (set! q-out (cdr q-out)) ans) ) ))) ) (vector reset-queue empty-queue? enqueue dequeue)) ))) Queue represented as a pair of list with the second list in reverse order to enable constant time access to both ends in practice. When the first list is empty, the second list is reversed --- “inefficient” dequeue. (Cf. CS776 Queue) (Data invariant – normal form) Assignment operation in Scheme + encapsulation (static scoping) + (infinite lifetime). ========================================================================= 4.1.5 Conditionals syntax: if <test> <consequent> <alternate> syntax: if <test> <consequent> Syntax: <Test>, <consequent>, and <alternate> may be arbitrary expressions. Semantics: An `if' expression is evaluated as follows: first, <test> is evaluated. If it yields a true value (see section see section 6.3.1 Booleans), then <consequent> is evaluated and its value(s) is(are) returned. Otherwise <alternate> is evaluated and its value(s) is(are) returned. If <test> yields a false value and no <alternate> is specified, then the result of the expression is unspecified. (if (> 3 2) 'yes 'no) ==> yes (if (> 2 3) 'yes 'no) ==> no (if (> 3 2) (- 3 2) (+ 3 2)) ==> 1 ================================================================================= 9/23/2014 revision: In Racket, the if-then syntax is no longer allowed. Furthermore, empty begin-expression is illegal for nop. cs7100 (Prasad) L7AST