Programming Languages Haskell Part 2

Dr. Philip Cannata

1

A programming language is a language with a welldefined syntax (lexicon and grammar), type system, and semantics that can be used to implement a set of algorithms. Haskell

Dr. Philip Cannata

2

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 $ 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>

Dr. Philip Cannata

To load the file “10H2.hs” from the directory in which you started hugs. Similar for 10Logic and 10Movie 3

Propositional Logic 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 Dr. Philip Cannata

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

4

Truth tables: P && Q P || Q not P P ==> Q P Q P Q

P

Q

False

False

False

P

Q

P || Q

False

False

False

False

True

False

False

True

True

True

False

False

True

False

True

True

True

True

True

True

True

P

P

False

True

True

False

P

Dr. Philip Cannata

Q

P && Q

PQ

P False False True True



Q P Q False True True True False False True True

P

Q

P Q

False

False

True

False

False

False

False

True

False

False

True

True

True

False

False

True

False

True

True

True

True

True

True

False 5

Reasoning with Truth Tables Proposition (WFF): ((P  Q)((P)Q)) (P  Q)

(P)

((P)Q)

False False

False

True

False

False

False

True

True

True

True

True

True

False

True

False

True

True

True

True

True

False

True

True

P

Q

If prop is True when all variables are True: P, Q

((PQ)((P)Q))

A Truth double turnstile

((PQ)((P)Q))

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

Dr. Philip Cannata

6

Truth Table Application truthTable :: (Bool -> Bool -> Bool) -> [Bool] truthTable wff = [ (wff p q) | 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 Dr. Philip Cannata

7

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 Bool -> Bool) -> Bool satisfiable3 wff = or [ (wff p q r) | p 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

( \ p -> not p) ( \ p q -> (not p) || (not q) ) ( \ p q r -> (not p) || (not q) && (not r) )

Dr. Philip Cannata

8

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 )

Dr. Philip Cannata

9

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 Bool -> Bool) -> Bool contradiction3 wff = and [ not (wff p q r) | p (p && not p) || (q && not q) && (r && not r) ) Dr. Philip Cannata

10

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) )

Dr. Philip Cannata

11

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 Bool -> Bool) -> (Bool -> Bool -> Bool -> Bool) -> Bool logEquiv3 bf1 bf2 = and [(bf1 r s t) (bf2 r s t) | r 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)) Dr. Philip Cannata

-- Idempotence -- Idempotence -- Implication -- Contrapositive -- Contrapositive -- Contrapositive -- Contrapositive

-- Commutativity -- Commutativity -- deMorgan -- deMorgan -- Associativity -- Associativity -- Distributivity -- Distributivity 13

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. Dr. Philip Cannata

14

Haskell and SQL

Dr. Philip Cannata

15

Standard Oracle scott/tiger emp dept database

Dr. Philip Cannata

16

Standard Oracle scott/tiger emp dept database in Haskell 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") ]

Dr. Philip Cannata

17

Main>Main> [(empno, ename, job, sal, deptno) | (empno, ename, job, _, _, sal, deptno)

Dr. Philip Cannata

18

Main> [(empno, ename, job, sal, deptno) | (empno, ename, job, _, _, sal, deptno)

Dr. Philip Cannata

19

Main> [(empno, ename, job, sal, dname) | (empno, ename, job, _, _, sal, edeptno) length [sal | (_, _, _, _, _, sal, _)

Main> (\y -> fromIntegral(sum y) / fromIntegral(length y)) ([sal | (_, _, _, _, _, sal, _)

Dr. Philip Cannata

21

Main> map sqrt (map fromIntegral [sal | (_, _, _, _, _, sal, _)

Dr. Philip Cannata

22

Main> (map sqrt

. map fromIntegral) [sal | (_, _, _, _, _, sal, _)

Dr. Philip Cannata

23

Main> zip ([name | (_, name, _, _, _, _, _) [(name, sal, dept) | (_, name, _, _, _, sal, dept)

Dr. Philip Cannata

25

Main> [(name, sal, dept) | (_, name, _, _, _, sal, dept)

Dr. Philip Cannata

26

Main> [(name, sal, dept) | (_, name, _, _, _, sal, dept) [(empno, ename, job, sal, deptno) |(x0, empno) 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> Dr. Philip Cannata

40

Composition of Relations 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).

List Comprehension Main> [(empno, ename, job, sal, dname) | (empno, ename, job, _, _, sal, edeptno)