Scope and Introduction to Functional Languages. Review and Finish Scoping. Announcements. Assignment 3 due Thu at 11:55pm. Website has SML resources

Similar documents
Introduction to SML Getting Started

CIS24 Project #3. Student Name: Chun Chung Cheung Course Section: SA Date: 4/28/2003 Professor: Kopec. Subject: Functional Programming Language (ML)

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CMSC 330: Organization of Programming Languages. Functional Programming with OCaml

Introduction to ML. Based on materials by Vitaly Shmatikov. General-purpose, non-c-like, non-oo language. Related languages: Haskell, Ocaml, F#,

Lecture 2: SML Basics

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

Functional Programming

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

ITT8060 Advanced Programming

CSCI-GA Scripting Languages

Introduction to OCaml

Cunning Plan. One-Slide Summary. Functional Programming. Functional Programming. Introduction to COOL #1. Classroom Object-Oriented Language

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

Functional Programming. Big Picture. Design of Programming Languages

So what does studying PL buy me?

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

CS558 Programming Languages

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

OCaml. History, Variants. ML s holy trinity. Interacting with ML. Base type: Integers. Base type: Strings. *Notes from Sorin Lerner at UCSD*

Functional programming Primer I

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

A Functional Evaluation Model

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Programming Languages

CS 314 Principles of Programming Languages

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

The Substitution Model

Type Checking and Type Inference

G Programming Languages - Fall 2012

Variables and Bindings

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

CS558 Programming Languages

Functional Programming

History of Programming Languages. Functional Programming (1/2)

Recap: Functions as first-class values

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Some Advanced ML Features

Any questions. Say hello to OCaml. Say hello to OCaml. Why readability matters. History, Variants. Plan (next 4 weeks)

CMSC 330: Organization of Programming Languages

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Programming Languages, Summary CSC419; Odelia Schwartz

CHAPTER ONE OVERVIEW. 1.1 Continuation-passing style

CS558 Programming Languages

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

The Substitution Model. Nate Foster Spring 2018

Computer Science CSC324 Wednesday February 13, Homework Assignment #3 Due: Thursday February 28, 2013, by 10 p.m.

Concepts of Programming Languages

Programming Languages and Compilers (CS 421)

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Basic concepts. Chapter Toplevel loop

Type Inference Systems. Type Judgments. Deriving a Type Judgment. Deriving a Judgment. Hypothetical Type Judgments CS412/CS413

Announcements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML

The role of semantic analysis in a compiler

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

CS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview

Cunning Plan. Gone In Sixty Seconds. History of Programming Languages. Functional Programming. Functional Programming. Review and Administrivia

CPS 506 Comparative Programming Languages. Programming Language Paradigm

02157 Functional Programming Lecture 1: Introduction and Getting Started

A Brief Introduction to Standard ML

Formal Systems and their Applications

G Programming Languages - Fall 2012

CSE 341 Section 5. Winter 2018

COS 320. Compiling Techniques

Begin at the beginning

CS422 - Programming Language Design

Course outline. CSE 341: Programming Languages. Why study programming languages? Course motivation and objectives. 1 lecture: Concepts

Chapter 15. Functional Programming Languages

Homework 5. Notes. Turn-In Instructions. Reading. Problems. Handout 19 CSCI 334: Spring (Required) Read Mitchell, Chapters 6 and 7.

CS 242. Fundamentals. Reading: See last slide

L3 Programming September 19, OCaml Cheatsheet

G Programming Languages - Fall 2012

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Polymorphism and Type Inference

CS Lectures 2-3. Introduction to OCaml. Polyvios Pratikakis

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

Simply-Typed Lambda Calculus

Meeting14:Denotations

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Lambda Calculus. Variables and Functions. cs3723 1

15 150: Principles of Functional Programming. More about Higher-Order Functions

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Variables. Substitution

Written Presentation: JoCaml, a Language for Concurrent Distributed and Mobile Programming

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

Types, Type Inference and Unification

Meeting13:Denotations

G Programming Languages Spring 2010 Lecture 4. Robert Grimm, New York University

DO NOT OPEN UNTIL INSTRUCTED

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

Recursive Definitions, Fixed Points and the Combinator

CSCI B522 Lecture 11 Naming and Scope 8 Oct, 2009

Transcription:

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 Static scoping: Let s extend arithmetic with a binding construct e ::= n e 1 + e 2 e 1 * e 2 let x = e 1 in e 2 x Review and Finish Scoping let binds a new variable x to e 1 for use in e 2 variable x is bound in the body e 2 the scope of x is the body e 2 4 How do resolve variables? Lexical 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 Can we do a program transformation to make it clear where a variable is bound? How? 5 6 1

Rename bound variables Examples We identify terms up to renaming (αconversion) Higher-Order Abstract Syntax: All terms that differ only in names of bound variables are considered equivalent. 7 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 e fv( var(x) ) = { x } fv( num(n) ) = fv( plus(e 1, e 2 ) ) = fv( times(e 1, e 2 ) ) = fv( let(x, e 1, e 2 ) ) = 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(e 1, e 2 ) = [y/x]times(e 1, e 2 ) = [y/x]let(z, e 1, e 2 ) = 9 10 α-conversion An equivalence relation given by the following axiom: let(x, e 1, e 2 ) let(y, e 1, [y/x]e 2 ) In any context (formally, a congruence) 11 2

Dynamic Scoping Scope depends on the calling sequence of functions Within a function, is there any difference with static scoping? Exercise: Which x is used? PROCEDURE f() = VAR x : INTEGER; PROCEDURE g() = VAR x : INTEGER; BEGIN END; PROCEDURE h() = BEGIN x END; BEGIN h(); g(); END BEGIN g(); h(); END 13 14 Summary: Dynamic Scoping No variable is local making programs difficult to debug e.g., in LISP, TCL, TeX On to Functional Languages Generally, considered a bug in PL implementation (but watch out, very easy to do!) 15 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) 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) 17 18 3

State Imperative programs destructively modify existing state Functional programs yield new similar states over time SET1 SET add_elem(set, item) add_elem(set1, item) SET2 19 Functional Example Simple Functional Set (built out of lists) fun add_elem (s, e) = if s = [] then [e] else if hd s = e then s else hd s :: add_elem(tl s, e) Pattern-Matching Functional fun add_elem ([],e) = [e] add_elem (hd :: tl, e) = if hd = e then s else hd :: add_elem(tl,e) 20 In Imperative Code Functional-Style Advantages More cases to handle List* 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) { I have stopped reading Stephen King novels. Now I just read C code instead. - Richard O Keefe s->tl = list(e, NULL); return s; } else return add_elem(s->tl, e); } 21 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 How do functional languages give you referential transparency? 23 24 4

No side-effects Functional-Style Disadvantages? Why is referential transparency useful? 25 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. SML 27 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 semantics 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 30 5

A Small SML Program SML Top-Level Value and function declarations (* *) for comments (nestable) (* A small SML program *) val four = 4 fun plusfour y = y + four val eight = plusfour four; 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 No type declarations needed! ; ends top-level expressions, not needed with just declarations 31 Run a file by typing use file.sml ; 32 Simple SML Examples: Basic Types 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) 33 34 6