CS 320: Concepts of Programming Languages

Similar documents
CS 320: Concepts of Programming Languages

CS 320 Homework One Due 2/5 11:59pm

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS558 Programming Languages

CS558 Programming Languages

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

User-defined Functions. Conditional Expressions in Scheme

Exercise 1 ( = 24 points)

GADTs. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 7. [Faculty of Science Information and Computing Sciences]

GADTs. Alejandro Serrano. AFP Summer School. [Faculty of Science Information and Computing Sciences]

IA014: Advanced Functional Programming

Exercises on ML. Programming Languages. Chanseok Oh

(Refer Slide Time: 4:00)

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Exercise 1 ( = 18 points)

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

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

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

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

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

GADTs. Wouter Swierstra. Advanced functional programming - Lecture 7. Faculty of Science Information and Computing Sciences

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Exercise 1 ( = 22 points)

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

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

Graph reduction. Simple graph reduction with visualisation

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

Lecture: Functional Programming

Lambda Calculi With Polymorphism

Lecture #23: Conversion and Type Inference

Exercise 1 (2+2+2 points)

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

Coq Summer School, Session 2 : Basic programming with numbers and lists. Pierre Letouzey

CS558 Programming Languages

CS1102: Macros and Recursion

Case by Case. Chapter 3

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

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

Programming Languages

Introduction to Programming, Aug-Dec 2006

Type families and data kinds

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

Programming Languages

Logic - CM0845 Introduction to Haskell

SCHEME 10 COMPUTER SCIENCE 61A. July 26, Warm Up: Conditional Expressions. 1. What does Scheme print? scm> (if (or #t (/ 1 0)) 1 (/ 1 0))

Pattern Matching and Abstract Data Types

Introducing Wybe a language for everyone

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

MoreIntro_annotated.v. MoreIntro_annotated.v. Printed by Zach Tatlock. Oct 04, 16 21:55 Page 1/10

Programming Languages

Fundamental Concepts. Chapter 1

SCHEME AND CALCULATOR 5b

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Programming in Omega Part 1. Tim Sheard Portland State University

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

Typed Scheme: Scheme with Static Types

CS115 - Module 10 - General Trees

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

Lesson 4 Typed Arithmetic Typed Lambda Calculus

The syntax and semantics of Beginning Student

The syntax and semantics of Beginning Student

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

Programming Languages and Techniques (CIS120)

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

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

Introduction to Functional Programming in Haskell 1 / 56

Type-indexed functions in Generic Haskell

An Explicit Continuation Evaluator for Scheme

Haskell 98 in short! CPSC 449 Principles of Programming Languages

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

Functional Programming - 2. Higher Order Functions

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Types and Programming Languages. Lecture 5. Extensions of simple types

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Lecture 3: Recursion; Structural Induction

CITS3211 FUNCTIONAL PROGRAMMING. 10. Programming in the pure λ calculus

Programming Languages

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

CS 11 Ocaml track: lecture 2

Programming Language Concepts: Lecture 14

Lambda Calculus and Type Inference

Reasoning About Programs Panagiotis Manolios

CSCE 314 Programming Languages

Intro. Scheme Basics. scm> 5 5. scm>

MoreIntro.v. MoreIntro.v. Printed by Zach Tatlock. Oct 07, 16 18:11 Page 1/10. Oct 07, 16 18:11 Page 2/10. Monday October 10, 2016 lec02/moreintro.

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Lists. Michael P. Fourman. February 2, 2010

Programming with Math and Logic

Project 1: Scheme Pretty-Printer

Metaprogramming assignment 3

Typed Racket: Racket with Static Types


Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Transcription:

CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 03: Bare-Bones Haskell Continued: o Function Application = Rewriting by Pattern Matching o Haskell Types and Polymorphism

Function Application by Matching and Rewriting Recall: Rewriting involves matching the left-hand side of a function definition with a subexpression, where variables are instantiated to subexpressions. Function definitions are tried in order from the top. Data (constructors) in green, Variables (including function names) in black. I ll use => to indicate rewrites to and the redex = term being rewritten, will be underlined. cond (not True) (Succ Zero) (pred (Succ Zero)) => cond False (Succ Zero) (pred (Succ Zero)) => (pred (Succ Zero)) => Zero

Function Application by Matching and Rewriting Three important things to remember about defining functions by pattern matching: (1) The left-side of a function definition must consist of a function name followed by expressions consisting only of constructors and variables, and variables can occur at most once: Not allowed: cond True x y = x cond (not True) x y = y xor x x = False xor x y = True

Function Application by Matching and Rewriting Three important details on matching in Haskell: (1) Continued... Note that constructor expressions can be as complicated as you want! rightassoc (Plus (Plus (Val Zero) (Val Zero)) (Val Zero) ) => Plus (Val Zero) (Plus (Val Zero) (Val Zero))

Functional Application by Matching and Rewriting Three important details on matching in Haskell: (2) The patterns (LHSs) have to account for all possible expressions, that is, the range of the patterns has to be exhaustive. Haskell can check this for you! incr Zero = (Succ Zero) What about (Succ Zero)?? Better: incr Zero = (Succ Zero) incr (Succ x) = (Succ (Succ x) ) Best: incr x = (Succ x)

Functional Application by Matching and Rewriting Three important details on matching in Haskell: (3) You can use wildcard variables, that match anything and don t create a binding: iszero Zero = True incr _ = False If you put such a rule LAST, it can account for anything other expressions have not matched yet.

Haskell Type System Reading: Hutton Ch. 3 Type declarations are given by the syntax: expression :: type-name Examples: False :: Bool (not (not False)) :: Bool Function types have the form: argument-type -> result-type Example: not :: Bool -> Bool

Haskell Type System Reading: Hutton Ch. 3 You can find the type of an expression in the repl using :type or :t Main> :type (not (not True)) (not (not True)) :: Bool Main> :t (iszero Zero) (iszero Zero) :: Bool Main> :t not not :: Bool -> Bool Main> :t iszero iszero :: Nat -> Bool Main> :t odd odd :: Nat -> Bool

Haskell Type System Reading: Hutton Ch. 3 You should specify a type as part of the definition of a function: In general, this is good practice, and expected as part of good Haskell programming style. It provides documentation about how the function works and in some cases, is necessary to be specific about what you want the function to do.

Haskell Type System Reading: Hutton Ch. 3 If you don t specify a type, Haskell can infer the types from the expressions: data Bool = True False data Nat = Zero Succ Nat even Zero = True Must be Nat -> Bool! even (Succ x) = odd x Haskell uses the following rule to infer the types of expressions: f :: A -> B (f e) :: B e :: A premises conclusion Therefore, (even Zero) must have the type Bool: even :: Nat -> Bool Zero :: Nat (even Zero) :: Bool

Haskell Type System Reading: Hutton Ch. 3 The type system also applies to the data types, and constructors have types just like function types, except the constructors don t do anything except structure the data. Main> :t Succ Succ :: Nat -> Nat Main> :t Val Succ :: Nat -> Expr Main> :t Plus Plus :: Expr -> Expr -> Expr

Haskell Type System Reading: Hutton Ch. 3 Functions and constructors of more than one argument have types with multiple arrows ; the last type is the result type and the others are the argument types: Main> :t cond cond :: Bool -> Nat -> Nat -> Nat

Polymorphic Types Reading: Hutton Ch. 3.7 Recall: Many functions (and data types) do not need to know everything about the types of the arguments and results. Let s start with data types. Why should we have to define a list type for every possible kind of data in the list? Instead, we can define polymorphic types using type variables: a is a type variable, and just like any other variable, it can stand for anything (in this case, any type). Compare Java Generics: class List< T > { (List Nat) is isomorphic to ListNat } T element;...

Polymorphic Types Reading: Hutton Ch. 3.7 Main> :t (ConsNat Zero NilNat) (ConsNat Zero NilNat) :: ListNat Main> :t (Cons Zero Nil) (Cons Zero Nil) :: List Nat Main> :t (Cons True (Cons False Nil)) (Cons True (Cons False Nil)) :: List Bool Main> :t (Cons (Cons True Nil) Nil) What s the type?

Polymorphic Types Reading: Hutton Ch. 3.7 Main> :t (Cons (Cons True Nil) Nil) (Cons (Cons True Nil) Nil) :: List (List Bool) Haskell can also infer polymorphic types: Main> :t Nil Nil :: List a Main> :t Cons Cons :: a -> List a -> List a Main> identity x = x Main> :t identity identity :: a -> a Main> test x y = x Main> :t test test :: a -> b -> a

Polymorphic Types Reading: Hutton Ch. 3.7 Functions also can have polymorphic types when they don t need to know exactly what type of data they manipulate. Most of these functions involve restructuring or selecting out pieces of data, for example in lists: Main> :t second second :: List a -> a Main> a = Cons True (Cons False Nil) Main> :t a a :: List Bool Main> head a True Main> tail a Cons False Nil