HORIZON HASKELLDocslts/ghc-9.14.xd82dee82026-10-12Search names, modules, packages, or :: a typeCtrl K

GHC 9.14.1 · lts/ghc-9.14.x · d82dee8 · 2026-10-12

Modulealgebraic-graphs-0.8Haskell2010

Algebra.Graph.ToGraph

Alga is a library for algebraic construction and manipulation of graphs in Haskell. See this paper for the motivation behind the library, the underlying theory, and implementation details.

This module defines the type class ToGraph for capturing data types that can be converted to algebraic graphs. To make an instance of this class you need to define just a single method (toGraph or foldg), which gives you access to many other useful methods for free (although note that the default implementations may be suboptimal performance-wise).

This type class is similar to the standard type class Foldable defined for lists. Furthermore, one can define Foldable methods foldMap and toList using ToGraph.foldg:

foldMap f = foldg mempty f    (<>) (<>)
toList    = foldg []     pure (++) (++)

However, the resulting Foldable instance is problematic. For example, folding equivalent algebraic graphs 1 and 1 + 1 leads to different results:

toList (1    ) == [1]
toList (1 + 1) == [1, 1]

To avoid such cases, we do not provide Foldable instances for algebraic graph datatypes. Furthermore, we require that the four arguments passed to foldg satisfy the laws of the algebra of graphs. The above definitions of foldMap and toList violate this requirement, for example [1] ++ [1] /= [1], and are therefore disallowed.

  • 1 class
  • 4 values

Type class

1 declaration
classclass ToGraph t where
#

The ToGraph type class captures data types that can be converted to algebraic graphs. Instances of this type class should satisfy the laws specified by the default method definitions.

Associated types

  • type family ToVertex t

    The type of vertices of the resulting graph.

Methods

Instances9ToGraph, …

Derived functions

4 declarations