union 0.1.3 → 0.1.3.2
raw patch · 2 files changed
+16/−2 lines, 2 filesdep ~base
Dependency ranges changed: base
Files
- src/Data/Union.hs +13/−0
- union.cabal +3/−2
src/Data/Union.hs view
@@ -66,28 +66,35 @@ absurdUnion :: Union f [] -> a absurdUnion = \case{} +-- | Map over the interpretation of a union. umap :: (forall a . f a -> g a) -> Union f as -> Union g as umap f = \case This a -> This (f a) That u -> That (umap f u) +-- | A prism for the head label of a union. _This :: Prism (Union f (a : as)) (Union f (b : as)) (f a) (f b) _This = prism This (union (Left . That) Right) {-# INLINE _This #-} +-- | A prism for the tail of labels of a union. _That :: Prism (Union f (a : as)) (Union f (a : bs)) (Union f as) (Union f bs) _That = prism That (union Right (Left . This)) {-# INLINE _That #-} +-- | Membership of a label @a@ in a union with labels @as@. class i ~ RIndex a as => UElem (a :: u) (as :: List u) (i :: Nat) where {-# MINIMAL uprism | ulift, umatch #-} + -- | A prism for a value at label @a@ in the union. uprism :: Prism' (Union f as) (f a) uprism = prism' ulift umatch + -- | Inject a value at label @a@ into the union. ulift :: f a -> Union f as ulift = review uprism + -- | Match a value at label @a@ from the union. umatch :: Union f as -> Maybe (f a) umatch = preview uprism @@ -103,15 +110,19 @@ uprism = _That . uprism {-# INLINE uprism #-} +-- | Subset relation between two lists of union labels. class is ~ RImage as bs => USubset (as :: List u) (bs :: List u) is where {-# MINIMAL usubset | urelax, urestrict #-} + -- | A prism for a sub-union within a larger union. usubset :: Prism' (Union f bs) (Union f as) usubset = prism' urelax urestrict + -- | Widen a union to a larger set of labels. urelax :: Union f as -> Union f bs urelax = review usubset + -- | Narrow a union to a smaller set of labels, failing if the actual label is not in the target set @as@. urestrict :: Union f bs -> Maybe (Union f as) urestrict = preview usubset @@ -126,8 +137,10 @@ urelax = union urelax ulift urestrict ubs = This <$> umatch ubs <|> That <$> urestrict ubs +-- | A union with the trivial interpretation 'Identity', where each label @a@ carries a value of type @a@. type OpenUnion = Union Identity +-- | A prism for a value of type @a@ in an 'OpenUnion'. openUnion :: forall a as . UElem a as (RIndex a as) => Prism' (OpenUnion as) a openUnion = uprism . iso runIdentity Identity {-# INLINE openUnion #-}
union.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: union-version: 0.1.3+version: 0.1.3.2 synopsis: Extensible type-safe unions description: @@ -18,6 +18,7 @@ bug-reports: https://github.com/int-index/union/issues category: Data build-type: Simple+tested-with: GHC ==9.10.3, GHC ==9.12.4, GHC ==9.14.1 source-repository head type: git@@ -26,7 +27,7 @@ library exposed-modules: Data.Union other-modules: Data.Union.Prism- build-depends: base >=4.20 && <4.22+ build-depends: base >=4.20 && <4.23 , vinyl >= 0.14.3 && <0.15 , profunctors >=5.1 && <5.7 , tagged >=0.8 && <0.9