1 {-# LANGUAGE FlexibleInstances #-}
2 {-# LANGUAGE MultiParamTypeClasses #-}
3 {-# LANGUAGE TupleSections #-}
4 module Hcompta.Lib.Interval where
6 import qualified Data.Functor
12 { adherence :: Adherence
15 instance Functor Limit where
16 fmap f (Limit a x) = Limit a (f x)
18 data Adherence = Out | In
21 -- | Return given 'Limit' with its 'adherence' set to the opposite one.
22 flip_limit :: Limit x -> Limit x
23 flip_limit (Limit a x) = Limit (case a of { In -> Out; Out -> In }) x
25 -- ** Comparing 'Limit's
27 -- | Compare two 'low' 'Limit's.
30 instance Ord x => Ord (LL (Limit x)) where
31 compare (LL x) (LL y) =
32 case compare (limit x) (limit y) of
34 case (adherence x, adherence y) of
40 -- | Compare two 'high' 'Limit's.
43 instance Ord x => Ord (HH (Limit x)) where
44 compare (HH x) (HH y) =
45 case compare (limit x) (limit y) of
47 case (adherence x, adherence y) of
55 newtype Ord x => Interval x = Interval (Limit x, Limit x)
58 low :: Ord x => Interval x -> Limit x
59 low (Interval t) = fst t
61 high :: Ord x => Interval x -> Limit x
62 high (Interval t) = snd t
64 -- | Return 'Interval' with given 'low' then 'high' 'Limit's,
65 -- if they form a valid 'Interval'.
66 interval :: Ord x => Limit x -> Limit x -> Maybe (Interval x)
68 case compare_without_adherence x y of
69 LT -> Just $ Interval (x, y)
71 case (adherence x, adherence y) of
72 (In, In) -> Just $ Interval (x, y)
76 -- | Like 'Data.Functor.fmap', but may return 'Nothing', if mapped 'Interval' is not valid.
77 fmap :: (Ord x, Ord y) => (x -> y) -> Interval x -> Maybe (Interval y)
78 fmap f (Interval (il, ih)) = interval (Data.Functor.fmap f il) (Data.Functor.fmap f ih)
80 -- | Like 'Data.Functor.fmap', but only safe if given map preserves 'Ordering'.
81 fmap_unsafe :: (Ord x, Ord y) => (x -> y) -> Interval x -> Interval y
82 fmap_unsafe f (Interval (il, ih)) = Interval (Data.Functor.fmap f il, Data.Functor.fmap f ih)
85 -- | Like 'Data.Functor.fmap', but on 'Limit's,
86 -- and may return 'Nothing', if mapped 'Interval' is not valid.
87 fmap_limits :: (Ord x, Ord y) => (Limit x -> Limit y) -> Interval x -> Maybe (Interval y)
88 fmap_limits f (Interval (il, ih)) = interval (f il) (f ih)
90 -- | Like 'Data.Functor.fmap', but on 'Limit's
91 -- and only safe if given map preserves 'Ordering'.
92 fmap_limits_unsafe :: (Ord x, Ord y) => (Limit x -> Limit y) -> Interval x -> Interval y
93 fmap_limits_unsafe f (Interval (il, ih)) = Interval (f il, f ih)
96 -- | Lexicographical order, handling 'Adherence' correctly.
97 instance Ord x => Ord (Interval x) where
98 compare (Interval (il, ih)) (Interval (jl, jh)) =
99 case compare (LL il) (LL jl) of
100 EQ -> compare (HH ih) (HH jh)
103 -- | Return 'limit's of given 'Interval' as a tuple.
104 limits :: Ord x => Interval x -> (Limit x, Limit x)
105 limits (Interval t) = t
107 -- | Return an 'Interval' spanning over a single 'limit'.
108 point :: Ord x => x -> Interval x
109 point x = Interval (Limit In x, Limit In x)
111 -- | Return given 'Interval' with 'flip_limit' applied to its 'limit's.
112 flip_limits :: Ord x => Interval x -> Interval x
113 flip_limits (Interval (l, h)) = Interval (flip_limit l, flip_limit h)
115 -- | Return 'Ordering' comparing given 'Interval's according to their 'limit's.
116 compare_without_adherence :: Ord x => Limit x -> Limit x -> Ordering
117 compare_without_adherence (Limit _ x) (Limit _ y) = compare x y
121 -- * 'LT': if given value is lower than all values in given 'Interval'.
122 -- * 'EQ': if given value is into the given 'Interval'.
123 -- * 'GT': if given value is higher than all values in given 'Interval'.
124 locate :: Ord x => x -> Interval x -> Ordering
125 locate x (Interval (l, h)) =
126 case compare x (limit l) of
128 EQ | adherence l == In -> EQ
131 case compare x (limit h) of
133 EQ | adherence h == In -> EQ
137 -- | Return 'True' iif. given value is into the given 'Interval'.
138 within :: Ord x => x -> Interval x -> Bool
139 within x i = locate x i == EQ
141 -- | Return 'True' iif. every value of the first 'Interval' is into the second 'Interval'.
142 into :: Ord x => Interval x -> Interval x -> Bool
145 (Prefix , LT) -> True
146 (Suffixed, GT) -> True
147 (Include , GT) -> True
151 -- | Return 'True' iif. every value of the second 'Interval' is into the first 'Interval'.
152 onto :: Ord x => Interval x -> Interval x -> Bool
156 (<=..<=) :: Ord x => x -> x -> Maybe (Interval x)
159 LT -> Just $ Interval (Limit In x, Limit In y)
160 EQ -> Just $ Interval (Limit In x, Limit In y)
164 (<..<=) :: Ord x => x -> x -> Maybe (Interval x)
167 LT -> Just $ Interval (Limit Out x, Limit In y)
172 (<=..<) :: Ord x => x -> x -> Maybe (Interval x)
175 LT -> Just $ Interval (Limit In x, Limit Out y)
180 (<..<) :: Ord x => x -> x -> Maybe (Interval x)
183 LT -> Just $ Interval (Limit Out x, Limit Out y)
190 = Away -- ^ @-_|@ ('LT') or @|_-@ ('GT')
191 | Adjacent -- ^ @-|@ ('LT') or @|-@ ('GT')
192 | Overlap -- ^ @-+|@ ('LT') or @|+-@ ('GT')
193 | Prefix -- ^ @+|@ ('LT') or @+-@ ('GT')
194 | Suffixed -- ^ @-+@ ('LT') or @|+@ ('GT')
195 | Include -- ^ @-+-@ ('LT') or @|+|@ ('GT')
196 | Equal -- ^ @+@ ('EQ')
199 position :: Ord x => Interval x -> Interval x -> (Position, Ordering)
200 position (Interval (il, ih)) (Interval (jl, jh)) =
201 case compare (LL il) (LL jl) of
203 case compare_without_adherence ih jl of
204 LT -> Away -- PATTERN: -_|
206 case (adherence ih, adherence jl) of
207 (In , In) -> Overlap -- PATTERN: -+|
208 (Out, Out) -> Away -- PATTERN: -_|
209 _ -> Adjacent -- PATTERN: -|
211 case compare (HH ih) (HH jh) of
212 LT -> Overlap -- PATTERN: -+|
213 EQ -> Suffixed -- PATTERN: -+
214 GT -> Include -- PATTERN: -+-
216 case compare (HH ih) (HH jh) of
217 LT -> (Prefix, LT) -- PATTERN: +|
218 EQ -> (Equal , EQ) -- PATTERN: +
219 GT -> (Prefix, GT) -- PATTERN: +-
221 case compare_without_adherence il jh of
223 case compare (HH ih) (HH jh) of
224 LT -> Include -- PATTERN: |+|
225 EQ -> Suffixed -- PATTERN: |+
226 GT -> Overlap -- PATTERN: |+-
228 case (adherence il, adherence jh) of
229 (In , In) -> Overlap -- PATTERN: |+-
230 (Out, Out) -> Away -- PATTERN: |_-
231 _ -> Adjacent -- PATTERN: |-
232 GT -> Away -- PATTERN: |_-
235 -- | Return 'True' iif. 'Position' of given 'Interval's is ('Away', 'LT').
236 (..<<..) :: Ord x => Interval x -> Interval x -> Bool
237 (..<<..) i j = case position i j of
242 -- | Return 'True' iif. 'Position' of given 'Interval's is ('Away', 'GT').
243 (..>>..) :: Ord x => Interval x -> Interval x -> Bool
244 (..>>..) i j = case position i j of
249 -- | Return 'True' iif. 'Position' of given 'Interval's is ('Away', 'LT') or ('Adjacent', 'LT').
250 (..<..) :: Ord x => Interval x -> Interval x -> Bool
251 (..<..) i j = case position i j of
253 (Adjacent, LT) -> True
256 -- | Return 'True' iif. 'Position' of given 'Interval's is ('Away', 'GT') or ('Adjacent', 'GT').
257 (..>..) :: Ord x => Interval x -> Interval x -> Bool
258 (..>..) i j = case position i j of
260 (Adjacent, GT) -> True
264 -- | Return 'True' iif. 'Position' of given 'Interval's is ('Away', 'LT'), ('Adjacent', 'LT'), ('Overlap', 'LT'), ('Prefix', 'LT'), ('Suffixed', 'LT'), ('Include', 'GT'), or ('Equal', _).
265 (..<=..) :: Ord x => Interval x -> Interval x -> Bool
266 (..<=..) i j = case position i j of
268 (Adjacent, LT) -> True
269 (Overlap , LT) -> True
270 (Prefix , LT) -> True
271 (Suffixed, LT) -> True
272 (Include , GT) -> True
277 -- | Return 'True' iif. 'Position' of given 'Interval's is ('Away', 'GT'), ('Adjacent', 'GT'), ('Overlap', 'GT'), ('Prefix', 'GT'), ('Suffixed', 'GT'), ('Include', 'LT'), or ('Equal', _).
278 (..>=..) :: Ord x => Interval x -> Interval x -> Bool
279 (..>=..) i j = case position i j of
281 (Adjacent, GT) -> True
282 (Overlap , GT) -> True
283 (Prefix , GT) -> True
284 (Suffixed, GT) -> True
285 (Include , LT) -> True
291 union :: Ord x => Interval x -> Interval x -> Maybe (Interval x)
294 (Away, _) -> -- PATTERN: -_| or |_-
298 LT -> Just $ Interval (low i, high j) -- PATTERN: -|
300 GT -> Just $ Interval (low j, high i) -- PATTERN: |-
303 LT -> Just $ Interval (low i, high j) -- PATTERN: -+|
305 GT -> Just $ Interval (low j, high i) -- PATTERN: |+-
308 LT -> Just $ j -- PATTERN: +|
310 GT -> Just $ i -- PATTERN: +-
313 LT -> Just $ i -- PATTERN: -+
315 GT -> Just $ j -- PATTERN: |+
318 LT -> Just $ i -- PATTERN: -+-
320 GT -> Just $ j -- PATTERN: |+|
321 (Equal, _) -> -- PATTERN: +
324 intersection :: Ord x => Interval x -> Interval x -> Maybe (Interval x)
327 (Away, _) -> -- PATTERN: -_| or |_-
329 (Adjacent, _) -> -- PATTERN: -| or |-
333 LT -> Just $ Interval (low j, high i) -- PATTERN: -+|
335 GT -> Just $ Interval (low i, high j) -- PATTERN: |+-
338 LT -> Just $ i -- PATTERN: +|
340 GT -> Just $ j -- PATTERN: +-
343 LT -> Just $ j -- PATTERN: -+
345 GT -> Just $ i -- PATTERN: |+
348 LT -> Just $ j -- PATTERN: -+-
350 GT -> Just $ i -- PATTERN: |+|
351 (Equal, _) -> -- PATTERN: +
354 -- * Type 'Unlimitable'
358 | Limited { limited :: x }
360 deriving (Eq, Ord, Show)
361 instance Functor Unlimitable where
362 fmap _f Unlimited_low = Unlimited_low
363 fmap _f Unlimited_high = Unlimited_high
364 fmap f (Limited x) = Limited (f x)
365 instance Bounded (Unlimitable x) where
366 minBound = Unlimited_low
367 maxBound = Unlimited_high
368 instance Bounded (Limit (Unlimitable x)) where
369 minBound = Limit In Unlimited_low
370 maxBound = Limit In Unlimited_high
372 unlimited :: Ord x => Interval (Unlimitable x)
373 unlimited = Interval ( Limit In Unlimited_low
374 , Limit In Unlimited_high )
376 unlimit :: Ord x => Interval x -> Interval (Unlimitable x)
377 unlimit = fmap_unsafe Limited
379 (<..) :: Ord x => x -> Interval (Unlimitable x)
380 (<..) x = Interval (Limit Out (Limited x), Limit In Unlimited_high)
382 (<=..) :: Ord x => x -> Interval (Unlimitable x)
383 (<=..) x = Interval (Limit In (Limited x), Limit In Unlimited_high)
385 (..<) :: Ord x => x -> Interval (Unlimitable x)
386 (..<) x = Interval (Limit In Unlimited_low, Limit Out (Limited x))
388 (..<=) :: Ord x => x -> Interval (Unlimitable x)
389 (..<=) x = Interval (Limit In Unlimited_low, Limit In (Limited x))
393 newtype Pretty x = Pretty x
395 instance (Ord x, Show x) => Show (Pretty (Interval x)) where
398 [ case adherence (low i) of
401 , show (limit $ low i)
403 , show (limit $ high i)
404 , case adherence (high i) of
408 instance (Ord x, Show x) => Show (Pretty (Unlimitable x)) where
411 Unlimited_low -> "-oo"
413 Unlimited_high -> "+oo"