Principles of programming languages 12: Functional programming

Slides:



Advertisements
Similar presentations
More ML Compiling Techniques David Walker. Today More data structures lists More functions More modules.
Advertisements

ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Intermediate Code Generation
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter ElevenModern Programming Languages1 A Fourth Look At ML.
A Fourth Look At ML Chapter ElevenModern Programming Languages, 2nd ed.1.
A First Look at ML Chapter FiveModern Programming Languages, 2nd ed.1.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
5/11/2015IT 3271 Types in ML (Ch 11) datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list n Predefined, but not.
Compiler Construction
Introduction to ML – Part 1 Kenny Zhu. Assignment 2 chive/fall07/cos441/assignments/a2.ht m
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Single assignment store Kernel language syntax Carlos Varela.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Introduction and Chapter 8 of Programming Languages by Ravi Sethi Elements of Functional Programming.
Functional Programming Element of Functional Programming.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
A FIRST LOOK AT ML Chapter Five Modern Programming Languages 1.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  Elements may appear more than once.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Cs776(Prasad)L6sml971 SML-97 Specifics SML/NJ 110.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Functional Programming
Type Checking and Type Inference
Types CSCE 314 Spring 2016.
Chapter 7: Expressions and Assignment Statements
ML: a quasi-functional language with strong typing
Information Science and Engineering
Selection (also known as Branching) Jumail Bin Taliba by
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Principles of programming languages 4: Parameter passing, Scope rules
Chapter 7: Expressions and Assignment Statements
Chapter 6 Intermediate-Code Generation
Functions, Patterns and Datatypes
CSE 341 Section 5 Winter 2018.
Compiler design.
Functions, Patterns and Datatypes
CSCE 314: Programming Languages Dr. Dylan Shell
Functions, Patterns and Datatypes
CSE-321 Programming Languages Introduction to Functional Programming
Declarative Computation Model Single assignment store (VRH 2
Compiler Construction
Functions, Patterns and Datatypes
CS 242 Types John Mitchell.
CS 242 Types John Mitchell Reading: Chapter 6.
Assignments and Procs w/Params
Modern Programming Languages Lecture 18 Fakhar Lodhi
PROGRAMMING IN HASKELL
Functions, Patterns and Datatypes
Compiler Construction
Presentation transcript:

Principles of programming languages 12: Functional programming Department of Information Science and Engineering Isao Sasano

ML ML is a functional language with imperative features.(function-oriented imperative language) Features of functional languages: functions are first class values A function can be created using an expression Functions can take functions as their arguments. Functions can return functions. Type system in ML is considered to be most beautiful and have much expressive power. ML supports important features in Lisp-like or Algol-like languages. We illustrate functional languages by using Standard ML. Other functional languages include OCaml, Haskell, etc.

Type system in ML Type system in ML is safe. When type checker judges that an expression has some type, it is guaranteed that evaluating the expression results in a value of the type. (ex.) When an expression is a pointer to a string, the value of the expression is guaranteed to be a pointer that points to some memory area that holds a string. (The value is guaranteed not to be a pointer that points to some memory area that have been already freed (dangling pointer) or a pointer that points to some memory area that holds a value other than a string.

Interactive session <exp>; val it = <value representation> : <type> (ex.) - 5+3-2; val it = 6 : int - it+3; val it = 9 : int - if true then 1 else 5; val it = 1 : int - 5=4; val it = false : bool

Evaluating (executing) expressions in ML Expressions are parsed, type-checked, compiled, and then executed. When an expression has some syntax error or type error, code are not generated and the expression is not executed. (ex.) The expression if true then 3 else false does not have syntax error but have type error since the type system of ML requires for the then-part and else-part have the same type. - if true then 3 else false; stdIn:5.1-5.26 Error: types of if branches do not agree [literal] then branch: int else branch: bool in expression: if true then 3 else false

Declarations val <id> = <exp>; val <id> = <value representation> : <type> “val” is a prefix of “value”. The expression <exp> is evaluated and the result is bound to the identifier <id>. (ex.) - val x = 7+2; val x = 9 : int - val y = x+3; val y = 12 : int - val z = x*y-(x div y); val z = 108 : int

(Just for reference) In ML, / is used for division of values of type real (floating point numer). Unlike C, implicit conversion is not done in ML. (ex.) - 9/12; stdIn:1.2-1.6 Error: operator and operand don't agree [literal] operator domain: real * real operand: int * int in expression: 9 / 12

Function declarations fun <id> <arg1> … <argn> = <exp>; val <id> = fn : <type1> -> … -> <typen> -> <result type> (ex.) - fun f (x) = x+5; val f = fn : int -> int The function of type int -> int is bound to the identifier f. - val f = fn x => x+5; This is equivalent to the above declaration. fn x => x+5 corresponds to the lambda abstraction λ x. x + 5.

Assignment In ML we cannot change the values of variables. For example, under the declaration val x = 3, the value of x is always 3. (ML decalarions introduce variables as constants.) In ML we use reference cells, which enabe us to change the values. (ex.) - val x = ref 3; val x = ref 3 : int ref - !x; val it = 3 : int - x:=4; val it = () : unit val it = 4 : int

Identifiers in C, Pascal, etc. In C and Pascal, identifiers of type int are mutable, but identifiers of function type are constants so that they cannot be changed to other values (functions). In other words, in C and Pascal, whether variables are constants or not depends on the types (whether they are functions or not).

Types A type consists of a set of values and operations on them. A type is represented by a type expression. <type-exp> ::= <type-name> | <type-exp> -> <type-exp> | <type-exp> * <type-exp> | <type-exp> list | {<id>:<type-exp>,…,<id>:<type-exp>} Syntax of (subset of) type expressions in ML

Basic types When a value cannot be decomposed (in language level), we say it is atomic and its type is basic. (ex.) Values in a set {true, false} are atomic. Operations on values of basic types are defined in each type (ex.) Operations on values of int type: 2+3, 5*6, 2=2, 2≠2, etc.

Basic types in ML Unit type Boolean type () : unit () is used as a return value of functions that do not need to return a value, or as an argument of functions that do not need to take an argument. (Unit type roughly plays a role similarly to void type in C.) Boolean type true: bool, false : bool In if expression if e1 then e2 else e3, e1 must have bool type, e2 and e3 must have the same type. ML does not provide if expression without having else part. Boolean operators andalso, orelse, not correspond to &&, ||, ! in C.)

Basic types in ML (cont.) int type 0,1,2,..,-1,-2,… : int +, -, *, div : int * int -> int (infix operators) string type Symbols surrounded by double qoutes Concatination operator ^ : string * string -> string (infix) real type 1.0, 2.0, 3.14159, 4.4444, … : real +, -, * are used on either real or int type. (Both sides must have the same type.) The function “real” converts int type to real type. For example, real 3 is evaluated to 3.0. ML does type inference, so explicit conversion is necessary.

Types and product types The product type A*B of two types A and B consists of pairs of values of type A and B. (ex.) (1, “one”) is a pair of 1 and ”one”. The product type A1*A2*…*An of n types consists of tuples of the form (a1,a2,…,an) where ai is a value of type Ai with 1 ≤ i ≤ n. Operations on a product type are a function that takes the first element and a function that takes the second element from a pair. fun first (x,y) = x; fun second (x,y) = y; Operations on n tuples are defined similarly.

Examples of tuples Pairs, triples, quadruples, … (ex.) - (3, 4); val it = (3,4) : int * int - (4, 5.5, true); val it = (4,5.5,true) : int * real * bool - ("Taro", "Jiro", 5); val it = ("Taro","Jiro",5) : string * string * int - #2 (3, 4); val it = 4 : int - #1 ("Taro", "Jiro", 5); val it = "Taro" : string

Records Records in ML correspond to structures in C and redcords in Pascal. Records and tuples are similar but records have names in each (ex.) - {first_name="Donald", last_name="Knuth"}; val it = {first_name="Donald",last_name="Knuth"} : {first_name:string, last_name:string}

Lists - 3::nil; (ex.) val it = [3] : int list - 4::5::it; A list is a (finite) sequence of elements of same type. The type A list consists of a set of lists having elements of type A. (ex.) int list type consists of lists of integers. A list is represented by writing elements delimited by commas and surrounded by brackets [ and ]. The empty list is written by [ ] or nil. (ex.) [1,2,3] is a list of three integers 1,2,3 and [“red”, “white”, “blue”] is a list of three strings ”red”, “white”, “blue”. Cons is written as ::. (ex.) - 3::nil; val it = [3] : int list - 4::5::it; val it = [4,5,3] : int list

Lists (cont.) Unlike Lisp, lists in ML must have elements of the same type. (ex.) - [1,2,3,4]; val it = [1,2,3,4] : int list - [true,false]; val it = [true,false] : bool list - ["red","blue"]; val it = ["red","blue"] : string list - [fn x=>x+1, fn x=>x+2]; val it = [fn,fn] : (int -> int) list

Operations on lists null(x) returns true when x is the empty list and false otherwise. hd(x) returns the first element in the list. tl(x) returns the list x where the first element is deleted. a::x returns the list where a is consed to the list x. (ex.) null [ ] returns true. (ex.) null [1,2,3] returns false. (ex.) [1,2,3] = 1::[2,3] = 1::2::[3] = 1::2::3::[ ] :: is right associative and thus 1::2::[3] is equivalent to 1::(2::[3]) and 1::2::3::[ ] is equivalent to 1::(2::(3::[ ])).

Patterns We can use a pattern in the LHS of a val declaration. val <pattern> = <exp>; <pattern> ::= <id>|<tuple>|<cons>|<record>|<constr> <tuple> ::= (<pattern>, …, <pattern>) <cons> ::= <pattern> :: <pattern> <record> ::= {<id>=<pattern>, …, <id>=<pattern>} <constr> ::= <id>(<pattern>,…,<pattern>) (Note 1) A same pattern variable cannot appear twice in a pattern. (It cannot be expressed as BNF and thus checked in some other phase than the parsing phase. (Note 2) nil, which belongs to <constr>, is a constructor with no arguments.

Examples of patterns - val t = (1,2,3); val t = (1,2,3) : int * int * int - val (x,y,z) = t; val x = 1 : int val y = 2 : int val z = 3 : int - val a::b = [1,2,3]; val a = 1 : int val b = [2,3] : int list - val (a,b::c) = (1,[2,3]); val b = 2 : int val c = [3] : int list

Function declarations with patterns fun <id> <pattern> = <exp> A function can be declared in either of these two forms. fun <id> <pattern> = <exp> |… | <id> <pattern> = <exp> (ex.) - fun f (x,y) = x+y; val f = fn : int * int -> int - fun length nil = 0 | length (x::xs) = 1 + length xs; val length = fn : 'a list -> int (Note) The function f seems to take two arguments, but actually it takes one argument, which is a pair.

(Cont.) - length ["a","b","c"]; val it = 3 : int The declaration of function length consists of two clauses, and the actual argument is pattern-matched from the first clause. When the argument is nil, length returns 0. Otherwise the argument is matched with the pattern x::xs. We use datatype declarations to declare a datatype. (ex.) datatype tree = LEAF of int | NODE of (tree * tree) - fun inTree(x,LEAF y)=x=y | inTree(x,NODE(y,z))=inTree(x,y) orelse inTree(x,z); val inTree = fn : int * tree -> bool