AK
Size: a a a
AK
VY
AK
AK
VY
ЗП
VY
ЗП
AK
data Rep = RepI Int | RepS String
class HasRep a where rep ∷ a → Rep
data Foo a = Foo a (a → Rep)
type FooE = Exists Foo
mkFooE :: forall a. (HasRep a) => a -> FooE
mkFooE a = mkExists (Foo a rep)
newtype FooMap k = FooMap (Map k FooE)
newtype X = X Int
newtype Y = Y String
instance hasRepX ∷ HasRep X where rep (X n) = RepI n
instance hasRepY ∷ HasRep Y where rep (Y s) = RepS s
foos' ∷ FooMap String
foos' = FooMap
$ Map.insert "y" (mkFooE $ Y "y")
$ Map.singleton "x" (mkFooE $ X 1)
class MyClass c d where rep ∷ c → d
data Foo d c = Foo c (c -> d)
type FooE d = Exists (Foo d)
mkFooE :: forall c d. (MyClass c d) => c -> FooE d
mkFooE a = mkExists (Foo a rep)
newtype DifferentFoos d = DifferentFoos (Map String (FooE d))
newtype X = X Int
newtype Y = Y String
data F = G | H
instance myClassXF ∷ MyClass X F where rep (X n) = G
instance myClassYF ∷ MyClass Y F where rep (Y s) = H
foos' ∷ DifferentFoos F
foos' = DifferentFoos
$ Map.insert "y" (mkFooE $ Y "y")
$ Map.singleton "x" (mkFooE $ X 1)
AK
DifferentFoos F, так будет работатьЗП
data F = G | H?data XY = X Int | Y StringAK
F (d) был общим для всех значений, а вариативность нужна по X и Y (c), важно чтобы их можно было создать динамически и засунуть в MapЗП
F расширятьVY
AK
class MyClass c d where rep ∷ c → d
data Foo d c = Foo c (c -> d)
type FooE d = Exists (Foo d)
mkFooE :: forall c d. (MyClass c d) => c -> FooE d
mkFooE a = mkExists (Foo a rep)
newtype DifferentFoos d = DifferentFoos (Map String (FooE d))
newtype X = X Int
newtype Y = Y String
data F = G | H
instance myClassXF ∷ MyClass X F where rep (X n) = G
instance myClassYF ∷ MyClass Y F where rep (Y s) = H
foos' ∷ DifferentFoos F
foos' = DifferentFoos
$ Map.insert "y" (mkFooE $ Y "y")
$ Map.singleton "x" (mkFooE $ X 1)
X и Y тоже могли быть data, не только newtypeclass MyClass d c where fn ∷ c → d
data Foo d c = Foo { f1 :: c, f2:: d }
type FooE d = Exists (Foo d)
mkFooE :: forall d c. (MyClass d c) => Foo d c -> FooE d
mkFooE = mkExists
newtype DifferentFoos d = DifferentFoos (Map.Map String (FooE d))
data X = XZ | XW
data Y = YQ | YU
data F = FG | FH
instance myClassXF ∷ MyClass F X where fn _ = FG
instance myClassYF ∷ MyClass F Y where fn _ = FH
foos' ∷ DifferentFoos F
foos' =
let
fooA :: Foo F X
fooA = Foo { f1 : XZ, f2 : FG }
fooB :: Foo F Y
fooB = Foo { f1 : YQ, f2 : FH }
in
DifferentFoos
$ Map.insert "y" (mkFooE fooA)
$ Map.singleton "x" (mkFooE fooB)
ЗП
ЗП
VY
(b -> a) (f b) мне понятноЗП