11{-# LANGUAGE TypeFamilies #-}
2- {-# LANGUAGE TupleSections #-}
32{-# LANGUAGE FlexibleInstances #-}
4- {-# LANGUAGE DeriveGeneric #-}
5- {-# LANGUAGE ViewPatterns #-}
3+ {-# LANGUAGE UnicodeSyntax #-}
64{-# LANGUAGE GeneralizedNewtypeDeriving #-}
75{-# LANGUAGE DeriveTraversable #-}
6+ {-# LANGUAGE RankNTypes #-}
7+ {-# LANGUAGE QuantifiedConstraints #-}
8+ {-# LANGUAGE StandaloneDeriving #-}
9+ {-# LANGUAGE UndecidableInstances #-}
810{-|
911
1012Module defining e-nodes ('ENode'), the e-node function symbol ('Operator'), and
@@ -13,7 +15,6 @@ mappings from e-nodes ('NodeMap').
1315-}
1416module Data.Equality.Graph.Nodes where
1517
16- import Data.Functor.Classes
1718import Data.Foldable
1819import Data.Bifunctor
1920
@@ -34,6 +35,10 @@ import Data.Equality.Graph.Classes.Id
3435-- parametrized over 'ClassId', i.e. all recursive fields are rather e-class ids.
3536newtype ENode l = Node { unNode :: l ClassId }
3637
38+ deriving instance Eq (l ClassId ) => (Eq (ENode l ))
39+ deriving instance Ord (l ClassId ) => (Ord (ENode l ))
40+ deriving instance Show (l ClassId ) => (Show (ENode l ))
41+
3742-- | Get the children e-class ids of an e-node
3843children :: Traversable l => ENode l -> [ClassId ]
3944children = toList . unNode
@@ -45,68 +50,54 @@ children = toList . unNode
4550-- this means children e-classes are ignored.
4651newtype Operator l = Operator { unOperator :: l () }
4752
53+ deriving instance Eq (l () ) => (Eq (Operator l ))
54+ deriving instance Ord (l () ) => (Ord (Operator l ))
55+ deriving instance Show (l () ) => (Show (Operator l ))
56+
4857-- | Get the operator (function symbol) of an e-node
4958operator :: Traversable l => ENode l -> Operator l
5059operator = Operator . void . unNode
5160{-# INLINE operator #-}
5261
53- instance Eq1 l => (Eq (ENode l )) where
54- (==) (Node a) (Node b) = liftEq (==) a b
55- {-# INLINE (==) #-}
56-
57- instance Ord1 l => (Ord (ENode l )) where
58- compare (Node a) (Node b) = liftCompare compare a b
59- {-# INLINE compare #-}
60-
61- instance Show1 l => (Show (ENode l )) where
62- showsPrec p (Node l) = liftShowsPrec showsPrec showList p l
63-
64- instance Eq1 l => (Eq (Operator l )) where
65- (==) (Operator a) (Operator b) = liftEq (\ _ _ -> True ) a b
66- {-# INLINE (==) #-}
67-
68- instance Ord1 l => (Ord (Operator l )) where
69- compare (Operator a) (Operator b) = liftCompare (\ _ _ -> EQ ) a b
70- {-# INLINE compare #-}
71-
72- instance Show1 l => (Show (Operator l )) where
73- showsPrec p (Operator l) = liftShowsPrec (const . const $ showString " " ) (const $ showString " " ) p l
74-
7562-- * Node Map
7663
7764-- | A mapping from e-nodes of @l@ to @a@
7865newtype NodeMap (l :: Type -> Type ) a = NodeMap { unNodeMap :: M. Map (ENode l ) a }
7966-- TODO: Investigate whether it would be worth it requiring a trie-map for the
8067-- e-node definition. Probably it isn't better since e-nodes aren't recursive.
81- deriving (Show , Functor , Foldable , Traversable , Semigroup , Monoid )
68+ deriving (Functor , Foldable , Traversable )
69+
70+ deriving instance (Show a , Show (l ClassId )) => Show (NodeMap l a )
71+ deriving instance Ord (l ClassId ) => Semigroup (NodeMap l a )
72+ deriving instance Ord (l ClassId ) => Monoid (NodeMap l a )
8273
8374-- | Insert a value given an e-node in a 'NodeMap'
84- insertNM :: Ord1 l => ENode l -> a -> NodeMap l a -> NodeMap l a
75+ insertNM :: Ord ( l ClassId ) => ENode l -> a -> NodeMap l a -> NodeMap l a
8576insertNM e v (NodeMap m) = NodeMap (M. insert e v m)
8677{-# INLINE insertNM #-}
8778
8879-- | Lookup an e-node in a 'NodeMap'
89- lookupNM :: Ord1 l => ENode l -> NodeMap l a -> Maybe a
80+ lookupNM :: Ord ( l ClassId ) => ENode l -> NodeMap l a -> Maybe a
9081lookupNM e = M. lookup e . unNodeMap
9182{-# INLINE lookupNM #-}
9283
9384-- | Delete an e-node in a 'NodeMap'
94- deleteNM :: Ord1 l => ENode l -> NodeMap l a -> NodeMap l a
85+ deleteNM :: Ord ( l ClassId ) => ENode l -> NodeMap l a -> NodeMap l a
9586deleteNM e (NodeMap m) = NodeMap (M. delete e m)
9687{-# INLINE deleteNM #-}
9788
9889-- | Insert a value and lookup by e-node in a 'NodeMap'
99- insertLookupNM :: Ord1 l => ENode l -> a -> NodeMap l a -> (Maybe a , NodeMap l a )
90+ insertLookupNM :: Ord ( l ClassId ) => ENode l -> a -> NodeMap l a -> (Maybe a , NodeMap l a )
10091insertLookupNM e v (NodeMap m) = second NodeMap $ M. insertLookupWithKey (\ _ a _ -> a) e v m
10192{-# INLINE insertLookupNM #-}
10293
10394-- | As 'Data.Map.foldlWithKeyNM'' but in a 'NodeMap'
104- foldlWithKeyNM' :: Ord1 l => (b -> ENode l -> a -> b ) -> b -> NodeMap l a -> b
95+ foldlWithKeyNM' :: Ord ( l ClassId ) => (b -> ENode l -> a -> b ) -> b -> NodeMap l a -> b
10596foldlWithKeyNM' f b = M. foldlWithKey' f b . unNodeMap
10697{-# INLINE foldlWithKeyNM' #-}
10798
10899-- | As 'Data.Map.foldrWithKeyNM'' but in a 'NodeMap'
109- foldrWithKeyNM' :: Ord1 l => (ENode l -> a -> b -> b ) -> b -> NodeMap l a -> b
100+ foldrWithKeyNM' :: Ord ( l ClassId ) => (ENode l -> a -> b -> b ) -> b -> NodeMap l a -> b
110101foldrWithKeyNM' f b = M. foldrWithKey' f b . unNodeMap
111102{-# INLINE foldrWithKeyNM' #-}
112103
0 commit comments