[Haskell-cafe] Manual type-checking in graphs: Avoidable?

butrfeld Andrew.Butterfield at scss.tcd.ie
Fri Feb 19 12:21:15 UTC 2016


What you need is a Relation, not a Graph with nodes all of the same type!

For me the problem below is most easily solved using Data.Map

either use   Map Person [Hamster]
or Map Hamster [Person]
to get the most general relations (many-many)

If every Hamster has at most one owner, use Map Hamster Person


> Jeffrey Brown <jeffbrown.the at gmail.com> writes:
>> I use FGL, which (roughly) defines type Gr a b as a graph on nodes of
>> type a and edges of type b.
>> 
>> Suppose you wanted a graph that described which people own which
>> hamsters, knowing only their name. You would have to make node and
>> edge types like this:
>> data GraphNode = Person String | Hamster String
>> data GraphEdge = Has
>> where the strings represent their names.
>> 
>> Suppose then you wanted to write a function that, given a person,
>> returns the names of all their hamsters. To make sure the call makes
>> sense, the function would have to first check that the input is in
>> fact a person. Since persons and hamsters are both constructors of the
>> same type, you can't let Haskell's robust, beautiful type-checking
>> system distinguish them for you; you've got to write something like
>> "case n of Person _ -> True; _ -> False".
> 

Andrew Butterfield
School of Computer Science & Statistics
Trinity College
Dublin 2, Ireland



More information about the Haskell-Cafe mailing list