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

Kosyrev Serge _deepfire at feelingofgreen.ru
Fri Feb 19 12:06:42 UTC 2016


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".

I'll allow myself to rephrase what Tom Ellis already said more strongly.

Type systems are tools for making invalid values unrepresentable.

If you find yourself in a situation where a certain solution doesn't
allow you to do that -- keep looking.

-- 
с уважениeм / respectfully,
Косырев Сергей


More information about the Haskell-Cafe mailing list