aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/abstract/predicate.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/library/lux/abstract/predicate.lux')
-rw-r--r--stdlib/source/library/lux/abstract/predicate.lux61
1 files changed, 61 insertions, 0 deletions
diff --git a/stdlib/source/library/lux/abstract/predicate.lux b/stdlib/source/library/lux/abstract/predicate.lux
new file mode 100644
index 000000000..205ccc316
--- /dev/null
+++ b/stdlib/source/library/lux/abstract/predicate.lux
@@ -0,0 +1,61 @@
+(.module:
+ [library
+ [lux #*
+ [control
+ ["." function]]]]
+ [//
+ [monoid (#+ Monoid)]
+ [functor
+ ["." contravariant]]])
+
+(type: #export (Predicate a)
+ (-> a Bit))
+
+(template [<identity_name> <identity_value> <composition_name> <composition>]
+ [(def: #export <identity_name>
+ Predicate
+ (function.constant <identity_value>))
+
+ (def: #export (<composition_name> left right)
+ (All [a] (-> (Predicate a) (Predicate a) (Predicate a)))
+ (function (_ value)
+ (<composition> (left value)
+ (right value))))]
+
+ [none #0 unite or]
+ [all #1 intersect and]
+ )
+
+(template [<name> <identity> <composition>]
+ [(implementation: #export <name>
+ (All [a] (Monoid (Predicate a)))
+
+ (def: identity <identity>)
+ (def: compose <composition>))]
+
+ [union ..none ..unite]
+ [intersection ..all ..intersect]
+ )
+
+(def: #export (complement predicate)
+ (All [a] (-> (Predicate a) (Predicate a)))
+ (|>> predicate not))
+
+(def: #export (difference sub base)
+ (All [a] (-> (Predicate a) (Predicate a) (Predicate a)))
+ (function (_ value)
+ (and (base value)
+ (not (sub value)))))
+
+(def: #export (rec predicate)
+ (All [a]
+ (-> (-> (Predicate a) (Predicate a))
+ (Predicate a)))
+ (function (recur input)
+ (predicate recur input)))
+
+(implementation: #export functor
+ (contravariant.Functor Predicate)
+
+ (def: (map f fb)
+ (|>> f fb)))