... https://en.wikipedia.org/wiki/Interval_(mathematics) (.require [library [lux (.except)]] [// [equivalence (.only Equivalence)] [enum (.only Enum)] ["[0]" order]]) (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))) (with_template [ ] [(def .public ( interval) (All (_ a) (-> (Interval a) Bit)) (let [(open ",#[0]") interval] ( ,#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))))) (with_template [ ] [(def .public ( elem interval) (All (_ a) (-> a (Interval a) Bit)) (let [(open "[0]") interval] (= 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))) (def .public (union left right) (All (_ a) (-> (Interval a) (Interval a) (Interval a))) (implementation (def enum (the enum right)) (def bottom (order.min (at right order) (at left bottom) (at right bottom))) (def top (order.max (at right order) (at left top) (at right top))))) (def .public (intersection left right) (All (_ a) (-> (Interval a) (Interval a) (Interval a))) (implementation (def enum (the enum right)) (def bottom (order.max (at right order) (at left bottom) (at right bottom))) (def top (order.min (at right order) (at left top) (at right top))))) (def .public (complement interval) (All (_ a) (-> (Interval a) (Interval a))) (implementation (def enum (the enum interval)) (def bottom (at interval succ (at interval top))) (def top (at interval pred (at interval bottom))))) (def .public (precedes? reference sample) (All (_ a) (-> (Interval a) (Interval a) Bit)) (let [(open "[0]") reference limit (at reference bottom)] (and (< limit (at sample bottom)) (< limit (at sample top))))) (def .public (succeeds? reference sample) (All (_ a) (-> (Interval a) (Interval a) Bit)) (precedes? sample reference)) (with_template [ ] [(def .public ( reference sample) (All (_ a) (-> a (Interval a) Bit)) (let [(open ",#[0]") sample] (and ( reference ,#bottom) ( reference ,#top))))] [before? ,#<] [after? (order.> ,#order)] ) (def .public (meets? reference sample) (All (_ a) (-> (Interval a) (Interval a) Bit)) (let [(open ",#[0]") reference limit (at reference bottom)] (and (,#= limit (at sample top)) (order.<= ,#order limit (at sample bottom))))) (def .public (touches? reference sample) (All (_ a) (-> (Interval a) (Interval a) Bit)) (or (meets? reference sample) (meets? sample reference))) (with_template [ ] [(def .public ( reference sample) (All (_ a) (-> (Interval a) (Interval a) Bit)) (let [(open ",#[0]") reference] (and (,#= (at reference ) (at sample )) ( ,#order (at reference ) (at sample )))))] [starts? ,#bottom order.<= ,#top] [finishes? ,#top order.>= ,#bottom] ) (def .public equivalence (All (_ a) (Equivalence (Interval a))) (implementation (def (= reference sample) (let [(open ",#[0]") reference] (and (,#= ,#bottom (at sample bottom)) (,#= ,#top (at 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 (at reference bottom) (at sample bottom)) (order.<= ,#order (at reference top) (at sample top)))) (or (singleton? reference) (and (inner? reference) (outer? sample))) false ... (and (outer? reference) (inner? sample)) (let [(open ",#[0]") reference] (or (and (order.>= ,#order (at reference bottom) (at sample bottom)) (order.> ,#order (at reference bottom) (at sample top))) (and (,#< (at reference top) (at sample bottom)) (order.<= ,#order (at reference top) (at sample top))))) )) (def .public (overlaps? reference sample) (All (_ a) (-> (Interval a) (Interval a) Bit)) (let [(open ",#[0]") reference] (and (not (at ..equivalence = reference sample)) (cond (singleton? sample) false (singleton? reference) (nested? sample reference) (or (and (inner? sample) (outer? reference)) (and (outer? sample) (inner? reference))) (or (order.>= ,#order (at reference bottom) (at sample top)) (order.<= ,#order (at reference top) (at sample bottom))) ... both inner (inner? sample) (inner? (intersection reference sample)) ... both outer (not (nested? reference sample)) ))))