Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.0.3

- Migrated more documentation (EADT, EGADT)

## 1.0.2

- Migrated documentation from the old independent user manual into Haddocks
Expand Down
29 changes: 29 additions & 0 deletions src/lib/Data/Variant.hs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,35 @@ Appending and prepending are very cheap operations: appending just messes with
types and performs nothing at runtime; prepending only increases the tag value
at runtime by a constant number.

The 'Concat' type family lets us write generic functions that extend an input
variant with a fixed set of additional types. For instance, here is a function
that turns specific 'Int' values into dedicated error constructors and forwards
everything else by appending the new error types to the input variant type:

> data Error0 = Error0 deriving Show
> data Error1 = Error1 deriving Show
>
> checkErr ::
> ( Int :< is
> , os ~ Concat is [Error0, Error1]
> , Error0 :< os
> , Error1 :< os
> ) => V is -> V os
> checkErr = \case
> V (0 :: Int) -> V Error0
> V (1 :: Int) -> V Error1
> v -> appendVariant @[Error0, Error1] v
>
> > checkErr (V @Int 0 :: V [Float,Int])
> V @Error0 Error0
>
> > checkErr (V @Float 5.0 :: V [Float,Int])
> V @Float 5.0
>
> > :t checkErr (V @Float 5.0 :: V [Float,Int,String,Double])
> checkErr (V @Float 5.0 :: V [Float,Int,String,Double])
> :: V [Float, Int, String, Double, Error0, Error1]

=== Variant lifting (extending and reordering)

We can extend and reorder the value types of a variant with 'liftVariant':
Expand Down
Loading
Loading