【发布时间】:2017-12-06 21:09:35
【问题描述】:
我在 Haskell 中有以下内容(作为最小示例)。
{-# LANGUAGE GADTs #-}
import Data.Dynamic
data Expr a where
Lift :: (Show a) => a -> Expr a --Lift some type into Expr
Lam :: (Expr a -> Expr b) -> Expr (a -> b)
Const :: Expr a -> Expr b -> Expr a
当我尝试从以下创建动态时,我收到一个错误,我不确定如何更正。
--This is the code
toDyn $ (Lam (Const (Lift 1)))
-- This is the error
-- • No instance for (Typeable b0) arising from a use of ‘toDyn’
-- • In the expression: toDyn (Lam (Const (Lift 1)))
-- In an equation for ‘it’: it = toDyn (Lam (Const (Lift 1)))
有没有办法解决这个问题?其他构造函数都可以正常工作(我的实际程序有超过 100 个其他构造函数!)但是 Const 真的给我带来了麻烦!
【问题讨论】:
-
我猜你需要给那个
(Lam ...)提供一个特定的类型,否则太笼统了,只能给一个多态类型。基本上,您必须在Const应用程序中选择b是什么。