aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/abstract/interval.lux
blob: 131d180175ea325e0b54ff954a66a8b64ac40683 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
... https://en.wikipedia.org/wiki/Interval_(mathematics)
(.using
 [library
  [lux "*"]]
 [//
  [equivalence {"+" Equivalence}]
  ["[0]" order]
  [enum {"+" Enum}]])

(type: .public (Interval a)
  (Interface
   (is (Enum a)
       enum)

   (is a
       bottom)

   (is a
       top)))

(def: .public (between enum bottom top)
  (All (_ a) (-> (Enum a) a a (Interval a)))
  (implementation
   (def: enum enum)
   (def: bottom bottom)
   (def: top top)))

(def: .public (singleton enum elem)
  (All (_ a) (-> (Enum a) a (Interval a)))
  (implementation
   (def: enum enum)
   (def: bottom elem)
   (def: top elem)))

(template [<name> <comp>]
  [(def: .public (<name> interval)
     (All (_ a) (-> (Interval a) Bit))
     (let [(open ",#[0]") interval]
       (<comp> ,#bottom ,#top)))]

  [inner?     (order.> ,#order)]
  [outer?     ,#<]
  [singleton? ,#=]
  )

(def: .public (within? interval elem)
  (All (_ a) (-> (Interval a) a Bit))
  (let [(open ",#[0]") interval]
    (cond (inner? interval)
          (and (order.>= ,#order ,#bottom elem)
               (order.<= ,#order ,#top elem))
          
          (outer? interval)
          (or (order.>= ,#order ,#bottom elem)
              (order.<= ,#order ,#top elem))
          
          ... singleton
          (and (,#= ,#bottom elem)
               (,#= ,#top elem)))))

(template [<name> <limit>]
  [(def: .public (<name> elem interval)
     (All (_ a) (-> a (Interval a) Bit))
     (let [(open "[0]") interval]
       (= <limit> elem)))]

  [starts_with? bottom]
  [ends_with?   top]
  )

(def: .public (borders? interval elem)
  (All (_ a) (-> (Interval a) a Bit))
  (or (starts_with? elem interval)
      (ends_with? elem interval)))

(implementation: .public (union left right)
  (All (_ a) (-> (Interval a) (Interval a) (Interval a)))

  (def: enum (the enum right))
  (def: bottom (order.min (# right order) (# left bottom) (# right bottom)))
  (def: top (order.max (# right order) (# left top) (# right top))))

(implementation: .public (intersection left right)
  (All (_ a) (-> (Interval a) (Interval a) (Interval a)))

  (def: enum (the enum right))
  (def: bottom (order.max (# right order) (# left bottom) (# right bottom)))
  (def: top (order.min (# right order) (# left top) (# right top))))

(implementation: .public (complement interval)
  (All (_ a) (-> (Interval a) (Interval a)))

  (def: enum (the enum interval))
  (def: bottom (# interval succ (# interval top)))
  (def: top (# interval pred (# interval bottom))))

(def: .public (precedes? reference sample)
  (All (_ a) (-> (Interval a) (Interval a) Bit))
  (let [(open "[0]") reference
        limit (# reference bottom)]
    (and (< limit (# sample bottom))
         (< limit (# sample top)))))

(def: .public (succeeds? reference sample)
  (All (_ a) (-> (Interval a) (Interval a) Bit))
  (precedes? sample reference))

(template [<name> <comp>]
  [(def: .public (<name> reference sample)
     (All (_ a) (-> a (Interval a) Bit))
     (let [(open ",#[0]") sample]
       (and (<comp> reference ,#bottom)
            (<comp> reference ,#top))))]

  [before? ,#<]
  [after?  (order.> ,#order)]
  )

(def: .public (meets? reference sample)
  (All (_ a) (-> (Interval a) (Interval a) Bit))
  (let [(open ",#[0]") reference
        limit (# reference bottom)]
    (and (,#= limit (# sample top))
         (order.<= ,#order limit (# sample bottom)))))

(def: .public (touches? reference sample)
  (All (_ a) (-> (Interval a) (Interval a) Bit))
  (or (meets? reference sample)
      (meets? sample reference)))

(template [<name> <eq_side> <ineq> <ineq_side>]
  [(def: .public (<name> reference sample)
     (All (_ a) (-> (Interval a) (Interval a) Bit))
     (let [(open ",#[0]") reference]
       (and (,#= (# reference <eq_side>)
                 (# sample <eq_side>))
            (<ineq> ,#order
                    (# reference <ineq_side>)
                    (# sample <ineq_side>)))))]

  [starts?   ,#bottom order.<= ,#top]
  [finishes? ,#top    order.>= ,#bottom]
  )

(implementation: .public equivalence
  (All (_ a) (Equivalence (Interval a)))
  
  (def: (= reference sample)
    (let [(open ",#[0]") reference]
      (and (,#= ,#bottom (# sample bottom))
           (,#= ,#top (# sample top))))))

(def: .public (nested? reference sample)
  (All (_ a) (-> (Interval a) (Interval a) Bit))
  (cond (or (singleton? sample)
            (and (inner? reference) (inner? sample))
            (and (outer? reference) (outer? sample)))
        (let [(open ",#[0]") reference]
          (and (order.>= ,#order (# reference bottom) (# sample bottom))
               (order.<= ,#order (# reference top) (# sample top))))

        (or (singleton? reference)
            (and (inner? reference) (outer? sample)))
        #0

        ... (and (outer? reference) (inner? sample))
        (let [(open ",#[0]") reference]
          (or (and (order.>= ,#order (# reference bottom) (# sample bottom))
                   (order.> ,#order (# reference bottom) (# sample top)))
              (and (,#< (# reference top) (# sample bottom))
                   (order.<= ,#order (# reference top) (# sample top)))))
        ))

(def: .public (overlaps? reference sample)
  (All (_ a) (-> (Interval a) (Interval a) Bit))
  (let [(open ",#[0]") reference]
    (and (not (# ..equivalence = reference sample))
         (cond (singleton? sample)
               #0

               (singleton? reference)
               (nested? sample reference)

               (or (and (inner? sample) (outer? reference))
                   (and (outer? sample) (inner? reference)))
               (or (order.>= ,#order (# reference bottom) (# sample top))
                   (order.<= ,#order (# reference top) (# sample bottom)))

               ... both inner
               (inner? sample)
               (inner? (intersection reference sample))

               ... both outer
               (not (nested? reference sample))
               ))))