Permalink
Browse files

Update to work with hashable-1.2

Also do not use lookupDefault for error reporting
  • Loading branch information...
1 parent e4f30f8 commit b584eed51fd1a06e7f2912769d915551b6a6dbae @travitch committed Jan 2, 2013
@@ -10,8 +10,8 @@ data Binding = B {- Bound -} | F {- Free -}
deriving (Eq, Ord, Show)
instance Hashable Binding where
- hash B = 105
- hash F = 709
+ hashWithSalt s B = s `hashWithSalt` (105 :: Int)
+ hashWithSalt s F = s `hashWithSalt` (709 :: Int)
newtype BindingPattern = BindingPattern { bindingPattern :: [Binding] }
deriving (Eq, Ord)
@@ -20,14 +20,16 @@ instance Show BindingPattern where
show (BindingPattern bs) = concatMap show bs
instance Hashable BindingPattern where
- hash (BindingPattern bs) = hash bs
+ hashWithSalt s (BindingPattern bs) = s `hashWithSalt` bs
data Adornment = Free !Int -- ^ The index to bind a free variable
| BoundAtom
| Bound !Int -- ^ The index to look for the binding of this variable
deriving (Eq, Show)
instance Hashable Adornment where
- hash (Free i) = 1 `combine` hash i
- hash BoundAtom = 7776
- hash (Bound i) = 2 `combine` hash i
+ hashWithSalt s BoundAtom = s `hashWithSalt` (7776 :: Int)
+ hashWithSalt s (Free i) =
+ s `hashWithSalt` (1 :: Int) `hashWithSalt` i
+ hashWithSalt s (Bound i) =
+ s `hashWithSalt` (2 :: Int) `hashWithSalt` i
@@ -42,7 +42,7 @@ newtype Tuple a = Tuple { unTuple :: [a] }
deriving (Eq, Show)
instance (Hashable a) => Hashable (Tuple a) where
- hash (Tuple es) = hash es
+ hashWithSalt s (Tuple es) = s `hashWithSalt` es
-- | A relation whose elements are fixed-length lists of a
-- user-defined type. This is only used internally and is not exposed
@@ -99,7 +99,7 @@ orderRules rs = map toList (stronglyConnComp deps)
toList (CyclicSCC rss) = rss
toKeyM = HM.fromList (zip rs [0..])
toKey :: Rule a -> Int
- toKey r = HM.lookupDefault (error "Missing toKeyM entry") r toKeyM
+ toKey r = fromMaybe (error "Missing toKeyM entry") $ HM.lookup r toKeyM
deps = foldr toContext [] rs
toContext r@(Rule _ b _) acc =
@@ -9,6 +9,7 @@ import qualified Data.HashMap.Strict as HM
import Data.HashSet ( HashSet )
import qualified Data.HashSet as HS
import Data.List ( foldl' )
+import Data.Maybe ( fromMaybe )
import Data.Monoid
import Data.Sequence ( Seq, (><), ViewL(..) )
import qualified Data.Sequence as S
@@ -114,7 +115,7 @@ magicSetsRules q rs =
-- Already processed this binding pattern
Just _ -> transformRules rest generated
Nothing -> do
- let matchingRules = HM.lookupDefault (error "No rules for pattern") (queryPatternRelation elt) rawRules
+ let matchingRules = fromMaybe (error "No rules for pattern") $ HM.lookup (queryPatternRelation elt) rawRules
(magic, newWork) <- foldM (magicTransform elt) (mempty, mempty) matchingRules
transformRules (rest >< newWork) (HM.insert elt magic generated)
@@ -244,7 +245,8 @@ data QueryPattern = QueryPattern { queryPatternRelation :: Relation
deriving (Eq, Show)
instance Hashable QueryPattern where
- hash (QueryPattern r bs) = hash r `combine` hash bs
+ hashWithSalt s (QueryPattern r bs) =
+ s `hashWithSalt` r `hashWithSalt` bs
hasBinding :: QueryPattern -> Bool
hasBinding (QueryPattern _ bs) = any (==B) (bindingPattern bs)
@@ -34,5 +34,7 @@ instance Show Relation where
-- translation
instance Hashable Relation where
- hash (Relation t) = 99 `combine` hash t
- hash (MagicRelation p t) = 2 `combine` hash t `combine` hash p
+ hashWithSalt s (Relation t) =
+ s `hashWithSalt` t `hashWithSalt` (99 :: Int)
+ hashWithSalt s (MagicRelation p t) =
+ s `hashWithSalt` p `hashWithSalt` (2 :: Int)
@@ -94,11 +94,14 @@ instance (Show a) => Show (Term a) where
show (FreshVar _) = "*"
instance (Hashable a) => Hashable (Term a) where
- hash (LogicVar t) = hash t `combine` 1
- hash (BindVar t) = hash t `combine` 2
- hash (Atom a) = hash a
- hash Anything = 99
- hash (FreshVar i) = 22 `combine` hash i
+ hashWithSalt s (LogicVar t) =
+ s `hashWithSalt` t `hashWithSalt` (1 :: Int)
+ hashWithSalt s (BindVar t) =
+ s `hashWithSalt` t `hashWithSalt` (2 :: Int)
+ hashWithSalt s (Atom a) = s `hashWithSalt` a
+ hashWithSalt s Anything = s `hashWithSalt` (99 :: Int)
+ hashWithSalt s (FreshVar i) =
+ s `hashWithSalt` i `hashWithSalt` (22 :: Int)
instance (Eq a) => Eq (Term a) where
(LogicVar t1) == (LogicVar t2) = t1 == t2
@@ -128,7 +131,8 @@ instance (Eq a) => Eq (AdornedClause a) where
(AdornedClause r1 cs1) == (AdornedClause r2 cs2) = r1 == r2 && cs1 == cs2
instance (Hashable a) => Hashable (AdornedClause a) where
- hash (AdornedClause r ts) = hash r `combine` hash ts
+ hashWithSalt s (AdornedClause r ts) =
+ s `hashWithSalt` r `hashWithSalt` ts
instance (Show a) => Show (AdornedClause a) where
show (AdornedClause p ats) =
@@ -157,10 +161,12 @@ instance (Eq a, Eq (ctype a)) => Eq (Literal ctype a) where
_ == _ = False
instance (Hashable a, Hashable (ctype a)) => Hashable (Literal ctype a) where
- hash (Literal c) = 1 `combine` hash c
- hash (NegatedLiteral c) = 2 `combine` hash c
- hash (ConditionalClause cid _ ts vm) =
- 3 `combine` hash cid `combine` hash ts `combine` hash (HM.size vm)
+ hashWithSalt s (Literal c) =
+ s `hashWithSalt` c `hashWithSalt` (1 :: Int)
+ hashWithSalt s (NegatedLiteral c) =
+ s `hashWithSalt` c `hashWithSalt` (2 :: Int)
+ hashWithSalt s (ConditionalClause cid _ ts vm) =
+ s `hashWithSalt` cid `hashWithSalt` ts `hashWithSalt` HM.size vm
lit :: (Failure DatalogError m) => Relation -> [Term a] -> QueryBuilder m a (Literal Clause a)
lit p ts = return $ Literal $ Clause p ts
@@ -229,7 +235,8 @@ instance (Eq a) => Eq (Rule a) where
h1 == h2 && b1 == b2 && vms1 == vms2
instance (Hashable a) => Hashable (Rule a) where
- hash (Rule h b vms) = hash h `combine` hash b `combine` hash (HM.size vms)
+ hashWithSalt s (Rule h b vms) =
+ s `hashWithSalt` h `hashWithSalt` b `hashWithSalt` HM.size vms
newtype Query a = Query { unQuery :: Clause a }
View
@@ -29,11 +29,11 @@ data WorkInfo = EID !Int -- id
deriving (Eq, Ord, Show)
instance Hashable WorkInfo where
- hash (EID i) = 1 `combine` hash i
- hash (EN n) = 2 `combine` hash n
- hash (EP p) = 3 `combine` hash p
- hash (J j) = 4 `combine` hash j
- hash (EA a) = 5 `combine` hash a
+ hashWithSalt s (EID i) = s `hashWithSalt` i `hashWithSalt` (1 :: Int)
+ hashWithSalt s (EN n) = s `hashWithSalt` n `hashWithSalt` (2 :: Int)
+ hashWithSalt s (EP p) = s `hashWithSalt` p `hashWithSalt` (3 :: Int)
+ hashWithSalt s (J j) = s `hashWithSalt` j `hashWithSalt` (4 :: Int)
+ hashWithSalt s (EA a) = s `hashWithSalt` a `hashWithSalt` (5 :: Int)
db1 :: Maybe (Database WorkInfo)
db1 = makeDatabase $ do

0 comments on commit b584eed

Please sign in to comment.