1{-# LANGUAGE AllowAmbiguousTypes #-}
2{-# LANGUAGE DataKinds #-}
3{-# LANGUAGE FlexibleInstances #-}
4{-# LANGUAGE FunctionalDependencies #-}
5{-# LANGUAGE KindSignatures #-}
6{-# LANGUAGE MultiParamTypeClasses #-}
7{-# LANGUAGE ScopedTypeVariables #-}
8{-# LANGUAGE TypeApplications #-}
9{-# LANGUAGE TypeFamilies #-}
10{-# LANGUAGE TypeOperators #-}
11{-# LANGUAGE UndecidableInstances #-}
13-----------------------------------------------------------------------------
15-- Module : Data.Generics.Sum.Constructors
16-- Copyright : (C) 2017 Csongor Kiss
18-- Maintainer : Csongor Kiss <kiss.csongor.kiss@gmail.com>
19-- Stability : experimental
20-- Portability : non-portable
22-- Derive constructor-name-based prisms generically.
24-----------------------------------------------------------------------------
26module Data.Generics.Sum.Constructors
33import Data.Generics.Internal.Families
34import Data.Generics.Internal.Lens
35import Data.Generics.Sum.Internal.Constructors
37import Data.Kind (Constraint, Type)
38import GHC.Generics (Generic (Rep))
39import GHC.TypeLits (Symbol, TypeError, ErrorMessage (..))
43-- module Example where
45-- import Data.Generics.Sum
52-- deriving (Generic, Show)
58-- deriving (Generic, Show)
63-- dog, cat, duck :: Animal
65-- dog = Dog (MkDog "Shep" 3)
70-- |Sums that have a constructor with a given name.
71class AsConstructor (ctor :: Symbol) a s | s ctor -> a where
72 -- |A prism that projects a named constructor from a sum. Compatible with the
73 -- lens package's 'Control.Lens.Prism' type.
75 -- >>> dog ^? _Ctor @"Dog"
76 -- Just (MkDog {name = "Shep", age = 3})
78 -- >>> dog ^? _Ctor @"Cat"
81 -- >>> cat ^? _Ctor @"Cat"
84 -- >>> _Ctor @"Cat" # ("Garfield", 6) :: Animal
85 -- Cat ("Garfield", 6)
90 , ErrorUnless ctor s (HasCtorP ctor (Rep s))
91 , GAsConstructor ctor (Rep s) a
92 ) => AsConstructor ctor a s where
94 _Ctor = repIso . _GCtor @ctor
96type family ErrorUnless (ctor :: Symbol) (s :: Type) (contains :: Bool) :: Constraint where
97 ErrorUnless ctor s 'False
101 ':<>: 'Text " does not contain a constructor named "
105 ErrorUnless _ _ 'True
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