2
2
3
3
module Strongweak.Weaken
4
4
(
5
- -- * 'Weaken' class
6
- Weaken (.. )
5
+ Weaken (Weakened , weaken )
7
6
, type WeakenedN
8
7
, liftWeakF
9
-
10
- -- * Strength switch helper
11
- , Strength (.. )
12
- , type SW
13
-
14
- , ErrZeroInvariantNewtype'
8
+ , SWCoercibly (.. )
15
9
) where
16
10
17
11
import Rerefined
@@ -26,10 +20,6 @@ import Data.List.NonEmpty qualified as NonEmpty
26
20
import Data.List.NonEmpty ( NonEmpty )
27
21
import GHC.TypeNats
28
22
29
- import GHC.TypeError
30
- import Strongweak.Util.TypeErrors
31
- import GHC.TypeLits ( type Symbol )
32
-
33
23
{- | Weaken some @a@, relaxing certain invariants.
34
24
35
25
See "Strongweak" for class design notes and laws.
@@ -41,38 +31,32 @@ class Weaken a where
41
31
-- | Weaken some @a@ to its associated weak type @'Weakened' a@.
42
32
weaken :: a -> Weakened a
43
33
44
- -- | Strength enumeration: is it strong, or weak?
45
- --
46
- -- Primarily interesting at the type level (using DataKinds).
47
- data Strength = Strong | Weak
48
-
49
34
-- | Lift a function on a weak type to the associated strong type by weakening
50
35
-- first.
51
36
liftWeakF :: Weaken a => (Weakened a -> b ) -> a -> b
52
37
liftWeakF f = f . weaken
53
38
54
- {- | Get either the strong or weak representation of a type, depending on the
55
- type-level "switch" provided.
56
-
57
- This is intended to be used in data types that take a 'Strength' type. Define
58
- your type using strong fields wrapped in @SW s@. You then get the weak
59
- representation for free, using the same definition.
60
-
61
- @
62
- data A (s :: Strength) = A
63
- { a1 :: SW s Word8
64
- , a2 :: String }
65
- @
66
- -}
67
- type family SW (s :: Strength ) a :: Type where
68
- SW Strong a = a
69
- SW Weak a = Weakened a
70
-
71
39
-- | The type of @a@ after weakening @n@ times.
72
40
type family WeakenedN (n :: Natural ) a :: Type where
73
41
WeakenedN 0 a = a
74
42
WeakenedN n a = Weakened (WeakenedN (n - 1 ) a )
75
43
44
+ -- | A "via type" newtype for defining strongweak instances for zero-invariant,
45
+ -- coercible newtypes.
46
+ --
47
+ -- Use like so:
48
+ --
49
+ -- @
50
+ -- deriving via 'SWCoercibly' a instance 'Weaken' ('Identity' a)
51
+ -- @
52
+ newtype SWCoercibly a = SWCoercibly { unSWCoercibly :: a }
53
+ instance Weaken (SWCoercibly a ) where
54
+ type Weakened (SWCoercibly a ) = a
55
+ weaken = unSWCoercibly
56
+
57
+ deriving via SWCoercibly a instance Weaken (Identity a )
58
+ deriving via SWCoercibly a instance Weaken (Const a b )
59
+
76
60
-- | Strip refined type refinement.
77
61
instance Weaken (Refined p a ) where
78
62
type Weakened (Refined p a ) = a
@@ -144,37 +128,3 @@ instance (Weaken a, Weaken b) => Weaken (Either a b) where
144
128
type Weakened (Either a b ) = Either (Weakened a ) (Weakened b )
145
129
weaken = \ case Left a -> Left $ weaken a
146
130
Right b -> Right $ weaken b
147
-
148
- ---
149
-
150
- newtype ErrZeroInvariantNewtype' (typeName :: Symbol ) a
151
- = ErrZeroInvariantNewtype' a
152
- instance Unsatisfiable (ErrZeroInvariantNewtype typeName )
153
- => Weaken (ErrZeroInvariantNewtype' typeName a ) where
154
- type Weakened (ErrZeroInvariantNewtype' typeName a ) =
155
- TypeError (ErrZeroInvariantNewtype typeName )
156
- weaken = unsatisfiable
157
-
158
- -- deriving via ErrZeroInvariantNewtype' "Identity" a
159
- -- instance Weaken (Identity a)
160
-
161
- {- TODO 2024-10-16T04:21:22+0100
162
- aww this doesn't work haha. ok fine just gotta make some utils for filling out
163
- the context and Weakened associated type family for custom erroring instances
164
- -}
165
-
166
- {-
167
- instance Unsatisfiable (ErrZeroInvariantNewtype "Identity")
168
- => Weaken (Identity a) where
169
- type Weakened (Identity a) = a
170
- weaken = unsatisfiable
171
- -}
172
-
173
- -- TODO define custom errors using Unsatisfiable to point users to Coercibly
174
- -- Unsatisfiable is base-4.19 -> GHC 9.8
175
- -- deriving via Coercibly1 Shallow Identity a instance Weaken (Identity a)
176
- -- deriving via Coercibly Shallow (Const a b) a instance Weaken (Const a b)
177
-
178
- instance Weaken (Identity a ) where
179
- type Weakened (Identity a ) = a
180
- weaken (Identity a) = a
0 commit comments