[Haskell-cafe] type variable is ambiguous in a non-injective type family

MarLinn monkleyon at gmail.com
Mon Jul 29 12:47:40 UTC 2019


Hi Henry,

I'm not sure if this is suitable for your use case, but maybe simple 
multi parameter type classes would be an alternative? Especially because 
then you could use FunDeps if more dependencies turn up. Like this:

     class Game gameState command mutable static | gameState -> mutable,static where
         toGameId  :: static    -> GameId
         toMutable :: gameState -> IO (Maybe mutable)
         everyone  :: mutable   -> [Sink]

     instance Game Chess ChessCommand ChessMutable ChessStatic where
         …

Admittedly, the type looks a bit long. But maybe more unification is 
possible to reduce that.

Yet another idea is that instead of sprinkling TypeApplications, you 
could also try a bit of refactoring:

     class Game g where
         data Command g         :: *
         data Mutable g         :: *
         data Static  g         :: *
         toGameId               :: g -> GameId
         toMutable              :: g -> IO (Maybe (Mutable a))
         everyone               :: Mutable g -> [Sink]

     instance Game Chess where
         data Command Chess = MovePiece { movedPiece    :: ChessField, moveTarget :: ChessField }
                            | Castling  { withLeftRook  :: Bool }
                            | Promote   { promotedPiece :: ChessField, promotedTo :: ChessPiece }
                            | EnPassant { enPassantFrom :: ChessField }
                            | GiveUp
         …

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190729/f96d320b/attachment.html>


More information about the Haskell-Cafe mailing list