Koen Lindström Claessen

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
String is a synonym for the type [Char].
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Recursive Data Types Koen Lindström Claessen. Modelling Arithmetic Expressions Imagine a program to help school-children learn arithmetic, which presents.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
Datastructures Koen Lindström Claessen (guest lecture by Björn Bringert)
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.
Stacks (Revised and expanded from CIT 591). What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on.
Stacks. 2 What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
Stacks. What is a stack? A stack is a Last In, First Out (LIFO) data structure Anything added to the stack goes on the “top” of the stack Anything removed.
CS510AP Quick Check Monads. QuickCheck Quick check is a Haskell library for doing random testing. You can read more about quickcheck at –
0 PROGRAMMING IN HASKELL Chapter 11 - Interactive Programs, Declaring Types and Classes.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
COMP Parsing 2 of 4 Lecture 22. How do we write programs to do this? The process of getting from the input string to the parse tree consists of.
Advanced Functional Programming 2009 Ulf Norell (lecture by Jean-Philippe Bernardy)
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Chapter 9: Functional Programming in a Typed Language.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Types and Classes Dr. Hyunyoung Lee.
COMP Parsing 3 of 4 Lectures 23. Using the Scanner Break input into tokens Use Scanner with delimiter: public void parse(String input ) { Scanner.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Math, Data Types. Python Math Operations OperationOperator Addition + Subtraction – Multiplication * Division (floating point) / Division (integer) //
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 Proving Properties of Recursive Functions and Data Structures CS 270 Math Foundations of CS Jeremy Johnson.
Advanced Functional Programming 2010
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Parsing 2 of 4: Scanner and Parsing
String is a synonym for the type [Char].
Laziness and Infinite Datastructures
Conditional Expressions
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
COMP261 Lecture 18 Parsing 3 of 4.
Chapter 5: Control Structures II (Repetition)
Koen Lindström Claessen
A bit of C programming Lecture 3 Uli Raich.
Tagged Data Tag: a symbol in a data structure that identifies its type
Theory of Computation Lecture 4: Programs and Computable Functions II
Agenda Warmup AP Exam Review: Litvin A2
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Test Data Generators.
Koen Lindström Claessen
CSE 3302 Programming Languages
Proving Properties of Recursive Functions and Data Structures
PROGRAMMING IN HASKELL
Higher-Order Procedures
Mini Language Interpreter Programming Languages (CS 550)
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The Metacircular Evaluator
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
Type & Typeclass Syntax in function
PROGRAMMING IN HASKELL
Types and Classes in Haskell
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Lecture #9 מבוא מורחב.
Stacks.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
6.001 SICP Interpretation Parts of an interpreter
CS 101 First Exam Review.
Theory of Computation Lecture 4: Programs and Computable Functions II
Lecture 5 Scanning.
PROGRAMMING IN HASKELL
More Scheme CS 331.
Presentation transcript:

Koen Lindström Claessen Recursive Data Types Koen Lindström Claessen

Modelling Arithmetic Expressions Imagine a program to help school-children learn arithmetic, which presents them with an expression to work out, and checks their answer. What is (1+2)*3? 8 Sorry, wrong answer!

Modelling Arithmetic Expressions The expression (1+2)*3 is data as far as this program is concerned (not the same as 9!). How shall we represent it? A string?? What is ”1+2”++”3”? What is ”1+hello world**”?

Modelling Expressions Let’s design a datatype to model arithmetic expressions -- not their values, but their structure. What information should we store in each alternative? An expression can be: a number n an addition a+b a multiplication a*b data Expr = Num | Add | Mul

Modelling Expressions Let’s design a datatype to model arithmetic expressions -- not their values, but their structure. An expression can be: a number n an addition a+b a multiplication a*b data Expr = Num Integer | Add Expr Expr | Mul Expr Expr A recursive data type !!

Examples data Expr = Num Integer | Add Expr Expr | Mul Expr Expr The expression: is represented by: 2 Num 2 2+2 Add (Num 2) (Num 2) (1+2)*3 Mul (Add (Num 1) (Num 2)) (Num 3) 1+2*3 Add (Num 1) (Mul (Num 2) (Num 3))

A Difference There is a difference between 17 :: Integer Num 17 :: Expr Why are these different? Can do different things with them Some things only work for one of them So, their types should be different

eval :: Expr -> Integer Quiz Can you define a function eval :: Expr -> Integer which evaluates an expression? Example: eval (Add (Num 1) (Mul (Num 2) (Num 3))) 7 Hint: Recursive types often mean recursive functions!

Quiz Can you define a function eval :: Expr -> Integer which evaluates an expression? eval (Num n) = eval (Add a b) = eval (Mul a b) = Use pattern matching: one equation for each case. What can we put here? A and b are of type Expr.

eval :: Expr -> Integer Quiz Can you define a function eval :: Expr -> Integer which evaluates an expression? eval (Num n) = n eval (Add a b) = eval a + eval b eval (Mul a b) = eval a * eval b Recursive types mean recursive functions!

Showing Expressions Expressions will be more readable if we convert them to strings. showExpr (Mul (Num 1) (Add (Num 2) (Num 3))) ”1*2+3” showExpr :: Expr -> String showExpr (Num n) = show n showExpr (Add a b) = showExpr a ++ ”+” ++ showExpr b showExpr (Mul a b) = showExpr a ++ ”*” ++ showExpr b

Quiz Which brackets are necessary? 1+(2+3) 1+(2*3) 1*(2+3) What kind of expression may need to be bracketed? When does it need to be bracketed?

Inside multiplications. Quiz NO! Which brackets are necessary? 1+(2+3) 1+(2*3) 1*(2+3) What kind of expression may need to be bracketed? When does it need to be bracketed? NO! YES! Additions Inside multiplications.

Idea Format factors differently: showExpr :: Expr -> String showExpr (Num n) = show n showExpr (Add a b) = showExpr a ++ "+" ++ showExpr b showExpr (Mul a b) = showFactor a ++ "*" ++ showFactor b showFactor :: Expr -> String showFactor (Add a b) = "("++showExpr (Add a b)++")" showFactor e = showExpr e showFactor :: Expr -> String ?

Making a Show instance instance Show Expr where show = showExpr data Expr = Num Integer | Add Expr Expr | Mul Expr Expr deriving ( Show, Eq )

(Almost) Complete Program New random seed An expression generator—needs to be written What’s this? questions :: IO ( ) questions = do rnd <- newStdGen let e = unGen arbitrary rnd 10 putStr ("What is "++show e++"? ") ans <- getLine putStrLn (if read ans == eval e then "Right!" else "Wrong!") questions let: Give name to a result Opposite of show

Using QuickCheck Generators in Other Programs Test.QuickCheck exports unGen :: Gen a -> StdGen -> Int -> a Size is used, for example, to bound Integers Random seed QuickCheck generator Size parameter for generation

Generating Arbitrary Expressions instance Arbitrary Expr where arbitrary = arbExpr arbExpr :: Gen Expr arbExpr = oneof [ do n <- arbitrary return (Num n) , do a <- arbExpr b <- arbExpr return (Add a b) , do a <- arbExpr b <- arbExpr return (Mul a b) ] Does not work! (why?) Generates infinite expressions!

Generating Arbitrary Expressions instance Arbitrary Expr where arbitrary = sized arbExpr arbExpr :: Int -> Gen Expr arbExpr s = frequency [ (1, do n <- arbitrary return (Num n)) , (s, do a <- arbExpr s’ b <- arbExpr s’ return (Add a b)) , (s, do a <- arbExpr s’ b <- arbExpr s’ return (Mul a b)) ] where s’ = s `div` 2 Size argument changes at each recursive call

Demo Main> questions What is -3*4*-1*-3*-1*-1? -36 Right! Wrong! What is 0? 0 What is (-4+13)*-9*13+7+15+12? dunno Program error: Prelude.read: no parse

The Program putStrLn (if read ans==eval e then "Right!" else "Wrong!") failing putStrLn (if read ans==eval e then "Right!" else "Wrong!") cannot fail putStrLn (if ans==show (eval e) then "Right!" else "Wrong!")

We see how to implement this in the next lecture Reading Expressions How about a function readExpr :: String -> Expr Such that readExpr “12+173” = Add (Num 12) (Num 173) readExpr “12+3*4” = Add (Num 12) (Mul (Num 3) (Num 4)) We see how to implement this in the next lecture

Add Var and change functions accordingly Symbolic Expressions How about expressions with variables in them? data Expr = Num Integer | Add Expr Expr | Mul Expr Expr | Var Name type Name = String Add Var and change functions accordingly

Gathering Variables It is often handy to know exactly which variables occur in a given expression vars :: Expr -> [Name] vars = ?

From Data.List; combines two lists without duplication Gathering Variables It is often handy to know exactly which variables occur in a given expression vars :: Expr -> [Name] vars (Num n) = [] vars (Add a b) = vars a `union` vars b vars (Mul a b) = vars a `union` vars b vars (Var x) = [x] From Data.List; combines two lists without duplication

Evaluating Expressions Table of values for variables We would like to evaluate expressions with variables. What is the type? eval :: Expr -> ? eval :: [(Name,Integer)] -> Expr -> Integer eval env (Num n) = n eval env (Var y) = fromJust (lookup y env) eval env (Add a b) = eval env a + eval env b eval env (Mul a b) = eval env a * eval env b eval :: [(Name,Integer)] -> Expr -> Int

Symbolic Differentiation Variable to differentiate wrt. Differentiating an expression produces a new expression. We implement it as: diff :: Expr -> Name -> Expr diff (Num n) x = Num 0 diff (Var y) x | x==y = Num 1 | x/=y = Num 0 diff (Add a b) x = Add (diff a x) (diff b x) diff (Mul a b) x = Add (Mul a (diff b x)) (Mul b (diff a x))

Testing differentiate Main> diff (Mul (Num 2) (Var “x”)) “x” 2*1+0*x Not quite what we expected! -- not simplified

What happens? How can we make differentiate simplify the result? d (2*x) = 2 dx differentiate (Mul (Num 2) (Var ”x”)) ”x” Add (Mul (Num 2) (differentiate (Var ”x”) ”x”)) (Mul (Var ”x”) (differentiate (Num 2) ”x”)) Add (Mul (Num 2) (Num 1)) (Mul (Var ”x”) (Num 0)) 2*1 + x*0 How can we make differentiate simplify the result?

more simplification is possible… “Smart” Constructors Define add :: Expr -> Expr -> Expr add (Num 0) b = b add a (Num 0) = a add (Num x) (Num y) = Num (x+y) add a b = Add a b more simplification is possible… By using add instead of Add, certain simplifications are performed when constructing the expression!

Testing add Main> Add (Num 2) (Num 5) 2+5 7

Symbolic Differentiation Differentiating an expression produces a new expression. We implement it as: diff :: Expr -> Name -> Expr diff (Num n) x = Num 0 diff (Var y) x | x==y = Num 1 | x/=y = Num 0 diff (Add a b) x = add (diff a x) (diff b x) diff (Mul a b) x = add (mul a (diff b x)) (mul b (diff a x)) note note note

“Smart” Constructors -- mul How to define mul? mul :: Expr -> Expr -> Expr mul (Num 0) b = Num 0 mul a (Num 0) = Num 0 mul (Num 1) b = b mul a (Num 1) = a mul (Num x) (Num y) = Num (x*y) mul a b = Mul a b

Expressions Expr as a datatype can represent expressions Unsimplified Results Data presented to the user Need to be able to convert between these

An Expression Simplifier Simplification function simplify :: Expr -> Expr simplify :: Expr -> Expr simplify e | null (vars e) = ? … You continue at the group exercises!

Testing the Simplifier arbExpr :: Int -> Gen Expr arbExpr s = frequency [ (1, do n <- arbitrary return (Num n)) , (s, do a <- arbExpr s’ b <- arbExpr s’ return (Add a b)) , (s, do a <- arbExpr s’ b <- arbExpr s’ return (Mul a b)) , (1, do x <- elements [”x”,”y”,”z”] return (Var x))] where s’ = s `div` 2

Testing an Expression Simplifier (1) Simplification should not change the value prop_SimplifyCorrect e env = eval env e == eval env (simplify e) Generate lists of values for variables prop_SimplifyCorrect e (Env env) = eval env e == eval env (simplify e)

Testing an Expression Simplifier data Env = Env [(Name,Integer)] deriving ( Eq, Show ) instance Arbitrary Env where arbitrary = do a <- arbitrary b <- arbitrary c <- arbitrary return (Env [(”x”,a),(”y”,b),(”z”,c)])

Testing an Expression Simplifier (2) Simplification should do a good job prop_SimplifyNoJunk e = noJunk (simplify e) where noJunk (Add a b) = not (isNum a && isNum b) && noJunk a && noJunk b ... You continue at the group exercises!

Forthcoming Group Exercise Build and test an expression simplifier! I found many subtle bugs in my own simplifier! Often simplifier goes into an infinite loop

Summary Recursive data-types can take many forms other than lists Recursive data-types can model languages (expressions, natural languages, programming languages) Functions working with recursive types are often recursive themselves When generating random elements in recursive datatypes, think about the size

Next Time How to write parsers readExpr :: String -> Expr Case study: example of other recursive datatype a simple game: ”the zoo” guessing animals using yes/no questions