@@ -52,17 +52,19 @@ deriveInstances (Module ss coms mn ds exts) = Module ss coms mn <$> mapM (derive
5252deriveInstance :: (Functor m , MonadError MultipleErrors m , MonadSupply m ) => ModuleName -> [Declaration ] -> Declaration -> m Declaration
5353deriveInstance mn ds (TypeInstanceDeclaration nm deps className tys@ [ty] DerivedInstance )
5454 | className == Qualified (Just dataGeneric) (ProperName C. generic)
55- , Just (Qualified mn' tyCon) <- unwrapTypeConstructor ty
55+ , Just (Qualified mn' tyCon, args ) <- unwrapTypeConstructor ty
5656 , mn == fromMaybe mn mn'
57- = TypeInstanceDeclaration nm deps className tys . ExplicitInstance <$> deriveGeneric mn ds tyCon
57+ = TypeInstanceDeclaration nm deps className tys . ExplicitInstance <$> deriveGeneric mn ds tyCon args
5858deriveInstance _ _ (TypeInstanceDeclaration _ _ className tys DerivedInstance )
5959 = throwError . errorMessage $ CannotDerive className tys
6060deriveInstance mn ds (PositionedDeclaration pos com d) = PositionedDeclaration pos com <$> deriveInstance mn ds d
6161deriveInstance _ _ e = return e
6262
63- unwrapTypeConstructor :: Type -> Maybe (Qualified ProperName )
64- unwrapTypeConstructor (TypeConstructor tyCon) = Just tyCon
65- unwrapTypeConstructor (TypeApp ty (TypeVar _)) = unwrapTypeConstructor ty
63+ unwrapTypeConstructor :: Type -> Maybe (Qualified ProperName , [Type ])
64+ unwrapTypeConstructor (TypeConstructor tyCon) = Just (tyCon, [] )
65+ unwrapTypeConstructor (TypeApp ty arg) = do
66+ (tyCon, args) <- unwrapTypeConstructor ty
67+ return (tyCon, arg : args)
6668unwrapTypeConstructor _ = Nothing
6769
6870dataGeneric :: ModuleName
@@ -71,12 +73,12 @@ dataGeneric = ModuleName [ ProperName "Data", ProperName "Generic" ]
7173dataMaybe :: ModuleName
7274dataMaybe = ModuleName [ ProperName " Data" , ProperName " Maybe" ]
7375
74- deriveGeneric :: (Functor m , MonadError MultipleErrors m , MonadSupply m ) => ModuleName -> [Declaration ] -> ProperName -> m [Declaration ]
75- deriveGeneric mn ds tyConNm = do
76+ deriveGeneric :: (Functor m , MonadError MultipleErrors m , MonadSupply m ) => ModuleName -> [Declaration ] -> ProperName -> [ Type ] -> m [Declaration ]
77+ deriveGeneric mn ds tyConNm args = do
7678 tyCon <- findTypeDecl tyConNm ds
7779 toSpine <- mkSpineFunction mn tyCon
7880 fromSpine <- mkFromSpineFunction mn tyCon
79- let toSignature = mkSignatureFunction mn tyCon
81+ let toSignature = mkSignatureFunction mn tyCon args
8082 return [ ValueDeclaration (Ident C. toSpine) Public [] (Right toSpine)
8183 , ValueDeclaration (Ident C. fromSpine) Public [] (Right fromSpine)
8284 , ValueDeclaration (Ident C. toSignature) Public [] (Right toSignature)
@@ -118,8 +120,8 @@ mkSpineFunction mn (DataDeclaration _ _ _ args) = lamCase "$x" <$> mapM mkCtorCl
118120mkSpineFunction mn (PositionedDeclaration _ _ d) = mkSpineFunction mn d
119121mkSpineFunction _ _ = internalError " mkSpineFunction: expected DataDeclaration"
120122
121- mkSignatureFunction :: ModuleName -> Declaration -> Expr
122- mkSignatureFunction mn (DataDeclaration _ name _ args) = lamNull . mkSigProd $ map mkProdClause args
123+ mkSignatureFunction :: ModuleName -> Declaration -> [ Type ] -> Expr
124+ mkSignatureFunction mn (DataDeclaration _ name tyArgs args) classArgs = lamNull . mkSigProd $ map mkProdClause args
123125 where
124126 mkSigProd :: [Expr ] -> Expr
125127 mkSigProd = App (App (Constructor (Qualified (Just dataGeneric) (ProperName " SigProd" )))
@@ -134,7 +136,7 @@ mkSignatureFunction mn (DataDeclaration _ name _ args) = lamNull . mkSigProd $ m
134136
135137 mkProdClause :: (ProperName , [Type ]) -> Expr
136138 mkProdClause (ctorName, tys) = ObjectLiteral [ (" sigConstructor" , StringLiteral (showQualified runProperName (Qualified (Just mn) ctorName)))
137- , (" sigValues" , ArrayLiteral . map mkProductSignature $ tys)
139+ , (" sigValues" , ArrayLiteral . map ( mkProductSignature . instantiate) $ tys)
138140 ]
139141
140142 mkProductSignature :: Type -> Expr
@@ -146,8 +148,9 @@ mkSignatureFunction mn (DataDeclaration _ name _ args) = lamNull . mkSigProd $ m
146148 ]
147149 mkProductSignature typ = lamNull $ App (mkGenVar C. toSignature)
148150 (TypedValue False (mkGenVar " anyProxy" ) (proxy typ))
149- mkSignatureFunction mn (PositionedDeclaration _ _ d) = mkSignatureFunction mn d
150- mkSignatureFunction _ _ = internalError " mkSignatureFunction: expected DataDeclaration"
151+ instantiate = replaceAllTypeVars (zipWith (\ (arg, _) ty -> (arg, ty)) tyArgs classArgs)
152+ mkSignatureFunction mn (PositionedDeclaration _ _ d) classArgs = mkSignatureFunction mn d classArgs
153+ mkSignatureFunction _ _ _ = internalError " mkSignatureFunction: expected DataDeclaration"
151154
152155mkFromSpineFunction :: forall m . (Functor m , MonadSupply m ) => ModuleName -> Declaration -> m Expr
153156mkFromSpineFunction mn (DataDeclaration _ _ _ args) = lamCase " $x" <$> (addCatch <$> mapM mkAlternative args)
0 commit comments