Defining Racket: Functions

Defining Racket: Functions

CS 251 Fall 20192019 Principles of of Programming Programming Languages Languages Ben Wood Topics λ Ben Wood • Function definitions Defining Racket: • Function application Functions • Functions are first-class values. https://cs.wellesley.edu/~cs251/ Functions 1 Functions 2 Anonymous function definition expressions Function application (call) Syntax: (lambda (x1 … xn) e) Syntax: (e0 e1 ... en) – parameters: x1 through xn are identifiers – body: e is any expression Evaluation: 1. Under the current dynamic environment, E, Evaluation: evaluate e0 through en to values v0, …, vn. 1. The result is a function closure, ⟨E, (lambda (x1 … xn) e)⟩, 2. If v0 is a function closure of n arguments, holding the current environment, E, and the function. ⟨E', (lambda (x1 … xn) e)⟩ then [closure] The result is the result of evaluating the closure E ⊢ (lambda (x1 … xn) e) ↓ ⟨E, (lambda (x1 … xn) e)⟩ body, e, under the closure environment, E', Note: extended with argument bindings: – An anonymous function definition is an expression. x1 ⟼ v1, …, xn ⟼ vn. – A function closure is a new kind of value. Closures are not expressions. Otherwise, there is a type error. – This is a definition, not a call. The body, e, is not evaluated now. – lambda from the λ-calculus. Functions 3 Functions 4 Function application (call) Function application derivation example Syntax: (e0 e1 … en) Assume initial environment is empty. ((lambda (x) (* x x)) (- 12 8)) Evaluation: E ⊢ e0 ↓ ⟨E', (lambda (x1 … xn) e)⟩ E ⊢ e1 ↓ v1 … E ⊢ en ↓ vn x1 ⟼ v1, …, xn ⟼ vn, E' ⊢ e ↓ v [apply] E ⊢ (e0 e1 … en) ↓ v Functions 5 Functions 6 Function bindings and recursion PL design/implementation: layers A function is an expression, so define works: (define square (lambda (x) (* x x))) kernel define also adds self-binding to function's environment*, primitive values, supporting recursion. data types (define pow (lambda (base exp) syntactic sugar (if (< exp 1) 1 (* base (pow base (- exp 1)))))) standard libraries During an application of this function, pow will be bound to this function. * Magic for now. We will be precise later. user libraries Functions 7 Functions 8 syntax syntax syntax syntax syntax syntax syntax syntax syntax syntax syntax Syntactic sugar for function bindings syntax More syntactic sugar (define (pow base exp) (if (< exp 1) What else looks like a function application? 1 (* base (pow base (- exp 1))))) Syntactic sugar: simpler syntax for common idiom. What looks like a function application but – Static textual translation to existing features. really is not? – i.e., not a new feature. Desugar (define (x0 x1 … xn) e) to (define x0 (lambda (x1 … xn) e)) Functions 9 Functions 10 So sweet Racket so far Expressions like (+ e1 e2), (< e1 e2), and (not e)are Racket declaration bindings: really just function calls! (define x e) Racket expressions (most of the kernel language!) Initial top-level environment binds built-in functions: literal values (numbers, booleans, strings): 251, 3.141, #t, "PL" + ⟼ addition function, variable references: x, fact, positive?, fib@n-1 - ⟼ subtraction function, conditionals: (if e1 e2 e3) * ⟼ multiplication function, functions: (lambda (x1 … xn) e) < ⟼ less-than function, function application: (e0 e1 … en) not ⟼ boolean negation function, What about: – Assignment? Unnecessary! Thread state through. … – Loops? Unnecessary! Use recursion. (where some built-in functions do primitive things) – Data structures? lambda is all we need, but other options soon. Functions 11 Functions 12 Racket kernel syntax so far Meta-syntax so far Bindings – Syntax of our evaluation model. b ::= (define x e) – Not part of the Racket syntax. "*" is grammar meta-syntax – Cannot write in source programs. Expressions that means "zero or more of e ::= v | x | (if e e e) the preceding symbol." | (lambda (x*) e) | (e e*) Values (+closures) v ::= … | ⟨E, (lambda (x*) e)⟩ Literal Values (booleans, numbers, strings) v ::= #f | #t | n | s Environments Identifiers (variable names) E ::= . | x ⟼ v, E x (see valid identifier explanation) Functions 13 Functions 14 Racket kernel dynamic semantics so far Binding evaluation: E ⊢ b ß E' [define] E ⊢ e ↓ v E ⊢ (define x e) ß x ⟼ v, E' Expression evaluation: E ⊢ e ↓ v [if nonfalse] [value] [var] E ⊢ e1 ↓ v1 E ⊢ v ↓ v E(x) = v E ⊢ e2 ↓ v2 E ⊢ x ↓ v v1 is not #f [apply] E ⊢ (if e1 e2 e3) ↓ v2 E ⊢ e0 ↓ ⟨E', (lambda (x1 … xn) e)⟩ [if false] E ⊢ e1 ↓ v1 … E ⊢ en ↓ vn E ⊢ e1 ↓ #f x1 ⟼ v1, …, xn ⟼ vn, E' ⊢ e ↓ v E ⊢ e3 ↓ v3 E ⊢ (e0 e1 … en) ↓ v E ⊢ (if e1 e2 e3) ↓ v3 Functions 15.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    4 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us