@@ -78,6 +78,7 @@ module Dhall
78
78
, inputConstructorWith
79
79
, inputConstructor
80
80
, inputUnion
81
+ , (>|<)
81
82
82
83
-- * Miscellaneous
83
84
, rawInput
@@ -1547,26 +1548,27 @@ inputRecord (RecordInputType inputTypeRecord) = InputType makeRecordLit recordTy
1547
1548
1548
1549
> injectStatus :: InputType Status
1549
1550
> injectStatus =
1550
- > inputUnion
1551
- > ( inputConstructorWith "Queued" inject (\case Queued n -> Just n; _ -> Nothing)
1552
- > <> inputConstructorWith "Result" inject (\case Result t -> Just t; _ -> Nothing)
1553
- > <> inputConstructorWith "Errored" inject (\case Errored e -> Just e; _ -> Nothing)
1554
- > )
1551
+ > adapt
1552
+ > >$< inputConstructorWith "Queued" inject
1553
+ > >|< inputConstructorWith "Result" inject
1554
+ > >|< inputConstructorWith "Errored" inject
1555
+ > where
1556
+ > adapt (Queued n) = Left (Left n)
1557
+ > adapt (Result t) = Left (Right t)
1558
+ > adapt (Errored e) = Right e
1555
1559
1556
1560
Or, since we are simply using the `Inject` instance to inject each branch, we could write
1557
1561
1558
1562
> injectStatus :: InputType Status
1559
1563
> injectStatus =
1560
- > inputUnion
1561
- > ( inputConstructor "Queued" (\case Queued n -> Just n; _ -> Nothing)
1562
- > <> inputConstructor "Result" (\case Result t -> Just t; _ -> Nothing)
1563
- > <> inputConstructor "Errored" (\case Errored e -> Just e; _ -> Nothing)
1564
- > )
1565
-
1566
- Note that the resulting 'InputType' is __not total__ unless we
1567
- appropriately aggregate each possibility. If we forgot to handle
1568
- a branch, the 'InputType' will throw a runtime exception when used on
1569
- an unhandled branch.
1564
+ > adapt
1565
+ > >$< inputConstructor "Queued"
1566
+ > >|< inputConstructor "Result"
1567
+ > >|< inputConstructor "Errored"
1568
+ > where
1569
+ > adapt (Queued n) = Left (Left n)
1570
+ > adapt (Result t) = Left (Right t)
1571
+ > adapt (Errored e) = Right e
1570
1572
-}
1571
1573
newtype UnionInputType a =
1572
1574
UnionInputType
@@ -1577,53 +1579,54 @@ newtype UnionInputType a =
1577
1579
( Expr Src X )
1578
1580
)
1579
1581
)
1580
- ( Op (Data.Monoid. First ( Expr Src X ) ) )
1582
+ ( Op (Expr Src X ) )
1581
1583
a
1582
1584
)
1583
1585
deriving (Contravariant )
1584
1586
1585
- -- As of base 4.12, we can use :*: to derive this instance automatically
1586
- instance Semigroup (UnionInputType a ) where
1587
- UnionInputType (Data.Functor.Product. Pair mx fx)
1588
- <> UnionInputType (Data.Functor.Product. Pair my fy)
1589
- = UnionInputType (Data.Functor.Product. Pair (mx <> my) (fx <> fy))
1587
+ -- | Combines two 'UnionInputType' values. See 'UnionInputType' for usage
1588
+ -- notes.
1589
+ --
1590
+ -- Ideally, this matches 'Data.Functor.Contravariant.Divisible.chosen';
1591
+ -- however, this allows 'UnionInputType' to not need a 'Divisible' instance
1592
+ -- itself (since no instance is possible).
1593
+ (>|<) :: UnionInputType a -> UnionInputType b -> UnionInputType (Either a b )
1594
+ UnionInputType (Data.Functor.Product. Pair (Control.Applicative. Const mx) (Op fx))
1595
+ >|< UnionInputType (Data.Functor.Product. Pair (Control.Applicative. Const my) (Op fy)) =
1596
+ UnionInputType
1597
+ ( Data.Functor.Product. Pair
1598
+ ( Control.Applicative. Const (mx <> my) )
1599
+ ( Op (either fx fy) )
1600
+ )
1590
1601
1591
- -- As of base 4.12, we can use :*: to derive this instance automatically
1592
- instance Monoid (UnionInputType a ) where
1593
- mempty = UnionInputType (Data.Functor.Product. Pair mempty mempty )
1602
+ infixr 5 >|<
1594
1603
1595
1604
inputUnion :: UnionInputType a -> InputType a
1596
1605
inputUnion ( UnionInputType ( Data.Functor.Product. Pair ( Control.Applicative. Const fields ) ( Op embedF ) ) ) =
1597
1606
InputType
1598
1607
{ embed =
1599
- embedder
1608
+ embedF
1600
1609
, declared =
1601
1610
Union fields
1602
1611
}
1603
- where
1604
- embedder = fromMaybe (errorWithoutStackTrace unmatched) . Data.Monoid. getFirst . embedF
1605
- unmatched = " inputUnion: UnionInputType is incomplete"
1606
1612
1607
1613
inputConstructorWith
1608
1614
:: Text
1609
- -> InputType b
1610
- -> (a -> Maybe b )
1615
+ -> InputType a
1611
1616
-> UnionInputType a
1612
- inputConstructorWith name inputType projector = UnionInputType $
1617
+ inputConstructorWith name inputType = UnionInputType $
1613
1618
Data.Functor.Product. Pair
1614
1619
( Control.Applicative. Const
1615
1620
( Dhall.Map. singleton
1616
1621
name
1617
- (declared inputType)
1622
+ ( declared inputType )
1618
1623
)
1619
1624
)
1620
- ( Op
1621
- ( Data.Monoid. First . fmap (embed inputType) . projector )
1625
+ ( Op ( embed inputType )
1622
1626
)
1623
1627
1624
1628
inputConstructor
1625
- :: Inject b
1629
+ :: Inject a
1626
1630
=> Text
1627
- -> (a -> Maybe b )
1628
1631
-> UnionInputType a
1629
1632
inputConstructor name = inputConstructorWith name inject
0 commit comments