th-instance-reification 0.1.5 → 0.1.5.1
raw patch · 3 files changed
+18/−23 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- src/THInstanceReification.hs +12/−22
- test/Main.hs +5/−0
- th-instance-reification.cabal +1/−1
src/THInstanceReification.hs view
@@ -1,4 +1,3 @@-{-# LANGUAGE CPP #-} -- | -- Fixed versions of 'reifyInstances' and 'isInstance' as per -- the following ghc issue:@@ -41,14 +40,14 @@ -- Expand type synonyms in type signatures, -- using 'expandSyns' from the "th-expand-syns" library: expandedTypes <- mapM expandSyns tl- expendedInstanceTypes <- mapM expandSyns htl+ expandedInstanceTypes <- mapM expandSyns htl maybe (fail $ "Unmatching amounts of types: " <> show expandedTypes <> ", " <> - show expendedInstanceTypes)+ show expandedInstanceTypes) (analyze context) -- 'pair' is a safe version of 'zip' from the "list-extras" library, -- which returns 'Nothing', when lists differ in size.- (pair expandedTypes expendedInstanceTypes)+ (pair expandedTypes expandedInstanceTypes) d -> fail $ "Not an instance dec: " <> show d where -- |@@ -60,10 +59,8 @@ -- | -- A partial function, -- which returns a tested type by a variable name.- actualTypeByVarName :: Name -> Type- actualTypeByVarName = \n ->- Map.lookup n m ?: - (error $ "Unexpected key: " <> show n <> ", in a map: " <> show m)+ actualTypeByVarName :: Name -> Maybe Type+ actualTypeByVarName = \n -> Map.lookup n m where -- A memoization cache. m = Map.fromList $ concat $ map accRecords $ typeAssocs@@ -75,25 +72,18 @@ accRecords = \case (AppT al ar, AppT hl hr) -> accRecords (al, hl) ++ accRecords (ar, hr) (a, VarT n) -> [(n, a)]- (a, h) | a /= h -> error $ "Unmatching types: " <> show a <> ", " <> show h _ -> [] -- | -- Test a predicate by substituting all type vars with associated -- tested types. analyzePredicate :: Pred -> Q Bool analyzePredicate = \case-#if MIN_VERSION_template_haskell(2,10,0) AppT (AppT EqualityT _) _ -> return True- AppT (ConT n) t -> do- let t' = replaceTypeVars actualTypeByVarName t- isProperInstance n [t']+ AppT (ConT n) t ->+ case replaceTypeVars actualTypeByVarName t of+ Just t' -> isProperInstance n [t']+ _ -> return False _ -> return True-#else- EqualP _ _ -> return True- ClassP n tl -> do- let tl' = map (replaceTypeVars actualTypeByVarName) tl- isProperInstance n tl'-#endif unapplyType :: Type -> [Type] unapplyType = \case@@ -101,9 +91,9 @@ t -> [t] -- | Deeply traverse the type signature and replace all vars in it.-replaceTypeVars :: (Name -> Type) -> Type -> Type+replaceTypeVars :: (Name -> Maybe Type) -> Type -> Maybe Type replaceTypeVars f = \case- AppT l r -> AppT (replaceTypeVars f l) (replaceTypeVars f r)+ AppT l r -> AppT <$> replaceTypeVars f l <*> replaceTypeVars f r VarT n -> f n- t -> t+ t -> Just t
test/Main.hs view
@@ -14,6 +14,11 @@ main = defaultMain $ testGroup "" $ [+ testCase "Issue #6" $ do+ assertBool "" $ read $( do+ r <- isProperInstance ''Monoid [VarT (mkName "v")]+ stringE $ show $ r+ ), testCase "existingInstance" $ do assertBool "" $ read $( do t <- [t| (Int, Int) |]
th-instance-reification.cabal view
@@ -1,5 +1,5 @@ name: th-instance-reification-version: 0.1.5+version: 0.1.5.1 synopsis: Fixed versions of instances reification functions description: Provides fixed versions of 'reifyInstances' and 'isInstance' as per