From 15ff808a5ddd2d2c9e21774c1147ff82e015a498 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Mon, 27 Mar 2017 23:07:24 -0400 Subject: - Expanded the lux/control/interval module. - Implemented Enum. --- stdlib/source/lux/control/interval.lux | 187 ++++++++++++++++++++++++++++++--- stdlib/source/lux/data/number.lux | 15 +-- 2 files changed, 181 insertions(+), 21 deletions(-) (limited to 'stdlib/source') diff --git a/stdlib/source/lux/control/interval.lux b/stdlib/source/lux/control/interval.lux index 7071c49ef..1b197840b 100644 --- a/stdlib/source/lux/control/interval.lux +++ b/stdlib/source/lux/control/interval.lux @@ -1,27 +1,184 @@ (;module: lux - (lux (control ord))) + (lux (control eq + [ord] + [enum #+ Enum]))) ## Signatures (sig: #export (Interval a) {#;doc "A representation of top and bottom boundaries for an ordered type."} - (: (Ord a) - ord) + (: (Enum a) + enum) (: a - top) + bottom) (: a - bottom)) + top)) -(def: #export (within? Interval elem) +(def: #export (between Enum bottom top) + (All [a] (-> (Enum a) a a (Interval a))) + (struct (def: enum Enum) + (def: bottom bottom) + (def: top top))) + +(def: #export (singleton Enum elem) + (All [a] (-> (Enum a) a (Interval a))) + (struct (def: enum Enum) + (def: bottom elem) + (def: top elem))) + +(do-template [ ] + [(def: #export ( interval) + (All [a] (-> (Interval a) Bool)) + (let [(^open) interval] + ( bottom top)))] + + [inner? >] + [outer? <] + [singleton? =] + ) + +(def: #export (within? interval elem) + (All [a] (-> (Interval a) a Bool)) + (let [(^open) interval] + (cond (inner? interval) + (and (>= bottom elem) + (<= top elem)) + + (outer? interval) + (or (>= bottom elem) + (<= top elem)) + + ## singleton + (and (= bottom elem) + (= top elem))))) + +(do-template [ ] + [(def: #export ( elem interval) + (All [a] (-> a (Interval a) Bool)) + (let [(^open) interval] + (= elem)))] + + [starts-with? bottom] + [ends-with? top] + ) + +(def: #export (borders? interval elem) (All [a] (-> (Interval a) a Bool)) - (let [(^open) Interval] - (if (>= bottom top) - ## Inside order - (and (>= bottom elem) - (<= top elem)) - ## Outside order - (and (<= bottom elem) - (>= top elem)) - ))) + (or (starts-with? elem interval) + (ends-with? elem interval))) + +(def: #export (union left right) + (All [a] (-> (Interval a) (Interval a) (Interval a))) + (struct (def: enum (get@ #enum right)) + (def: bottom (ord;min (get@ [#enum #enum;ord] right) (:: left bottom) (:: right bottom))) + (def: top (ord;max (get@ [#enum #enum;ord] right) (:: left top) (:: right top))))) + +(def: #export (intersection left right) + (All [a] (-> (Interval a) (Interval a) (Interval a))) + (struct (def: enum (get@ #enum right)) + (def: bottom (ord;max (get@ [#enum #enum;ord] right) (:: left bottom) (:: right bottom))) + (def: top (ord;min (get@ [#enum #enum;ord] right) (:: left top) (:: right top))))) + +(def: #export (complement interval) + (All [a] (-> (Interval a) (Interval a))) + (let [(^open) interval] + (struct (def: enum (get@ #enum interval)) + (def: bottom (succ top)) + (def: top (pred bottom))))) + +(def: #export (precedes? reference sample) + (All [a] (-> (Interval a) (Interval a) Bool)) + (let [(^open) reference + limit (:: reference bottom)] + (and (< limit (:: sample bottom)) + (< limit (:: sample top))))) + +(def: #export (succeeds? reference sample) + (All [a] (-> (Interval a) (Interval a) Bool)) + (precedes? sample reference)) + +(def: #export (meets? reference sample) + (All [a] (-> (Interval a) (Interval a) Bool)) + (let [(^open) reference + limit (:: reference bottom)] + (and (<= limit (:: sample bottom)) + (= limit (:: sample top))))) + +(def: #export (touches? reference sample) + (All [a] (-> (Interval a) (Interval a) Bool)) + (or (meets? reference sample) + (meets? sample reference))) + +(do-template [ ] + [(def: #export ( reference sample) + (All [a] (-> (Interval a) (Interval a) Bool)) + (let [(^open) reference] + (and (= (:: reference ) (:: sample )) + ( (:: reference ) (:: sample )))))] + + [starts? bottom <= top] + [finishes? top >= bottom] + ) + +(do-template [ ] + [(def: #export ( reference sample) + (All [a] (-> a (Interval a) Bool)) + (let [(^open) sample] + (and ( reference bottom) + ( reference top))))] + + [before? <] + [after? >] + ) + +(struct: #export Eq (All [a] (Eq (Interval a))) + (def: (= reference sample) + (let [(^open) reference] + (and (= bottom (:: sample bottom)) + (= top (:: sample top)))))) + +(def: #export (nested? reference sample) + (All [a] (-> (Interval a) (Interval a) Bool)) + (cond (or (singleton? sample) + (and (inner? reference) (inner? sample)) + (and (outer? reference) (outer? sample))) + (let [(^open) reference] + (and (>= (:: reference bottom) (:: sample bottom)) + (<= (:: reference top) (:: sample top)))) + + (or (singleton? reference) + (and (inner? reference) (outer? sample))) + false + + ## (and (outer? reference) (inner? sample)) + (let [(^open) reference] + (or (and (>= (:: reference bottom) (:: sample bottom)) + (> (:: reference bottom) (:: sample top))) + (and (< (:: reference top) (:: sample bottom)) + (<= (:: reference top) (:: sample top))))) + )) + +(def: #export (overlaps? reference sample) + (All [a] (-> (Interval a) (Interval a) Bool)) + (let [(^open) reference] + (cond (singleton? sample) + false + + (singleton? reference) + (and (>= (:: sample bottom) (:: reference bottom)) + (<= (:: sample top) (:: reference top))) + + (or (and (inner? sample) (outer? reference)) + (and (outer? sample) (inner? reference))) + (or (>= (:: reference bottom) (:: sample top)) + (<= (:: reference top) (:: sample bottom))) + + (inner? sample) + (and (not (:: Eq = reference sample)) + (inner? (intersection reference sample))) + + ## (outer? sample) + (not (:: Eq = reference sample)) + ))) diff --git a/stdlib/source/lux/data/number.lux b/stdlib/source/lux/data/number.lux index 1a29fc5b6..8c1c021b4 100644 --- a/stdlib/source/lux/data/number.lux +++ b/stdlib/source/lux/data/number.lux @@ -92,18 +92,21 @@ [Nat Ord n.inc n.dec] [Int Ord i.inc i.dec] + [Real Ord (r.+ (_lux_proc [ "real" "smallest-value"] [])) (r.- (_lux_proc [ "real" "smallest-value"] []))] + [Deg Ord (d.+ (_lux_proc [ "deg" "min-value"] [])) (d.- (_lux_proc [ "deg" "min-value"] []))] ) -(do-template [ ] +(do-template [ ] [(struct: #export _ (Interval ) - (def: ord ) + (def: enum ) (def: top ) (def: bottom ))] - [ Nat Ord (_lux_proc [ "nat" "max-value"] []) (_lux_proc [ "nat" "min-value"] [])] - [ Int Ord (_lux_proc [ "int" "max-value"] []) (_lux_proc [ "int" "min-value"] [])] - [Real Ord (_lux_proc ["real" "max-value"] []) (_lux_proc ["real" "min-value"] [])] - [ Deg Ord (_lux_proc [ "deg" "max-value"] []) (_lux_proc [ "deg" "min-value"] [])]) + [ Nat Enum (_lux_proc [ "nat" "max-value"] []) (_lux_proc [ "nat" "min-value"] [])] + [ Int Enum (_lux_proc [ "int" "max-value"] []) (_lux_proc [ "int" "min-value"] [])] + [Real Enum (_lux_proc ["real" "max-value"] []) (_lux_proc ["real" "min-value"] [])] + [ Deg Enum (_lux_proc [ "deg" "max-value"] []) (_lux_proc [ "deg" "min-value"] [])] + ) (do-template [ ] [(struct: #export (Monoid ) -- cgit v1.2.3