-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Convert between strong and weak representations of types -- -- Please see README.md. @package strongweak @version 0.3.2 module Strongweak.Weaken -- | Transform an a to a Weaken a. -- -- A given strong type a has exactly one associated weak type -- Weaken a. Multiple strong types may weaken to the same -- weak type. -- -- Law: a === b -> weaken a === weaken b -- -- Instances should either handle an invariant, or decompose. See -- Strongweak for a discussion on this design. class Weaken a where { -- | The type to weaken to. type Weak a :: Type; } -- | Transform a strong value to its associated weak one. weaken :: Weaken a => a -> Weak a -- | Lift a function on a weak type to the associated strong type. liftWeakF :: Weaken a => (Weak a -> b) -> a -> b -- | Strength enumeration: is it strong, or weak? -- -- Primarily interesting at the type level (using DataKinds). data Strength Strong :: Strength Weak :: Strength -- | Get either the strong or weak representation of a type, depending on -- the type-level "switch" provided. -- -- This is intended to be used in data types that take a Strength -- type. Define your type using strong fields wrapped in SW s. -- You then get the weak representation for free, using the same -- definition. -- --
--   data A (s :: Strength) = A
--     { a1 :: SW s Word8
--     , a2 :: String }
--   
type family SW (s :: Strength) a :: Type instance Strongweak.Weaken.Weaken (GHC.Base.NonEmpty a) instance Strongweak.Weaken.Weaken (Data.Vector.Sized.Vector n a) instance forall k (p :: k) a. Strongweak.Weaken.Weaken (Refined.Unsafe.Type.Refined p a) instance Strongweak.Weaken.Weaken GHC.Word.Word8 instance Strongweak.Weaken.Weaken GHC.Word.Word16 instance Strongweak.Weaken.Weaken GHC.Word.Word32 instance Strongweak.Weaken.Weaken GHC.Word.Word64 instance Strongweak.Weaken.Weaken GHC.Int.Int8 instance Strongweak.Weaken.Weaken GHC.Int.Int16 instance Strongweak.Weaken.Weaken GHC.Int.Int32 instance Strongweak.Weaken.Weaken GHC.Int.Int64 instance Strongweak.Weaken.Weaken a => Strongweak.Weaken.Weaken [a] instance (Strongweak.Weaken.Weaken a, Strongweak.Weaken.Weaken b) => Strongweak.Weaken.Weaken (a, b) instance Strongweak.Weaken.Weaken a => Strongweak.Weaken.Weaken (GHC.Maybe.Maybe a) instance (Strongweak.Weaken.Weaken a, Strongweak.Weaken.Weaken b) => Strongweak.Weaken.Weaken (Data.Either.Either a b) instance Strongweak.Weaken.Weaken a => Strongweak.Weaken.Weaken (Data.Functor.Identity.Identity a) instance forall k a (b :: k). Strongweak.Weaken.Weaken a => Strongweak.Weaken.Weaken (Data.Functor.Const.Const a b) module Strongweak.Strengthen.Unsafe -- | Unsafely transform a Weaken a to an a, -- without asserting invariants. -- -- For example, you may unsafely strengthen some Natural -- n into a Word8 by unsafely coercing the value, ignoring -- the possibility that n >= 255. -- -- What happens if it turns out you're lying to the computer and your -- weak value doesn't fit in its strong counterpart? That depends on the -- strengthen. -- -- -- -- Only consider using this if you have a guarantee that your value is -- safe to treat as strong. -- -- Instances should either handle an invariant, or decompose. See -- Strongweak for a discussion on this design. class Weaken a => UnsafeStrengthen a -- | Unsafely transform a weak value to its associated strong one. unsafeStrengthen :: UnsafeStrengthen a => Weak a -> a instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen (GHC.Base.NonEmpty a) instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen (Data.Vector.Sized.Vector n a) instance forall k (p :: k) a. Strongweak.Strengthen.Unsafe.UnsafeStrengthen (Refined.Unsafe.Type.Refined p a) instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen GHC.Word.Word8 instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen GHC.Word.Word16 instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen GHC.Word.Word32 instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen GHC.Word.Word64 instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen GHC.Int.Int8 instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen GHC.Int.Int16 instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen GHC.Int.Int32 instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen GHC.Int.Int64 instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen a => Strongweak.Strengthen.Unsafe.UnsafeStrengthen [a] instance (Strongweak.Strengthen.Unsafe.UnsafeStrengthen a, Strongweak.Strengthen.Unsafe.UnsafeStrengthen b) => Strongweak.Strengthen.Unsafe.UnsafeStrengthen (a, b) instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen a => Strongweak.Strengthen.Unsafe.UnsafeStrengthen (GHC.Maybe.Maybe a) instance (Strongweak.Strengthen.Unsafe.UnsafeStrengthen a, Strongweak.Strengthen.Unsafe.UnsafeStrengthen b) => Strongweak.Strengthen.Unsafe.UnsafeStrengthen (Data.Either.Either a b) instance Strongweak.Strengthen.Unsafe.UnsafeStrengthen a => Strongweak.Strengthen.Unsafe.UnsafeStrengthen (Data.Functor.Identity.Identity a) instance forall k a (b :: k). Strongweak.Strengthen.Unsafe.UnsafeStrengthen a => Strongweak.Strengthen.Unsafe.UnsafeStrengthen (Data.Functor.Const.Const a b) module Strongweak.Strengthen -- | You may attempt to transform a Weak a to an -- a. -- -- Laws: -- -- -- -- We take Weaken as a superclass in order to maintain strong/weak -- type pair consistency. We choose this dependency direction because we -- treat the strong type as the "canonical" one, so Weaken is the -- more natural (and straightforward) class to define. -- -- Instances should either handle an invariant, or decompose. See -- Strongweak for a discussion on this design. class Weaken a => Strengthen a -- | Attempt to transform a weak value to its associated strong one. strengthen :: Strengthen a => Weak a -> Validation (NonEmpty StrengthenFail) a -- | Strengthen failure data type. Don't use these constructors directly, -- use the existing helper functions. -- -- Field indices are from 0 in the respective constructor. Field names -- are provided if present. data StrengthenFail StrengthenFailBase :: String -> String -> String -> String -> StrengthenFail StrengthenFailField :: String -> String -> String -> String -> Natural -> Maybe String -> Natural -> Maybe String -> NonEmpty StrengthenFail -> StrengthenFail strengthenFailPretty :: NonEmpty StrengthenFail -> Doc a strengthenFailBase :: forall s w. (Typeable w, Show w, Typeable s) => w -> String -> Validation (NonEmpty StrengthenFail) s -- | Weaken a strong value, then strengthen it again. -- -- Potentially useful if you have previously used unsafeStrengthen -- and now wish to check the invariants. For example: -- --
--   >>> restrengthen $ unsafeStrengthen @(Vector 2 Natural) [0]
--   Failure ...
--   
restrengthen :: (Strengthen a, Weaken a) => a -> Validation (NonEmpty StrengthenFail) a strengthenBounded :: forall b n. (Integral b, Bounded b, Show b, Typeable b, Integral n, Show n, Typeable n) => n -> Validation (NonEmpty StrengthenFail) b -- | The type to weaken to. type Weak a :: Type instance GHC.Classes.Eq Strongweak.Strengthen.StrengthenFail instance (Data.Typeable.Internal.Typeable a, GHC.Show.Show a) => Strongweak.Strengthen.Strengthen (GHC.Base.NonEmpty a) instance (GHC.TypeNats.KnownNat n, Data.Typeable.Internal.Typeable a, GHC.Show.Show a) => Strongweak.Strengthen.Strengthen (Data.Vector.Sized.Vector n a) instance forall k (p :: k) a. (Refined.Predicate p a, Data.Typeable.Internal.Typeable k, Data.Typeable.Internal.Typeable a, GHC.Show.Show a) => Strongweak.Strengthen.Strengthen (Refined.Unsafe.Type.Refined p a) instance Strongweak.Strengthen.Strengthen GHC.Word.Word8 instance Strongweak.Strengthen.Strengthen GHC.Word.Word16 instance Strongweak.Strengthen.Strengthen GHC.Word.Word32 instance Strongweak.Strengthen.Strengthen GHC.Word.Word64 instance Strongweak.Strengthen.Strengthen GHC.Int.Int8 instance Strongweak.Strengthen.Strengthen GHC.Int.Int16 instance Strongweak.Strengthen.Strengthen GHC.Int.Int32 instance Strongweak.Strengthen.Strengthen GHC.Int.Int64 instance Strongweak.Strengthen.Strengthen a => Strongweak.Strengthen.Strengthen [a] instance (Strongweak.Strengthen.Strengthen a, Strongweak.Strengthen.Strengthen b) => Strongweak.Strengthen.Strengthen (a, b) instance Strongweak.Strengthen.Strengthen a => Strongweak.Strengthen.Strengthen (GHC.Maybe.Maybe a) instance (Strongweak.Strengthen.Strengthen a, Strongweak.Strengthen.Strengthen b) => Strongweak.Strengthen.Strengthen (Data.Either.Either a b) instance Strongweak.Strengthen.Strengthen a => Strongweak.Strengthen.Strengthen (Data.Functor.Identity.Identity a) instance forall k a (b :: k). Strongweak.Strengthen.Strengthen a => Strongweak.Strengthen.Strengthen (Data.Functor.Const.Const a b) instance GHC.Show.Show Strongweak.Strengthen.StrengthenFail instance Prettyprinter.Internal.Pretty Strongweak.Strengthen.StrengthenFail -- | strengthen over generic representations. -- -- Strengthen failures are annotated with precise information describing -- where the failure occurred: datatype name, constructor name, field -- index (and name if present). To achieve this, we split the generic -- derivation into 3 classes, each handling/"unwrapping" a different -- layer of the generic representation: datatype (D), constructor (C) and -- selector (S). module Strongweak.Generic.Strengthen -- | Strengthen a value generically. -- -- The weak and strong types must be compatible. See -- Generic for the definition of compatibility in this context. strengthenGeneric :: (Generic w, Generic s, GStrengthenD (Rep w) (Rep s)) => w -> Validation (NonEmpty StrengthenFail) s -- | Generic strengthening at the datatype level. class GStrengthenD w s gstrengthenD :: GStrengthenD w s => w p -> Validation (NonEmpty StrengthenFail) (s p) -- | Generic strengthening at the constructor sum level. class GStrengthenC w s gstrengthenC :: GStrengthenC w s => String -> String -> w p -> Validation (NonEmpty StrengthenFail) (s p) -- | Generic strengthening at the selector product level. -- -- In order to calculate field indices, we return the current field index -- alongside the result. This way, the product case can strengthen the -- left branch, then increment the returned field index and use it for -- strengthening the right branch. class GStrengthenS w s gstrengthenS :: GStrengthenS w s => String -> String -> String -> String -> Natural -> w p -> (Natural, Validation (NonEmpty StrengthenFail) (s p)) -- | Get the record name for a selector if present. -- -- On the type level, a 'Maybe Symbol' is stored for record names. But -- the reification is done using fromMaybe "". So we have to -- inspect the resulting string to determine whether the field uses -- record syntax or not. (Silly.) selName'' :: forall s. Selector s => Maybe String -- | conName without the value (only used as a proxy). Lets us push -- our undefineds into one place. conName' :: forall c. Constructor c => String -- | datatypeName without the value (only used as a proxy). Lets us -- push our undefineds into one place. datatypeName' :: forall d. Datatype d => String -- | selName without the value (only used as a proxy). Lets us push -- our undefineds into one place. selName' :: forall s. Selector s => String instance forall k (w :: k -> Type) (s :: k -> Type) (cw :: GHC.Generics.Meta) (cs :: GHC.Generics.Meta). (Strongweak.Generic.Strengthen.GStrengthenS w s, GHC.Generics.Constructor cw, GHC.Generics.Constructor cs) => Strongweak.Generic.Strengthen.GStrengthenC (GHC.Generics.C1 cw w) (GHC.Generics.C1 cs s) instance Strongweak.Generic.Strengthen.GStrengthenS GHC.Generics.U1 GHC.Generics.U1 instance forall k (lw :: k -> Type) (ls :: k -> Type) (rw :: k -> Type) (rs :: k -> Type). (Strongweak.Generic.Strengthen.GStrengthenS lw ls, Strongweak.Generic.Strengthen.GStrengthenS rw rs) => Strongweak.Generic.Strengthen.GStrengthenS (lw GHC.Generics.:*: rw) (ls GHC.Generics.:*: rs) instance Strongweak.Generic.Strengthen.GStrengthenS (GHC.Generics.S1 mw (GHC.Generics.Rec0 w)) (GHC.Generics.S1 ms (GHC.Generics.Rec0 w)) instance (Strongweak.Strengthen.Strengthen s, Strongweak.Weaken.Weak s GHC.Types.~ w, GHC.Generics.Selector mw, GHC.Generics.Selector ms) => Strongweak.Generic.Strengthen.GStrengthenS (GHC.Generics.S1 mw (GHC.Generics.Rec0 w)) (GHC.Generics.S1 ms (GHC.Generics.Rec0 s)) instance forall k (w :: k -> Type) (s :: k -> Type) (dw :: GHC.Generics.Meta) (ds :: GHC.Generics.Meta). (Strongweak.Generic.Strengthen.GStrengthenC w s, GHC.Generics.Datatype dw, GHC.Generics.Datatype ds) => Strongweak.Generic.Strengthen.GStrengthenD (GHC.Generics.D1 dw w) (GHC.Generics.D1 ds s) instance Strongweak.Generic.Strengthen.GStrengthenC GHC.Generics.V1 GHC.Generics.V1 instance forall k (lw :: k -> Type) (ls :: k -> Type) (rw :: k -> Type) (rs :: k -> Type). (Strongweak.Generic.Strengthen.GStrengthenC lw ls, Strongweak.Generic.Strengthen.GStrengthenC rw rs) => Strongweak.Generic.Strengthen.GStrengthenC (lw GHC.Generics.:+: rw) (ls GHC.Generics.:+: rs) -- | weaken over generic representations. module Strongweak.Generic.Weaken -- | Weaken a value generically. -- -- The weak and strong types must be compatible. See -- Generic for the definition of compatibility in this context. weakenGeneric :: (Generic s, Generic w, GWeaken (Rep s) (Rep w)) => s -> w class GWeaken s w gweaken :: GWeaken s w => s p -> w p instance forall k (s :: k -> Type) (w :: k -> Type) is (ms :: GHC.Generics.Meta) iw (mw :: GHC.Generics.Meta). Strongweak.Generic.Weaken.GWeaken s w => Strongweak.Generic.Weaken.GWeaken (GHC.Generics.M1 is ms s) (GHC.Generics.M1 iw mw w) instance Strongweak.Generic.Weaken.GWeaken GHC.Generics.V1 GHC.Generics.V1 instance Strongweak.Generic.Weaken.GWeaken GHC.Generics.U1 GHC.Generics.U1 instance Strongweak.Generic.Weaken.GWeaken (GHC.Generics.Rec0 s) (GHC.Generics.Rec0 s) instance (Strongweak.Weaken.Weaken s, Strongweak.Weaken.Weak s GHC.Types.~ w) => Strongweak.Generic.Weaken.GWeaken (GHC.Generics.Rec0 s) (GHC.Generics.Rec0 w) instance forall k (ls :: k -> Type) (lw :: k -> Type) (rs :: k -> Type) (rw :: k -> Type). (Strongweak.Generic.Weaken.GWeaken ls lw, Strongweak.Generic.Weaken.GWeaken rs rw) => Strongweak.Generic.Weaken.GWeaken (ls GHC.Generics.:*: rs) (lw GHC.Generics.:*: rw) instance forall k (ls :: k -> Type) (lw :: k -> Type) (rs :: k -> Type) (rw :: k -> Type). (Strongweak.Generic.Weaken.GWeaken ls lw, Strongweak.Generic.Weaken.GWeaken rs rw) => Strongweak.Generic.Weaken.GWeaken (ls GHC.Generics.:+: rs) (lw GHC.Generics.:+: rw) -- | Generic strengthen and weaken. module Strongweak.Generic -- | Weaken a value generically. -- -- The weak and strong types must be compatible. See -- Generic for the definition of compatibility in this context. weakenGeneric :: (Generic s, Generic w, GWeaken (Rep s) (Rep w)) => s -> w -- | Strengthen a value generically. -- -- The weak and strong types must be compatible. See -- Generic for the definition of compatibility in this context. strengthenGeneric :: (Generic w, Generic s, GStrengthenD (Rep w) (Rep s)) => w -> Validation (NonEmpty StrengthenFail) s module Strongweak