Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overlays without the empty leaf at the end #103

Merged
merged 25 commits into from
Aug 10, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/Algebra/Graph.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ import qualified Data.IntSet as IntSet
import qualified Data.Set as Set
import qualified Data.Tree as Tree

import Data.List.NonEmpty (nonEmpty)

{-| The 'Graph' data type is a deep embedding of the core graph construction
primitives 'empty', 'vertex', 'overlay' and 'connect'. We define a 'Num'
instance as a convenient notation for working with graphs:
Expand Down Expand Up @@ -317,7 +319,7 @@ edges = overlays . map (uncurry edge)
-- 'isEmpty' . overlays == 'all' 'isEmpty'
-- @
overlays :: [Graph a] -> Graph a
overlays = foldr overlay empty
overlays = concatg overlay

-- | Connect a given list of graphs.
-- Complexity: /O(L)/ time and memory, and /O(S)/ size, where /L/ is the length
Expand All @@ -331,7 +333,21 @@ overlays = foldr overlay empty
-- 'isEmpty' . connects == 'all' 'isEmpty'
-- @
connects :: [Graph a] -> Graph a
connects = foldr connect empty
connects = concatg connect

concatg :: (Graph a -> Graph a -> Graph a) -> [Graph a] -> Graph a
concatg combine = maybe empty (foldr1fId combine) . nonEmpty
{-# INLINE [0] concatg #-}

{-# RULES
"concatg/map"
forall c f xs.
concatg c (map f xs) = concatgMap c f xs
#-}

-- | Utilitary function for rewrite rules of 'overlays' and 'connects'
concatgMap :: (Graph a -> Graph a -> Graph a) -> (b -> Graph a) -> [b] -> Graph a
concatgMap combine f = maybe empty (foldr1f combine f) . nonEmpty

-- | Generalised 'Graph' folding: recursively collapse a 'Graph' by applying
-- the provided functions to the leaves and internal nodes of the expression.
Expand Down
19 changes: 18 additions & 1 deletion src/Algebra/Graph/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ module Algebra.Graph.Internal (
List (..),

-- * Data structures for graph traversal
Focus (..), emptyFocus, vertexFocus, overlayFoci, connectFoci, Hit (..)
Focus (..), emptyFocus, vertexFocus, overlayFoci, connectFoci, Hit (..),

-- Special fold
foldr1f, foldr1fId
) where

import Prelude ()
Expand All @@ -31,6 +34,8 @@ import Data.Semigroup

import qualified GHC.Exts as Exts

import Data.List.NonEmpty (NonEmpty (..))

-- | An abstract list data type with /O(1)/ time concatenation (the current
-- implementation uses difference lists). Here @a@ is the type of list elements.
-- 'List' @a@ is a 'Monoid': 'mempty' corresponds to the empty list and two lists
Expand Down Expand Up @@ -107,3 +112,15 @@ connectFoci x y = Focus (ok x || ok y) (xs <> is y) (os x <> ys) (vs x <> vs y)
-- | An auxiliary data type for 'hasEdge': when searching for an edge, we can hit
-- its 'Tail', i.e. the source vertex, the whole 'Edge', or 'Miss' it entirely.
data Hit = Miss | Tail | Edge deriving (Eq, Ord)

foldr1f :: (a -> a -> a) -> (b -> a) -> NonEmpty b -> a
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add Haddock comments.

foldr1f k f = go
where
go (y :| ys) =
case ys of
[] -> f y
(x:xs) -> f y `k` go (x :| xs)
{-# INLINE foldr1f #-}

foldr1fId :: (a -> a -> a) -> NonEmpty a -> a
foldr1fId k = foldr1f k id