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