module LAI9

where 
import List
import Char

ld n = ldf 2 n 

divides d n = rem n d == 0 

ldf k n | divides k n = k 
        | k^2 > n     = n 
        | otherwise   = ldf (k+1) n

prime n | n < 1     = error "not a positive integer"
        | n == 1    = False 
        | otherwise = ld n == n

somePrimes    = filter prime [1..1000]

primesUntil n = filter prime [1..n]

allPrimes     = filter prime [1..]

sqr :: Int -> Int 
sqr = \ x -> x * x 

mnmInt :: [Int] -> Int
mnmInt [] = error "empty list" 
mnmInt [x] = x
mnmInt (x:xs) = min x (mnmInt xs)

factors :: Integer -> [Integer]
factors n | n < 1     = error "arg not positive"
          | n == 1    = []
          | otherwise = p : factors (div n p) 
                        where p = ld n

containedIn :: Eq a => [a] -> [a] -> Bool
containedIn xs ys = all (\ x -> elem x ys) xs

type Rel a = [(a,a)]

r1 = [(1,2),(2,1)]

r2 = [(1,2),(2,1),(2,1)]

sameR :: Ord a => Rel a -> Rel a -> Bool
sameR r s = sort (nub r) == sort (nub s)

cnv :: Rel a -> Rel a
cnv r = [ (y,x) | (x,y) <- r ]

infixr 5 @@

(@@) :: Eq a => Rel a -> Rel a -> Rel a
r @@ s = 
  nub [ (x,z) | (x,y) <- r, (w,z) <- s, y == w ]

euclR :: Eq a => Rel a -> Bool
euclR r = (cnv r @@ r) `containedIn` r

serialR :: Eq a => Rel a -> Bool
serialR r = 
  all (not.null) 
    (map (\ (x,y) -> [ v | (u,v) <- r, y == u]) r)

--isTSE :: Eq a => Rel a -> Bool
--isTSE r = transR r && serialR r && euclR r

data Agent = A | B | C | D | E deriving (Eq,Ord,Enum)

a,alice, b,bob, c,carol, d,dave, e,ernie  :: Agent
a = A; alice = A
b = B; bob   = B
c = C; carol = C
d = D; dave  = D
e = E; ernie = E

instance Show Agent where
  show A = "a"; show B = "b"; show C = "c"; 
  show D = "d" ; show E = "e"

data Prop = P Int | Q Int | R Int deriving (Eq,Ord)

instance Show Prop where 
  show (P 0) = "p"; show (P i) = "p" ++ show i 
  show (Q 0) = "q"; show (Q i) = "q" ++ show i 
  show (R 0) = "r"; show (R i) = "r" ++ show i

data EpistM state = Mo
             [state]
             [Agent]
             [(state,[Prop])]
             [(Agent,state,state)]
             [state]  deriving (Eq,Show)



