1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE FlexibleInstances #-}
3 {-# LANGUAGE MultiParamTypeClasses #-}
4 {-# LANGUAGE TupleSections #-}
5 module Hcompta.Lib.Interval where
7 import Data.Data (Data(..))
8 import qualified Data.Functor
9 import Data.Typeable (Typeable)
15 { adherence :: Adherence
17 deriving (Eq, Data, Show, Typeable)
19 instance Functor Limit where
20 fmap f (Limit a x) = Limit a (f x)
22 data Adherence = Out | In
23 deriving (Eq, Data, Show, Typeable)
25 -- | Return given 'Limit' with its 'adherence' set to the opposite one.
26 flip_limit :: Limit x -> Limit x
27 flip_limit (Limit a x) = Limit (case a of { In -> Out; Out -> In }) x
29 -- ** Comparing 'Limit's
31 -- | Compare two 'low' 'Limit's.
32 newtype LL x = LL { unLL :: x }
34 instance Ord x => Ord (LL (Limit x)) where
35 compare (LL x) (LL y) =
36 case compare (limit x) (limit y) of
38 case (adherence x, adherence y) of
44 -- | Compare two 'high' 'Limit's.
45 newtype HH x = HH { unHH :: x }
47 instance Ord x => Ord (HH (Limit x)) where
48 compare (HH x) (HH y) =
49 case compare (limit x) (limit y) of
51 case (adherence x, adherence y) of
59 newtype Ord x => Interval x = Interval (Limit x, Limit x)
60 deriving (Eq, Show, Data, Typeable)
62 low :: Ord x => Interval x -> Limit x
63 low (Interval t) = fst t
65 high :: Ord x => Interval x -> Limit x
66 high (Interval t) = snd t
68 -- | Return 'Interval' with given 'low' then 'high' 'Limit's,
69 -- if they form a valid 'Interval'.
70 interval :: Ord x => Limit x -> Limit x -> Maybe (Interval x)
72 case compare_without_adherence x y of
73 LT -> Just $ Interval (x, y)
75 case (adherence x, adherence y) of
76 (In, In) -> Just $ Interval (x, y)
80 -- | Like 'Data.Functor.fmap', but may return 'Nothing', if mapped 'Interval' is not valid.
81 fmap :: (Ord x, Ord y) => (x -> y) -> Interval x -> Maybe (Interval y)
82 fmap f (Interval (il, ih)) = interval (Data.Functor.fmap f il) (Data.Functor.fmap f ih)
84 -- | Like 'Data.Functor.fmap', but only safe if given map preserves 'Ordering'.
85 fmap_unsafe :: (Ord x, Ord y) => (x -> y) -> Interval x -> Interval y
86 fmap_unsafe f (Interval (il, ih)) = Interval (Data.Functor.fmap f il, Data.Functor.fmap f ih)
89 -- | Like 'Data.Functor.fmap', but on 'Limit's,
90 -- and may return 'Nothing', if mapped 'Interval' is not valid.
91 fmap_limits :: (Ord x, Ord y) => (Limit x -> Limit y) -> Interval x -> Maybe (Interval y)
92 fmap_limits f (Interval (il, ih)) = interval (f il) (f ih)
94 -- | Like 'Data.Functor.fmap', but on 'Limit's
95 -- and only safe if given map preserves 'Ordering'.
96 fmap_limits_unsafe :: (Ord x, Ord y) => (Limit x -> Limit y) -> Interval x -> Interval y
97 fmap_limits_unsafe f (Interval (il, ih)) = Interval (f il, f ih)
100 -- | Lexicographical order, handling 'Adherence' correctly.
101 instance Ord x => Ord (Interval x) where
102 compare (Interval (il, ih)) (Interval (jl, jh)) =
103 case compare (LL il) (LL jl) of
104 EQ -> compare (HH ih) (HH jh)
107 -- | Return 'limit's of given 'Interval' as a tuple.
108 limits :: Ord x => Interval x -> (Limit x, Limit x)
109 limits (Interval t) = t
111 -- | Return an 'Interval' spanning over a single 'limit'.
112 point :: Ord x => x -> Interval x
113 point x = Interval (Limit In x, Limit In x)
115 -- | Return given 'Interval' with 'flip_limit' applied to its 'limit's.
116 flip_limits :: Ord x => Interval x -> Interval x
117 flip_limits (Interval (l, h)) = Interval (flip_limit l, flip_limit h)
119 -- | Return 'Ordering' comparing given 'Interval's according to their 'limit's.
120 compare_without_adherence :: Ord x => Limit x -> Limit x -> Ordering
121 compare_without_adherence (Limit _ x) (Limit _ y) = compare x y
125 -- * 'LT': if given value is lower than all values in given 'Interval'.
126 -- * 'EQ': if given value is into the given 'Interval'.
127 -- * 'GT': if given value is higher than all values in given 'Interval'.
128 locate :: Ord x => x -> Interval x -> Ordering
129 locate x (Interval (l, h)) =
130 case compare x (limit l) of
132 EQ | adherence l == In -> EQ
135 case compare x (limit h) of
137 EQ | adherence h == In -> EQ
141 -- | Return 'True' iif. given value is into the given 'Interval'.
142 within :: Ord x => x -> Interval x -> Bool
143 within x i = locate x i == EQ
145 -- | Return 'True' iif. every value of the first 'Interval' is into the second 'Interval'.
146 into :: Ord x => Interval x -> Interval x -> Bool
149 (Prefix , LT) -> True
150 (Suffixed, GT) -> True
151 (Include , GT) -> True
155 -- | Return 'True' iif. every value of the second 'Interval' is into the first 'Interval'.
156 onto :: Ord x => Interval x -> Interval x -> Bool
160 (<=..<=) :: Ord x => x -> x -> Maybe (Interval x)
163 LT -> Just $ Interval (Limit In x, Limit In y)
164 EQ -> Just $ Interval (Limit In x, Limit In y)
168 (<..<=) :: Ord x => x -> x -> Maybe (Interval x)
171 LT -> Just $ Interval (Limit Out x, Limit In y)
176 (<=..<) :: Ord x => x -> x -> Maybe (Interval x)
179 LT -> Just $ Interval (Limit In x, Limit Out y)
184 (<..<) :: Ord x => x -> x -> Maybe (Interval x)
187 LT -> Just $ Interval (Limit Out x, Limit Out y)
194 = Away -- ^ @-_|@ ('LT') or @|_-@ ('GT')
195 | Adjacent -- ^ @-|@ ('LT') or @|-@ ('GT')
196 | Overlap -- ^ @-+|@ ('LT') or @|+-@ ('GT')
197 | Prefix -- ^ @+|@ ('LT') or @+-@ ('GT')
198 | Suffixed -- ^ @-+@ ('LT') or @|+@ ('GT')
199 | Include -- ^ @-+-@ ('LT') or @|+|@ ('GT')
200 | Equal -- ^ @+@ ('EQ')
203 position :: Ord x => Interval x -> Interval x -> (Position, Ordering)
204 position (Interval (il, ih)) (Interval (jl, jh)) =
205 case compare (LL il) (LL jl) of
207 case compare_without_adherence ih jl of
208 LT -> Away -- PATTERN: -_|
210 case (adherence ih, adherence jl) of
211 (In , In) -> Overlap -- PATTERN: -+|
212 (Out, Out) -> Away -- PATTERN: -_|
213 _ -> Adjacent -- PATTERN: -|
215 case compare (HH ih) (HH jh) of
216 LT -> Overlap -- PATTERN: -+|
217 EQ -> Suffixed -- PATTERN: -+
218 GT -> Include -- PATTERN: -+-
220 case compare (HH ih) (HH jh) of
221 LT -> (Prefix, LT) -- PATTERN: +|
222 EQ -> (Equal , EQ) -- PATTERN: +
223 GT -> (Prefix, GT) -- PATTERN: +-
225 case compare_without_adherence il jh of
227 case compare (HH ih) (HH jh) of
228 LT -> Include -- PATTERN: |+|
229 EQ -> Suffixed -- PATTERN: |+
230 GT -> Overlap -- PATTERN: |+-
232 case (adherence il, adherence jh) of
233 (In , In) -> Overlap -- PATTERN: |+-
234 (Out, Out) -> Away -- PATTERN: |_-
235 _ -> Adjacent -- PATTERN: |-
236 GT -> Away -- PATTERN: |_-
239 -- | Return 'True' iif. 'Position' of given 'Interval's is ('Away', 'LT').
240 (..<<..) :: Ord x => Interval x -> Interval x -> Bool
241 (..<<..) i j = case position i j of
246 -- | Return 'True' iif. 'Position' of given 'Interval's is ('Away', 'GT').
247 (..>>..) :: Ord x => Interval x -> Interval x -> Bool
248 (..>>..) i j = case position i j of
253 -- | Return 'True' iif. 'Position' of given 'Interval's is ('Away', 'LT') or ('Adjacent', 'LT').
254 (..<..) :: Ord x => Interval x -> Interval x -> Bool
255 (..<..) i j = case position i j of
257 (Adjacent, LT) -> True
260 -- | Return 'True' iif. 'Position' of given 'Interval's is ('Away', 'GT') or ('Adjacent', 'GT').
261 (..>..) :: Ord x => Interval x -> Interval x -> Bool
262 (..>..) i j = case position i j of
264 (Adjacent, GT) -> True
268 -- | Return 'True' iif. 'Position' of given 'Interval's is ('Away', 'LT'), ('Adjacent', 'LT'), ('Overlap', 'LT'), ('Prefix', 'LT'), ('Suffixed', 'LT'), ('Include', 'GT'), or ('Equal', _).
269 (..<=..) :: Ord x => Interval x -> Interval x -> Bool
270 (..<=..) i j = case position i j of
272 (Adjacent, LT) -> True
273 (Overlap , LT) -> True
274 (Prefix , LT) -> True
275 (Suffixed, LT) -> True
276 (Include , GT) -> True
281 -- | Return 'True' iif. 'Position' of given 'Interval's is ('Away', 'GT'), ('Adjacent', 'GT'), ('Overlap', 'GT'), ('Prefix', 'GT'), ('Suffixed', 'GT'), ('Include', 'LT'), or ('Equal', _).
282 (..>=..) :: Ord x => Interval x -> Interval x -> Bool
283 (..>=..) i j = case position i j of
285 (Adjacent, GT) -> True
286 (Overlap , GT) -> True
287 (Prefix , GT) -> True
288 (Suffixed, GT) -> True
289 (Include , LT) -> True
295 union :: Ord x => Interval x -> Interval x -> Maybe (Interval x)
298 (Away, _) -> -- PATTERN: -_| or |_-
302 LT -> Just $ Interval (low i, high j) -- PATTERN: -|
304 GT -> Just $ Interval (low j, high i) -- PATTERN: |-
307 LT -> Just $ Interval (low i, high j) -- PATTERN: -+|
309 GT -> Just $ Interval (low j, high i) -- PATTERN: |+-
312 LT -> Just $ j -- PATTERN: +|
314 GT -> Just $ i -- PATTERN: +-
317 LT -> Just $ i -- PATTERN: -+
319 GT -> Just $ j -- PATTERN: |+
322 LT -> Just $ i -- PATTERN: -+-
324 GT -> Just $ j -- PATTERN: |+|
325 (Equal, _) -> -- PATTERN: +
328 intersection :: Ord x => Interval x -> Interval x -> Maybe (Interval x)
331 (Away, _) -> -- PATTERN: -_| or |_-
333 (Adjacent, _) -> -- PATTERN: -| or |-
337 LT -> Just $ Interval (low j, high i) -- PATTERN: -+|
339 GT -> Just $ Interval (low i, high j) -- PATTERN: |+-
342 LT -> Just $ i -- PATTERN: +|
344 GT -> Just $ j -- PATTERN: +-
347 LT -> Just $ j -- PATTERN: -+
349 GT -> Just $ i -- PATTERN: |+
352 LT -> Just $ j -- PATTERN: -+-
354 GT -> Just $ i -- PATTERN: |+|
355 (Equal, _) -> -- PATTERN: +
358 span :: Ord x => Interval x -> Interval x -> Interval x
361 ( unLL (min (LL $ low i) (LL $ low j))
362 , unHH (max (HH $ high i) (HH $ high j))
365 -- * Type 'Unlimitable'
369 | Limited { limited :: x }
371 deriving (Eq, Ord, Show)
372 instance Functor Unlimitable where
373 fmap _f Unlimited_low = Unlimited_low
374 fmap _f Unlimited_high = Unlimited_high
375 fmap f (Limited x) = Limited (f x)
376 instance Bounded (Unlimitable x) where
377 minBound = Unlimited_low
378 maxBound = Unlimited_high
379 instance Bounded (Limit (Unlimitable x)) where
380 minBound = Limit In Unlimited_low
381 maxBound = Limit In Unlimited_high
383 unlimited :: Ord x => Interval (Unlimitable x)
384 unlimited = Interval ( Limit In Unlimited_low
385 , Limit In Unlimited_high )
387 unlimit :: Ord x => Interval x -> Interval (Unlimitable x)
388 unlimit = fmap_unsafe Limited
390 (<..) :: Ord x => x -> Interval (Unlimitable x)
391 (<..) x = Interval (Limit Out (Limited x), Limit In Unlimited_high)
393 (<=..) :: Ord x => x -> Interval (Unlimitable x)
394 (<=..) x = Interval (Limit In (Limited x), Limit In Unlimited_high)
396 (..<) :: Ord x => x -> Interval (Unlimitable x)
397 (..<) x = Interval (Limit In Unlimited_low, Limit Out (Limited x))
399 (..<=) :: Ord x => x -> Interval (Unlimitable x)
400 (..<=) x = Interval (Limit In Unlimited_low, Limit In (Limited x))
404 newtype Pretty x = Pretty x
406 instance (Ord x, Show x) => Show (Pretty (Interval x)) where
409 [ case adherence (low i) of
412 , show (limit $ low i)
414 , show (limit $ high i)
415 , case adherence (high i) of
419 instance (Ord x, Show x) => Show (Pretty (Unlimitable x)) where
422 Unlimited_low -> "-oo"
424 Unlimited_high -> "+oo"