Tree Equality: The Problem

Similar documents
CSC324 Principles of Programming Languages

Advanced features of Functional Programming (Haskell)

Haskell Overview III (3A) Young Won Lim 10/4/16

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Introduction to Programming, Aug-Dec 2006

Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2)

Advances in Programming Languages

Overloading, Type Classes, and Algebraic Datatypes

Haskell Types, Classes, and Functions, Currying, and Polymorphism

Lecture 19: Functions, Types and Data Structures in Haskell

CSC/MAT-220: Lab 6. Due: 11/26/2018

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Exercise 1 (2+2+2 points)

Introduction to Programming: Lecture 10

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

User-Defined Algebraic Data Types

Functional Programming in Haskell Part I : Basics

CS 320: Concepts of Programming Languages

Exercise 1 ( = 24 points)

Type Classes in Haskell Tom Schrijvers. Leuven Haskell User Group

Chapter 20: Binary Trees

CS558 Programming Languages

Exercise 1 ( = 18 points)

CS558 Programming Languages

Programming in Haskell Aug-Nov 2015

Logic - CM0845 Introduction to Haskell

CS 320: Concepts of Programming Languages

Functional Programming in Haskell Part 2 : Abstract dataypes and infinite structures

Haskell Programs. Haskell Fundamentals. What are Types? Some Very Basic Types. Types are very important in Haskell:

Binary Search. Roland Backhouse February 5th, 2001

Type-indexed functions in Generic Haskell

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

PROGRAMMING IN HASKELL. Chapter 2 - First Steps

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

CS102 Binary Search Trees

A general introduction to Functional Programming using Haskell

Binary Heaps in Dynamic Arrays

Programming Languages Fall 2013

Haskell Types COMP360

CSci 450: Org. of Programming Languages Overloading and Type Classes

CS 457/557: Functional Languages

Programming with Math and Logic

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

Haskell Overview IV (4A) Young Won Lim 10/13/16

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CSc 372 Comparative Programming Languages

CS 360: Programming Languages Lecture 12: More Haskell

CSc 372. Comparative Programming Languages. 8 : Haskell Function Examples. Department of Computer Science University of Arizona

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

I2206 Data Structures TS 6: Binary Trees - Binary Search Trees

Topic 9: Type Checking

Topic 9: Type Checking

Course year Typeclasses and their instances

Advanced Programming Handout 6. Purely Functional Data Structures: A Case Study in Functional Programming

Exercise 1 ( = 22 points)

Haskell Overview II (2A) Young Won Lim 8/9/16

Haskell Types COMP360

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS 315 Data Structures mid-term 2

Programming Languages and Techniques (CIS120)

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

Fundamentals of Programming Languages. Data Types Lecture 07 sl. dr. ing. Ciprian-Bogdan Chirila

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Generic BST Interface

Imperative languages

Suppose that the following is from a correct C++ program:

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Sections p.

Haskell 98 in short! CPSC 449 Principles of Programming Languages

CS 3114 Data Structures and Algorithms READ THIS NOW!

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Exam Data structures DAT036/DAT037/DIT960

Functional Programming Mid-term exam Tuesday 3/10/2017

Type Processing by Constraint Reasoning

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

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

Lecture Notes on Binary Search Trees

Programming Language Concepts, CS2104 Lecture 7

A6-R3: DATA STRUCTURE THROUGH C LANGUAGE

Lecture 11: Subprograms & their implementation. Subprograms. Parameters

Programming Paradigms

Haskell Overview IV (4A) Young Won Lim 10/24/16

CIS 252 Introduction to Computer Science Section M013: Exam 3 (Blue) April 26, Points Possible

CS 106X Sample Final Exam #2

Solution sheet 1. Introduction. Exercise 1 - Types of values. Exercise 2 - Constructors

CS3110 Spring 2017 Lecture 18: Binary Search Trees

CSE 341 Section 5. Winter 2018

Advanced Programming Handout 5. Purely Functional Data Structures: A Case Study in Functional Programming

Algebraic data types. The case against null

Topic 7: Algebraic Data Types

CS 440: Programming Languages and Translators, Spring 2019 Mon

CSC 210, Exam Two Section February 1999

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

LINEAR PROGRAMMING: A GEOMETRIC APPROACH. Copyright Cengage Learning. All rights reserved.

Functional Programming. Another representative from the Declarative paradigm

* Due 11:59pm on Sunday 10/4 for Monday lab and Tuesday 10/6 Wednesday Lab

Programming Languages and Techniques (CIS120e)

GO MOCK TEST GO MOCK TEST I

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

Data Structures in Java

Transcription:

Recall the tree data type we defined: Tree Equality: The Problem data LTree a = LLeaf a LBranch (LTree a) (LTree a) Suppose we want to write a function that determines if two trees are equal: treeeq (LLeaf x) (LLeaf y) = x==y treeeq (LBranch t1 t2) (LBranch s1 s2) = treeeq t1 s1 && treeeq t2 s2 treeeq = False There are two problems: 1. What is its type? 2. We would like to overload == and not use the name treeeq. FP Lecture 6 1

Operator Overloading: The Questions Recall our previous story about numbers: we said 3 :: Integer (+) :: Integer -> Integer -> Integer But this is obviously lying. For example, + also works with Int, Rational, Float, and Double. On the other hand, it does not work with lists. So we raise the questions: What is the actual type of + then? Is it polymorphic? How is this overloading implemented? Can we extend this overloading to my data types and my operators? FP Lecture 6 2

Type Classes All of the above problems and questions are resolved in Haskell by type classes. Conceptually, a type class represents a restricted set of types. (Contrast: a type variable represents the set of all types, unrestricted.) Pragmatically, a type class declares a few operators and functions for overloading. E.g., == is declared in the type class Eq: class Eq a where (==), (/=) :: a -> a -> Bool This says: For a type a that belongs to the class Eq, it has two operators: == and /= of type a->a->bool. FP Lecture 6 3

Type Instances: Declaration You can put any data type into a type class, but you have to implement the operators. Put it in another way: in order to overload an operator for your data type, you put it into the appropriate type class. E.g., recall Tree: data Tree = Leaf Branch Tree Tree Put it into class Eq: instance Eq Tree where Leaf==Leaf = True (Branch t1 t2)==(branch s1 s2) = t1==s1 && t2==s2 == = False FP Lecture 6 4

Type Instances: Defaults Note that we only implemented == but not /=. default implementations: This is because Eq has class Eq a where (==), (/=) :: a -> a -> Bool x == y = not (x/=y) x /= y = not (x==y) If we only implement ==, the above code for /= will work, and vice versa. So now we can compare trees: Leaf /= Branch Leaf Leaf --True Branch Leaf Leaf == Branch Leaf Leaf --True FP Lecture 6 5

Types of Overloaded Operators What is the type of == then? It ain t a->a->bool, as a could be outside Eq. Here it is: (==) :: Eq a => a->a->bool It says: it is of type a->a->bool assuming that a belongs to Eq. Likewise, there is a class Num consisting of all numeric types, and we have: (+) :: Num a => a->a->a 3 :: Num a => a So + and 3 will work for Int, Integer, Rational, Float, Double, etc., because they all belong to Num (and they all implement + accordingly). FP Lecture 6 6

Tree Equality: Solution To overload == for LTree: data LTree a = LLeaf a LBranch (LTree a) (LTree a) we need a prerequisite: a should belong to Eq first. instance Eq a => Eq (LTree a) where This says: LTree a belongs to class Eq provided that a already belongs to class Eq. (LLeaf x)==(lleaf y) = x==y That is why we need the Eq a assumption. (LBranch t1 t2)==(lbranch s1 s2) = t1==s1 && t2==s2 == = False FP Lecture 6 7

Tree Equality: Solution 2 Could we still write treeeq and use it? Yes. treeeq (LLeaf x) (LLeaf y) = x==y treeeq (LBranch t1 t2) (LBranch s1 s2) = treeeq t1 s1 && treeeq t2 s2 treeeq = False The type is Eq a => LTree a -> LTree a -> Bool You can then use it to define == for trees: instance Eq a => Eq (LTree a) where (==) = treeeq FP Lecture 6 8

Tree Equality: Solution 3 Probably 99% of your data types will have == defined in a similar fashion as the above. Haskell can automatically generate such naïve definitions: data LTree a = LLeaf a LBranch (LTree a) (LTree a) deriving Eq Then you immediately have == and /= defined for you the way above. Another class, Show, is for types that can be printed. You can derive it and get the naïve printing too: dataltree a = LLeaf a LBranch (LTree a) (LTree a) deriving (Eq, Show) FP Lecture 6 9

Binary Search Tree We can now define polymorphic but restricted data types. E.g., let us define binary search trees. 5 3 8 1 4 7 The key in a node is greater than all keys in the left subtree, and less than all keys in the right subtree. For simplicity, we disallow duplicate keys. FP Lecture 6 10

BST as Constrained Polymorphic Type We wish to allow any type of keys, but the type must come with the < and the > operators. These come from the Ord class (for ordered ): class Eq a => Ord a where (<), (<=), (>=), (>) :: a->a->bool... It declares the comparison operators. It also requires the type to belong to Eq first. Now we can define our polymorphic binary search tree: data Ord a => BST a = Nil Node a (BST a) (BST a) We require the key type to come from the Ord class. For simplicity, keys go into internal nodes, and null pointers are modelled by Nil. FP Lecture 6 11

BST Operations The membership operation: does a key occur in the tree? member :: Ord a => a -> BST a -> Bool member key Nil = False member key (Node x t s) key<x = member key t key>x = member key s key==x = True The insert operation: add a key to a tree, returning the new tree. insert :: Ord a => a -> BST a -> BST a insert k Nil = Node k Nil Nil insert k n@(node x t s) k<x = Node x (insert k t) s k>x = Node x t (insert k s) otherwise = n FP Lecture 6 12