[Haskell-cafe] Stacking data types

Serguey Zefirov sergueyz at gmail.com
Wed Apr 6 22:22:04 CEST 2011


I think I should suggest HList from Oleg Kiseliov.

http://hackage.haskell.org/package/HList

That way you will have something along those lines:

-- fields descriptors:
data Character
data Gun
data Armor
data Life

-- values for fields:
data Vulcan = Vulcan { vulcanAmmoCount :: Int}
data Player = Player { playerName :: String }

player = (Character, Player) :*: (Gun,Vulcan) :*: (Armor,50) :*: HNil

HList has all the machinery to create records, get a field from record
(something like "getField rec Character"), test for field in record
(Maybe value), etc.

2011/4/6 Yves Parès <limestrael at gmail.com>:
> Hello Café,
>
> I'm trying to get some modular data types.
> The idea that came to me is that I could stack them, for instance :
>
> data Character a = Character { life :: Int,
>                                charaInner :: a }
>
> data Gun a = Gun { firepower :: Int,
>                    gunInner :: a }
>
> data Armor a = Armor { resistance :: Int,
>                        armorInner :: a }
>
> Then a character with a gun and an armor can be build this way:
>
> chara = Character 100 $ Armor 40 $ Gun 12
>
> The idea now is to be able to get some part of the character:
>
> itsGun :: Character ?? -> Gun ??
> itsGun = content
>
> Then content would be a class method:
>
> class Has b a where
>     content :: a -> b
>
> And it would be recursively defined so that:
>
> instance (Has c b, Has b a) => Has c a where
>     content = (content :: b -> c) . (content :: a -> b)
>
> Then itsGun would be more like:
>
> itsGun :: (Has Gun a) => a -> Gun ??
> itsGun = content
>
> But after some juggling with extensions (ScopedTypeVariables,
> UndecidableInstances, IncoherentInstances...) I can't get it working.
>
> Has someone a simpler way to achieve modular types?
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>



More information about the Haskell-Cafe mailing list