1{-# LANGUAGE AllowAmbiguousTypes #-}
2{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE FlexibleContexts #-}
4{-# LANGUAGE FlexibleInstances #-}
5{-# LANGUAGE KindSignatures #-}
6{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE ScopedTypeVariables #-}
8{-# LANGUAGE TypeFamilies #-}
9{-# LANGUAGE TypeOperators #-}
10{-# LANGUAGE UndecidableInstances #-}
12-----------------------------------------------------------------------------
14-- Module : Data.Generics.Sum.Typed
15-- Copyright : (C) 2017 Csongor Kiss
17-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
18-- Stability : experimental
19-- Portability : non-portable
21-- Derive constructor-field-type-based prisms generically.
23-----------------------------------------------------------------------------
25module Data.Generics.Sum.Typed
34import GHC.TypeLits (TypeError, ErrorMessage (..))
35import Data.Generics.Sum.Internal.Typed
37import Data.Generics.Internal.Families
38import Data.Generics.Internal.Lens
42-- module Example where
44-- import Data.Generics.Sum
51-- deriving (Generic, Show)
57-- deriving (Generic, Show)
62-- dog, cat, duck :: Animal
64-- dog = Dog (MkDog "Shep" 3)
69-- |Sums that have a constructor with a field of the given type.
71 -- |A prism that projects a constructor uniquely identifiable by the type of
72 -- its field. Compatible with the lens package's 'Control.Lens.Prism' type.
74 -- >>> dog ^? _Typed @Dog
75 -- Just (MkDog {name = "Shep", age = 3})
76 -- >>> dog ^? _Typed @Age
78 -- >>> cat ^? _Typed @(Name, Age)
80 -- >>> duck ^? _Typed @Age
86 , ErrorUnlessOne a s (CountPartialType a (Rep s))
90 _Typed = repIso . _GTyped
92type family ErrorUnlessOne (a :: Type) (s :: Type) (count :: Count) :: Constraint where
93 ErrorUnlessOne a s 'None
97 ':<>: 'Text " does not contain a constructor whose field is of type "
101 ErrorUnlessOne a s 'Multiple
105 ':<>: 'Text " contains multiple constructors whose fields are of type "
107 ':<>: 'Text "; the choice of constructor is thus ambiguous"
110 ErrorUnlessOne _ _ 'One
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list