1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Legacy.Clothes
where
import Prelude hiding ((.), id)
import Control.Category
import Data.Functor.Identity
import qualified Data.List.NonEmpty as NE
import Data.Typeable
import Test.Tasty.QuickCheck
data UnitF a = UnitF deriving(Eq, Show, Typeable)
data F a = F [a]
deriving(Eq, Show, Typeable)
data G a = NoG | G1 a | Gn [a]
deriving(Eq, Show, Typeable)
data H a = NoH1 | NoH2 | H1 [a] | H2 [a] | H3 [a]
deriving(Eq, Show, Typeable)
data I a = NoI1 | NoI2 | NoI3 | I1 a | I2 (a,a)
deriving(Eq, Show, Typeable)
instance Arbitrary a => Arbitrary (F a) where
arbitrary = F <$> arbitrary
instance Arbitrary a => Arbitrary (G a) where
arbitrary = oneof
[ pure NoG
, G1 <$> arbitrary
, Gn <$> arbitrary
]
instance Arbitrary a => Arbitrary (H a) where
arbitrary = oneof
[ pure NoH1
, pure NoH2
, H1 <$> arbitrary
, H2 <$> arbitrary
, H3 <$> arbitrary
]
instance Arbitrary a => Arbitrary (I a) where
arbitrary = oneof
[ pure NoI1
, pure NoI2
, pure NoI3
, I1 <$> arbitrary
, I2 <$> arbitrary
]
newtype NatTransf f g
= NatTransf {applyNat :: (forall a . f a -> g a)}
instance Category NatTransf where
id = NatTransf id
f . g = NatTransf (applyNat f . applyNat g)
point :: (forall a . a -> f a) -> NatTransf Identity f
point mkPoint
= NatTransf (\(Identity a) -> mkPoint a)
unit :: (forall a . f a) -> NatTransf UnitF f
unit u
= NatTransf (\UnitF -> u)
headF :: NatTransf NE.NonEmpty Identity
headF
= NatTransf (\(a NE.:| _) -> Identity a)
terminal :: NatTransf f UnitF
terminal
= NatTransf (const UnitF)
instance (ArbitraryF f, ArbitraryF g) => Arbitrary (NatTransf f g) where
arbitrary
= do fromList <- arbitraryf
pure (fromList . flattenf)
class ArbitraryF f where
arbitraryf :: Gen (NatTransf [] f)
flattenf :: NatTransf f []
instance ArbitraryF F where
arbitraryf
= pure $ NatTransf F
flattenf
= NatTransf (\(F as) -> as)
instance ArbitraryF G where
arbitraryf
= mkArbitraryf
[unit NoG]
[point G1 , point (Gn . pure)]
[NatTransf (Gn . NE.toList)]
flattenf
= NatTransf $ \case
NoG -> []
G1 a -> [a]
Gn as -> as
instance ArbitraryF H where
arbitraryf
= mkArbitraryf
[unit NoH1, unit NoH2]
[point (H1 . pure), point (H2 . pure)]
[ NatTransf (H1 . NE.toList)
, NatTransf (H2 . NE.toList)
, NatTransf (H2 . NE.toList)
]
flattenf
= NatTransf $ \case
NoH1 -> []
NoH2 -> []
H1 as -> as
H2 as -> as
H3 as -> as
instance ArbitraryF I where
arbitraryf
= mkArbitraryf
[unit NoI1, unit NoI2, unit NoI3]
[point I1, NatTransf (\(Identity a) -> I2 (a, a))]
[ NatTransf mkI2 ]
where
mkI2 = \case
a NE.:| [] -> I2 (a, a)
a NE.:| (b:_) -> I2 (a, b)
flattenf
= NatTransf $ \case
NoI1 -> []
NoI2 -> []
NoI3 -> []
I1 a -> [a]
I2 (a,b) -> [a,b]
mkArbitraryf
:: [NatTransf UnitF f]
-> [NatTransf Identity f]
-> [NatTransf NE.NonEmpty f]
-> Gen (NatTransf [] f)
mkArbitraryf us is ls
= do let nullary = us
unary = is ++ map (. terminal) nullary
nary = ls ++ map (. headF) unary
build <$> elements nullary <*> elements unary <*> elements nary
where
build u i l
= NatTransf $ \case
[] -> applyNat u UnitF
[a] -> applyNat i (Identity a)
a:as -> applyNat l (a NE.:| as)
newtype FG
= FG (NatTransf F G)
deriving (Arbitrary)
newtype GH
= GH (NatTransf G H)
deriving (Arbitrary)
newtype HI
= HI (NatTransf H I)
deriving (Arbitrary)
instance Show FG
where show _ = "<natural-transformation :: F -> G>"
instance Show GH
where show _ = "<natural-transformation :: G -> H>"
instance Show HI
where show _ = "<natural-transformation :: H -> I>"
|