-
Notifications
You must be signed in to change notification settings - Fork 0
/
Torus.hs
56 lines (43 loc) · 1.58 KB
/
Torus.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{-# LANGUAGE BangPatterns,
FlexibleInstances,
MultiParamTypeClasses #-}
module Torus where
import LoopZipper
import Universe
import Control.Comonad
import Data.Default
import qualified Data.Foldable as Foldable
import Data.Monoid
import Data.Sequence
import Prelude hiding (take, zipWith)
import qualified Prelude
data Torus a = Torus !(Loop (Loop a))
(<|) :: Default a => Torus a -> Torus a -> Torus a
(<|) (Torus (Loop l ld)) (Torus (Loop r rd)) =
Torus (Loop (l <> r) (zipWith (<>) ld rd))
(-^-) :: Default a => Torus a -> Torus a -> Torus a
(-^-) (Torus t) (Torus b) =
Torus (t <> b)
torusShape (Torus a) = (loopLength a, extract $ fmap loopLength a)
instance Universe Torus a where
get = extract
set s (Torus (Loop (Loop _ rs) ds)) = Torus (Loop (Loop s rs) ds)
left (Torus z) = Torus (fmap loopLeft z)
right (Torus z) = Torus (fmap loopRight z)
up (Torus z) = Torus (loopLeft z)
down (Torus z) = Torus (loopRight z)
instance Matrix Torus a where
fromMatrix m = Torus $ Loop (head w) (fromList . tail $ w)
where
w = map (\(h:t) -> Loop h (fromList t)) m
toMatrix (Torus (Loop m d)) = toRow m : (Foldable.toList $ fmap toRow d)
where
toRow (Loop m r) = m : (Foldable.toList r)
instance Functor Torus where
fmap f (Torus z) = Torus (fmap (fmap f) z)
instance Comonad Torus where
extract (Torus z) = loopRead . loopRead $ z
duplicate !u =
Torus $ fmap
(\ !r -> Loop r (seqTail . iterateN ((+ 1) . pred . snd . torusShape $ r) right $ r))
(Loop u (seqTail . iterateN ((+ 1) . pred . fst . torusShape $ u) down $ u))