Presentation is loading. Please wait.

Presentation is loading. Please wait.

Is everyone signed up on piazza?

Similar presentations


Presentation on theme: "Is everyone signed up on piazza?"— Presentation transcript:

1 Is everyone signed up on piazza?

2 This Course Jython in Java High Level Languages Java (Object Oriented)
10 High Level Languages Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) This Course Jython in Java Relation

3

4 Programming Language Concepts
Substitution Definition 8 (Substitution, take 4) To substitute identifier i in e with expression v, replace all non-binding identifiers in e having the name i with the expression v, except within nested scopes of i. Finally, we have a version of substitution that works. A different, more succint way of phrasing this definition is Definition 9 (Substitution, take 5) To substitute identifier i in e with expression v, replace all free instances of i in e with v.

5 Develop Substitution for the Following Expressions
Start with schema from Chapter 2 Get the following expressions to work: (subst (id 'x) 'x (id 'y)) (subst (num 2) 'x (num 1)) (subst (id 'x) 'x (num 1)) (subst (id 'y) 'x (num 1)) (subst (add (id 'x) (id 'x)) 'x (num 1)) (subst (with 'y (num 2) (id 'x)) 'x (num 1)) (subst (with 'y (num 2) (add (id 'x) (id 'y))) 'x (num 1)) (subst (with 'y (id 'x) (add (id 'x) (id 'y))) 'x (num 1)) (subst (with 'x (id 'x) (id 'x)) 'x (num 1)) (calc (subst (with 'y (add (num 2) (id 'x)) (add (id 'y)(id 'x))) 'x (num 1)))

6 Scheme for Textbook Chapter 3
#lang plai (define-type WAE ;; calc : WAE!number [num (n number?)] (define (calc expr) [add (lhs WAE?) (rhs WAE?)] [sub (lhs WAE?) (rhs WAE?)] [num (n) n] [with (name symbol?) (named-expr WAE?) (body WAE?)] [add (l r) (+ (calc l) (calc r))] [sub (l r) (- (calc l) (calc r))] [id (name symbol?)]) (calc (subst bound-body ;; subst : WAE symbol WAE!WAE bound-id (define (subst expr sub-id val) (num (calc named-expr))))] (type-case WAE expr [id (v) (error 'calc "free identifier")])) [num (n) expr] [add (l r) (add (subst l sub-id val) (subst r sub-id val))] [sub (l r) (sub (subst l sub-id val) [with (bound-id named-expr bound-body) (if (symbol=? bound-id sub-id) (with bound-id (subst named-expr sub-id val) bound-body) (subst bound-body sub-id val)))] [id (v) (if (symbol=? v sub-id) val expr)]))

7 Programming Language Concepts
Chapters 1 and 2 Concepts: Concrete Sytax Abstract Syntax Token (Terminal) Non-Terminal BNF s-expression Parse Interpret (calc) Chapter 3 Concepts: Identifier Substitution Binding Instance Bound Identifier Free Instance (Free Identifier) Scope Eager Evaluation Lazy Evaluation

8 Programming Language Concepts Eager and Lazy Evaluation
We began this material motivating the introduction of with: as a means for eliminating redundancy. Let’s revisit this sequence of substitutions (skipping a few intermediate steps): {with {x {+ 5 5}} {with {y {- x 3}} {+ y y}}} = {with {x 10} {with {y {- x 3}} {+ y y}}} = {with {y {- 10 3}} {+ y y}} = {with {y 7} {+ y y}} = {+ 7 7} = 14 Couldn’t we have also written it this way? = {with {y {- {+ 5 5} 3}} {+ y y}} = {+ {- {+ 5 5} 3} {- {+ 5 5} 3}} = {+ {- 10 3} {- {+ 5 5} 3}} = {+ {- 10 3} {- 10 3}} = {+ 7 {- 10 3}} In the top example, we invoke calc before substitution (because the result of calc is what we supply as an argument to subst). This model of substitution is called eager: we “eagerly” reduce the named expression to a value before substituting it. This is in contrast to the second example of reductions above, which we call lazy, wherein we reduce the named expression to a value only when we need to (such as at the application of an arithmetic primitive).


Download ppt "Is everyone signed up on piazza?"

Similar presentations


Ads by Google