CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.

Slides:



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

A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Programming Languages and Paradigms
Kathleen Fisher cs242 Reading: “A history of Haskell: Being lazy with class”,A history of Haskell: Being lazy with class Section 6.4 and Section 7 “Monads.
Programming Languages Section 1 1 Programming Languages Section 1. SML Fundamentals Xiaojuan Cai Spring 2015.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
Introduction to ML – Part 1 Frances Spalding. Assignment 1 chive/fall05/cos441/assignments/a1.ht m
Standard ML- Part I Compiler Baojian Hua
Type Checking.
1 CS 312 – Lecture 28 Continuations –Probably the most confusing thing you’ve seen all semester… Course summary –Life after CS 312.
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 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
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.
Introduction to ML You will be responsible for learning ML on your own. Today I will cover some basics Read Robert Harper’s notes on “an introduction to.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Sequence Control Chapter 6. 2 l Control structures: the basic framework within which operations and data are combined into programs. Sequence control.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Ryan Chu. Arithmetic Expressions Arithmetic expressions consist of operators, operands, parentheses, and function calls. The purpose is to specify an.
CS235 Languages and Automata Department of Computer Science Wellesley College Introduction to Standard ML Wednesday, September 23, 2009 Reading: Beginning.
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Putting it all together: LINQ as an Example. The Problem: SQL in Code Programs often connect to database servers. Database servers only “speak” SQL. Programs.
ISBN Chapter 15 Functional Programming Languages.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
Dan Johnson.  Functional language that started development in  Committee designed language  Pure and Lazy  Compiled or Interpreted  Named after.
Chapter 9: Functional Programming in a Typed Language.
Chapter Fifteen: Functional Programming Languages Lesson 12.
Compiling Functional Programs Mooly Sagiv Chapter 7
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
1 Alex Proctor and Brian Lee for CSCI 431 at UNCA, Fall 2002 ML (Meta Language) a brief introduction Alex Proctor and Brian Lee For CSCI 431 at the University.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Copyright © 2009 Elsevier Chapter 10 :: Functional Languages Programming Language Pragmatics Michael L. Scott.
Functional Programming. Some Functional Languages Lisp Scheme - a dialect of Lisp Haskell Miranda.
CSE 341 Section 10 Subtyping, Review, and The Future.
Cs7120 (Prasad)L1-FP-HOF1 Functional Programming Basics Correctness > Clarity > Efficiency.
Cs776 (Prasad)L2HOF1 Higher-Order Functions. cs776 (Prasad)L2HOF2 Higher-Order Functions A function that takes a function as argument and/or returns a.
Python Programming Language by Vasu Chetty. Origins of Python Created by: Guido van Rossum, a Dutch Programmer Created during: Christmas Break, 1989 Created.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Functional Programming
Type Checking and Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Functional Programming
Principles of programming languages 12: Functional programming
ML: a quasi-functional language with strong typing
CS 3304 Comparative Languages
Functional Programming
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
CSE 341 Section 5 Winter 2018.
Assignments and Procs w/Params
CSE 341 Lecture 11 b closures; scoping rules
Functional Programming and Haskell
CSE341: Programming Languages Lecture 11 Type Inference
Drew Wyborski Programming Languages
Functional Programming and Haskell
Presentation transcript:

CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson

ML (so-called meta-language) (SML/NJ at ML is a functional language. – Side effects are possible via arrays and ref variables, but using them is not the norm. – No loops, normally code is free of side effects. Unlike Lisp, ML uses strong static typing. – Context sensitive type inference minimizes the amount of explicit typing in the code. – Implicit parametric polymorphism supports generic functions.

ML Data Types The usual primitive types Lists, tuples, and structured aggregate types User defined types, modules, and abstraction interfaces for information hiding Immutable bindings for variables and function names Mutable array and ref variable types Strong, static, implicit type inference from context Nested lexical scope using let construct, closures supported

Variable bindings are immutable - val a = 5; val a = 5 : int - fun adda(x) = x + a; val adda = fn : int -> int - val a = 6 (* This is a totally different immutable variable! *); val a = 6 : int - fun adda_again(x) = x + a; val adda_again = fn : int -> int - adda(1); val it = 6 : int - adda_again(1); val it = 7 : int

Type inference infers object types. Type resolution occurs at compile time - val a = 5; val a = 5 : int - fun adda(x) = x + a; val adda = fn : int -> int - adda; val it = fn : int -> int Implicit parametric polymorphism supports generic functions. - fun identity(x) = x ; (* Unlike Python, everything has a static type. *) val identity = fn : 'a -> 'a(* ‘a is a type variable, similar to a C++ template type.) - identity(4); val it = 4 : int - identity("foo"); val it = "foo" : string

Strong, static types. - val a = 8 ; val a = 8 : int - val b = 9.0 ; val b = 9.0 : real - a * b; stdIn: Error: operator and operand don't agree [tycon mismatch] operator domain: int * int operand: int * real in expression: a * b

Strong, static types continued. - b * a; stdIn: Error: operator and operand don't agree [tycon mismatch] operator domain: real * real operand: real * int in expression: b * a - a * floor(b); val it = 72 : int

Strong, static typing implies. No variable number of function parameters. No eval function. What implies this concept? No heterogeneous lists. - val l = [1, 2, "a"]; stdIn: Error: operator and operand don't agree [literal] operator domain: int * int list operand: int * string list in expression: 2 :: "a" :: nil

Higher Order Functions map, reduce and filter Similar to Lisp, Scheme and (later) Python function composition and currying Built into ML using “o” operator (composition) and unparenthesized parameters (currying). Parenthesized parameters constitute a tuple. function parameter evaluation is eager functions are first-class objects (passed as arguments, returned from functions)

Examples of composition, curried functions and lambda expressions. ~parson/ThryLang/func/ Function composition built into ML is more powerful and notationally convenient than anything we (or at least I) can define using the language. Likewise for curried functions. The inability to use vararg parameters because of strong static typing limits flexibility. The language constructs are powerful, but overall flexibility and extensibility are less than with dynamically typed languages like Python or Scheme.

Comparison to Scheme and Python Functional Programming Strong type inference trades flexibility and certain kinds of extensibility in favor of stronger compile-time error checking. Perspective is compile-time verification of program correctness. Not only are there typically no side effects (as in pure Lisp), but also no chance for type incompatibilities. Type inference minimizes explicit type specification in the code.

Other Noteworthy Features PROLOG-like patterns on function variants and data structure variants. Eager evaluation fun returndiv(quotient, divisor) = if divisor <> 0 then quotient else ~1000 ; (* Like a C conditional statement. *) - returndiv(8 div 2, 2); val it = 4 : int - returndiv(8 div 0, 0); uncaught exception Div [divide by zero] raised at: stdIn:

Haskell Haskell has many features similar to ML. tuples, lists, structured data, modules, type inference Haskell is strictly pure functional programming – no side effects – monads hide I/O side effects behind an action abstraction Lazy evaluation is one of the more novel features. Formally, normal order evaluation. Similar to short-circuited boolean expressions and if- then-else control constructs in imperative languages. The unused expressions are never evaluated.

Lazy evaluation ~parson/ThryLang/func/hugsdemo.hs returndiv quotient divisor = if (divisor /= 0) then quotient else Main> returndiv (div 8 2) 2 4 Main> returndiv (div 8 0) (div 8 0) is never evaluated unless used It may be evaluated once, then the value is cached.

Infinite Lists are Generators addfirsttwo (x:y:zs) = x + y ones = 1 : ones from n = n : from (n+1) fromstep n m = n : fromstep (n+m) m Main> addfirsttwo(ones) 2 Main> addfirsttwo(from 7) 15 Main> addfirsttwo(fromstep 10 10) 30 Main> addfirsttwo([ 3.. ]) 7 Main> addfirsttwo([ 3, 6.. ]) 9

Lazy Evaluation is a generalization of special purpose mechanisms in imperative languages Similar to short-circuited boolean expressions and if- then-else control constructs in imperative languages. Laziness allows a called function or accessed data structure to decide when to evaluate an expression and possibly bind its value to a parameter. Make the imperative special cases into a general approach to evaluation. Lazy evaluation is a conditional rewrite approach.

Conclusions Lisp and Python are good for experimenting with extensibility within a functional programming framework. ML and Haskell are good for type-safe functional programming with powerful builtin mechanisms. Extensibility of the language itself is not well supported in ML and Haskell.