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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
module Interval
( Interval
, mkInterval, intervalToInfinityFrom
, integersInInterval
, DisjointIntervalSet
, emptyIntervalSet, extendIntervalSet, deleteFromIntervalSet
, subIntervals
)
where
import Panic
#include "HsVersions.h"
------------------------------------------------------------------
-- Intervals and Sets of Intervals
------------------------------------------------------------------
-- This module implements intervals over the integer line and sets of
-- disjoint intervals.
{-
An interval $[x,y)$ over ordered points represents a half-open
interval of points: $\{ p \mid x \leq p < y \}$. Half-open intervals
have the nice property $[x,y) \cup [y,z) = [x,z)$. Non-empty
intervals can precede or overlap each other; an empty interval never
overlaps or precedes any other. The set of ordered elements contains
a unique element $\mathit{zero}$; using it in any interval is an
\emph{unchecked} run-time error.
-}
data Interval = Interval { i_min :: Int, i_lim :: Int }
-- width == i_lim - i_min >= 0
type Width = Int
mkInterval :: Int -> Width -> Interval
mkInterval min w = ASSERT (w>=0) Interval min (min+w)
intervalToInfinityFrom :: Int -> Interval
intervalToInfinityFrom min = Interval min maxBound
integersInInterval :: Interval -> [Int]
integersInInterval (Interval min lim) = gen min lim
where gen min lim | min >= lim = []
| otherwise = min : gen (min+1) lim
precedes, overlaps, adjoins, contains :: Interval -> Interval -> Bool
precedes (Interval m l) (Interval m' l') = l <= m' || l' <= m
overlaps i i' = not (i `precedes` i' || i' `precedes` i)
adjoins (Interval _ l) (Interval m _) = l == m
contains (Interval m l) (Interval m' l') = m <= m' && l >= l'
merge :: Interval -> Interval -> Interval
merge _i@(Interval m _) _i'@(Interval _ l) = {- ASSERT (adjoins i i') -} (Interval m l)
----------
newtype DisjointIntervalSet = Intervals [Interval]
-- invariants: * No two intervals overlap
-- * Adjacent intervals have a gap between
-- * Intervals are sorted by min element
emptyIntervalSet :: DisjointIntervalSet
emptyIntervalSet = Intervals []
extendIntervalSet :: DisjointIntervalSet -> Interval -> DisjointIntervalSet
extendIntervalSet (Intervals l) i = Intervals (insert [] i l)
where insert :: [Interval] -> Interval -> [Interval] -> [Interval]
-- precondition: in 'insert prev' i l', every element of prev'
-- precedes and does not adjoin i
insert prev' i [] = rev_app prev' [i]
insert prev' i (i':is) =
if i `precedes` i' then
if i `adjoins` i' then
insert prev' (merge i i') is
else
rev_app prev' (i : i' : is)
else if i' `precedes` i then
if i' `adjoins` i then
insert prev' (merge i' i) is
else
insert (i' : prev') i is
else
panic "overlapping intervals"
deleteFromIntervalSet :: DisjointIntervalSet -> Interval -> DisjointIntervalSet
deleteFromIntervalSet (Intervals l) i = Intervals (rm [] i l)
where rm :: [Interval] -> Interval -> [Interval] -> [Interval]
-- precondition: in 'rm prev' i l', every element of prev'
-- precedes and does not adjoin i
rm _ _ [] = panic "removed interval not present in set"
rm prev' i (i':is) =
if i `precedes` i' then
panic "removed interval not present in set"
else if i' `precedes` i then
rm (i' : prev') i is
else
-- remove i from i', leaving 0, 1, or 2 leftovers
undefined {-
ASSERTX (i' `contains` i)
let (Interval m l, Interval m' l'
panic "overlapping intervals"
-}
subIntervals :: DisjointIntervalSet -> Width -> [Interval]
subIntervals = undefined
rev_app :: [a] -> [a] -> [a]
rev_app [] xs = xs
rev_app (y:ys) xs = rev_app ys (y:xs)
_unused :: FS.FastString
_unused = undefined i_min i_lim overlaps contains
|