Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haskell 20.96 KB | None | 0 0
  1. \documentclass[a4]{tufte-handout}
  2. % The documentclass can be changed, but keep fonts at a reasonable size.
  3.  
  4. % comments
  5. \usepackage{comment}
  6.  
  7. % code environments
  8. \usepackage{listings}
  9. \lstnewenvironment{code}{
  10.   \lstset{language=haskell, basicstyle=\ttfamily }}{}
  11. \lstnewenvironment{spec}{
  12.   \lstset{language=haskell, basicstyle=\ttfamily }}{}
  13. \lstset{language=haskell, basicstyle=\ttfamily }
  14.  
  15.  
  16. \title{CO202: Coursework 1}
  17. \date{Autumn Term, 2019}
  18. \author{Group \#number}
  19.  
  20.  
  21. \begin{document}
  22. \maketitle
  23.  
  24. The source of this document is \texttt{Submision.lhs}, and should form the
  25. basis of your report as well as contain all the code for your submission. You
  26. should remove text (such as all the text in this section) that is here for your
  27. information only and that does not contribute to your submission.
  28. You should start by modifying the \verb|\author{}| command above to include
  29. your group number.
  30.  
  31. The source code of the provided \texttt{Submission.lhs} contains code and
  32. comments that are hidden from the final \texttt{pdf} file, so you should
  33. inspect it carefully.  For instance, the code declares the use of various
  34. language features that are used in this code base.  You can learn more about
  35. these language features in the language extensions section of the GHC
  36. documentation at
  37. \url{https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html}
  38. if you wish, but for the most part you need not worry about them.
  39. \begin{comment}
  40. The code in commented blocks such as this one is required for this file to
  41. compile.
  42. \begin{code}
  43. {-# LANGUAGE FlexibleInstances #-}
  44. {-# LANGUAGE ScopedTypeVariables #-}
  45. {-# LANGUAGE FunctionalDependencies #-}
  46. {-# LANGUAGE GeneralizedNewtypeDeriving #-}
  47. {-# LANGUAGE StandaloneDeriving #-}
  48. {-# LANGUAGE InstanceSigs #-}
  49. {-# LANGUAGE UndecidableInstances #-}
  50. {-# LANGUAGE TypeApplications #-}
  51. \end{code}
  52. \end{comment}
  53.  
  54. The following imports various modules that are used. You should avoid depending
  55. on any libraries other than those distributed with GHC:
  56. \href{http://hackage.haskell.org/package/base}{\texttt{base}} and
  57. \href{https://hackage.haskell.org/package/containers}{\texttt{containers}}
  58. ought to contain everything you need.
  59. \begin{comment}
  60. \begin{code}
  61. module Submission where
  62.  
  63. import Prelude hiding (maximum)
  64. import Data.Maybe (fromJust)
  65. import Data.Coerce (coerce)
  66. import Data.Function (on)
  67.  
  68. import Data.Array
  69. import Data.List (nub, sortBy, maximumBy, minimumBy, tails, inits, mapAccumL, deleteBy,(\\))
  70. import Data.Map (Map)
  71. import qualified Data.Map as M
  72.  
  73. \end{code}
  74. \end{comment}
  75.  
  76. All of the necessary types and definitions from the specification of this
  77. coursework have been given to you in the source of this document. You need not repeat
  78. that code in your submission, but it is required within the \verb|\begin{code}| and \verb|\end{code}| markers so that it can be compiled.
  79.  
  80. Before submitting your coursework, you should ensure that your code compiles
  81. properly. Use the following command with the supplied
  82. \texttt{Submission.lhs-boot} file to check that it can be marked:
  83. \begin{spec}
  84. ghc -fforce-recomp -c Submission.lhs-boot Submission.lhs
  85. \end{spec}
  86. This checks to see if all the type signatures of exposed functions are as
  87. expected.
  88.  
  89.  
  90. \begin{comment}
  91. \begin{code}
  92. data Player = Player1 | Player2
  93. data Planet = Planet Owner Ships Growth
  94. newtype Ships = Ships Int
  95. newtype Growth = Growth Int
  96. data Owner = Neutral | Owned Player
  97. newtype PlanetId = PlanetId Int
  98. type Planets = Map PlanetId Planet
  99. data Wormhole = Wormhole Source Target Turns
  100.  
  101. newtype Source = Source PlanetId
  102. newtype Target = Target PlanetId
  103. newtype Turns  = Turns Int
  104. newtype WormholeId = WormholeId Int
  105. type Wormholes = Map WormholeId Wormhole
  106. data Fleet = Fleet Player Ships WormholeId Turns
  107. type Fleets = [Fleet]
  108. data GameState = GameState Planets Wormholes Fleets
  109. data Order = Order WormholeId Ships
  110. \end{code}
  111. \end{comment}
  112.  
  113. \begin{comment}
  114. \begin{code}
  115. fib :: Int -> Integer
  116. fib 0 = 0
  117. fib 1 = 1
  118. fib n = fib (n-2) + fib (n-1)
  119.  
  120. fib' :: Int -> Integer
  121. fib' n = table ! n
  122.   where
  123.     table :: Array Int Integer
  124.     table = tabulate (0, n) mfib
  125.  
  126.     mfib 0 = 0
  127.     mfib 1 = 1
  128.     mfib n = table ! (n-1) + table ! (n-2)
  129.  
  130. tabulate :: Ix i => (i,i) -> (i -> a) -> Array i a
  131. tabulate (u,v) f = array (u,v) [ (i, f i) | i <- range (u, v)]
  132. \end{code}
  133. \end{comment}
  134.  
  135. \begin{comment}
  136. \begin{code}
  137. example1 :: GameState
  138. example1 = GameState planets wormholes fleets where
  139.   planets = M.fromList
  140.     [ (PlanetId 0, Planet (Owned Player1) (Ships 300) (Growth 0))
  141.     , (PlanetId 1, Planet Neutral         (Ships 200) (Growth 50))
  142.     , (PlanetId 2, Planet Neutral         (Ships 150) (Growth 10))
  143.     , (PlanetId 3, Planet Neutral         (Ships 30)  (Growth 5))
  144.     , (PlanetId 4, Planet Neutral         (Ships 100) (Growth 20))
  145.     ]
  146.   wormholes = M.fromList
  147.     [ (WormholeId 0, Wormhole homePlanet (Target 1) (Turns 1))
  148.     , (WormholeId 1, Wormhole homePlanet (Target 2) (Turns 1))
  149.     , (WormholeId 2, Wormhole homePlanet (Target 3) (Turns 1))
  150.     , (WormholeId 3, Wormhole homePlanet (Target 4) (Turns 1))
  151.     ] where homePlanet = Source 0
  152.   fleets = []
  153.  
  154. targetPlanets :: GameState -> Source -> [(PlanetId, Ships, Growth)]
  155. targetPlanets st s
  156.   = map (planetDetails . target) (M.elems (wormholesFrom s st))
  157.   where
  158.     planetDetails :: PlanetId -> (PlanetId, Ships, Growth)
  159.     planetDetails pId = (pId, ships, growth)
  160.       where Planet _ ships growth = lookupPlanet pId st
  161.  
  162. shipsOnPlanet :: GameState -> PlanetId -> Ships
  163. shipsOnPlanet st pId = ships
  164.   where Planet _ ships _ = lookupPlanet pId st
  165.  
  166. lookupPlanet :: PlanetId -> GameState -> Planet
  167. lookupPlanet pId (GameState ps _ _) = fromJust (M.lookup pId ps)
  168.  
  169. wormholesFrom :: Source -> GameState -> Wormholes
  170. wormholesFrom pId (GameState _ ws _)
  171.   = M.filter (\(Wormhole s _ _) -> s == pId) ws
  172.  
  173. wormholesTo :: Target -> GameState -> Wormholes
  174. wormholesTo pId (GameState _ ws _)
  175.   = M.filter (\(Wormhole _ t _) -> t == pId) ws
  176.  
  177. knapsack :: (Ord weight, Num weight, Ord value, Num value) =>
  178.   [(name, weight, value)] -> weight -> value
  179. knapsack wvs c = maximum 0 [ v + knapsack wvs (c - w) | (_,w,v) <- wvs , w <= c ]
  180.  
  181. maximum :: Ord a => a -> [a] -> a
  182. maximum x xs = foldr max x xs
  183. \end{code}
  184. \end{comment}
  185.  
  186. \marginnote{Make sure that the problems you are solving are clearly indicated.
  187. Using a section is a good idea. You should endeavor to concisely explain the
  188. code you have written. Feel free to make use of your own margin notes, and do
  189. please remove this one.}
  190. \section*{Problem 1: Dynamic Knapsack}
  191.  
  192. \begin{code}
  193. knapsack' :: forall name weight value .
  194.  (Ix weight, Num weight, Ord value, Num value) =>
  195.  [(name, weight, value)] -> weight -> value
  196. knapsack' wvs c = table ! c
  197.   where
  198.     table :: Array weight value
  199.     table = tabulate (0,c) mknapsack
  200.  
  201.     mknapsack :: weight -> value
  202.     mknapsack c = maximum 0 [ v + table ! (c - w) | (_,w,v) <- wvs , w <= c ]
  203. \end{code}
  204.  
  205. \section*{Problem 2: Knapsack Elements}
  206.  
  207. \begin{code}
  208. knapsack'' :: forall name weight value .
  209.   (Ix weight, Num weight, Ord value, Num value) =>
  210.   [(name, weight, value)] -> weight -> (value, [name])
  211. knapsack'' wvs c = table ! c
  212.   where
  213.     table :: Array weight (value, [name])
  214.     table = tabulate (0,c) mknapsack
  215.  
  216.     mknapsack :: weight -> (value, [name])
  217.     mknapsack c =  maximumBy (compare `on` fst) vns
  218.       where
  219.         vns = (0, []) : [(v + v', n : es) | (n, w, v) <- wvs , w <= c, let (v', es) = table ! (c - w)]
  220. \end{code}
  221.  
  222. \section*{Problem 3: Bounded Knapsack}
  223. \begin{code}
  224. bknapsack :: (Ord weight, Num weight, Ord value, Num value)
  225.   => [(name, weight, value)] -> weight -> (value, [name])
  226. bknapsack [] c = (0, [])
  227. bknapsack ((n, w, v) : xs) c
  228.   | w > c     = bknapsack xs c
  229.   | otherwise = maximumBy (compare `on` fst) vns
  230.   where
  231.     vns         = [(v + v', (ns ++ [n])), vn]
  232.    (v', ns)    = bknapsack xs (c - w)
  233.     vn          = bknapsack xs c
  234. \end{code}
  235.  
  236. \section*{Problem 4: Reasonable Indexes}
  237.  
  238. The tabulate function would have to not only calculate for every weight in the
  239. provided range. However, it is harder to provide it a meaningful range of lists
  240. for tabulate to understand. Moreover, the computation of every weight and list
  241. permutation will be heavy and may not be benefitial.
  242.  
  243. \section*{Problem 5: Bounded Knapsack Revisited}
  244.  
  245. \begin{code}
  246. -- bknapsack' expects to take the list size as an input
  247. bknapsack' :: forall name weight value .
  248.  (Ord weight, Num weight, Ord value, Num value) =>
  249.  [(name, weight, value)] -> Int ->
  250.  weight -> (value, [name])
  251. bknapsack' _ 0 _   = (0, [])
  252. bknapsack' wvs i c
  253.  | w > c     = bknapsack' wvs (i - 1) c
  254.   | otherwise = maximumBy (compare `on` fst) vns
  255.   where
  256.     vns         = [(v + v', (n : ns)), vn]
  257.    (n, w, v)   = wvs !! (i - 1)
  258.    (v', ns)    = bknapsack' wvs (i - 1) (c - w)
  259.    vn          = bknapsack' wvs (i - 1) c
  260. \end{code}
  261.  
  262. \section*{Problem 6: Dynamic Bounded Knapsack}
  263.  
  264. \begin{code}
  265. bknapsack'' :: forall name weight value .
  266.   (Ord name, Ix weight, Ord weight, Num weight,
  267.     Ord value, Num value) =>
  268.   [(name, weight, value)] -> weight -> (value, [name])
  269. bknapsack'' wvs c = table ! (listSize, c)
  270.   where
  271.     listSize = length wvs
  272.  
  273.     table :: Array (Int, weight) (value, [name])
  274.     table = tabulate ((0, 0), (listSize, c)) mbknapsack''
  275.  
  276.     mbknapsack'' :: (Int, weight) -> (value, [name])
  277.     mbknapsack'' (0, _)   = (0, [])
  278.     mbknapsack'' (i, c')
  279.      | w > c'     = table ! (i - 1, c')
  280.      | otherwise = maximumBy (compare `on` fst) vns
  281.      where
  282.        vns         = [(v + v', (n : ns)), vn]
  283.         (n, w, v)   = wvs !! (i - 1)
  284.         (v', ns)    = table ! (i - 1, c' - w)
  285.         vn          = table ! (i - 1, c')
  286. \end{code}
  287.  
  288. \section*{Problem 7: Dijkstra Dualized}
  289.  
  290. \begin{comment}
  291. \begin{code}
  292. optimise :: GameState -> Source -> (Growth, [PlanetId])
  293. optimise st s@(Source p) = bknapsack'' (targetPlanets st s) (shipsOnPlanet st p)
  294.  
  295. type Weight = Integer
  296.  
  297. class Eq v => Edge e v | e -> v where
  298.  source :: e -> v
  299.  target :: e -> v
  300.  weight :: e -> Weight
  301.  
  302. instance Edge (String, String, Integer) String where
  303.  source (s, _, _) = s
  304.  target (_, t, _) = t
  305.  weight (_, _, i) = i
  306.  
  307. instance Edge Wormhole PlanetId where
  308.  source (Wormhole (Source s) _ _)    = s
  309.  target (Wormhole _ (Target t) _)    = t
  310.  weight (Wormhole _ _ (Turns turns)) = toInteger turns
  311.  
  312. instance Edge (WormholeId, Wormhole) PlanetId where
  313.  source (_, w) = source w
  314.  target (_, w) = target w
  315.  weight (_, w) = weight w
  316.  
  317. data Path e = Path Weight [e]
  318. \end{code}
  319.  
  320. \begin{code}
  321. pathFromEdge :: Edge e v => e -> Path e
  322. pathFromEdge e = Path (weight e) [e]
  323. \end{code}
  324.  
  325. \begin{code}
  326. extend :: Edge e v => Path e -> e -> Path e
  327. extend (Path _ []) _ = error "extend: Empty path"
  328. extend (Path d (e:es)) e'
  329.   | target e == source e' = Path (d + weight e') (e':e:es)
  330.  | otherwise = error "extend: Incompatible endpoints"
  331. \end{code}
  332.  
  333. \begin{code}
  334. pathFromEdges :: Edge e v => [e] -> Path e
  335. pathFromEdges (x : xs) = foldl extend (pathFromEdge x) xs
  336. pathFromEdges [] = error "pathFromEdges: Empty list of edges"
  337. \end{code}
  338.  
  339. \begin{code}
  340. instance Edge e v => Edge (Path e) v where
  341.  source (Path _ es) = source (last es)
  342.  target (Path _ es) = target (head es)
  343.  weight (Path w _)  = w
  344. \end{code}
  345.  
  346. \begin{code}
  347. class Edge e v => Graph g e v | g -> e where
  348.  vertices  :: g -> [v]
  349.  edges     :: g -> [e]
  350.  edgesFrom :: g -> v -> [e]
  351.  edgesTo   :: g -> v -> [e]
  352.  velem     :: v -> g -> Bool
  353.  eelem     :: e -> g -> Bool
  354. \end{code}
  355.  
  356. \begin{code}
  357. instance (Eq e, Edge e v) => Graph [e] e v where
  358.  vertices es = nub (map source es ++ map target es)
  359.  edges es    = es
  360.  edgesFrom es v = [ e | e <- es, v == source e ]
  361.  edgesTo   es v = [ e | e <- es, v == target e ]
  362.  velem v es = v `elem` vertices es
  363.  eelem v es = v `elem` edges es
  364. \end{code}
  365.  
  366. \begin{code}
  367. example2 :: [(String, String, Integer)]
  368. example2 = [("s","t",10), ("s","y",5), ("t","x",1), ("t","y",2), ("y","t",3),
  369.            ("y","x", 9), ("x","z",4), ("z","x",6), ("y","z",2), ("z","s",7)]
  370. \end{code}
  371.  
  372. \begin{code}
  373. instance Graph GameState (WormholeId, Wormhole) PlanetId where
  374.  vertices (GameState ps _ _) = M.keys ps
  375.  edges    (GameState _ ws _) = M.assocs ws
  376.  edgesTo   st pId = M.toList (wormholesTo (Target pId) st)
  377.  edgesFrom st pId = M.toList (wormholesFrom (Source pId) st)
  378.  velem pId      (GameState ps _ _) = M.member pId ps
  379.  eelem (wId, _) (GameState _ ws _) = M.member wId ws
  380. \end{code}
  381. \end{comment}
  382.  
  383. \begin{comment}
  384. \begin{code}
  385. lte :: (a -> a -> Ordering) -> (a -> a -> Bool)
  386. lte cmp x y = cmp x y /= GT
  387.  
  388. eq :: (a -> a -> Ordering) -> (a -> a -> Bool)
  389. eq cmp x y = cmp x y == EQ
  390.  
  391. lt :: (a -> a -> Ordering) -> (a -> a -> Bool)
  392. lt cmp x y = cmp x y == LT
  393. \end{code}
  394.  
  395. \begin{code}
  396. class PQueue pqueue where
  397.  toPQueue   :: (a -> a -> Ordering) -> [a] -> pqueue a
  398.  fromPQueue :: pqueue a -> [a]
  399.  
  400.  priority :: pqueue a -> (a -> a -> Ordering)
  401.  
  402.  empty :: (a -> a -> Ordering) -> pqueue a
  403.  isEmpty :: pqueue a -> Bool
  404.  
  405.  insert :: a -> pqueue a -> pqueue a
  406.  delete :: a -> pqueue a -> pqueue a
  407.  
  408.  extract :: pqueue a -> a
  409.  discard :: pqueue a -> pqueue a
  410.  detach  :: pqueue a -> (a, pqueue a)
  411.  
  412. data PList a = PList (a -> a -> Ordering) [a]
  413.  
  414. instance PQueue PList where
  415.  
  416.  toPQueue cmp xs = PList cmp (sortBy cmp xs)
  417.  
  418.  fromPQueue (PList _ xs) = xs
  419.  
  420.  empty cmp = PList cmp []
  421.  
  422.  isEmpty (PList _ xs) = null xs
  423.  
  424.  priority (PList cmp _) = cmp
  425.  
  426.  insert x (PList cmp []) = PList cmp [x]
  427.  insert x ps@(PList cmp xs)
  428.    | x <= y    = cons x ps
  429.    | otherwise = cons y (insert x ys)
  430.    where (<=) = lte cmp
  431.          (y, ys) = detach ps
  432.          cons x (PList cmp xs) = PList cmp (x:xs)
  433.  
  434.  delete x (PList cmp []) = PList cmp []
  435.  delete x ps@(PList cmp _)
  436.    | x == y    = ys
  437.    | otherwise = cons y (delete x ys)
  438.    where (==) = eq cmp
  439.          (y, ys) = detach ps
  440.          cons x (PList cmp xs) = PList cmp (x:xs)
  441.  
  442.  extract (PList cmp (x:xs)) = x
  443.  
  444.  discard (PList cmp (x:xs)) = PList cmp xs
  445.  
  446.  detach  (PList cmp (x:xs)) = (x, PList cmp xs)
  447.  
  448. cmpPath :: Path v -> Path v -> Ordering
  449. cmpPath (Path d _) (Path d' _) = compare d d'
  450. \end{code}
  451. \end{comment}
  452.  
  453. \begin{comment}
  454. \begin{code}
  455. shortestPaths :: forall g e v. Graph g e v => g -> v -> [Path e]
  456. shortestPaths g v = dijkstra g (vertices g \\ [v]) ps
  457. where
  458.  ps :: PList (Path e)
  459.  ps = foldr insert (empty cmpPath) (map pathFromEdge (edgesFrom g v))
  460. \end{code}
  461.  
  462. \begin{code}
  463. example3 :: GameState
  464. example3 = GameState planets wormholes fleets where
  465.  planets = M.fromList
  466.    [ (PlanetId 0, Planet (Owned Player1) (Ships 300) (Growth 0))
  467.    , (PlanetId 1, Planet Neutral         (Ships 200) (Growth 50))
  468.    , (PlanetId 2, Planet Neutral         (Ships 150) (Growth 10))
  469.    , (PlanetId 3, Planet Neutral         (Ships 30)  (Growth 5))
  470.    , (PlanetId 4, Planet Neutral         (Ships 100) (Growth 20))
  471.    , (PlanetId 5, Planet Neutral         (Ships 100) (Growth 20))
  472.    ]
  473.  wormholes = M.fromList
  474.    [ (WormholeId 0, Wormhole homePlanet (Target 1) (Turns 1))
  475.    , (WormholeId 1, Wormhole homePlanet (Target 2) (Turns 2))
  476.    , (WormholeId 2, Wormhole homePlanet (Target 3) (Turns 3))
  477.    , (WormholeId 3, Wormhole homePlanet (Target 4) (Turns 4))
  478.    , (WormholeId 4, Wormhole (Source 4) (Target 5) (Turns 1))
  479.    , (WormholeId 5, Wormhole (Source 2) (Target 5) (Turns 1))
  480.    ] where homePlanet = Source 0
  481.  fleets = []
  482. \end{code}
  483.  
  484. \begin{code}
  485. dijkstra :: (Graph g e v, PQueue pqueue) =>
  486.  g -> [v] -> pqueue (Path e) -> [Path e]
  487. dijkstra g [] ps = []
  488. dijkstra g us ps
  489.  | isEmpty ps  = []
  490.  | v `elem` us = p : dijkstra g (us \\ [v])
  491.                                 (foldr insert ps' (map (extend p) (edgesFrom g v)))
  492.   | otherwise  = dijkstra g us ps'
  493.  where
  494.    (p, ps') = detach ps
  495.     v = target p
  496. \end{code}
  497. \end{comment}
  498.  
  499. \section*{Problem 8: Heap Operations}
  500.  
  501.  
  502. \begin{code}
  503. data Heap a = Heap (a -> a -> Ordering) (Tree a)
  504. data Tree a = Nil | Node Int (Tree a) a (Tree a)
  505.  
  506. balance :: (a -> a -> Ordering) -> Tree a -> Tree a
  507. balance _ Nil                = Nil
  508. balance _ t@(Node _ Nil _ _) = t
  509. balance cmp t@(Node n l x Nil)
  510.   | x' < x    = Node n (Node n' l' x r') x' Nil
  511.  | otherwise = t
  512.    where
  513.      (<)                = lt cmp
  514.      (Node n' l' x' r') = l
  515. balance cmp t@(Node n l x r)
  516.  | x < x'    = t
  517.   | otherwise = Node n (Node n' l' x r') x' r
  518.     where
  519.       (<)                = lt cmp
  520.       (Node n' l' x' r') = minNode l r
  521.  
  522.       minNode p@(Node _ _ x1 _) q@(Node _ _ x2 _)
  523.         | x2 < x1   = p
  524.         | otherwise = q
  525.  
  526. instance PQueue Heap where
  527.   toPQueue = undefined
  528.   fromPQueue = undefined
  529.  
  530.   priority :: Heap a -> (a -> a -> Ordering)
  531.   priority (Heap cmp _) = cmp
  532.  
  533.   empty :: (a -> a -> Ordering) -> Heap a
  534.   empty cmp = Heap cmp Nil
  535.  
  536.   isEmpty :: Heap a -> Bool
  537.   isEmpty (Heap cmp Nil) = False
  538.   isEmpty _              = True
  539.  
  540.   insert :: a -> Heap a -> Heap a
  541.   insert a (Heap cmp Nil) = Heap cmp (Node 1 Nil a Nil)
  542.   insert a (Heap cmp t)   = Heap cmp (balance cmp (insert' a t))
  543.    where
  544.      insert' a (Node n Nil x r) = balance cmp (Node (n + 1) (Node 1 Nil a Nil) x r)
  545.       insert' a (Node n l x Nil) = balance cmp (Node (n + 1) l x (Node 1 Nil a Nil))
  546.      insert' a (Node n l x r)
  547.         | lsize == rsize              = balance cmp (insert' a l)
  548.        | isInt(logBase 2 lsizeFloat) = balance cmp (insert' a r)
  549.         | otherwise                   = balance cmp (insert' a l)
  550.        where
  551.          isInt x = x == fromInteger (round x)
  552.          (Node lsize _ _ _) = l
  553.          (Node rsize _ _ _) = r
  554.          lsizeFloat = (fromIntegral(lsize + 1) :: Float)
  555.  
  556.  delete :: a -> Heap a -> Heap a
  557.  delete _ (Heap cmp (Node 1 Nil x Nil)) = empty cmp
  558.  delete a (Heap cmp t) = Heap cmp (balance cmp (delete' a t))
  559.     where
  560.       findLast (Node n l x r)
  561.         |
  562.  
  563.       delete' a (Node n l x r) =
  564.  
  565.  extract :: Heap a -> a
  566.  extract (Heap _ (Node _ _ x _)) = x
  567.  
  568.  discard :: Heap a -> Heap a
  569.  discard h = snd (detach h)
  570.  
  571.  detach :: Heap a -> (a, Heap a)
  572.  detach = undefined
  573. \end{code}
  574.  
  575. \begin{comment}
  576. \begin{code}
  577. shortestPaths' :: forall g e v . Graph g e v => g -> v -> [Path e]
  578. shortestPaths' g v = dijkstra g (vertices g) ps
  579. where
  580.  ps :: Heap (Path e)
  581.  ps = foldr insert (empty cmpPath) (map pathFromEdge (edgesFrom g v))
  582. \end{code}
  583. \end{comment}
  584.  
  585. \section*{Problem 9: Adjacency List Graphs}
  586.  
  587. \begin{code}
  588. newtype AdjList e v = AdjList [(v, [e])]
  589.  
  590. instance (Eq e, Edge e v) => Graph (AdjList e v) e v where
  591.  vertices (AdjList ves)    = undefined
  592.  edges (AdjList ves)       = undefined
  593.  edgesFrom (AdjList ves) s = undefined
  594.  edgesTo   (AdjList ves) t = undefined
  595.  velem v (AdjList ves)     = undefined
  596.  eelem e (AdjList ves)     = undefined
  597. \end{code}
  598.  
  599. \section*{Problem 10: Conflict Zones}
  600.  
  601. \begin{code}
  602. conflictZones :: GameState -> PlanetId -> PlanetId
  603.  -> ([PlanetId], [PlanetId], [PlanetId])
  604. conflictZones g p q = undefined
  605. \end{code}
  606.  
  607. \begin{comment}
  608. \begin{code}
  609. deriving instance Show Player
  610. deriving instance Read Player
  611. deriving instance Show Owner
  612. deriving instance Read Owner
  613. deriving instance Show Planet
  614. deriving instance Read Planet
  615. deriving instance Show Fleet
  616. deriving instance Read Fleet
  617.  
  618. deriving instance Show Wormhole
  619. deriving instance Read Wormhole
  620.  
  621. deriving instance Show Order
  622. deriving instance Read Order
  623. deriving instance Show GameState
  624. deriving instance Read GameState
  625.  
  626. deriving instance Ord PlanetId
  627. deriving instance Eq PlanetId
  628. deriving instance Num PlanetId
  629. instance Show PlanetId where
  630.  show (PlanetId x) = show x
  631. instance Read PlanetId where
  632.  readsPrec = coerce (readsPrec @Int)
  633.  
  634. deriving instance Ord Turns
  635. deriving instance Eq Turns
  636. deriving instance Num Turns
  637. instance Show Turns where
  638.  show (Turns x) = show x
  639. instance Read Turns where
  640.  readsPrec = coerce (readsPrec @Int)
  641.  
  642. deriving instance Ord Source
  643. deriving instance Eq Source
  644. instance Show Source where
  645.  show (Source x) = show x
  646. instance Read Source where
  647.  readsPrec = coerce (readsPrec @Int)
  648.  
  649. deriving instance Num Growth
  650. deriving instance Ord Growth
  651. deriving instance Eq Growth
  652. instance Show Growth where
  653.  show (Growth x) = show x
  654. instance Read Growth where
  655.  readsPrec = coerce (readsPrec @Int)
  656.  
  657. deriving instance Ix Ships
  658. deriving instance Num Ships
  659. deriving instance Ord Ships
  660. deriving instance Eq Ships
  661. instance Show Ships where
  662.  show (Ships x) = show x
  663. instance Read Ships where
  664.  readsPrec = coerce (readsPrec @Int)
  665.  
  666. deriving instance Ord Target
  667. deriving instance Eq Target
  668. instance Show Target where
  669.  show (Target x) = show x
  670. instance Read Target where
  671.  readsPrec = coerce (readsPrec @Int)
  672.  
  673. deriving instance Eq WormholeId
  674. deriving instance Ord WormholeId
  675. instance Show WormholeId where
  676.  show (WormholeId x) = show x
  677. instance Read WormholeId where
  678.  readsPrec = coerce (readsPrec @Int)
  679.  
  680. deriving instance Eq e   => Eq (Path e)
  681. deriving instance Show e => Show (Path e)
  682. instance Show a => Show (PList a) where
  683.  show (PList _ xs) = show xs
  684.  
  685. \end{code}
  686. \end{comment}
  687.  
  688. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement