From 8934a10fb289ea0c09891bdd7a409b8dd1152256 Mon Sep 17 00:00:00 2001 From: Eduardo Julian Date: Sat, 26 May 2018 19:49:45 -0400 Subject: - Added filter/partition functionality to refinement types. --- stdlib/source/lux/type/refinement.lux | 36 ++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/stdlib/source/lux/type/refinement.lux b/stdlib/source/lux/type/refinement.lux index 051166eb9..7431c93fe 100644 --- a/stdlib/source/lux/type/refinement.lux +++ b/stdlib/source/lux/type/refinement.lux @@ -16,10 +16,13 @@ {#value t #predicate (Pred t)} + (type: #export (Refiner t r) + (-> t (Maybe (Ref t r)))) + (def: #export (refinement predicate) (All [t] (Ex [r] - (-> (Pred t) (-> t (Maybe (Ref t r)))))) + (-> (Pred t) (Refiner t r)))) (function (_ un-refined) (if (predicate un-refined) (#.Some (@abstraction {#value un-refined @@ -48,6 +51,37 @@ #.None)))) ) +(def: #export (filter refiner values) + (All [t r] (-> (Refiner t r) (List t) (List (Ref t r)))) + (case values + #.Nil + #.Nil + + (#.Cons head tail) + (case (refiner head) + (#.Some refined) + (#.Cons refined (filter refiner tail)) + + #.None + (filter refiner tail)))) + +(def: #export (partition refiner values) + (All [t r] (-> (Refiner t r) (List t) [(List (Ref t r)) (List t)])) + (case values + #.Nil + [#.Nil #.Nil] + + (#.Cons head tail) + (let [[yes no] (partition refiner tail)] + (case (refiner head) + (#.Some refined) + [(#.Cons refined yes) + no] + + #.None + [yes + (#.Cons head no)])))) + (def: (refinement-type constructor-type) (-> Type (Error Type)) (<| (poly.run constructor-type) -- cgit v1.2.3