Download presentation

Presentation is loading. Please wait.

Published byParis Higbee Modified about 1 year ago

1
Dr. Philip Cannata 1 Programming Languages Haskell Part 2

2
Dr. Philip Cannata 2 A programming language is a language with a well- defined syntax (lexicon and grammar), type system, and semantics that can be used to implement a set of algorithms. Haskell

3
Dr. Philip Cannata 3 Haskell: /lusr/bin/hugs, should be in your default $PATH or for windows, download and install winhugs at http://cvs.haskell.org/Hugs/pages/downloading.htm http://cvs.haskell.org/Hugs/pages/downloading.htm $ hugs __ __ __ __ ____ ___ _________________________________________ || || || || || || ||__ Hugs 98: Based on the Haskell 98 standard ||___|| ||__|| ||__|| __|| Copyright (c) 1994-2005 ||---|| ___|| World Wide Web: http://haskell.org/hugs || || Bugs: http://hackage.haskell.org/trac/hugs || || Version: 20051031 _________________________________________ Haskell 98 mode: Restart with command line option -98 to enable extensions Type :? for help Hugs> :load 10H2 Main> To load the file “10H2.hs” from the directory in which you started hugs. Similar for 10Logic and 10Movie

4
Dr. Philip Cannata 4 Propositions: Statements that can be either True or False Logical Operators: Negation: not not :: Bool-> Bool not True = False not False = True Conjunction: && (&&) :: Bool-> Bool-> Bool False && x = False True && x = x Disjunction: || (||) :: Bool-> Bool-> Bool True || x = True False || x = x Propositional Logic Logical Operators: Implication (if – then): ==> Antecedent ==> Consequent (==>) :: Bool -> Bool -> Bool x ==> y = (not x) || y Equivalence (if, and only if): ( ) :: Bool -> Bool -> Bool x y = x == y Not Equivalent ( ) :: Bool -> Bool -> Bool x y = x /= y

5
Dr. Philip Cannata 5 Truth tables: P && Q P || Q not P P ==> Q P Q P Q P && Q False False False False True False True False False True True True P Q P || Q False False False False True True True False True True True True P P False True True False P Q P Q False False True False True True True False False True True True P Q P Q False False True False True False True False False True True True P Q P Q False False False False True True True False True True True False

6
Dr. Philip Cannata 6 Proposition (WFF): ((P Q) (( P) Q)) P Q False False True True False True False True (P Q) ( P) True False (( P) Q) False True ((P Q) (( P) Q)) False True Some True: prop is Satisfiable* If they were all True: Valid / Tautology All False: Contradiction (not satisfiable*) * Satisfiability was the first known NP-complete problem If prop is True when all variables are True: P, Q ((P Q) (( P) Q)) A Truth double turnstile Reasoning with Truth Tables

7
Dr. Philip Cannata 7 truthTable :: (Bool -> Bool -> Bool) -> [Bool] truthTable wff = [ (wff p q) | p <- [True,False], q <- [True,False]] tt = (\ p q -> not (p ==> q)) Hugs> :load 10Logic.hs LOGIC> :type tt tt :: Bool -> Bool -> Bool LOGIC> truthTable tt [False,True,False,False] LOGIC> or (truthTable tt) True LOGIC> and (truthTable tt) False Truth Table Application

8
Dr. Philip Cannata 8 Satisfiable: Are there well formed propositional formulas that return True for some input? satisfiable1 :: (Bool -> Bool) -> Bool satisfiable1 wff = (wff True) || (wff False) satisfiable2 :: (Bool -> Bool -> Bool) -> Bool satisfiable2 wff = or [ (wff p q) | p <- [True,False], q <- [True,False]] satisfiable3 :: (Bool -> Bool -> Bool -> Bool) -> Bool satisfiable3 wff = or [ (wff p q r) | p <- [True,False], q <- [True,False], r <- [True,False]] ( \ p -> not p) ( \ p q -> (not p) || (not q) ) ( \ p q r -> (not p) || (not q) && (not r) ) Define these first infix 1 ==> (==>) :: Bool -> Bool -> Bool x ==> y = (not x) || y infix 1 ( ) :: Bool -> Bool -> Bool x y = x == y infixr 2 ( ) :: Bool -> Bool -> Bool x y = x /= y

9
Dr. Philip Cannata 9 Validity (Tautology): Are there well formed propositional formulas that return True no matter what their input values are? valid1 :: (Bool -> Bool) -> Bool valid1 wff = (wff True) && (wff False) valid2 :: (Bool -> Bool -> Bool) -> Bool valid2 wff = (wff True True) && (wff True False) && (wff False True) && (wff False False) ( \ p -> p || not p ) -- Excluded Middle ( \ p -> p ==> p ) ( \ p q -> p ==> (q ==> p) ) ( \ p q -> (p ==> q) ==> p )

10
Dr. Philip Cannata 10 Contradiction (Not Satisfiable): Are there well formed propositional formulas that return False no matter what their input values are? contradiction1 :: (Bool -> Bool) -> Bool contradiction1 wff = not (wff True) && not (wff False) contradiction2 :: (Bool -> Bool -> Bool) -> Bool contradiction2 wff = and [not (wff p q) | p <- [True,False], q <- [True,False]] contradiction3 :: (Bool -> Bool -> Bool -> Bool) -> Bool contradiction3 wff = and [ not (wff p q r) | p <- [True,False], q <- [True,False], r <- [True,False]] ( \ p -> p && not p) ( \ p q -> (p && not p) || (q && not q) ) ( \ p q r -> (p && not p) || (q && not q) && (r && not r) )

11
Dr. Philip Cannata 11 Truth: Are there well formed propositional formulas that return True when their input is True truth1 :: (Bool -> Bool) -> Bool truth1 wff = (wff True) truth2 :: (Bool -> Bool -> Bool) -> Bool truth2 wff = (wff True True) ( \ p -> not p) ( \ p q -> (p && q) || (not p ==> q)) ( \ p q -> not p ==> q) ( \ p q -> (not p && q) && (not p ==> q) )

12
Dr. Philip Cannata 12 Equivalence: logEquiv1 :: (Bool -> Bool) -> (Bool -> Bool) -> Bool logEquiv1 bf1 bf2 = (bf1 True bf2 True) && (bf1 False bf2 False) logEquiv2 :: (Bool -> Bool -> Bool) -> (Bool -> Bool -> Bool) -> Bool logEquiv2 bf1 bf2 = and [(bf1 r s) (bf2 r s) | r <- [True,False], s <- [True,False]] logEquiv3 :: (Bool -> Bool -> Bool -> Bool) -> (Bool -> Bool -> Bool -> Bool) -> Bool logEquiv3 bf1 bf2 = and [(bf1 r s t) (bf2 r s t) | r <- [True,False], s <- [True,False], t <- [True,False]] formula3 p q = p formula4 p q = (p q) q formula5 p q = p ((p q) q) *Haskell> logEquiv2 formula3 formula4 True *Haskell> logEquiv2 formula4 formula5 False

13
Dr. Philip Cannata 13 Equivalence continued: logEquiv1 id (\ p -> not (not p)) logEquiv1 id (\ p -> p && p) logEquiv1 id (\ p -> p || p) logEquiv2 (\ p q -> p ==> q) (\ p q -> not p || q) logEquiv2 (\ p q -> not (p ==> q)) (\ p q -> p && not q) logEquiv2 (\ p q -> not p ==> not q) (\ p q -> q ==> p) logEquiv2 (\ p q -> p ==> not q) (\ p q -> q ==> not p) logEquiv2 (\ p q -> not p ==> q) (\ p q -> not q ==> p) logEquiv2 (\ p q -> p q) (\ p q -> (p ==> q) && (q ==> p)) logEquiv2 (\ p q -> p q) (\ p q -> (p && q) || (not p && not q)) logEquiv2 (\ p q -> p && q) (\ p q -> q && p) logEquiv2 (\ p q -> p || q) (\ p q -> q || p) logEquiv2 (\ p q -> not (p && q)) (\ p q -> not p || not q) logEquiv2 (\ p q -> not (p || q)) (\ p q -> not p && not q) logEquiv3 (\ p q r -> p && (q && r)) (\ p q r -> (p && q) && r) logEquiv3 (\ p q r -> p || (q || r)) (\ p q r -> (p || q) || r) logEquiv3 (\ p q r -> p && (q || r)) (\ p q r -> (p && q) || (p && r)) test9b logEquiv3 (\ p q r -> p || (q && r)) (\ p q r -> (p || q) && (p || r)) -- Idempotence -- Implication -- Contrapositive -- Commutativity -- deMorgan -- Associativity -- Distributivity

14
Dr. Philip Cannata 14 Why Reasoning with Truth Tables is Infeasible Works fine when there are 2 variables {T,F} {T,F} = set of potential values of variables 2 2 lines in truth table Three variables — starts to get tedious {T,F} {T,F} {T,F} = set of potential values 2 2 2 lines in truth table Twenty variables — definitely out of hand 2 2 … 2 lines (220) You want to look at a million lines? If you did, how would you avoid making errors? Hundreds of variables — not in a million years A need for Predicate Logic. We’ll look at this with Prolog.

15
Dr. Philip Cannata 15 Haskell and SQL

16
Dr. Philip Cannata 16 Standard Oracle scott/tiger emp dept database

17
Dr. Philip Cannata 17 emp = [ (7839, "KING", "PRESIDENT", 0, "17-NOV-81", 5000, 10), (7698, "BLAKE", "MANAGER", 7839, "01-MAY-81", 2850, 30), (7782, "CLARK", "MANAGER", 7839, "09-JUN-81", 2450, 10), (7566, "JONES", "MANAGER", 7839, "02-APR-81", 2975, 20), (7788, "SCOTT", "ANALYST", 7566, "09-DEC-82", 3000, 20), (7902, "FORD", "ANALYST", 7566, "03-DEC-81", 3000, 20), (7369, "SMITH", "CLERK", 7902, "17-DEC-80", 800, 20), (7499, "ALLEN", "SALESMAN", 7698, "20-FEB-81", 1600, 30), (7521, "WARD", "SALESMAN", 7698, "22-FEB-81", 1250, 30), (7654, "MARTIN", "SALESMAN", 7698, "28-SEP-81", 1250, 30), (7844, "TURNER", "SALESMAN", 7698, "08-SEP-81", 1500, 30), (7876, "ADAMS", "CLERK", 7788, "12-JAN-83", 1100, 20), (7900, "JAMES", "CLERK", 7698, "03-DEC-81", 950, 30), (7934, "MILLER", "CLERK", 7782, "23-JAN-82", 1300, 10) ] dept = [ (10, "ACCOUNTING", "NEW YORK"), (20, "RESEARCH", "DALLAS"), (30, "SALES", "CHICAGO"), (40, "OPERATIONS", "BOSTON") ] Standard Oracle scott/tiger emp dept database in Haskell

18
Dr. Philip Cannata 18 Main>Main> [(empno, ename, job, sal, deptno) | (empno, ename, job, _, _, sal, deptno) <- emp] [(7839,"KING","PRESIDENT",5000,10), (7698,"BLAKE","MANAGER",2850,30), (7782,"CLARK","MANAGER",2450,10), (7566,"JONES","MANAGER",2975,20), (7788,"SCOTT","ANALYST",3000,20), (7902,"FORD","ANALYST",3000,20), (7369,"SMITH","CLERK",800,20), (7499,"ALLEN","SALESMAN",1600,30), (7521,"WARD","SALESMAN",1250,30), (7654,"MARTIN","SALESMAN",1250,30), (7844,"TURNER","SALESMAN",1500,30), (7876,"ADAMS","CLERK",1100,20), (7900,"JAMES","CLERK",950,30), (7934,"MILLER","CLERK",1300,10)] Main>

19
Dr. Philip Cannata 19 Main> [(empno, ename, job, sal, deptno) | (empno, ename, job, _, _, sal, deptno) <- emp, deptno == 10] [(7839,"KING","PRESIDENT",5000,10), (7782,"CLARK","MANAGER",2450,10), (7934,"MILLER","CLERK",1300,10)] Main>

20
Dr. Philip Cannata 20 Main> [(empno, ename, job, sal, dname) | (empno, ename, job, _, _, sal, edeptno) <- emp, (deptno, dname, loc) <- dept, edeptno == deptno ] [(7839,"KING","PRESIDENT",5000,"ACCOUNTING"), (7698,"BLAKE","MANAGER",2850,"SALES"), (7782,"CLARK","MANAGER",2450,"ACCOUNTING"), (7566,"JONES","MANAGER",2975,"RESEARCH"), (7788,"SCOTT","ANALYST",3000,"RESEARCH"), (7902,"FORD","ANALYST",3000,"RESEARCH"), (7369,"SMITH","CLERK",800,"RESEARCH"), (7499,"ALLEN","SALESMAN",1600,"SALES"), (7521,"WARD","SALESMAN",1250,"SALES"), (7654,"MARTIN","SALESMAN",1250,"SALES"), (7844,"TURNER","SALESMAN",1500,"SALES"), (7876,"ADAMS","CLERK",1100,"RESEARCH"), (7900,"JAMES","CLERK",950,"SALES"), (7934,"MILLER","CLERK",1300,"ACCOUNTING")] Main>

21
Dr. Philip Cannata 21 Main> length [sal | (_, _, _, _, _, sal, _) <- emp] 14 Main> Main> (\y -> fromIntegral(sum y) / fromIntegral(length y)) ([sal | (_, _, _, _, _, sal, _) <- emp]) 2073.21428571429 Main>

22
Dr. Philip Cannata 22 Main> map sqrt (map fromIntegral [sal | (_, _, _, _, _, sal, _) <- emp]) [70.7106781186548, 53.3853912601566, 49.4974746830583, 54.5435605731786, 54.7722557505166, 28.2842712474619, 40.0, 35.3553390593274, 38.7298334620742, 33.166247903554, 30.8220700148449, 36.0555127546399] Main>

23
Dr. Philip Cannata 23 Main> (map sqrt. map fromIntegral) [sal | (_, _, _, _, _, sal, _) <- emp] [70.7106781186548, 53.3853912601566, 49.4974746830583, 54.5435605731786, 54.7722557505166, 28.2842712474619, 40.0, 35.3553390593274, 38.7298334620742, 33.166247903554, 30.8220700148449, 36.0555127546399] Main>

24
Dr. Philip Cannata 24 Main> zip ([name | (_, name, _, _, _, _, _) <- emp]) (map sqrt (map fromIntegral [sal | (_, _, _, _, _, sal, _) <- emp])) [("KING",70.7106781186548), ("BLAKE",53.3853912601566), ("CLARK",49.4974746830583), ("JONES",54.5435605731786), ("SCOTT",54.7722557505166), ("FORD",54.7722557505166), ("SMITH",28.2842712474619), ("ALLEN",40.0), ("WARD",35.3553390593274), ("MARTIN",35.3553390593274), ("TURNER",38.7298334620742), ("ADAMS",33.166247903554), ("JAMES",30.8220700148449), ("MILLER",36.0555127546399)] Main>

25
Dr. Philip Cannata 25 Main> [(name, sal, dept) | (_, name, _, _, _, sal, dept) <- emp, dept `elem` [20, 30]] [("BLAKE",2850,30), ("JONES",2975,20), ("SCOTT",3000,20), ("FORD",3000,20), ("SMITH",800,20), ("ALLEN",1600,30), ("WARD",1250,30), ("MARTIN",1250,30), ("TURNER",1500,30), ("ADAMS",1100,20), ("JAMES",950,30)] Main>

26
Dr. Philip Cannata 26 Main> [(name, sal, dept) | (_, name, _, _, _, sal, dept) <- emp, dept `notElem` [20, 30]] [("KING",5000,10), ("CLARK",2450,10), ("MILLER",1300,10)] Main>

27
Dr. Philip Cannata 27 Main> [(name, sal, dept) | (_, name, _, _, _, sal, dept) <- emp, dept `notElem` [20, 30]] ++ [(name, sal, dept) | (_, name, _, _, _, sal, dept) <- emp, dept `elem` [20, 30]] [("KING",5000,10), ("CLARK",2450,10), ("MILLER",1300,10), ("BLAKE",2850,30), ("JONES",2975,20), ("SCOTT",3000,20), ("FORD",3000,20), ("SMITH",800,20), ("ALLEN",1600,30), ("WARD",1250,30), ("MARTIN",1250,30), ("TURNER",1500,30), ("ADAMS",1100,20), ("JAMES",950,30)] Main>

28
Dr. Philip Cannata 28

29
Dr. Philip Cannata 29 db :: DB db = [ ["release", "Blade Runner", "1982"], ["release", "Alien", "1979"], ["release", "Aliens", "1986"], ["release", "Titanic", "1997"], ["release", "Good Will Hunting", "1997"], ["release", "Pulp Fiction", "1994"], ["release", "Reservoir Dogs", "1992"], ["release", "Romeo and Juliet", "1996"], … ["direct", "Brian De Palma", "The Untouchables"], ["direct", "James Cameron", "Titanic"], ["direct", "James Cameron", "Aliens"], ["direct", "Ridley Scott", "Alien"], ["direct", "Ridley Scott", "Blade Runner"], ["direct", "Ridley Scott", "Thelma and Louise"], ["direct", "Gus Van Sant", "Good Will Hunting"], ["direct", "Quentin Tarantino", "Pulp Fiction"], … ["play", "Leonardo DiCaprio", "Romeo and Juliet", "Romeo"], ["play", "Leonardo DiCaprio", "Titanic", "Jack Dawson"], ["play", "Robin Williams", "Good Will Hunting", "Sean McGuire"], ["play", "John Travolta", "Pulp Fiction", "Vincent Vega"], ["play", "Harvey Keitel", "Reservoir Dogs", "Mr White"], ["play", "Harvey Keitel", "Pulp Fiction", "Winston Wolf"], ["play", "Uma Thurman", "Pulp Fiction", "Mia"], ["play", "Quentin Tarantino", "Pulp Fiction", "Jimmie"], ["play", "Quentin Tarantino", "Reservoir Dogs", "Mr Brown"], ["play", "Sigourney Weaver", "Alien", "Ellen Ripley"], … Movie Database

30
Dr. Philip Cannata 30 We’ll need the following: -- nub is predefined in List but here's what it looks like: -- nub :: (Eq a) => [a] -> [a] -- nub [] = [] -- nub (x:xs) = x : nub (remove x xs) -- where -- remove y [] = [] -- remove y (z:zs) | y == z = remove y zs -- | otherwise = z : remove y zs *Haskell> nub "Mississippi" "Misp"

31
Dr. Philip Cannata 31 db :: DB db = [ ["release", "Blade Runner", "1982"], ["release", "Alien", "1979"], ["release", "Aliens", "1986"], ["release", "Titanic", "1997"], ["release", "Good Will Hunting", "1997"], ["release", "Pulp Fiction", "1994"], ["release", "Reservoir Dogs", "1992"], ["release", "Romeo and Juliet", "1996"], … ["direct", "Brian De Palma", "The Untouchables"], ["direct", "James Cameron", "Titanic"], ["direct", "James Cameron", "Aliens"], ["direct", "Ridley Scott", "Alien"], ["direct", "Ridley Scott", "Blade Runner"], ["direct", "Ridley Scott", "Thelma and Louise"], ["direct", "Gus Van Sant", "Good Will Hunting"], ["direct", "Quentin Tarantino", "Pulp Fiction"], … ["play", "Leonardo DiCaprio", "Romeo and Juliet", "Romeo"], ["play", "Leonardo DiCaprio", "Titanic", "Jack Dawson"], ["play", "Robin Williams", "Good Will Hunting", "Sean McGuire"], ["play", "John Travolta", "Pulp Fiction", "Vincent Vega"], ["play", "Harvey Keitel", "Reservoir Dogs", "Mr White"], ["play", "Harvey Keitel", "Pulp Fiction", "Winston Wolf"], ["play", "Uma Thurman", "Pulp Fiction", "Mia"], ["play", "Quentin Tarantino", "Pulp Fiction", "Jimmie"], ["play", "Quentin Tarantino", "Reservoir Dogs", "Mr Brown"], ["play", "Sigourney Weaver", "Alien", "Ellen Ripley"], … characters = nub [ x | ["play",_,_,x] <- db ] actors = nub [ x | ["play",x,_,_] <- db ] directors = nub [ x | ["direct",x,_] <- db ] movies = [ x | ["release",x,_] <- db ] dates = nub [ x | ["release",_,x] <- db ] universe = nub (characters++actors++directors++ movies++dates)

32
Dr. Philip Cannata 32 db :: DB db = [ ["release", "Blade Runner", "1982"], ["release", "Alien", "1979"], ["release", "Aliens", "1986"], ["release", "Titanic", "1997"], ["release", "Good Will Hunting", "1997"], ["release", "Pulp Fiction", "1994"], ["release", "Reservoir Dogs", "1992"], ["release", "Romeo and Juliet", "1996"], … ["direct", "Brian De Palma", "The Untouchables"], ["direct", "James Cameron", "Titanic"], ["direct", "James Cameron", "Aliens"], ["direct", "Ridley Scott", "Alien"], ["direct", "Ridley Scott", "Blade Runner"], ["direct", "Ridley Scott", "Thelma and Louise"], ["direct", "Gus Van Sant", "Good Will Hunting"], ["direct", "Quentin Tarantino", "Pulp Fiction"], … ["play", "Leonardo DiCaprio", "Romeo and Juliet", "Romeo"], ["play", "Leonardo DiCaprio", "Titanic", "Jack Dawson"], ["play", "Robin Williams", "Good Will Hunting", "Sean McGuire"], ["play", "John Travolta", "Pulp Fiction", "Vincent Vega"], ["play", "Harvey Keitel", "Reservoir Dogs", "Mr White"], ["play", "Harvey Keitel", "Pulp Fiction", "Winston Wolf"], ["play", "Uma Thurman", "Pulp Fiction", "Mia"], ["play", "Quentin Tarantino", "Pulp Fiction", "Jimmie"], ["play", "Quentin Tarantino", "Reservoir Dogs", "Mr Brown"], ["play", "Sigourney Weaver", "Alien", "Ellen Ripley"], … direct = [ (x,y) | ["direct",x,y] <- db ] act = [ (x,y) | ["play",x,y,_] <- db ] play = [ (x,y,z) | ["play",x,y,z] <- db ] release = [ (x,y) | ["release",x,y] <- db ]

33
Dr. Philip Cannata 33 db :: DB db = [ ["release", "Blade Runner", "1982"], ["release", "Alien", "1979"], ["release", "Aliens", "1986"], ["release", "Titanic", "1997"], ["release", "Good Will Hunting", "1997"], ["release", "Pulp Fiction", "1994"], ["release", "Reservoir Dogs", "1992"], ["release", "Romeo and Juliet", "1996"], … ["direct", "Brian De Palma", "The Untouchables"], ["direct", "James Cameron", "Titanic"], ["direct", "James Cameron", "Aliens"], ["direct", "Ridley Scott", "Alien"], ["direct", "Ridley Scott", "Blade Runner"], ["direct", "Ridley Scott", "Thelma and Louise"], ["direct", "Gus Van Sant", "Good Will Hunting"], ["direct", "Quentin Tarantino", "Pulp Fiction"], … ["play", "Leonardo DiCaprio", "Romeo and Juliet", "Romeo"], ["play", "Leonardo DiCaprio", "Titanic", "Jack Dawson"], ["play", "Robin Williams", "Good Will Hunting", "Sean McGuire"], ["play", "John Travolta", "Pulp Fiction", "Vincent Vega"], ["play", "Harvey Keitel", "Reservoir Dogs", "Mr White"], ["play", "Harvey Keitel", "Pulp Fiction", "Winston Wolf"], ["play", "Uma Thurman", "Pulp Fiction", "Mia"], ["play", "Quentin Tarantino", "Pulp Fiction", "Jimmie"], ["play", "Quentin Tarantino", "Reservoir Dogs", "Mr Brown"], ["play", "Sigourney Weaver", "Alien", "Ellen Ripley"], … charP = \ x -> elem x characters actorP = \ x -> elem x actors movieP = \ x -> elem x movies directorP = \ x -> elem x directors dateP = \ x -> elem x dates actP = \ (x,y) -> elem (x,y) act releaseP = \ (x,y) -> elem (x,y) release directP = \ (x,y) -> elem (x,y) direct playP = \ (x,y,z) -> elem (x,y,z) play

34
Dr. Philip Cannata 34 db :: DB db = [ ["release", "Blade Runner", "1982"], ["release", "Alien", "1979"], ["release", "Aliens", "1986"], ["release", "Titanic", "1997"], ["release", "Good Will Hunting", "1997"], ["release", "Pulp Fiction", "1994"], ["release", "Reservoir Dogs", "1992"], ["release", "Romeo and Juliet", "1996"], … ["direct", "Brian De Palma", "The Untouchables"], ["direct", "James Cameron", "Titanic"], ["direct", "James Cameron", "Aliens"], ["direct", "Ridley Scott", "Alien"], ["direct", "Ridley Scott", "Blade Runner"], ["direct", "Ridley Scott", "Thelma and Louise"], ["direct", "Gus Van Sant", "Good Will Hunting"], ["direct", "Quentin Tarantino", "Pulp Fiction"], … ["play", "Leonardo DiCaprio", "Romeo and Juliet", "Romeo"], ["play", "Leonardo DiCaprio", "Titanic", "Jack Dawson"], ["play", "Robin Williams", "Good Will Hunting", "Sean McGuire"], ["play", "John Travolta", "Pulp Fiction", "Vincent Vega"], ["play", "Harvey Keitel", "Reservoir Dogs", "Mr White"], ["play", "Harvey Keitel", "Pulp Fiction", "Winston Wolf"], ["play", "Uma Thurman", "Pulp Fiction", "Mia"], ["play", "Quentin Tarantino", "Pulp Fiction", "Jimmie"], ["play", "Quentin Tarantino", "Reservoir Dogs", "Mr Brown"], ["play", "Sigourney Weaver", "Alien", "Ellen Ripley"], … Actors that are also Directors q1 = [ x | x <- actors, directorP x ] Actors that are also Directors and their films q2 = [ (x,y) | (x,y) <- act, directorP x ] Directors, their Films and Release Dates – incorrect version q3 = [ (x,y,z) | (x,y) <- direct, (y,z) <- release ] Directors, their Films and Release Dates – correct version q4 = [ (x,y,z) | (x,y) <- direct, (u,z) <- release, y == u ] Directors and their films released in 1995 q5 = [ (x,y) | (x,y) <- direct, (u,"1995") <- release, y == u ] Directors, Films and Release Date for those released after 1995 q6 = [ (x,y,z) | (x,y) "1995"] Films in which Kevin Spacey acted q7 = [ x | ("Kevin Spacey",x) <- act ] William Hurt films released after 1997 q8 = [ x | (x,y) "1997", actP ("William Hurt",x) ] Are there any films in which the director was also the actor? q9 = q1 /= [] Does the database contain films directed by Woody Allen q10 = [ x | ("Woody Allen",x) <- direct ] /= [] q10' = directorP "Woody Allen"

35
Dr. Philip Cannata 35 A Different View of Relations

36
Dr. Philip Cannata 36 empno = [ (7369, 7369), (7499, 7499), (7521, 7521), (7566, 7566), (7654, 7654), (7698, 7698), (7782, 7782), (7788, 7788), (7839, 7839), (7844, 7844), (7876, 7876), (7900, 7900), (7902, 7902), (7934, 7934) ] ename = [ (7839, "KING"), (7698, "BLAKE"), (7782, "CLARK"), (7566, "JONES"), (7788, "SCOTT"), (7902, "FORD"), (7369, "SMITH"), (7499, "ALLEN"), (7521, "WARD"), (7654, "MARTIN"), (7844, "TURNER"), (7876, "ADAMS"), (7900, "JAMES"), (7934, "MILLER") ] job = [(7839, "PRESIDENT"), (7698, "MANAGER"), (7782, "MANAGER"), (7566, "MANAGER"), (7788, "ANALYST"), (7902, "ANALYST"), (7369, "CLERK"), (7499, "SALESMAN"), (7521, "SALESMAN"), (7654, "SALESMAN"), (7844, "SALESMAN"), (7876, "CLERK"), (7900, "CLERK"), (7934, "CLERK") ] sal = [ (7839, 5000), (5000, 7839), (7698, 2850), (2850, 7698), (7782, 2450), (2450, 7782), (7566, 2975), (2975, 7566), (7788, 3000), (3000, 7788), (7902, 3000), (3000, 7902), (7369, 800), (800, 7369), (7499, 1600), (1600, 7499), (7521, 1250), (1250, 7521), (7654, 1250), (1250, 7654), (7844, 1500), (1500, 7844), (7876, 1100), (1100, 7876), (7900, 950), (950, 7900), (7934, 1300), (1300, 7934) ] deptno = [ (7839, 10), (10, 7839), (7698, 30), (30, 7698), (7782, 10), (10, 7782), (7566, 20), (20, 7566), (7788, 20), (20, 7788), (7902, 20), (20, 7902), (7369, 20), (20, 7369), (7499, 30), (30, 7499), (7521, 30), (30, 7521), (7654, 30), (30, 7654), (7844, 30), (30, 7844), (7876, 20), (20, 7876), (7900, 30), (30, 7900), (7934, 10), (10, 7934) ] mgr = [ (7839, 0), (7698, 7839), (7782, 7839), (7566, 7839), (7788, 7566), (7902, 7566), (7369, 7902), (7499, 7698), (7521, 7698), (7654, 7698), (7844, 7698), (7876, 7788), (7900, 7698), (7934, 7782) ] I made these symmetric

37
Dr. Philip Cannata 37 Main> [(empno, ename, job, sal, deptno) |(x0, empno) <- empno, (x1, ename) <- ename, (x2, job) <- job, (x3, sal) <- sal, (x4, deptno) <- deptno, x0 == x1 && x1 == x2 && x2 == x3 && x3 == x4] [(7369,"SMITH","CLERK",800,20), (7499,"ALLEN","SALESMAN",1600,30), (7521,"WARD","SALESMAN",1250,30), (7566,"JONES","MANAGER",2975,20), (7654,"MARTIN","SALESMAN",1250,30), (7698,"BLAKE","MANAGER",2850,30), (7782,"CLARK","MANAGER",2450,10), (7788,"SCOTT","ANALYST",3000,20), (7839,"KING","PRESIDENT",5000,10), (7844,"TURNER","SALESMAN",1500,30), (7876,"ADAMS","CLERK",1100,20), (7900,"JAMES","CLERK",950,30), (7902,"FORD","ANALYST",3000,20), (7934,"MILLER","CLERK",1300,10)] Main>Main> [(empno, ename, job, sal, deptno) | (empno, ename, job, _, _, sal, deptno) <- emp] [(7839,"KING","PRESIDENT",5000,10), (7698,"BLAKE","MANAGER",2850,30), (7782,"CLARK","MANAGER",2450,10), (7566,"JONES","MANAGER",2975,20), (7788,"SCOTT","ANALYST",3000,20), (7902,"FORD","ANALYST",3000,20), (7369,"SMITH","CLERK",800,20), (7499,"ALLEN","SALESMAN",1600,30), (7521,"WARD","SALESMAN",1250,30), (7654,"MARTIN","SALESMAN",1250,30), (7844,"TURNER","SALESMAN",1500,30), (7876,"ADAMS","CLERK",1100,20), (7900,"JAMES","CLERK",950,30), (7934,"MILLER","CLERK",1300,10)] Main>

38
Dr. Philip Cannata 38 r {(1,2),(2,3),(2,4),(2,5),(2,6),(6,7),(6,8)} ComposeR r 2 {(1,3),(1,4),(1,5),(1,6),(2,7),(2,8)} ComposeR r 3 {(1,7),(1,8)} ComposeR r 4 {} {(1,2),(2,3),(2,4),(2,5),(2,6),(6,7),(6,8)} composed with {(1,2),(2,3),(2,4),(2,5),(2,6),(6,7),(6,8)} {(1,3),(1,4),(1,5),(1,6),(2,7),(2,8)} composed with {(1,2),(2,3),(2,4),(2,5),(2,6),(6,7),(6,8)} {(1,7),(1,8)} composed with {(1,2),(2,3),(2,4),(2,5),(2,6),(6,7),(6,8)} Composition of Relations

39
Dr. Philip Cannata 39 {(rose,phil),(phil,nicolette),(phil,antoinette),(phil,jeanette), (phil,philJ),(philJ,philJJ),(philJ,patrick)} {(rose,nicolette),(rose,antoinette),(rose,jeanette),(rose,philJ), (phil,philJJ),(phil,patrick)} {(rose,philJJ),(rose,patrick)} Grandparent Relation Composition of Relations If 1 is rose, 2 is phil, 3 is nicolette, 4 is antoinette, 5 is jeanette, 6 is philJ, 7 is philJJ and 8 is patrick Great Grandparent Relation r {(1,2),(2,3),(2,4),(2,5),(2,6),(6,7),(6,8)} ComposeR r 2 {(1,3),(1,4),(1,5),(1,6),(2,7),(2,8)} ComposeR r 3 {(1,7),(1,8)} ComposeR r 4 {}

40
Dr. Philip Cannata 40 Main> relationalComposition sal deptno [(5000,10), (2850,30), (2450,10), (2975,20), (3000,20), (800,20), (1600,30), (1250,30), (1500,30), (1100,20), (950,30), (1300,10)] Main> Main> head (relationalComposition [("BLAKE", 7698)] (relationalComposition empno sal)) : head (relationalComposition [("BLAKE", 7698)] (relationalComposition empno deptno)) : relationalComposition [("BLAKE", 7698)] (relationalComposition empno mgr) [("BLAKE",2850), ("BLAKE",30), ("BLAKE",7839)] Main>

41
Dr. Philip Cannata 41 This is not the last time we’ll see something similar to Composition of Relations: parent(hank,ben). parent(hank,denise). parent(irene,ben). parent(irene,denise). parent(alice,carl). parent(ben,carl). parent(denise,frank). parent(denise,gary). parent(earl,frank). parent(earl,gary). grandparent(X,Z) :- parent(X,Y), parent(Y,Z). Composition of Relations List Comprehension Main> [(empno, ename, job, sal, dname) | (empno, ename, job, _, _, sal, edeptno) <- emp, (deptno, dname, loc) <- dept, edeptno == deptno ]

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google