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

Similar documents
CSCE 314 Programming Languages

List Functions, and Higher-Order Functions

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

Introduction to Programming Using Java (98-388)

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages

Programming with Math and Logic

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

Applicative, traversable, foldable

Propositional Calculus: Boolean Functions and Expressions. CS 270: Mathematical Foundations of Computer Science Jeremy Johnson

Inductive Data Types

CS 457/557: Functional Languages

The Decaf Language. 1 Lexical considerations

CS 320: Concepts of Programming Languages

Applicative, traversable, foldable

Type-indexed functions in Generic Haskell

Introduction to Functional Programming in Haskell 1 / 56

Outline. What is semantics? Denotational semantics. Semantics of naming. What is semantics? 2 / 21

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

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

Goals: Define the syntax of a simple imperative language Define a semantics using natural deduction 1

CS 381: Programming Language Fundamentals

Working with recursion. From definition to template. Readings: HtDP, sections 11, 12, 13 (Intermezzo 2).

Working with recursion

Reasoning About Imperative Programs. COS 441 Slides 10

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

CS 320: Concepts of Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Scheme: Expressions & Procedures

Intro to Haskell Notes: Part 5

The Decaf language 1

6.001 Notes: Section 6.1

College Functors, Applicatives

CS115 - Module 10 - General Trees

COMS 1003 Fall Introduction to Computer Programming in C. Bits, Boolean Logic & Discrete Math. September 13 th

CMSC 330: Organization of Programming Languages. Operational Semantics

Programming Lecture 3

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Syntax and Grammars 1 / 21

Operators. Java operators are classified into three categories:

Intermediate Code Generation

Recap: Functions as first-class values

Lecture 19: Functions, Types and Data Structures in Haskell

The PCAT Programming Language Reference Manual

CS558 Programming Languages

Programming Languages Third Edition

CS 231 Data Structures and Algorithms, Fall 2016

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Algebraic Types. Chapter 14 of Thompson

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CSCI-GA Scripting Languages

CS 360: Programming Languages Lecture 12: More Haskell

Types, Expressions, and States

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

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

Haskell 98 in short! CPSC 449 Principles of Programming Languages

Programming Language Concepts, CS2104 Lecture 7

CSC324 Principles of Programming Languages

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

The role of semantic analysis in a compiler

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

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

Functional abstraction

Graphical Untyped Lambda Calculus Interactive Interpreter

A general introduction to Functional Programming using Haskell

Exercise 1 (2+2+2 points)

CIS 194: Homework 4. Due Wednesday, February 18, What is a Number?

CS52 - Assignment 8. Due Friday 4/15 at 5:00pm.

TUPLES AND RECURSIVE LISTS 5

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

CSCC24 Functional Programming Scheme Part 2

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS558 Programming Languages

Programming Languages

Recap. Recap. If-then-else expressions. If-then-else expressions. If-then-else expressions. If-then-else expressions

Fundamental of Programming (C)

Decaf Language Reference Manual

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

Denotational Semantics. Domain Theory

EECS 700 Functional Programming

Monad Background (3A) Young Won Lim 11/8/17

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

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

Topic 7: Algebraic Data Types

Lexical Considerations

Fundamental Concepts. Chapter 1

Recap: ML s Holy Trinity

CS115 - Module 8 - Binary trees

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

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

Object-Oriented Design (OOD) and C++

6.001 Notes: Section 8.1

JVM ByteCode Interpreter

Chapter 2 Basic Elements of C++

Semantic actions for declarations and expressions

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

Transcription:

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

2

The simplest way to create a new type in Haskell is to give a new name to an existing type. This is useful when we a complex type, often a tuple, is used in many places. For example the type Point is shorter to write and more meaningful than (Float, Float). 3

For example we can define a type of functions called Move that takes a float and a polygon and returns a new polygon. The vertical and horizontal move functions defined above would be of this type, but there could be others; Rotate comes to mind. 4

It is also possible to create parameterized types. Parameterized types are similar to generics in C# or Java. For example the type Predicate a denotes a function from any type to a Bool. The type Lookup k v represents list of key value pairs. 5

Type declarations are simple, one could say simplistic. They do not allow for recursive types, so one cannot define a new type Tree. Sometimes, as in the case of the Lookup type from the previous slide, one would like to use a context, e.g. to state that the key of a Lookup must belong to the Eq class. This too is illegal. Clearly a more powerful mechanism is needed, something that allow us to build more complex types. That is the Data declaration. 6

7

The Data declarations are like the Classes in Java or C#. To further the comparison, the name of the functions that build a value of such a type is called a Constructor. However the similitude cannot be pushed too far. While in Java/C# all constructors for a class have the same name, in Haskell all constructors must have a different name, regardless of the Data type. Remember that values in Haskell are immutable, that means every time we need a new value of the type we need to create it from a constructor. 8

Custom types regardless of the way they are declared (type or data) can be used just like any built-in type. 9

Just like the constructor of a class can have parameters and thus create new objects with different values, constructors in Haskell can have parameters as well and create different values. One way to think about that is as a way to encapsulate values. Since, once constructed, values never change, there is no need for any other representation, beyond the Constructor Param1 Param2 usual function call syntax. Furthermore one can use pattern matching to extract the original argument values. 10

Another way to put this is that Data declaration can be Generic (in the Java/C# sense). The type Maybe (also know as the Maybe Monad, but we ll see what that mean later in the course) is useful to formally encapsulate invalid data. C#/Java have the notion of null to handle this situation, but null is amorphic and this is a more formal and safer way to handle it. 11

12

(===) :: Nat -> Nat -> Bool (===) Zero Zero = True (===) _ Zero = False (===) Zero _ = False (===) (Succ x) (Succ y) = x === y (+++) :: Nat -> Nat -> Nat (+++) Zero x = x (+++) x Zero = x (+++) (Succ x) (y) = x +++ Succ y one, two, three, four :: Nat one = Succ Zero two = Succ one three = Succ two four = Succ three 13

14

15

16

17

18

19

20

We can define recursive functions on expression trees. The size function yields the number of nodes in the tree, and the eval function yields the result of traversing the tree and evaluating the result of the expression then the usual add and multiply semantics is applied to the Add and Mul nodes. 21

But Haskell is a functional programming language and it allows us to encapsulate the tree traversal into it s own function, which we call the fold function. We need to tell the fold function what to do when it finds a leaf and when it finds an inner node. For that we define two types: Unary represents a function of a single argument of type a that yields a result of type b. Binary represents a function that takes two arguments of type b and yield a result of type b. Armed with these two types, we can define the fold function for an Expr tree whose leaves are of type a. fold takes 4 arguments: - A tree whose leaves are of type a - A function that tells fold what to do for each leave (function from a to b) - A function that tells fold what to do for a node of type Add - A function that tells fold what to do for a node of type Mul Fold is surprisingly easy to write, it recursively traverses the tree and applies the appropriate functions. 22

Using fold size and eval are now one liners. Size counts every leave as 1 and yields the sum of the sizes of the left and right subtrees for any other node. Eval counts every leave as its integer value and applies addition or multiplication as expected for inner nodes. We can also easily define format, which yields a string human readable representation of the tree in the usual fully parenthesized arithmetic notation. 23

Remember that we have defined the Expr data type as deriving from the Eq class. This automatically gives an implementation for the == and the Show functions, thus allowing us to compare and display expression trees. But if we want to compare expr1 to expr2, the result is not what we would expect: they are not equal because they do not have the same shape (i.e. structure), even though they evaluate to the same value. 24

To work around this issue we need to redefine Expr as an instance of the Eq class and override the == function with a specialized implementation that compares the result of the evaluation of the two expression trees. Now expr1 is deemed equal to expr2. 25