module HL04STAL where import DB import Data.List -- | The infinite set of natural numbers: -- >>> take 10 naturals -- [0,1,2,3,4,5,6,7,8,9] naturals = [0 ..] -- | The infinite set of even numbers: -- >>> take 10 evens1 -- [0,2,4,6,8,10,12,14,16,18] evens1 = [n | n <- naturals, even n] -- | The infinite set of odd numbers: -- >>> take 10 odds1 -- [1,3,5,7,9,11,13,15,17,19] odds1 = [n | n <- naturals, odd n] -- | The infinite set of even numbers: -- >>> take 10 evens2 -- [0,2,4,6,8,10,12,14,16,18] evens2 = [2 * n | n <- naturals] -- | A finite set of squares: -- >>> take 10 small_squares1 -- [0,1,4,9,16,25,36,49,64,81] small_squares1 = [n ^ 2 | n <- [0 .. 999]] -- | A finite set of squares: -- >>> take 10 small_squares2 -- [0,1,4,9,16,25,36,49,64,81] small_squares2 = [n ^ 2 | n <- naturals, n < 1000] -- Here is a simple example of a program for which no proof of termination exists: run :: Integer -> [Integer] run n | n < 1 = error "argument not positive" | n == 1 = [1] | even n = n : run (div n 2) | odd n = n : run (3 * n + 1) -- | Infinite set of ones, recursively defined: -- >>> take 10 ones -- [1,1,1,1,1,1,1,1,1,1] ones = 1 : ones -- See DB.hs for an example database: characters = nub [x | ["play", _, _, x] <- db] movies = [x | ["release", x, _] <- db] actors = nub [x | ["play", x, _, _] <- db] directors = nub [x | ["direct", x, _] <- db] dates = nub [x | ["release", _, x] <- db] universe = nub (characters ++ actors ++ directors ++ movies ++ dates) 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] 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 q1 = [x | x <- actors, directorP x] q2 = [(x, y) | (x, y) <- act, directorP x] q3 = [(x, y, z) | (x, y) <- direct, (y, z) <- release] q4 = [(x, y, z) | (x, y) <- direct, (u, z) <- release, y == u] q5 = [(x, y) | (x, y) <- direct, (u, "1995") <- release, y == u] q6 = [ (x, y, z) | (x, y) <- direct, (u, z) <- release, y == u, z > "1995" ] q7 = [x | ("Kevin Spacey", x) <- act] q8 = [x | (x, y) <- release, y > "1997", actP ("William Hurt", x)] q9 = q1 /= [] q10 = [x | ("Woody Allen", x) <- direct] /= [] q10' = directorP "Woody Allen" -- Basic Set functions: elem' :: (Eq a) => a -> [a] -> Bool elem' x [] = False elem' x (y : ys) | x == y = True | otherwise = elem' x ys addElem :: a -> [[a]] -> [[a]] addElem x = map (x :) powerList :: [a] -> [[a]] powerList [] = [[]] powerList (x : xs) = (powerList xs) ++ (map (x :) (powerList xs)) data S = Void deriving (Eq, Show) empty :: [S] empty = [] display :: Int -> String -> IO () display n str = putStrLn (display' n 0 str) where display' _ _ [] = [] display' n m (x : xs) | n == m = '\n' : display' n 0 (x : xs) | otherwise = x : display' n (m + 1) xs