haskell - How do you get and use the dependent type from a type class with functional dependencies? -


how , use dependent type type class functional dependencies?

to clarify , give example of latest attempt (minimised actual code writing):

class identifiable b | -> b  -- if know a, know b     idof :: -> b  instance identifiable int int     idof =  f :: identifiable int b => int -> [b]  -- ghc infer b functional dependency used in identifiable, , instance? f = [5 :: int] 

but ghc not infer b, seems, prints error:

couldn't match expected type ‘b’ actual type ‘int’   ‘b’ rigid type variable bound       type signature f :: identifiable int b => int -> [b]       @ src/main.hs:57:6 relevant bindings include   f :: int -> [b] (bound @ src/main.hs:58:1) in expression: 5 :: int in expression: [5 :: int] in equation ‘f’: f = [5 :: int] 

for context, here's less minimised example:

data graph     graph :: (identifiable b) => graphimpl b -> graph  getimpl :: identifiable b => graph -> graphimpl b getimpl (graph impl) = impl 

the workaround here add b type arg graph:

data graph b | -> b     graph :: (identifiable b) => graphimpl b -> graph 

the full context: have graph of entities each have id, each entity assigned 1 node. can node entity. have graph'which consists of nodes (which can assigned entity), , lookup node need provide node's id, int. graph uses graph' internally. have idmap maps ids of entities ids of nodes in graph'. graph definition:

data graph     graph  :: (identifiable b) => {     _idmap :: idmap b,     _nextvertexid :: int,     _graph :: graph' } -> graph 

answer: use type families, see daniel wagner's answer. full story, see reid barton's answer.

it indeed seem bit odd ghc complains minimal f posted @ top. seems work okay type families:

{-# language typefamilies #-} class identifiable     type idof     idof :: -> idof  instance identifiable int     type idof int = int     idof =  f :: -> [idof int] f _ = [5 :: int] 

perhaps can adapt idea larger example.


Comments

Popular posts from this blog

Java 3D LWJGL collision -

spring - SubProtocolWebSocketHandler - No handlers -

methods - python can't use function in submodule -