CSCI-GA Scripting Languages

Similar documents
Introduction to OCaml

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

CS558 Programming Languages

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Why OCaml? Introduction to Objective Caml. Features of OCaml. Applications written in OCaml. Stefan Wehr

Introduction to Objective Caml

CMSC 330: Organization of Programming Languages

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

L3 Programming September 19, OCaml Cheatsheet

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

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

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

Type Systems, Type Inference, and Polymorphism

So what does studying PL buy me?

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

CS Lecture 6: Map and Fold. Prof. Clarkson Spring Today s music: Selections from the soundtrack to 2001: A Space Odyssey

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

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

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

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

Pierce Ch. 3, 8, 11, 15. Type Systems

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

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

Lists. Michael P. Fourman. February 2, 2010

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

CS 321 Programming Languages

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

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

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

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

CSC324 Principles of Programming Languages

Mini-ML. CS 502 Lecture 2 8/28/08

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

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

# true;; - : bool = true. # false;; - : bool = false 9/10/ // = {s (5, "hi", 3.2), c 4, a 1, b 5} 9/10/2017 4

Functional Programming

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

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

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CS558 Programming Languages

Programming Languages Lecture 15: Recursive Types & Subtyping

Types and Type Inference

CS 11 Ocaml track: lecture 2

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

Lecture 12: Data Types (and Some Leftover ML)

Polymorphism and Type Inference

(Refer Slide Time: 4:00)

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

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

Introduction to OCaml

CSE 341: Programming Languages

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

CS321 Languages and Compiler Design I Winter 2012 Lecture 13

CS558 Programming Languages

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

Types, Type Inference and Unification

Introduction to SML Getting Started

Overloading, Type Classes, and Algebraic Datatypes

Type Checking and Type Inference

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

CSE 505: Concepts of Programming Languages

Types and Type Inference

CSE341 Section 3. Standard-Library Docs, First-Class Functions, & More

Recursive Types and Subtyping

Symbolic Computation and Common Lisp

Booleans (aka Truth Values) Programming Languages and Compilers (CS 421) Booleans and Short-Circuit Evaluation. Tuples as Values.

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

The design of a programming language for provably correct programs: success and failure

CS 11 Haskell track: lecture 1

Software System Design and Implementation

CMSC 330: Organization of Programming Languages

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CMSC 330, Fall 2013, Practice Problem 3 Solutions

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

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

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

According to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont) - Type-safe!

CS Lecture 6: Map and Fold. Prof. Clarkson Fall Today s music: Selections from the soundtrack to 2001: A Space Odyssey

Handout 2 August 25, 2008

CS 360: Programming Languages Lecture 12: More Haskell

02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen

Lecture 8: Summary of Haskell course + Type Level Programming

22c:111 Programming Language Concepts. Fall Types I

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP)

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

Introduction to Haskell

CMSC 330, Fall 2013, Practice Problems 3

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

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

h0ps://

CS558 Programming Languages

Transcription:

CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1

Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2

About OCaml A functional programming language All computation is done with functions No state (mutable variables), simple and clean Functions are first class: you can pass them, return them, etc. Support for object-oriented programming (the O in OCaml) Statically typed, type-safe language You can t apply the wrong operations to the wrong data (e.g., can t try to divide two strings) Avoids many silly errors and provides security (e.g., no buffer overflows) 3

Ocaml Features Garbage collection: automatic memory management Type inference: You don t have to write the type information down. The compiler will figure it out. Parametric polymorphism: Write functions and data structures that can be used with any type. Note that Java provides subtype polymorphism. Algebraic data types: Can build data structures easily. Pattern matching makes working with them convenient. Advanced modules: Encapsulate implementations behind interfaces. Functors (functions that manipulate modules) add functionality beyond most languages module systems. 4

Concepts Weak/Strong, Static/Dynamic Typing Strong typing (explicit conversions) ML C Weak typing (implicit conversions) assembler Static typing (compile-time checks) Java VBA Scheme PHP JavaScript Perl Dynamic typing (runtime checks) 5

History Robin Milner and others at the University of Edinburgh were working on theorem provers Problem: the provers would sometimes put incorrect proofs together, and claim they were valid Designed ML (Meta Language) as a language to let you construct valid proofs In the early 80s, there was a schism in the ML community French developed Caml and later Objective Caml (OCaml) British and Americans developed Standard ML (SML) 6

How to Write + Run Code ocaml REPL ocaml file.ml Run the interpreter ocamlc file.ml;./a.out Run the compiler 7

Types primitive constructed int float bool char string unit lists tuples functions 8

Type Inference The compiler deduces the type of an expression y = x + 1 Compiler knows that + takes two integers and returns an integer, so x and y should be integers. z = x + 1.0 This should produce an error, since x is being used as both an int and a float Compiler infers types by aggregating type information, and reducing expressions to implicitly typed values 9

Type Inference Statically determining whether a program will have a type-error is impossible All statically typed languages are conservative, and may reject some programs that are perfectly okay Milner formulated the type-inference system for ML, and proved its soundness Note that Milner also worked on concurrent programming languages (CCS, CSP, pi-calculus), and won the Touring award in large part to his work on ML 10

Syntax (subset) Syntactic class Grammar rule Example identifiers x,f a, x,y,x_y, z100. constants c 1, 01, 1.0, true, hello A unary operator u -, not binary operator b +,*,-, <,>, ^,!=, terms declarations e::= x c u e e b e if e then e else e let d and d in e e (e,,e) t ::= x = e f (x,,x) : t = e not b, 2 + 2, foo one = 1 11

Program Errors An expression can have a legal syntax, but may be wrong. The expression must be well-typed. Ways that a program can be wrong: Syntax errors: let 0 x = Type errors: x + 3 Semantic errors: 1/0 More general errors: computes the wrong output 12

Example let abs (x : int) : int = if x < 0 then -x else x let abs : int -> int = function x -> if x < 0 then -x else x let abs = fun x -> if x < 0 then -x else x val abs : int -> int = <fun> Every expression and declaration has both a type and a value. Here, we have bound the name abs to a function whose type is int -> int. 13

Type Checking $ ocaml # let abs = fun x -> if x < 0 then -x else x ;; val abs : int -> int = <fun> # abs 3;; - : int = 3 # abs "foo";; Error: This expression has type string but an expression was expected of type int 14

Scope Variable declarations bind variables within a scope let x = e1 in e2 The scope of x is the expression e2. Functions also bind variables let f x = e1 in e2 The scope of the formal parameter x is the expression in e1. The scope of the variable f (which is bound to a function value) is the body of the let, e2. 15

Scope A let expression can introduce multiple variables let x = 2 and y = 3 in x + y To declare a recursive function, the function must be in scope in its own body. In Ocaml, you use let rec instead of let for this. let rec even x = x = 0 odd (x-1) and odd x = not (x = 0 not (even (x-1))) in odd 3110 16

Curried functions A function with multiple parameters is really just syntactic sugar for a function passed a tuple as an argument let plus (x, y) = x + y;; val plus : int * int -> int = <fun> plus (2, 3) Ocaml has another way to declare functions with multiple arguments. In curried form: let plus x y = x + y Notice there are no commas between the parameters 17

Curried functions There are also no commas when calling the function: plus 2 3 Functions really only have one argument. The plus function is being passed one argument, 2, and it returns a function that takes one argument. (plus 2) (3) plus 2 3 = ((function (x : int) -> function (y : int) -> x + y) 2) 3 = (function (y : int) -> 2 + y) 3 = 2 + 3 = 5 18

Lists Lists are immutable. You cannot change the elements of a list. Lists are homogenous. The can only contain one type. Examples: [1;2;3], 1::[2;3], [1;2]@[3;4] 19

Pattern Matching Use match expressions to extract elements from a list match lst with [] -> 0 [x] -> 1 _ -> 2 Returns 0 if the list is empty, 1 if the list has 1 element, and 2 if it has 2 or more elements 20

Pattern Matching Use recursive functions to do something to every element in a list let rec length (lst : string list) : int = match lst with [] -> 0 h :: t -> 1 + length t This function computes the length of the list lst. 21

Variant Types Allows you to have variables that contain more than one kind of value Similar to an enum in java, or a tagged union in C type answer = Yes No Maybe;; Type keyword declared name for the type. Declared with a set of constructors type eitherpoint = TwoD of float * float ThreeD of float * float * float 22

Pattern Matching Ocaml patterns are very rich Can match on variant types, tuples, records, and can pull the values apart let answer_to_string (a : answer) : string = match a with Yes -> yes No -> no 23

Polymorphism Suppose we want to write a function that swaps positions of a pair: let swapint ((x : int), (y : int)) : int * int = (y, x) and swapreal ((x : float), (y : float)) : float * float = (y, x) and swapstring ((x : string), (y : string)) : string * string = (y, x) Better way: let swap ((x : 'a), (y : 'b)) : 'b * 'a = (y, x);; Or without type declarations: # let swap (x, y) = (y, x);; val swap : 'a * 'b -> 'b * 'a = <fun> 24

Mapping Apply a function to every element in a list, and return a new list: List.map f [a; b; c] = [f a; f b; f c] Copy a list (using an anonymous function) let copy = map (fun x -> x) 25

Folding Apply a function to every element in a list, accumulates a result: fold_right f [a; b; c] r = f a (f b (f c r)) fold_left f r [a; b; c] = f (f (f r a) b) c Sum all the elements in a list: let sum_right_to_left il = fold_right (+) il 0 let sum_left_to_right = fold_left (+) 0 26

Folding Fold is very powerful! Can write many other list functions in terms of fold type intlist = Nil Cons of (int * intlist) let mapp f lst = fold_right (fun x y -> Cons (f x, y)) lst Nil Note that fold_left would give the result in reverse order let length = fold_left (fun x _ -> 1 + x) 0 Select a subset of the list let filter f lst = fold_right (fun x y -> if f x then Cons (x, y) else y) lst Nil 27

Administrative Last Slide Next lecture: Review! 28