Upload
others
View
0
Download
0
Embed Size (px)
Citation preview
1
Scope and Introduction to Functional Languages
Prof. Evan Chang
Meeting 7, CSCI 3155, Fall 2009
Announcements
• Assignment 3 due Thu at 11:55pm– Submit in pairs
• Website has SML resources– Text: Harper, Programming in SML
– Supplemental Text: Hansen and Rischel, Introduction to Programming using SML (on reserve)
– SML is installed on CSEL
2
Review and Finish Scoping
Static scoping: Let’s extend arithmetic with a binding construct
e ::= n | e1 + e2 | e1 * e2 | let x = e1 in e2 | x
• let binds a new variable x to e1
for use in e2
– variable x is bound in the body e2
– the scope of x is the body e2
4
How do resolve variables?
•• Lexical scope ruleLexical scope rule:– A variable is bound by the nearest enclosing binding• Operationally, we walk up through the AST looking for the nearest let that binds that variable
5
Can we do a program transformation to make it clear where a variable is bound?
• How?
6
2
Rename bound variables
• We identify terms up to renaming (α-conversion)
7
HigherHigher--Order Abstract SyntaxOrder Abstract Syntax:HigherHigher--Order Abstract SyntaxOrder Abstract Syntax:All terms that differ only in names of bound variables are considered equivalent.
Examples
8
Free Variables
• A free variable of an expression e is a variable that is not bound within e
• Define fv(e) - set of free vars of efv( var(x) ) = { x }
fv( num(n) ) =
fv( plus(e1, e2) ) =
fv( times(e1, e2) ) =
fv( let(x, e1, e2) ) =
9
Variable Renaming
• Define [y/x]e to be the renaming of all free occurrences of x to y in e[y/x]var(z) =
[y/x]num(n) =
[y/x]plus(e1, e2) =
[y/x]times(e1, e2) =
[y/x]let(z, e1, e2) =
10
αααα-conversion
• An equivalence relation ≡ given by the following axiom:
let(x, e1, e2) ≡ let(y, e1, [y/x]e2)
• “In any context” (formally, a congruence)
11
3
Dynamic Scoping
• “Scope depends on the calling sequence of functions”
• Within a function, is there any difference with static scoping?
13
Exercise: Which x is used?
PROCEDURE f() =
VAR x : INTEGER;
PROCEDURE g() =
VAR x : INTEGER; BEGIN … END;
PROCEDURE h() = BEGIN … x … END;
BEGIN BEGIN
h(); g(); g(); h();
END END
14
Summary: Dynamic Scoping
• No variable is local making programs difficult to debug– e.g., in LISP, TCL, TeX
• Generally, considered a bug in PL implementation– (but watch out, very easy to do!)
15
On to Functional Languages
Functional Programming
• You know OO and Imperative• Functional Programming
– Computation = evaluating (math) functions– Avoid “global state” and “mutable data”– Get stuff done = apply (higher-order) functions
• Important Features– Higher-order, first-class functions– Closures and recursion– Symbolic processing (lists, ASTs)
17
Computation
• In a pure functional language, computation is simply expression evaluation
fun add (x,y) = if x=0 then y else add(x-1,y+1)
add(2,5) →
18
4
State
• Imperative programs destructively modify existing state
• Functional programs yield new similar states over time
19
SET add_elem(SET, item)
add_elem(SET1, item)SET1 SET2
Functional Example
• Simple Functional Set (built out of lists)fun add_elem (s, e) = if s = [] then [e]else if hd s = e then selse hd s :: add_elem(tl s, e)
• Pattern-Matching Functionalfun add_elem ([],e) = [e]
| add_elem (hd :: tl, e) =if hd = e then selse hd :: add_elem(tl,e)
20
In Imperative Code
• More cases to handleList* add_elem(List *s, item e) {if (s == NULL) return list(e, NULL);
else if (s->hd == e)return s;
else if (s->tl == NULL) {s->tl = list(e, NULL); return s;
} else return add_elem(s->tl, e);
} 21
I have stopped reading Stephen
King novels. Now I just read C
code instead.
- Richard O’Keefe
Functional-Style Advantages
• Tractable program semantics– Procedures are functions
– Formulate and prove assertions about code
– More readable
• Referential transparency– What is it?
22
Functional-Style Advantages
• Tractable program semantics– Procedures are functions
– Formulate and prove assertions about code
– More readable
• Referential transparency– Replace any (sub)expression by its value without changing the result
23
How do functional languages give you referential transparency?
24
5
No side-effects
• Why is referential transparency useful?
25
Functional-Style Disadvantages?
26
Functional-Style Disadvantages
• Efficiency– Copying takes time (if needed)
• Compiler implementation – Frequent memory allocation
• Unfamiliar (to you!)– New programming style
• Not appropriate for every program– Operating systems, etc.
27
SML
Short History
• ML (Meta Language)– Edinburgh, 1973
– Part of a theorem proving system LCF
• SML/NJ (Standard ML of New Jersey)– Bell Labs and Princeton, 1990
• OCaml (Objective Caml)– INRIA, 1996
• Same core ideas but some (annoying) syntactic differences
29
ML Innovative Features
• Type system– Strongly typed
– Type inference
– Abstraction
• Modules
• Patterns
• Polymorphism
• Higher-order functions
• Concise formal semantics30
There are many ways of trying to
understand programs. People often rely
too much on one way, which is called
“debugging” and consists of running a
partly-understood program to see if it
does what you expected. Another way,
which ML advocates, is to install some
means of understanding in the very
programs themselves.
- Robin Milner, 1997
6
A Small SML Program
(* A small SML program *)
val four = 4
fun plusfour y = y + four
val eight = plusfour four;
31
(* *) for comments (nestable)
Value and function declarations
No type declarations needed!
; ends top-level ; ends top-level expressions, not needed with just declarations
SML Top-Level
• Interactive loop: expressions can be typed and evaluated at the top-level
Standard ML of New Jersey v110.65 [built: Wed Oct 31 17:24:44 2007]
- 1;
val it = 1 : int
• Run a file by typinguse “file.sml”;
32
Simple SML Examples: Basic Types
33
For Next Time
• Reading (a bit longer than usual)
• Online discussion forum– ≥1 substantive question, comment, or answer each week
• Homework assignment 3
• Acknowledgments: Wes Weimer (functional vs. imperative examples)
34