Zipping Search Trees. Tom Schrijvers. with Denoit Desouter and Bart Demoen

Size: px
Start display at page:

Download "Zipping Search Trees. Tom Schrijvers. with Denoit Desouter and Bart Demoen"

Transcription

1 Zipping Search Trees Tom Schrijvers with Denoit Desouter and Bart Demoen

2 Motivation 2

3 Earlier Work 3

4 Earlier Work Monadic Constraint Programming T. S., P. Stuckey, P. Wadler. (JFP 09) Haskell 3

5 Earlier Work Monadic Constraint Programming T. S., P. Stuckey, P. Wadler. (JFP 09) Haskell C++ Search Combinators T. S., G. Tack, P. Wuille, H. Samulowitz, P. Stuckey. (CONSTRAINTS 13) 3

6 Earlier Work Monadic Constraint Programming T. S., P. Stuckey, P. Wadler. (JFP 09) Haskell C++ Search Combinators T. S., G. Tack, P. Wuille, H. Samulowitz, P. Stuckey. (CONSTRAINTS 13) Tor: extensible search with hookable disjunction T.S., M. Triska, B. Demoen. (SCP 13) Prolog 3

7 On implementing Prolog in Functional Programming M. Carlsson. (NGC 84) Prological Features in a Functional Setting R. Hinze. (FLOPS 98) Algebra of Logic Programming S. Seres, M. Spivey, T. Hoare. (ICLP 99) Typed Logic Variables in Haskell K. Claessen, P. Ljunglöf. (Haskell 00) Many FP models of tree search Escape from Zurg: An Exercise in Logic Programming M. Erwig. (JFP 04) Deriving Backtracking Monad Transformers R. Hinze. (ICFP 00) Backtracking, Interleaving and Terminating Monad Transformers O. Kyselyov, C. Shan, D. Friedman, A. Sabry. (ICFP 05) Algebras for Combinatorial Search M. Spivey. (JFP 09)... 4

8 Depth-First Search Model problem :: [A] Lazy lists naturally express DFS 5

9 Flexible Search Strategies problem :: MonadPlus m => m A Different strategies by instantiating m differently 6

10 Unrealistic Model Most systems have a hard-wired strategy (usually DFS) Highly optimized for fixed strategy Prolog / WAM SAT solvers LP solvers CP solvers... 7

11 Best of Both? 8

12 Best of Both? works for actual systems 8

13 Best of Both? works for actual systems easy to change strategy 8

14 Best of Both? works for actual systems easy to change strategy FP-based approach 8

15 Example Enumeration label :: MonadPlus m => [[Int]] -> m [Int] label [] = return [] label ([]:ls) = mzero label ((v:vs):ls) = liftm (v:) (label ls) `mplus` label (vs:ls) 9

16 label [[1..3],[1..3]] X #= 1 X #\= 1 Y #= 1 Y #/= 1 X #=2 X #= 3 Y #= 2 Y #= 3 Y #= 1 Y #/= 1 Y #= 1 Y #/= 1 Y #= 2 Y #= 3 Y #= 2 Y #= 3 10

17 With Depth Limit X #= 1 X #\= 1 Y #= 1 Y #/= 1 X #=2 X #= 3 Y #= 2 Y #= 3 Y #= 1 Y #/= 1 Y #= 1 Y #/= 1 Y #= 2 Y #= 3 Y #= 2 Y #= 3 11

18 Example Enumeration label :: MonadPlus m => Int -> [[Int]] -> m [Int] label _ [] = return [] label _ ([]:ls) = mzero label d ((v:vs):ls) = guard (d > 0) >> ( liftm (v:) (label (d-1) ls) `mplus` label (d-1) (vs:ls)) 12

19 Range of Expressible Heuristics Limits Depth Limit Discrepancy Limit Node Limit Backtracking Limit Credit Limit Restarts Optimization Iterative Deepening Branch and Bound Limited Discrepancy Search 13

20 Progress! works for actual systems easy to change strategy 14

21 O(n m) code problems heuristics 15

22 Not the Way to Go copy-paste-modify anti-pattern wasting programmer time error-prone non-optimal code quality reduced exploration of heuristics simple heuristic + complex search = very complex implementation 16

23 What We Want problems heuristics O(n+m) code 17

24 Zipping Approach = search heuristic search problem heuristic search zip composition operator 5518

25 Depth-Bounded Search Heuristic 19

26 Depth-Bounded Search Heuristic dbs d = do guard (d > 0) dbs (d-1) `mplus` dbs (d-1) 20

27 dbs 4 problem 21

28 zip (dbs 4) problem 21

29 Problem Solved! works for actual systems easy to change strategy 22

30 Remaining Challenges 1.How to specify zip? 2.How to implement it? 23

31 Derived Heuristics = Depth Limit Discrepancy Limit Both Limits zip (zip (dbs depth) (discbs disc)) problem 24

32 Derived Heuristics = Delay Node Limit Node Limit at Depth zip (and (delay depth) (nbs nodes)) problem 25

33 FP Model 26

34 Search Trees data Tree = Success Failure Or Tree Tree refied essence of tree search (encapsulated search in FLP ) Success `Or` (Failure `Or` Success) 27

35 Depth-First Search Semantics :: Tree - > [()] Failure = [] Success = [()] Or t 1 t 2 = t 1 ++ t 2 28

36 Overloaded Semantics class Search d where :: Tree - > d 29

37 Compositional Semantics class CSearch d where fail :: d succeed :: d Signature or :: d - > d - > d 30

38 Compositional Semantics instance CSearch d => Search d where Failure = fail Success = succeed Or g 1 g 2 = g 1 `or` g 2 31

39 DFS Algebra instance CSearch [()] where fail = [] succeed = [()] or = (++) 32

40 Counting Algebra instance CSearch Int where fail = 0 succeed = 1 or = (+) 33

41 Boolean Algebra instance CSearch Bool where fail = False succeed = True or = ( ) 34

42 Initial Algebra instance CSearch Tree where fail = Failure succeed = Success or = Or 35

43 No Monoid Laws fail `or` d fail d `or` fail fail d1 `or`(d2 `or` d3) (d1 `or`d2) `or` d3 36

44 No Monoid Laws fail `or` d fail d `or` fail fail d1 `or`(d2 `or` d3) (d1 `or`d2) `or` d3 36

45 Non-Monoidal Algebra newtype BFS a = L {unl :: [[a]]} instance CSearch (BFS ()) where fail = L [] succeed = L [[()]] or d1 d2 = demote (merge d1 d2) Algebras for Combinatorial Search M. Spivey. (JFP 09) 37

46 Non-Monoidal Algebra fail L [] fail `or` fail L [[]] 38

47 Conjunction 39

48 Conjunction Syntax data Tree = Success Failure Or Tree Tree And Tree Tree 40

49 Overloaded Semantics class Search d => Search d where :: Tree - > d 41

50 Syntactic Instance instance Search Tree where Success = Success Failure = Failure Or t1 t2 = Or t1 t2 And t1 t2 = go t1 where go Success go Failure = t2 = Failure go (Or t3 t4) = Or (go t3) (go t4) 42

51 Semantics Specification. Tree -> d (Tree -> d). (Tree -> Tree) (useful as default implementation) 43

52 Compositional Semantics class CSearch d => CSearch d where and :: d - > d - > d 44

53 Stronger Specification and succeed d d and d succeed d and d1 (and d2 d3) and (and d1 d2) d3 and fail d fail and (or d1 d2) d3 or (and d1 d3) (and d2 d3) 45

54 DFS Algebra instance Search [()] where p 1 `and` p 2 = [() x <- p 1, y <- p 2 ] 46

55 Counting Instance instance Search Int where and = (*) 47

56 Monadic Search 48

57 Side Effects During Search State e.g., collecting constraints, unification, learning,... I/O 49

58 50

59 onads to the rescue! 50

60 Monadic Interface class Monad m => MSearch m where mfail mor :: m a :: m a - > m a - > m a 51

61 Monadic Interface class Monad m => MSearch m where mfail mor :: m a :: m a - > m a - > m a instance MSearch m => Search (m ()) where fail or = mfail = mor succeed = return () 51

62 Monadic Interface class Monad m => MSearch m where mfail mor :: m a :: m a - > m a - > m a instance MSearch m => Search (m ()) where fail or = mfail = mor succeed = return () instance MSearch m => Search (m ()) where and = (>>) 51

63 Instances instance MSearch [] where mfail = [] mor = (++) 52

64 Instances instance MSearch [] where mfail = [] mor = (++) instance MSearch (StateT Subst []) 52

65 Instances instance MSearch [] where mfail = [] mor = (++) instance MSearch (StateT Subst []) instance MSearch (WriterT String []) 52

66 Only 2 Laws (+ monad laws) and succeed d d and d succeed d and d1 (and d2 d3) and (and d1 d2) d3 and fail d fail and (or d1 d2) d3 or (and d1 d3) (and d2 d3) 53

67 Only 2 Laws (+ monad laws) and succeed d d and d succeed d and d1 (and d2 d3) and (and d1 d2) d3 mfail >>= f mfail (mor d1 d2) >>= f mor (d1 >>= f) (d2 >>= f) 54

68 Zipping 55

69 Zipping Lists zip :: [a] -> [b] -> [(a,b)] zip [] l = [] zip l [] = [] zip (x:xs) (y:ys) = (x,y) : zip xs ys 56

70 Zipping Naturals data Nat = Z S Nat min :: Nat -> Nat -> Nat min Z n = Z min n Z = Z min (S x) (S y) = S (min x y) 57

71 Zipping Plain Trees data PTree = L F PTree PTree zip :: PTree -> PTree -> PTree zip L n = L zip n L = L zip (F l1 r1) (F l2 r2) = F (zip l1 l2) (zip r1 r2) 58

72 Zipping Search Trees Failure n = Failure Success n = Success (Or l r) Failure (Or l r) Success = Failure = Success (Or l1 r1) (Or l2 r2) = Or (l1 l2) (r1 r2) 59

73 Monoid Tree,,ε ε t t ε = Or ε ε t ε t l1 (l2 l3) (l1 l2) l3 60

74 Zip Syntax data Tree = Success Failure Or Tree Tree And Tree Tree Zip Tree Tree 61

75 Overloaded Semantics class Search d => Search d where :: Tree - > d 62

76 Syntactic Instance instance Search Tree where Success = Success Failure = Failure Or t1 t2 = Or t1 t2 And t1 t2 = and t1 t2 Zip t1 t2 = t1 t2 63

77 Semantics Specification. Tree -> d (Tree -> d). (Tree -> Tree) (useful as default implementation) 64

78 Compositional Semantics class CSearch d => CSearch d where zip :: d - > d - > d 65

79 Stronger Specification zip fail d fail zip succeed d succeed zip (or d1 d2) fail fail zip (or d1 d2) succeed succeed zip (or d1 d2) (or d3 d4) or (zip d1 d3) (zip d2 d4) zip d1 (zip d2 d3) zip (zip d1 d2) d3 66

80 Efficient Instances? 67

81 Not for non-trivial Monoidal Algebras fail (ZIPFAIL law) fail `zip` succeed (OR/FAIL monoid law) (fail `or` succeed) `zip` succeed (ZIPORSUCCEED law) succeed 68

82 69

83 Free instance with free-monad transformer newtype ForkT m a = F { unf :: m (Result m a) } data Result m a = Ret a Fork (ForkT m a) (ForkT m a) 70

84 Free instance with free-monad transformer instance Monad m => Monad (ForkT m) where return = F. return. Ret m >>= f = F (unf m >>= go) where go (Ret x) = unf (f x) go (Fork l r) = return (Fork (l >>= f) (r >>= f)) 71

85 Free instance with free-monad transformer instance MSearch m => MSearch (ForkT m) where mfail = F mfail mor l r = F $ return (Fork l r) suspend disjunctions 72

86 Run runf :: MSearch m => ForkT m a - > m a runf m = unf m >>= go where go (Ret x) = return x go (Fork l r) = mor (go l) (go r) resume disjunctions 73

87 Lemma comp = runf comp for comp :: MSearch m => m A 74

88 Zip Implementation instance MSearch m => CSearch (ForkT m ()) where synchronize disjunctions zip l r = F $ unf l >>= gol where gol (Ret ()) = unf succeed gol (Fork l1 r1) = unf r >>= gor gor (Ret ()) = unf succeed gor (Fork l2 r2) = unf $ or (zip l1 l2) (zip r1 r2) 75

89 Theorm comp = runf comp for comp :: CSearch d => d 76

90 Unrealistic Solution Hard to add ForkT to existing systems - cross-cutting - overrides definition of (>>=) Prolog / WAM SAT solvers LP solvers CP solvers... 77

91 Implementation Alternative Yield : Mainstream Delimited Continuations R. James, A. Sabry. (TPDC 11) 78

92 Free instance with free-monad transformer instance MSearch m => MSearch (ForkT m) where mfail = lift mfail mor l r = shift (\k - > Fork (l >>= k) (r >>= k)) suspend disjunctions 79

93 Run runf :: MSearch m => ForkT m a - > m a runf m = unf (reset m >>= go) where go (Ret x) = return x go (Fork l r) = lift (mor (runf l) (runf r)) resume disjunctions 80

94 Delimited Continuations instance MSearch m => CSearch (ForkT m ()) synchronize disjunctions where zip l r = reset l >>= gol where gol (Ret ()) = succeed gol (Fork l1 r1) = reset r >>= gor gor (Ret ()) = succeed gor (Fork l2 r2) = or (zip l1 l2) (zip r1 r2) 81

95 Port to Prolog 82

96 Delimited Continuations for Prolog Not a standard Prolog feature 83

97 Delimited Continuations for Prolog Not a standard Prolog feature Easy to implement in CPS-based BinProlog Logic Programming and Logic Grammars with First-order Continuations. P. Tarau, V. Dahl. (LOPSTR 94) 83

98 Delimited Continuations for Prolog Not a standard Prolog feature Easy to implement in CPS-based BinProlog Logic Programming and Logic Grammars with First-order Continuations. P. Tarau, V. Dahl. (LOPSTR 94) New WAM extension Delimited continuations for Prolog. T.S., B. Demoen, B. Desouter, J. Wielemaker. (submitted to ICLP 13) 83

99 Wrapping Up 84

100 Summary zipping search trees to get modular search heuristics compatible with a fixed search strategy syntactic specification implemented with delimited continuations port to Prolog 85

101 Future Work variations on zip swapping branches other functors 86

102 Thank You! 87

Background Type Classes (1B) Young Won Lim 6/28/18

Background Type Classes (1B) Young Won Lim 6/28/18 Background Type Classes (1B) Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2

More information

Background Type Classes (1B) Young Won Lim 6/14/18

Background Type Classes (1B) Young Won Lim 6/14/18 Background Type Classes (1B) Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2

More information

Monads and all that III Applicative Functors. John Hughes Chalmers University/Quviq AB

Monads and all that III Applicative Functors. John Hughes Chalmers University/Quviq AB Monads and all that III Applicative Functors John Hughes Chalmers University/Quviq AB Recall our expression parser expr = do a

More information

A Monadic Semantics for Core Curry

A Monadic Semantics for Core Curry 12th Int. Workshop on Functional and (Constraint) Logic Programming (WFLP03), Valencia, Spain June 12-13, 2003. Technical Report DSIC-II/13/03, Departmento de Sistemàs y Computatciòn, Universidad Politécnica

More information

Reinventing Haskell Backtracking

Reinventing Haskell Backtracking Reinventing Haskell Backtracking Sebastian Fischer Christian-Albrechts University of Kiel, Germany sebf@informatik.uni-kiel.de Abstract: Almost ten years ago, Ralf Hinze has written a functional pearl

More information

Applicative, traversable, foldable

Applicative, traversable, foldable Applicative, traversable, foldable Advanced functional programming - Lecture 4 Wouter Swierstra and Alejandro Serrano 1 Beyond the monad So far, we have seen how monads define a common abstraction over

More information

Typed Logical Variables in Haskell

Typed Logical Variables in Haskell Electronic Notes in Theoretical Computer Science 41 No. 1 (2001) URL: http://www.elsevier.nl/locate/entcs/volume41.html 14 pages Typed Logical Variables in Haskell Koen Claessen 1 Department of Computer

More information

Polymorphism Overview (1A) Young Won Lim 2/20/18

Polymorphism Overview (1A) Young Won Lim 2/20/18 Polymorphism Overview (1A) Copyright (c) 2016-2017 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2

More information

Functional Pearl: A Smart View on Datatypes

Functional Pearl: A Smart View on Datatypes Functional Pearl: A Smart View on Datatypes Mauro Jaskelioff Exequiel Rivas CIFASIS-CONICET, Argentina Universidad Nacional de Rosario, Argentina jaskelioff@cifasis-conicet.gov.ar rivas@cifasis-conicet.gov.ar

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Final Review Part I Dr. Hyunyoung Lee 1 Programming Language Characteristics Different approaches to describe computations, to instruct computing devices E.g., Imperative,

More information

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group

Advanced Type System Features Tom Schrijvers. Leuven Haskell User Group Advanced Type System Features Tom Schrijvers Leuven Haskell User Group Data Recursion Genericity Schemes Expression Problem Monads GADTs DSLs Type Type Families Classes Lists and Effect Free Other Handlers

More information

Multi-paradigm Declarative Languages

Multi-paradigm Declarative Languages Michael Hanus (CAU Kiel) Multi-paradigm Declarative Languages ICLP 2007 1 Multi-paradigm Declarative Languages Michael Hanus Christian-Albrechts-University of Kiel Programming Languages and Compiler Construction

More information

CIS552: Advanced Programming

CIS552: Advanced Programming CIS552: Advanced Programming Handout 8 What is a Parser? A parser is a program that analyzes a piece of text to deine its structure (and, typically, returns a tree representing this structure). The World

More information

Overview of the Monadic Constraint Programming Framework

Overview of the Monadic Constraint Programming Framework Overview of the Monadic Constraint Programming Framework Tom Schrijvers Dept. of Computer Science, K.U.Leuven Celestijnenlaan 200A 3001 Heverlee, Belgium tom.schrijvers@cs.kuleuven.be Abstract. A constraint

More information

Type Classes in Haskell Tom Schrijvers. Leuven Haskell User Group

Type Classes in Haskell Tom Schrijvers. Leuven Haskell User Group Type Classes in Haskell Tom Schrijvers Leuven Haskell User Group Haskell Research Team Partners Monads Type Classes GHC Folds Pattern Matching Equational Reasoning DSLs Advanced Types Adhoc Overloading

More information

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

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 18 Thursday, March 29, 2018 In abstract algebra, algebraic structures are defined by a set of elements and operations

More information

Applicative, traversable, foldable

Applicative, traversable, foldable Applicative, traversable, foldable Advanced functional programming - Lecture 3 Wouter Swierstra 1 Beyond the monad So far, we have seen how monads define a common abstraction over many programming patterns.

More information

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping Overview Declarative Languages D7012E Lecture 4: The Haskell type system Fredrik Bengtsson / Johan Nordlander Overloading & polymorphism Type classes instances of type classes derived type classes Type

More information

Functional Programming

Functional Programming Functional Programming Monadic Prelude Jevgeni Kabanov Department of Computer Science University of Tartu Introduction Previously on Functional Programming Monadic laws Monad class (>>= and return) MonadPlus

More information

Monads and all that I. Monads. John Hughes Chalmers University/Quviq AB

Monads and all that I. Monads. John Hughes Chalmers University/Quviq AB Monads and all that I. Monads John Hughes Chalmers University/Quviq AB Binary Trees in Haskell data Tree a = Leaf a Branch (Tree a) (Tree a) deriving (Eq,Show) Cf Coq: Inductive tree (A:Set) : Set := leaf

More information

Declarative Programming with Function Patterns

Declarative Programming with Function Patterns LOPSTR 2005 Declarative Programming with Function Patterns Michael Hanus Christian-Albrechts-Universität Kiel (joint work with Sergio Antoy, Portland State University) FUNCTIONAL LOGIC LANGUAGES Approach

More information

Backtracking, Interleaving, and Terminating Monad Transformers

Backtracking, Interleaving, and Terminating Monad Transformers Backtracking, Interleaving, and Terminating Monad Transformers (Functional Pearl) Oleg Kiselyov FNMOC oleg@pobox.com Chung-chieh Shan Harvard University ccshan@post.harvard.edu Daniel P. Friedman Indiana

More information

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions PROGRAMMING IN HASKELL Chapter 5 - List Comprehensions 0 Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x 2 x {1...5}} The set {1,4,9,16,25}

More information

Functional Logic Programming. Kristjan Vedel

Functional Logic Programming. Kristjan Vedel Functional Logic Programming Kristjan Vedel Imperative vs Declarative Algorithm = Logic + Control Imperative How? Explicit Control Sequences of commands for the computer to execute Declarative What? Implicit

More information

Skeletons and the Anatomy of Monads

Skeletons and the Anatomy of Monads Skeletons and the Anatomy of Monads Chuan-kai Lin Department of Computer Science Portland State University cklin@cs.pdx.edu Abstract Monads are used heavily in Haskell for supporting computational effects,

More information

Programming Languages

Programming Languages Programming Languages Andrea Flexeder Chair for Theoretical Computer Science Prof. Seidl TU München winter term 2010/2011 Lecture 10 Side-Effects Main Points you should get: Why monads? What is a monad?

More information

Functional Logic Design Patterns

Functional Logic Design Patterns FLOPS 2002 Functional Logic Design Patterns Michael Hanus Christian-Albrechts-Universität Kiel joint work with Sergio Antoy Portland State University 1 SOME HISTORY AND MOTIVATION 1993 ([POPL 94, JACM

More information

Syntax Matters: Writing abstract computations in F#

Syntax Matters: Writing abstract computations in F# Syntax Matters: Writing abstract computations in F# Tomas Petricek 1, Don Syme 2 1 University of Cambridge, Cambridge, United Kingdom 2 Microsoft Research, Cambridge, United Kingdom tp322@cam.ac.uk, dsyme@microsoft.com

More information

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

Informatics 1 Functional Programming Lectures 15 and 16. IO and Monads. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lectures 15 and 16 IO and Monads Don Sannella University of Edinburgh Part I The Mind-Body Problem The Mind-Body Problem Part II Commands Print a character putchar

More information

Extending Monads with Pattern Matching

Extending Monads with Pattern Matching Extending Monads with Pattern Matching Tomas Petricek Alan Mycroft University of Cambridge {tomas.petricek, am}@cl.cam.ac.uk Don Syme Microsoft Research Cambridge don.syme@microsoft.com Abstract Sequencing

More information

Contents. A Source Code 32 A.1 ASCII versions of mathematical symbols A.2 Definitions of used library functions... 32

Contents. A Source Code 32 A.1 ASCII versions of mathematical symbols A.2 Definitions of used library functions... 32 Contents 1 Declarative Programming 1 1.1 Functional programming.................... 2 1.1.1 Type polymorphism and higher-order functions.... 3 1.1.2 Lazy evaluation..................... 5 1.1.3 Class-based

More information

Edward Kmett. Iteratees, Parsec, and Monoids A PARSING TRIFECTA

Edward Kmett. Iteratees, Parsec, and Monoids A PARSING TRIFECTA Edward Kmett Iteratees, Parsec, and Monoids A PARSING TRIFECTA Overview The Problem Deriving Iteratees á la Oleg Buffered Iteratees Layering Parsec Over Iteratees Local Context-Sensitivity Monoids From

More information

Example problems. Combinatorial auction. Towers of Hanoi Rectangle packing Shortest route. 8 queens Soduko Maximizing (minimizing) costs

Example problems. Combinatorial auction. Towers of Hanoi Rectangle packing Shortest route. 8 queens Soduko Maximizing (minimizing) costs FunLog Example problems Combinatorial auction sell all the items Towers of Hanoi Rectangle packing Shortest route. 8 queens Soduko Maximizing (minimizing) costs Finding a solution with given property The

More information

Algebra of Logic Programming Silvija Seres Michael Spivey Tony Hoare Oxford University Computing Laboratory Wolfson Building, Parks Road, Oxford OX1 3

Algebra of Logic Programming Silvija Seres Michael Spivey Tony Hoare Oxford University Computing Laboratory Wolfson Building, Parks Road, Oxford OX1 3 Algebra of Logic Programming Silvija Seres Michael Spivey Tony Hoare Oxford University Computing Laboratory Wolfson Building, Parks Road, Oxford OX1 3QD, U.K. Abstract A declarative programming language

More information

Functional Logic Programming Language Curry

Functional Logic Programming Language Curry Functional Logic Programming Language Curry Xiang Yin Department of Computer Science McMaster University November 9, 2010 Outline Functional Logic Programming Language 1 Functional Logic Programming Language

More information

Lambda-termide redutseerimine

Lambda-termide redutseerimine Lambda-termid type Var = String data Term = Var Var App Term Term Lam Var Term Vabade muutujate leidmine freevars :: Term -> [Var] freevars (Var x) = [x] freevars (App e1 e2) = freevars e1 union freevars

More information

INTRODUCTION TO HASKELL

INTRODUCTION TO HASKELL INTRODUCTION TO HASKELL PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2018 Dalhousie University 1/81 HASKELL: A PURELY FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be

More information

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

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18) Advanced Programming Handout 7 Monads and Friends (SOE Chapter 18) The Type of a Type In previous chapters we discussed: Monomorphic types such as Int, Bool, etc. Polymorphic types such as [a], Tree a,

More information

Purely functional lazy nondeterministic programming

Purely functional lazy nondeterministic programming JFP 21 (4 & 5): 413 465, 2011. c Cambridge University Press 2011 doi:10.1017/s0956796811000189 First published online 16 August 2011 413 Purely functional lazy nondeterministic programming SEBASTIAN FISCHER,

More information

All About Monads A comprehensive guide to the theory and practice of monadic programming in Haskell Version 1.1.0

All About Monads A comprehensive guide to the theory and practice of monadic programming in Haskell Version 1.1.0 All About Monads Next: Introduction All About Monads A comprehensive guide to the theory and practice of monadic programming in Haskell Version 1.1.0 This tutorial aims to explain the concept of a monad

More information

Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9)

Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9) Types in Programming Languages Dynamic and Static Typing, Type Inference (CTM 2.8.3, EPL* 4) Abstract Data Types (CTM 3.7) Monads (GIH** 9) Carlos Varela Rensselaer Polytechnic Institute September 23,

More information

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

n   n Try tutorial on front page to get started! n   spring13/ n Stack Overflow! Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

More information

Informatics 1 Functional Programming 19 Tuesday 23 November IO and Monads. Philip Wadler University of Edinburgh

Informatics 1 Functional Programming 19 Tuesday 23 November IO and Monads. Philip Wadler University of Edinburgh Informatics 1 Functional Programming 19 Tuesday 23 November 2010 IO and Monads Philip Wadler University of Edinburgh The 2010 Informatics 1 Competition Sponsored by Galois (galois.com) List everyone who

More information

CS 457/557: Functional Languages

CS 457/557: Functional Languages CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University 1 Why Lists? Lists are a heavily used data structure in many functional programs Special syntax is

More information

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

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics National Institute of Informatics May 31, June 7, June 14, 2010 All Right Reserved. Outline I 1 Parser Type 2 Monad Parser Monad 3 Derived Primitives 4 5 6 Outline Parser Type 1 Parser Type 2 3 4 5 6 What

More information

Fun with Parallel Monad Comprehensions

Fun with Parallel Monad Comprehensions Fun with Parallel Monad Comprehensions by Tomas Petricek tomas.petricek@cl.cam.ac.uk July 16, 2011 Monad comprehensions have an interesting history. They were the first syntactic extension for programming

More information

Monad class. Example: Lambda laughter. The functional IO problem. EDAN40: Functional Programming Functors and Monads

Monad class. Example: Lambda laughter. The functional IO problem. EDAN40: Functional Programming Functors and Monads Monad class EDAN40: Functional Programming Functors and Monads Jacek Malec Dept. of Computer Science, Lund University, Sweden April 23rd, 2018 Motivation: Separation of pure and impure code Properties

More information

Purely Functional Lazy Non-deterministic Programming

Purely Functional Lazy Non-deterministic Programming Purely Functional Lazy Non-deterministic Programming Sebastian Fischer Christian-Albrechts University, Germany sebf@informatik.uni-kiel.de Oleg Kiselyov FNMOC, CA, USA oleg@pobox.com Chung-chieh Shan Rutgers

More information

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions PROGRAMMING IN HASKELL CS-205 - Chapter 6 - Recursive Functions 0 Introduction As we have seen, many functions can naturally be defined in terms of other functions. factorial :: Int Int factorial n product

More information

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

CSci 450: Org. of Programming Languages Overloading and Type Classes CSci 450: Org. of Programming Languages Overloading and Type Classes H. Conrad Cunningham 27 October 2017 (after class) Contents 9 Overloading and Type Classes 1 9.1 Chapter Introduction.........................

More information

Haskell Rules: Embedding Rule Systems in Haskell

Haskell Rules: Embedding Rule Systems in Haskell Haskell Rules: Embedding Rule Systems in Haskell Steve Kollmansberger Martin Erwig Oregon State University {kollmast,erwig}@eecs.oregonstate.edu Abstract We present a domain-specific embedded language

More information

Programming Paradigms Written Exam (6 CPs)

Programming Paradigms Written Exam (6 CPs) Programming Paradigms Written Exam (6 CPs) 20.09.2017 First name Student number Last name Signature Instructions for Students Write your name and student number on the exam sheet and on every solution

More information

Dependent Polymorphism. Makoto Hamana

Dependent Polymorphism. Makoto Hamana 1 Dependent Polymorphism Makoto Hamana Department of Computer Science, Gunma University, Japan http://www.cs.gunma-u.ac.jp/ hamana/ This Talk 2 [I] A semantics for dependently-typed programming [II] A

More information

Just do It: Simple Monadic Equational Reasoning

Just do It: Simple Monadic Equational Reasoning Just do It: Simple Monadic Equational Reasoning Jeremy Gibbons and Ralf Hinze Department of Computer Science, University of Oxford Wolfson Building, Parks Road Oxford, OX1 3QD, England http://www.cs.ox.ac.uk/{jeremy.gibbons,ralf.hinze}/

More information

Introduction to Logic Programming. Ambrose

Introduction to Logic Programming. Ambrose Introduction to Logic Programming Ambrose Bonnaire-Sergeant @ambrosebs abonnairesergeant@gmail.com Introduction to Logic Programming Fundamental Logic Programming concepts Related to FP General implementation

More information

Declarative Multi-Paradigm Programming in

Declarative Multi-Paradigm Programming in Linköping, 11.2003 Declarative Multi-Paradigm Programming in Michael Hanus Christian-Albrechts-Universität Kiel DECLARATIVE PROGRAMMING General idea: no coding of algorithms description of logical relationships

More information

CSCE 314 Programming Languages Functors, Applicatives, and Monads

CSCE 314 Programming Languages Functors, Applicatives, and Monads CSCE 314 Programming Languages Functors, Applicatives, and Monads Dr. Hyunyoung Lee 1 Motivation Generic Functions A common programming pattern can be abstracted out as a definition. For example: inc ::

More information

Introduction to Functional Programming in Haskell 1 / 56

Introduction to Functional Programming in Haskell 1 / 56 Introduction to Functional Programming in Haskell 1 / 56 Outline Why learn functional programming? The essence of functional programming What is a function? Equational reasoning First-order vs. higher-order

More information

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona

CSc 372. Comparative Programming Languages. 2 : Functional Programming. Department of Computer Science University of Arizona 1/37 CSc 372 Comparative Programming Languages 2 : Functional Programming Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/37 Programming Paradigms

More information

From monoids to near-semirings: the essence of MonadPlus and Alternative

From monoids to near-semirings: the essence of MonadPlus and Alternative From monoids to near-semirings: the essence of MonadPlus and Alternative Exequiel Rivas Mauro Jaskelioff CIFASIS-CONICET Universidad Nacional de Rosario, Argentina jaskelioff@cifasis-conicet.gov.ar rivas@cifasis-conicet.gov.ar

More information

Adding Constraint Handling Rules to Curry Extended Abstract

Adding Constraint Handling Rules to Curry Extended Abstract Adding Constraint Handling Rules to Curry Extended Abstract Michael Hanus Institut für Informatik, CAU Kiel, D-24098 Kiel, Germany. mh@informatik.uni-kiel.de Abstract. This paper proposes an integration

More information

Lecture 4: Higher Order Functions

Lecture 4: Higher Order Functions Lecture 4: Higher Order Functions Søren Haagerup Department of Mathematics and Computer Science University of Southern Denmark, Odense September 26, 2017 HIGHER ORDER FUNCTIONS The order of a function

More information

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions

More information

1 Delimited continuations in Haskell

1 Delimited continuations in Haskell 1 Delimited continuations in Haskell This section describes programming with delimited control in Haskell. Delimited control, like its instance, exceptions, is an effect. Therefore, we have to use monads.

More information

INFOB3TC Solutions for the Exam

INFOB3TC Solutions for the Exam Department of Information and Computing Sciences Utrecht University INFOB3TC Solutions for the Exam Johan Jeuring Monday, 13 December 2010, 10:30 13:00 lease keep in mind that often, there are many possible

More information

Standard prelude. Appendix A. A.1 Classes

Standard prelude. Appendix A. A.1 Classes Appendix A Standard prelude In this appendix we present some of the most commonly used definitions from the standard prelude. For clarity, a number of the definitions have been simplified or modified from

More information

CSCE 314 Programming Languages. Functional Parsers

CSCE 314 Programming Languages. Functional Parsers CSCE 314 Programming Languages Functional Parsers Dr. Hyunyoung Lee 1 What is a Parser? A parser is a program that takes a text (set of tokens) and determines its syntactic structure. String or [Token]

More information

Programming with Math and Logic

Programming with Math and Logic .. Programming with Math and Logic an invitation to functional programming Ed Morehouse Wesleyan University The Plan why fp? terms types interfaces The What and Why of Functional Programming Computing

More information

Monadic Constraint Programming

Monadic Constraint Programming Under consideration for publication in J. Functional Programming Monadic Constraint Programming TOM SCHRIJVERS Department of Computer Science, K.U.Leuven, Belgium PETER STUCKEY National ICT Australia,

More information

Static Contract Checking for Haskell

Static Contract Checking for Haskell Static Contract Checking for Haskell Dana N. Xu INRIA France Work done at University of Cambridge Simon Peyton Jones Microsoft Research Cambridge Joint work with Koen Claessen Chalmers University of Technology

More information

EECS 700 Functional Programming

EECS 700 Functional Programming EECS 700 Functional Programming Dr. Andy Gill University of Kansas February 16, 2010 1 / 41 Parsing A parser is a program that analyses a piece of text to determine its syntactic structure. The expression

More information

Overlapping Rules and Logic Variables in Functional Logic Programs

Overlapping Rules and Logic Variables in Functional Logic Programs ICLP 2006 Overlapping Rules and Logic Variables in Functional Logic Programs Michael Hanus Christian-Albrechts-Universität Kiel (joint work with Sergio Antoy, Portland State University) FUNCTIONAL LOGIC

More information

Monads. Prof. Clarkson Fall Today s music: Vámanos Pal Monte by Eddie Palmieri

Monads. Prof. Clarkson Fall Today s music: Vámanos Pal Monte by Eddie Palmieri Monads Prof. Clarkson Fall 2017 Today s music: Vámanos Pal Monte by Eddie Palmieri Review Currently in 3110: Advanced topics Futures: Async: deferreds, return, bind Today: Monads Monad tutorials since

More information

Monadic Memoization Mixins

Monadic Memoization Mixins Monadic Memoization Mixins Daniel Brown and William R. Cook Department of Computer Sciences University of Texas at Austin Austin, Texas 78712 {danb,wcook}@cs.utexas.edu Abstract Memoization is a familiar

More information

Transforming Functional Logic Programs into Monadic Functional Programs

Transforming Functional Logic Programs into Monadic Functional Programs Transforming Functional Logic Programs into Monadic Functional Programs Bernd Braßel Sebastian Fischer Michael Hanus Fabian Reck Institut für Informatik, CAU Kiel, D-24098 Kiel, Germany {bbr sebf mh fre}@informatik.uni-kiel.de

More information

Control and Implementation of State Space Search

Control and Implementation of State Space Search 5 Control and Implementation of State Space Search 5.0 Introduction 5.1 Recursion-Based Search 5.2 Pattern-directed Search 5.3 Production Systems 5.4 The Blackboard Architecture for Problem Solving 5.5

More information

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino

FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS. Tatsuya Hagino 1 FUNCTIONAL PROGRAMMING NO.9 TYPE AND CLASS Tatsuya Hagino hagino@sfc.keio.ac.jp 2 Static Type Checking and Type Inference Type a set of values Bool = { True, False } Char = { 'a', 'b',... } Int = {...

More information

Monads. Functional Programming (CS4011) Monads

Monads. Functional Programming (CS4011) Monads Monads Functional Programming (CS4011) Andrew Butterfield Glenn Strong Foundations & Methods Group, Discipline of Software Systems Trinity College, University of Dublin {Andrew.Butterfield,Glenn.Strong}@cs.tcd.ie

More information

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

Haskell Types, Classes, and Functions, Currying, and Polymorphism 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Types, Classes, and Functions, Currying, and Polymorphism 2 Types A type is a collection of related values. For example, Bool contains the

More information

Monad Overview (3B) Young Won Lim 1/16/18

Monad Overview (3B) Young Won Lim 1/16/18 Based on Haskell in 5 steps https://wiki.haskell.org/haskell_in_5_steps 2 Copyright (c) 2016-2018 Young W. Lim. Permission is granted to copy, distribute and/or modify this document under the terms of

More information

FUNCTIONAL PEARLS The countdown problem

FUNCTIONAL PEARLS The countdown problem To appear in the Journal of Functional Programming 1 FUNCTIONAL PEARLS The countdown problem GRAHAM HUTTON School of Computer Science and IT University of Nottingham, Nottingham, UK www.cs.nott.ac.uk/

More information

Haskell An Introduction

Haskell An Introduction Haskell An Introduction What is Haskell? General purpose Purely functional No function can have side-effects IO is done using special types Lazy Strongly typed Polymorphic types Concise and elegant A First

More information

A Study In Monads Leif Harald Karlsen Master s Thesis Autumn 2013

A Study In Monads Leif Harald Karlsen Master s Thesis Autumn 2013 A Study In Monads Leif Harald Karlsen Master s Thesis Autumn 2013 Preface This thesis is a study in monads in functional programming. As a functional language I will use Haskell, and will therefore assume

More information

λ calculus is inconsistent

λ calculus is inconsistent Content Rough timeline COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification Gerwin Klein, June Andronick, Toby Murray λ Intro & motivation, getting started [1] Foundations & Principles

More information

Soutei, a Logic-Based Trust-Management System

Soutei, a Logic-Based Trust-Management System Soutei, a Logic-Based Trust-Management System System Description Andrew Pimlott 1 and Oleg Kiselyov 2 1 Planning Systems, Inc., Slidell, LA andrew@pimlott.net 2 Fleet Numerical Meteorology and Oceanography

More information

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers 1 CSCE 314: Programming Languages Dr. Flemming Andersen Functional Parsers What is a Parser? A parser is a program that takes a text (set of tokens) and determines its syntactic structure. String or [Token]

More information

Declarative Multi-paradigm Programming

Declarative Multi-paradigm Programming Michael Hanus (CAU Kiel) Declarative Multi-paradigm Programming WFLP/WLP 2014 1 Declarative Multi-paradigm Programming Michael Hanus Christian-Albrechts-University of Kiel Programming Languages and Compiler

More information

QuickCheck, SmallCheck & Reach: Automated Testing in Haskell. Tom Shackell

QuickCheck, SmallCheck & Reach: Automated Testing in Haskell. Tom Shackell QuickCheck, SmallCheck & Reach: Automated Testing in Haskell By Tom Shackell A Brief Introduction to Haskell Haskell is a purely functional language. Based on the idea of evaluation of mathematical functions

More information

Monads in Haskell. Nathanael Schilling. December 12, 2014

Monads in Haskell. Nathanael Schilling. December 12, 2014 Monads in Haskell Nathanael Schilling December 12, 2014 Abstract In Haskell, monads provide a mechanism for mapping functions of the type a -> m b to those of the type m a -> m b. This mapping is dependent

More information

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

Informatics 1 Functional Programming Lecture 11. Data Representation. Don Sannella University of Edinburgh Informatics 1 Functional Programming Lecture 11 Data Representation Don Sannella University of Edinburgh Part I Complexity t = n vs t = n 2 10.4 9.6 8.8 8 7.2 6.4 5.6 4.8 4 3.2 2.4 1.6 0.8 0 0.8 1.6 2.4

More information

Advanced features of Functional Programming (Haskell)

Advanced features of Functional Programming (Haskell) Advanced features of Functional Programming (Haskell) Polymorphism and overloading January 10, 2017 Monomorphic and polymorphic types A (data) type specifies a set of values. Examples: Bool: the type of

More information

Constraint Programming

Constraint Programming Depth-first search Let us go back to foundations: DFS = Depth First Search Constraint Programming Roman Barták Department of Theoretical Computer Science and Mathematical Logic 2 3 4 5 6 7 8 9 Observation:

More information

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor.

Trees. Solution: type TreeF a t = BinF t a t LeafF 1 point for the right kind; 1 point per constructor. Trees 1. Consider the following data type Tree and consider an example inhabitant tree: data Tree a = Bin (Tree a) a (Tree a) Leaf deriving Show tree :: Tree Int tree = Bin (Bin (Bin Leaf 1 Leaf ) 2 (Bin

More information

Shell CSCE 314 TAMU. Functions continued

Shell CSCE 314 TAMU. Functions continued 1 CSCE 314: Programming Languages Dr. Dylan Shell Functions continued 2 Outline Defining Functions List Comprehensions Recursion 3 A Function without Recursion Many functions can naturally be defined in

More information

A second life for Prolog

A second life for Prolog A second life for Prolog Algorithm = Logic + Control Jan Wielemaker J.Wielemaker@cwi.nl 1 Overview Algorithm = Logic + Control Limitations of SLD Beyond SLD 2 Algorithm = Logic + Control In Logic programming

More information

Implementing Equational Constraints in a Functional Language

Implementing Equational Constraints in a Functional Language Implementing Equational Constraints in a Functional Language Bernd Braßel, Michael Hanus, Björn Peemöller, and Fabian Reck Institut für Informatik, CAU Kiel, D-24098 Kiel, Germany {bbr mh bjp fre}@informatik.uni-kiel.de

More information

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

Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes. Haskell Overloading (3) Haskell Overloading (2) Haskell Overloading (1) LiU-FP2016: Lecture 8 Type Classes Henrik Nilsson University of Nottingham, UK What is the type of (==)? E.g. the following both work: 1 == 2 a == b I.e., (==) can be used to compare

More information

CSCE 314 Programming Languages. Monadic Parsing

CSCE 314 Programming Languages. Monadic Parsing CSCE 314 Programming Languages Monadic Parsing Dr. Hyunyoung Lee 1 What is a Parser? A parser is a program that takes a string of characters (or a set of tokens) as input and determines its syntactic structure.

More information

CPM: A Declarative Package Manager with Semantic Versioning

CPM: A Declarative Package Manager with Semantic Versioning Michael Hanus (CAU Kiel) CPM: A Declarative Package Manager with Semantic Versioning CICLOPS 2017 1 CPM: A Declarative Package Manager with Semantic Versioning Michael Hanus University of Kiel Programming

More information

Monads seen so far: IO vs Gen

Monads seen so far: IO vs Gen Monads David Sands Monads seen so far: IO vs Gen IO A Gen A Instructions to build a value of type A by interacting with the operating system Instructions to create a random value of type A Run by the ghc

More information