algebraic-graphs-0.3: A library for algebraic graph construction and transformation

Copyright(c) Andrey Mokhov 2016-2018
LicenseMIT (see the file LICENSE)
Maintainerandrey.mokhov@gmail.com
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

Algebra.Graph.Fold

Contents

Description

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 Fold data type -- the Boehm-Berarducci encoding of algebraic graphs, which is used for generalised graph folding and for the implementation of polymorphic graph construction and transformation algorithms. Fold is an instance of type classes defined in modules Algebra.Graph.Class and Algebra.Graph.HigherKinded.Class, which can be used for polymorphic graph construction and manipulation.

Synopsis

Boehm-Berarducci encoding of algebraic graphs

data Fold a Source #

The Fold data type is the Boehm-Berarducci encoding of the core graph construction primitives empty, vertex, overlay and connect. We define a Num instance as a convenient notation for working with graphs:

0           == vertex 0
1 + 2       == overlay (vertex 1) (vertex 2)
1 * 2       == connect (vertex 1) (vertex 2)
1 + 2 * 3   == overlay (vertex 1) (connect (vertex 2) (vertex 3))
1 * (2 + 3) == connect (vertex 1) (overlay (vertex 2) (vertex 3))

Note: the Num instance does not satisfy several "customary laws" of Num, which dictate that fromInteger 0 and fromInteger 1 should act as additive and multiplicative identities, and negate as additive inverse. Nevertheless, overloading fromInteger, + and * is very convenient when working with algebraic graphs; we hope that in future Haskell's Prelude will provide a more fine-grained class hierarchy for algebraic structures, which we would be able to utilise without violating any laws.

The Show instance is defined using basic graph construction primitives:

show (empty     :: Fold Int) == "empty"
show (1         :: Fold Int) == "vertex 1"
show (1 + 2     :: Fold Int) == "vertices [1,2]"
show (1 * 2     :: Fold Int) == "edge 1 2"
show (1 * 2 * 3 :: Fold Int) == "edges [(1,2),(1,3),(2,3)]"
show (1 * 2 + 3 :: Fold Int) == "overlay (vertex 3) (edge 1 2)"

The Eq instance is currently implemented using the AdjacencyMap as the canonical graph representation and satisfies all axioms of algebraic graphs:

  • overlay is commutative and associative:

          x + y == y + x
    x + (y + z) == (x + y) + z
  • connect is associative and has empty as the identity:

      x * empty == x
      empty * x == x
    x * (y * z) == (x * y) * z
  • connect distributes over overlay:

    x * (y + z) == x * y + x * z
    (x + y) * z == x * z + y * z
  • connect can be decomposed:

    x * y * z == x * y + x * z + y * z

The following useful theorems can be proved from the above set of axioms.

  • overlay has empty as the identity and is idempotent:

      x + empty == x
      empty + x == x
          x + x == x
  • Absorption and saturation of connect:

    x * y + x + y == x * y
        x * x * x == x * x

When specifying the time and memory complexity of graph algorithms, n will denote the number of vertices in the graph, m will denote the number of edges in the graph, and s will denote the size of the corresponding graph expression. For example, if g is a Fold then n, m and s can be computed as follows:

n == vertexCount g
m == edgeCount g
s == size g

Note that size counts all leaves of the expression:

vertexCount empty           == 0
size        empty           == 1
vertexCount (vertex x)      == 1
size        (vertex x)      == 1
vertexCount (empty + empty) == 0
size        (empty + empty) == 2

Converting a Fold to the corresponding AdjacencyMap takes O(s + m * log(m)) time and O(s + m) memory. This is also the complexity of the graph equality test, because it is currently implemented by converting graph expressions to canonical representations based on adjacency maps.

The total order on graphs is defined using size-lexicographic comparison:

  • Compare the number of vertices. In case of a tie, continue.
  • Compare the sets of vertices. In case of a tie, continue.
  • Compare the number of edges. In case of a tie, continue.
  • Compare the sets of edges.

Here are a few examples:

vertex 1 < vertex 2
vertex 3 < edge 1 2
vertex 1 < edge 1 1
edge 1 1 < edge 1 2
edge 1 2 < edge 1 1 + edge 2 2
edge 1 2 < edge 1 3

Note that the resulting order refines the isSubgraphOf relation and is compatible with overlay and connect operations:

isSubgraphOf x y ==> x <= y
empty <= x
x     <= x + y
x + y <= x * y
Instances
Monad Fold Source # 
Instance details

Defined in Algebra.Graph.Fold

Methods

(>>=) :: Fold a -> (a -> Fold b) -> Fold b #

(>>) :: Fold a -> Fold b -> Fold b #

return :: a -> Fold a #

fail :: String -> Fold a #

Functor Fold Source # 
Instance details

Defined in Algebra.Graph.Fold

Methods

fmap :: (a -> b) -> Fold a -> Fold b #

(<$) :: a -> Fold b -> Fold a #

Applicative Fold Source # 
Instance details

Defined in Algebra.Graph.Fold

Methods

pure :: a -> Fold a #

(<*>) :: Fold (a -> b) -> Fold a -> Fold b #

liftA2 :: (a -> b -> c) -> Fold a -> Fold b -> Fold c #

(*>) :: Fold a -> Fold b -> Fold b #

(<*) :: Fold a -> Fold b -> Fold a #

Alternative Fold Source # 
Instance details

Defined in Algebra.Graph.Fold

Methods

empty :: Fold a #

(<|>) :: Fold a -> Fold a -> Fold a #

some :: Fold a -> Fold [a] #

many :: Fold a -> Fold [a] #

MonadPlus Fold Source # 
Instance details

Defined in Algebra.Graph.Fold

Methods

mzero :: Fold a #

mplus :: Fold a -> Fold a -> Fold a #

Graph Fold Source # 
Instance details

Defined in Algebra.Graph.HigherKinded.Class

Methods

connect :: Fold a -> Fold a -> Fold a Source #

Ord a => Eq (Fold a) Source # 
Instance details

Defined in Algebra.Graph.Fold

Methods

(==) :: Fold a -> Fold a -> Bool #

(/=) :: Fold a -> Fold a -> Bool #

Num a => Num (Fold a) Source #

Note: this does not satisfy the usual ring laws; see Fold for more details.

Instance details

Defined in Algebra.Graph.Fold

Methods

(+) :: Fold a -> Fold a -> Fold a #

(-) :: Fold a -> Fold a -> Fold a #

(*) :: Fold a -> Fold a -> Fold a #

negate :: Fold a -> Fold a #

abs :: Fold a -> Fold a #

signum :: Fold a -> Fold a #

fromInteger :: Integer -> Fold a #

Ord a => Ord (Fold a) Source # 
Instance details

Defined in Algebra.Graph.Fold

Methods

compare :: Fold a -> Fold a -> Ordering #

(<) :: Fold a -> Fold a -> Bool #

(<=) :: Fold a -> Fold a -> Bool #

(>) :: Fold a -> Fold a -> Bool #

(>=) :: Fold a -> Fold a -> Bool #

max :: Fold a -> Fold a -> Fold a #

min :: Fold a -> Fold a -> Fold a #

(Ord a, Show a) => Show (Fold a) Source # 
Instance details

Defined in Algebra.Graph.Fold

Methods

showsPrec :: Int -> Fold a -> ShowS #

show :: Fold a -> String #

showList :: [Fold a] -> ShowS #

NFData a => NFData (Fold a) Source # 
Instance details

Defined in Algebra.Graph.Fold

Methods

rnf :: Fold a -> () #

ToGraph (Fold a) Source # 
Instance details

Defined in Algebra.Graph.Fold

Associated Types

type ToVertex (Fold a) :: Type Source #

Methods

toGraph :: Fold a -> Graph (ToVertex (Fold a)) Source #

foldg :: r -> (ToVertex (Fold a) -> r) -> (r -> r -> r) -> (r -> r -> r) -> Fold a -> r Source #

isEmpty :: Fold a -> Bool Source #

size :: Fold a -> Int Source #

hasVertex :: ToVertex (Fold a) -> Fold a -> Bool Source #

hasEdge :: ToVertex (Fold a) -> ToVertex (Fold a) -> Fold a -> Bool Source #

vertexCount :: Fold a -> Int Source #

edgeCount :: Fold a -> Int Source #

vertexList :: Fold a -> [ToVertex (Fold a)] Source #

edgeList :: Fold a -> [(ToVertex (Fold a), ToVertex (Fold a))] Source #

vertexSet :: Fold a -> Set (ToVertex (Fold a)) Source #

vertexIntSet :: Fold a -> IntSet Source #

edgeSet :: Fold a -> Set (ToVertex (Fold a), ToVertex (Fold a)) Source #

preSet :: ToVertex (Fold a) -> Fold a -> Set (ToVertex (Fold a)) Source #

preIntSet :: Int -> Fold a -> IntSet Source #

postSet :: ToVertex (Fold a) -> Fold a -> Set (ToVertex (Fold a)) Source #

postIntSet :: Int -> Fold a -> IntSet Source #

adjacencyList :: Fold a -> [(ToVertex (Fold a), [ToVertex (Fold a)])] Source #

adjacencyMap :: Fold a -> Map (ToVertex (Fold a)) (Set (ToVertex (Fold a))) Source #

adjacencyIntMap :: Fold a -> IntMap IntSet Source #

adjacencyMapTranspose :: Fold a -> Map (ToVertex (Fold a)) (Set (ToVertex (Fold a))) Source #

adjacencyIntMapTranspose :: Fold a -> IntMap IntSet Source #

dfsForest :: Fold a -> Forest (ToVertex (Fold a)) Source #

dfsForestFrom :: [ToVertex (Fold a)] -> Fold a -> Forest (ToVertex (Fold a)) Source #

dfs :: [ToVertex (Fold a)] -> Fold a -> [ToVertex (Fold a)] Source #

reachable :: ToVertex (Fold a) -> Fold a -> [ToVertex (Fold a)] Source #

topSort :: Fold a -> Maybe [ToVertex (Fold a)] Source #

isAcyclic :: Fold a -> Bool Source #

toAdjacencyMap :: Fold a -> AdjacencyMap (ToVertex (Fold a)) Source #

toAdjacencyMapTranspose :: Fold a -> AdjacencyMap (ToVertex (Fold a)) Source #

toAdjacencyIntMap :: Fold a -> AdjacencyIntMap Source #

toAdjacencyIntMapTranspose :: Fold a -> AdjacencyIntMap Source #

isDfsForestOf :: Forest (ToVertex (Fold a)) -> Fold a -> Bool Source #

isTopSortOf :: [ToVertex (Fold a)] -> Fold a -> Bool Source #

Graph (Fold a) Source # 
Instance details

Defined in Algebra.Graph.Class

Associated Types

type Vertex (Fold a) :: Type Source #

Methods

empty :: Fold a Source #

vertex :: Vertex (Fold a) -> Fold a Source #

overlay :: Fold a -> Fold a -> Fold a Source #

connect :: Fold a -> Fold a -> Fold a Source #

type ToVertex (Fold a) Source # 
Instance details

Defined in Algebra.Graph.Fold

type ToVertex (Fold a) = a
type Vertex (Fold a) Source # 
Instance details

Defined in Algebra.Graph.Class

type Vertex (Fold a) = a

Basic graph construction primitives

empty :: Fold a Source #

Construct the empty graph. Complexity: O(1) time, memory and size.

isEmpty     empty == True
hasVertex x empty == False
vertexCount empty == 0
edgeCount   empty == 0
size        empty == 1

vertex :: a -> Fold a Source #

Construct the graph comprising a single isolated vertex. Complexity: O(1) time, memory and size.

isEmpty     (vertex x) == False
hasVertex x (vertex x) == True
vertexCount (vertex x) == 1
edgeCount   (vertex x) == 0
size        (vertex x) == 1

edge :: a -> a -> Fold a Source #

Construct the graph comprising a single edge. Complexity: O(1) time, memory and size.

edge x y               == connect (vertex x) (vertex y)
hasEdge x y (edge x y) == True
edgeCount   (edge x y) == 1
vertexCount (edge 1 1) == 1
vertexCount (edge 1 2) == 2

overlay :: Fold a -> Fold a -> Fold a Source #

Overlay two graphs. This is a commutative, associative and idempotent operation with the identity empty. Complexity: O(1) time and memory, O(s1 + s2) size.

isEmpty     (overlay x y) == isEmpty   x   && isEmpty   y
hasVertex z (overlay x y) == hasVertex z x || hasVertex z y
vertexCount (overlay x y) >= vertexCount x
vertexCount (overlay x y) <= vertexCount x + vertexCount y
edgeCount   (overlay x y) >= edgeCount x
edgeCount   (overlay x y) <= edgeCount x   + edgeCount y
size        (overlay x y) == size x        + size y
vertexCount (overlay 1 2) == 2
edgeCount   (overlay 1 2) == 0

connect :: Fold a -> Fold a -> Fold a Source #

Connect two graphs. This is an associative operation with the identity empty, which distributes over overlay and obeys the decomposition axiom. Complexity: O(1) time and memory, O(s1 + s2) size. Note that the number of edges in the resulting graph is quadratic with respect to the number of vertices of the arguments: m = O(m1 + m2 + n1 * n2).

isEmpty     (connect x y) == isEmpty   x   && isEmpty   y
hasVertex z (connect x y) == hasVertex z x || hasVertex z y
vertexCount (connect x y) >= vertexCount x
vertexCount (connect x y) <= vertexCount x + vertexCount y
edgeCount   (connect x y) >= edgeCount x
edgeCount   (connect x y) >= edgeCount y
edgeCount   (connect x y) >= vertexCount x * vertexCount y
edgeCount   (connect x y) <= vertexCount x * vertexCount y + edgeCount x + edgeCount y
size        (connect x y) == size x        + size y
vertexCount (connect 1 2) == 2
edgeCount   (connect 1 2) == 1

vertices :: [a] -> Fold a Source #

Construct the graph comprising a given list of isolated vertices. Complexity: O(L) time, memory and size, where L is the length of the given list.

vertices []            == empty
vertices [x]           == vertex x
hasVertex x . vertices == elem x
vertexCount . vertices == length . nub
vertexSet   . vertices == Set.fromList

edges :: [(a, a)] -> Fold a Source #

Construct the graph from a list of edges. Complexity: O(L) time, memory and size, where L is the length of the given list.

edges []          == empty
edges [(x,y)]     == edge x y
edgeCount . edges == length . nub

overlays :: [Fold a] -> Fold a Source #

Overlay a given list of graphs. Complexity: O(L) time and memory, and O(S) size, where L is the length of the given list, and S is the sum of sizes of the graphs in the list.

overlays []        == empty
overlays [x]       == x
overlays [x,y]     == overlay x y
overlays           == foldr overlay empty
isEmpty . overlays == all isEmpty

connects :: [Fold a] -> Fold a Source #

Connect a given list of graphs. Complexity: O(L) time and memory, and O(S) size, where L is the length of the given list, and S is the sum of sizes of the graphs in the list.

connects []        == empty
connects [x]       == x
connects [x,y]     == connect x y
connects           == foldr connect empty
isEmpty . connects == all isEmpty

Graph folding

foldg :: b -> (a -> b) -> (b -> b -> b) -> (b -> b -> b) -> Fold a -> b Source #

Generalised Graph folding: recursively collapse a Graph by applying the provided functions to the leaves and internal nodes of the expression. The order of arguments is: empty, vertex, overlay and connect. Complexity: O(s) applications of given functions. As an example, the complexity of size is O(s), since all functions have cost O(1).

foldg empty vertex        overlay connect        == id
foldg empty vertex        overlay (flip connect) == transpose
foldg 1     (const 1)     (+)     (+)            == size
foldg True  (const False) (&&)    (&&)           == isEmpty
foldg False (== x)        (||)    (||)           == hasVertex x

Relations on graphs

isSubgraphOf :: Ord a => Fold a -> Fold a -> Bool Source #

The isSubgraphOf function takes two graphs and returns True if the first graph is a subgraph of the second. Complexity: O(s + m * log(m)) time. Note that the number of edges m of a graph can be quadratic with respect to the expression size s.

isSubgraphOf empty         x             ==  True
isSubgraphOf (vertex x)    empty         ==  False
isSubgraphOf x             (overlay x y) ==  True
isSubgraphOf (overlay x y) (connect x y) ==  True
isSubgraphOf (path xs)     (circuit xs)  ==  True
isSubgraphOf x y                         ==> x <= y

Graph properties

isEmpty :: Fold a -> Bool Source #

Check if a graph is empty. A convenient alias for null. Complexity: O(s) time.

isEmpty empty                       == True
isEmpty (overlay empty empty)       == True
isEmpty (vertex x)                  == False
isEmpty (removeVertex x $ vertex x) == True
isEmpty (removeEdge x y $ edge x y) == False

size :: Fold a -> Int Source #

The size of a graph, i.e. the number of leaves of the expression including empty leaves. Complexity: O(s) time.

size empty         == 1
size (vertex x)    == 1
size (overlay x y) == size x + size y
size (connect x y) == size x + size y
size x             >= 1
size x             >= vertexCount x

hasVertex :: Eq a => a -> Fold a -> Bool Source #

Check if a graph contains a given vertex. Complexity: O(s) time.

hasVertex x empty            == False
hasVertex x (vertex x)       == True
hasVertex 1 (vertex 2)       == False
hasVertex x . removeVertex x == const False

hasEdge :: Eq a => a -> a -> Fold a -> Bool Source #

Check if a graph contains a given edge. Complexity: O(s) time.

hasEdge x y empty            == False
hasEdge x y (vertex z)       == False
hasEdge x y (edge x y)       == True
hasEdge x y . removeEdge x y == const False
hasEdge x y                  == elem (x,y) . edgeList

vertexCount :: Ord a => Fold a -> Int Source #

The number of vertices in a graph. Complexity: O(s * log(n)) time.

vertexCount empty             ==  0
vertexCount (vertex x)        ==  1
vertexCount                   ==  length . vertexList
vertexCount x < vertexCount y ==> x < y

edgeCount :: Ord a => Fold a -> Int Source #

The number of edges in a graph. Complexity: O(s + m * log(m)) time. Note that the number of edges m of a graph can be quadratic with respect to the expression size s.

edgeCount empty      == 0
edgeCount (vertex x) == 0
edgeCount (edge x y) == 1
edgeCount            == length . edgeList

vertexList :: Ord a => Fold a -> [a] Source #

The sorted list of vertices of a given graph. Complexity: O(s * log(n)) time and O(n) memory.

vertexList empty      == []
vertexList (vertex x) == [x]
vertexList . vertices == nub . sort

edgeList :: Ord a => Fold a -> [(a, a)] Source #

The sorted list of edges of a graph. Complexity: O(s + m * log(m)) time and O(m) memory. Note that the number of edges m of a graph can be quadratic with respect to the expression size s.

edgeList empty          == []
edgeList (vertex x)     == []
edgeList (edge x y)     == [(x,y)]
edgeList (star 2 [3,1]) == [(2,1), (2,3)]
edgeList . edges        == nub . sort
edgeList . transpose    == sort . map swap . edgeList

vertexSet :: Ord a => Fold a -> Set a Source #

The set of vertices of a given graph. Complexity: O(s * log(n)) time and O(n) memory.

vertexSet empty      == Set.empty
vertexSet . vertex   == Set.singleton
vertexSet . vertices == Set.fromList

edgeSet :: Ord a => Fold a -> Set (a, a) Source #

The set of edges of a given graph. Complexity: O(s * log(m)) time and O(m) memory.

edgeSet empty      == Set.empty
edgeSet (vertex x) == Set.empty
edgeSet (edge x y) == Set.singleton (x,y)
edgeSet . edges    == Set.fromList

adjacencyList :: Ord a => Fold a -> [(a, [a])] Source #

The sorted adjacency list of a graph. Complexity: O(s + m * log(m)) time. Note that the number of edges m of a graph can be quadratic with respect to the expression size s.

adjacencyList empty          == []
adjacencyList (vertex x)     == [(x, [])]
adjacencyList (edge 1 2)     == [(1, [2]), (2, [])]
adjacencyList (star 2 [3,1]) == [(1, []), (2, [1,3]), (3, [])]
stars . adjacencyList        == id

Standard families of graphs

path :: [a] -> Fold a Source #

The path on a list of vertices. Complexity: O(L) time, memory and size, where L is the length of the given list.

path []        == empty
path [x]       == vertex x
path [x,y]     == edge x y
path . reverse == transpose . path

circuit :: [a] -> Fold a Source #

The circuit on a list of vertices. Complexity: O(L) time, memory and size, where L is the length of the given list.

circuit []        == empty
circuit [x]       == edge x x
circuit [x,y]     == edges [(x,y), (y,x)]
circuit . reverse == transpose . circuit

clique :: [a] -> Fold a Source #

The clique on a list of vertices. Complexity: O(L) time, memory and size, where L is the length of the given list.

clique []         == empty
clique [x]        == vertex x
clique [x,y]      == edge x y
clique [x,y,z]    == edges [(x,y), (x,z), (y,z)]
clique (xs ++ ys) == connect (clique xs) (clique ys)
clique . reverse  == transpose . clique

biclique :: [a] -> [a] -> Fold a Source #

The biclique on two lists of vertices. Complexity: O(L1 + L2) time, memory and size, where L1 and L2 are the lengths of the given lists.

biclique []      []      == empty
biclique [x]     []      == vertex x
biclique []      [y]     == vertex y
biclique [x1,x2] [y1,y2] == edges [(x1,y1), (x1,y2), (x2,y1), (x2,y2)]
biclique xs      ys      == connect (vertices xs) (vertices ys)

star :: a -> [a] -> Fold a Source #

The star formed by a centre vertex connected to a list of leaves. Complexity: O(L) time, memory and size, where L is the length of the given list.

star x []    == vertex x
star x [y]   == edge x y
star x [y,z] == edges [(x,y), (x,z)]
star x ys    == connect (vertex x) (vertices ys)

stars :: [(a, [a])] -> Fold a Source #

The stars formed by overlaying a list of stars. An inverse of adjacencyList. Complexity: O(L) time, memory and size, where L is the total size of the input.

stars []                      == empty
stars [(x, [])]               == vertex x
stars [(x, [y])]              == edge x y
stars [(x, ys)]               == star x ys
stars                         == overlays . map (uncurry star)
stars . adjacencyList         == id
overlay (stars xs) (stars ys) == stars (xs ++ ys)

Graph transformation

removeVertex :: Eq a => a -> Fold a -> Fold a Source #

Remove a vertex from a given graph. Complexity: O(s) time, memory and size.

removeVertex x (vertex x)       == empty
removeVertex 1 (vertex 2)       == vertex 2
removeVertex x (edge x x)       == empty
removeVertex 1 (edge 1 2)       == vertex 2
removeVertex x . removeVertex x == removeVertex x

removeEdge :: Eq a => a -> a -> Fold a -> Fold a Source #

Remove an edge from a given graph. Complexity: O(s) time, memory and size.

removeEdge x y (edge x y)       == vertices [x,y]
removeEdge x y . removeEdge x y == removeEdge x y
removeEdge x y . removeVertex x == removeVertex x
removeEdge 1 1 (1 * 1 * 2 * 2)  == 1 * 2 * 2
removeEdge 1 2 (1 * 1 * 2 * 2)  == 1 * 1 + 2 * 2
size (removeEdge x y z)         <= 3 * size z

transpose :: Fold a -> Fold a Source #

Transpose a given graph. Complexity: O(s) time, memory and size.

transpose empty       == empty
transpose (vertex x)  == vertex x
transpose (edge x y)  == edge y x
transpose . transpose == id
transpose (box x y)   == box (transpose x) (transpose y)
edgeList . transpose  == sort . map swap . edgeList

induce :: (a -> Bool) -> Fold a -> Fold a Source #

Construct the induced subgraph of a given graph by removing the vertices that do not satisfy a given predicate. Complexity: O(s) time, memory and size, assuming that the predicate takes O(1) to be evaluated.

induce (const True ) x      == x
induce (const False) x      == empty
induce (/= x)               == removeVertex x
induce p . induce q         == induce (\x -> p x && q x)
isSubgraphOf (induce p x) x == True

simplify :: Ord a => Fold a -> Fold a Source #

Simplify a graph expression. Semantically, this is the identity function, but it simplifies a given polymorphic graph expression according to the laws of the algebra. The function does not compute the simplest possible expression, but uses heuristics to obtain useful simplifications in reasonable time. Complexity: the function performs O(s) graph comparisons. It is guaranteed that the size of the result does not exceed the size of the given expression. Below the operator ~> denotes the is simplified to relation.

simplify             == id
size (simplify x)    <= size x
simplify empty       ~> empty
simplify 1           ~> 1
simplify (1 + 1)     ~> 1
simplify (1 + 2 + 1) ~> 1 + 2
simplify (1 * 1 * 1) ~> 1 * 1