Informatics 1 Functional Programming Lecture 9. Algebraic Data Types. Don Sannella University of Edinburgh

Similar documents
Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

Abstract Types, Algebraic Types, and Type Classes

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh

Informatics 1 Functional Programming Lecture 4. Lists and Recursion. Don Sannella University of Edinburgh

CS 320: Concepts of Programming Languages

Algebraic Types. Chapter 14 of Thompson

CSCE 314 Programming Languages

Programming Languages

Advanced features of Functional Programming (Haskell)

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

Programming Languages

Informatics 1 Functional Programming Lecture 5. Function properties. Don Sannella University of Edinburgh

Exercise 1 (2+2+2 points)

IA014: Advanced Functional Programming

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Logic - CM0845 Introduction to Haskell

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

Informatics 1 Functional Programming Lecture 12. Data Abstraction. Don Sannella University of Edinburgh

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

Type families and data kinds

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

CS 457/557: Functional Languages

Lecture 19: Functions, Types and Data Structures in Haskell

Recap: ML s Holy Trinity

CSE 130 Programming Languages. Lecture 3: Datatypes. Ranjit Jhala UC San Diego

An introduction to functional programming. July 23, 2010

A general introduction to Functional Programming using Haskell

CMSC 330: Organization of Programming Languages

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Recap: ML s Holy Trinity. Story So Far... CSE 130 Programming Languages. Datatypes. A function is a value! Next: functions, but remember.

CSE 130 Programming Languages. Datatypes. Ranjit Jhala UC San Diego

Programming in Haskell Aug-Nov 2015

Programming Languages. Datatypes

An introduction introduction to functional functional programming programming using usin Haskell

Informatics 1 Functional Programming Lecture 7. Map, filter, fold. Don Sannella University of Edinburgh

Programming with Math and Logic

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

Type system. Type theory. Haskell type system. EDAN40: Functional Programming Types and Type Classes (revisited)

Type-indexed functions in Generic Haskell

Practical Haskell. An introduction to functional programming. July 21, Practical Haskell. Juan Pedro Villa-Isaza. Introduction.

CS 440: Programming Languages and Translators, Spring 2019 Mon

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

CS 360: Programming Languages Lecture 12: More Haskell

CSE 3302 Programming Languages Lecture 8: Functional Programming

Exercise 1 ( = 24 points)

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

SIMON THOMPSON LEARNING FUNCTIONAL PROGRAMMING

CMSC 330: Organization of Programming Languages. OCaml Data Types

Haskell Making Our Own Types and Typeclasses

GADTs. GADTs in Haskell

Exercise 1 ( = 22 points)

Programming Languages. Next: Building datatypes. Suppose I wanted. Next: Building datatypes 10/4/2017. Review so far. Datatypes.

Custom Types. Outline. COMP105 Lecture 19. Today Creating our own types The type keyword The data keyword Records

Overloading, Type Classes, and Algebraic Datatypes

Lexical and Syntax Analysis. Abstract Syntax

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Haskell 101. (Version 1 (July 18, 2012)) Juan Pedro Villa Isaza

Programming Language Concepts, CS2104 Lecture 7

Informatics 1 Functional Programming Lectures 15 and 16. IO and Monads. Don Sannella University of Edinburgh

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

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

Homework 3 COSE212, Fall 2018

Exercise 1 ( = 18 points)

Problem Set CVO 103, Spring 2018

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

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

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

CISC 1600 Lecture 3.1 Introduction to Processing

02157 Functional Programming Tagged values and Higher-order list functions

Recap from last time. Programming Languages. CSE 130 : Fall Lecture 3: Data Types. Put it together: a filter function

CSc 372. Comparative Programming Languages. 18 : Haskell Type Classes. Department of Computer Science University of Arizona

More fun with recursion

SML A F unctional Functional Language Language Lecture 19

Beginning Programming (Pascal) Lecture 75. Figure 1. type-declarations. simple-type. pointer-type. array-type. file-type. set-type.

Programovací jazyky F# a OCaml. Chapter 3. Composing primitive types into data

Programming Languages Fall 2013

Structure and Interpretation of Computer Programs

Topic 7: Algebraic Data Types

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

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

CSC324 Principles of Programming Languages

Graphical Untyped Lambda Calculus Interactive Interpreter

How to Design Programs

CSE 130 [Winter 2014] Programming Languages

The type checker will complain that the two branches have different types, one is string and the other is int

Introduction to Functional Programming in Haskell 1 / 56

Dependent Polymorphism. Makoto Hamana

Module Title: Informatics 1 Functional Programming (first sitting) Exam Diet (Dec/April/Aug): December 2014 Brief notes on answers:

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

CS2500 Exam 2 Fall 2011

Advances in Programming Languages

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

CS 320: Concepts of Programming Languages

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

CS/ENGRD 2110 SPRING Lecture 7: Interfaces and Abstract Classes

Lexical and Syntax Analysis

Structure and Interpretation of Computer Programs

Fall Lecture 3 September 4. Stephen Brookes

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

Transcription:

Informatics 1 Functional Programming Lecture 9 Algebraic Data Types Don Sannella University of Edinburgh

Part I Algebraic types

Everything is an algebraic type data Bool = False True data Season = Winter Spring Summer Fall data Shape = Circle Float Rectangle Float Float data List a = Nil Cons a (List a) data Nat = Zero Succ Nat data Exp = Lit Int Add Exp Exp Mul Exp Exp data Tree a = Empty Leaf a Branch (Tree a) (Tree a) data Maybe a = Nothing Just a data Pair a b = Pair a b data Either a b = Left a Right b

Part II Boolean

Boolean data Bool = False True not :: Bool -> Bool not False = True not True = False (&&) :: Bool -> Bool -> Bool False && q = False True && q = q ( ) :: Bool -> Bool -> Bool False q = q True q = True

Boolean eq and show eqbool :: Bool -> Bool -> Bool eqbool False False = True eqbool False True = False eqbool True False = False eqbool True True = True showbool :: Bool -> String showbool False = "False" showbool True = "True"

Part III Seasons

Seasons data Season = Winter Spring Summer Fall next :: Season -> Season next Winter = Spring next Spring = Summer next Summer = Fall next Fall = Winter

Seasons eq and show eqseason :: Season -> Season -> Bool eqseason Winter Winter = True eqseason Spring Spring = True eqseason Summer Summer = True eqseason Fall Fall = True eqseason x y = False showseason :: Season -> String showseason Winter = "Winter" showseason Spring = "Spring" showseason Summer = "Summer" showseason Fall = "Fall"

Seasons and integers data Season = Winter Spring Summer Fall toint :: Season -> Int toint Winter = 0 toint Spring = 1 toint Summer = 2 toint Fall = 3 fromint :: Int -> Season fromint 0 = Winter fromint 1 = Spring fromint 2 = Summer fromint 3 = Fall next :: Season -> Season next x = fromint ((toint x + 1) mod 4) eqseason :: Season -> Season -> Bool eqseason x y = (toint x == toint y)

Part IV Shape

Shape type Radius = Float type Width = Float type Height = Float data Shape = Circle Radius Rect Width Height area :: Shape -> Float area (Circle r) = pi * rˆ2 area (Rect w h) = w * h

Shape eq and show eqshape :: Shape -> Shape -> Bool eqshape (Circle r) (Circle r ) = (r == r ) eqshape (Rect w h) (Rect w h ) = (w == w ) && (h == h ) eqshape x y = False showshape :: Shape -> String showshape (Circle r) = "Circle " ++ showf r showshape (Rect w h) = "Rect " ++ showf w ++ " " ++ showf h showf :: Float -> String showf x x >= 0 = show x otherwise = "(" ++ show x ++ ")"

Shape tests and selectors iscircle :: Shape -> Bool iscircle (Circle r) = True iscircle (Rect w h) = False isrect :: Shape -> Bool isrect (Circle r) = False isrect (Rect w h) = True radius :: Shape -> Float radius (Circle r) = r width :: Shape -> Float width (Rect w h) = w height :: Shape -> Float height (Rect w h) = h

Shape pattern matching area :: Shape -> Float area (Circle r) = pi * rˆ2 area (Rect w h) = w * h area :: Shape -> Float area s = if iscircle s then let r = radius s in pi * rˆ2 else if isrect s then let w = width s h = height s in w * h else error "impossible"

Part V Lists

Lists With declarations data List a = Nil Cons a (List a) append :: List a -> List a -> List a append Nil ys = ys append (Cons x xs) ys = Cons x (append xs ys) With built-in notation (++) :: [a] -> [a] -> [a] [] ++ ys = ys (x:xs) ++ ys = x : (xs ++ ys)

Part VI Natural numbers

Naturals With names data Nat = Zero Succ Nat power :: Float -> Nat -> Float power x Zero = 1.0 power x (Succ n) = x * power x n With built-in notation (ˆˆ) :: Float -> Int -> Float x ˆˆ 0 = 1.0 x ˆˆ n = x * (x ˆˆ (n-1))

Naturals With declarations add :: Nat -> Nat -> Nat add m Zero = m add m (Succ n) = Succ (add m n) mul :: Nat -> Nat -> Nat mul m Zero = Zero mul m (Succ n) = add (mul m n) m With built-in notation (+) :: Int -> Int -> Int m + 0 = m m + n = (m + (n-1)) + 1 (*) :: Int -> Int -> Int m * 0 = 0 m * n = (m * (n-1)) + m