对haskell的类型系统,还有一个困惑。

发布于 2022-08-19 12:06:14 字数 391 浏览 10 评论 9

haskell是静态类型,所以会有这样一个问题
class A t where
  funA :: t -> IO ()
instance A W where
  funA w = do
    bluh w
    return ()
...
wList :: (A a) => [a]
...
main = do
  mapM_ (funA) wList
最后一行编译无法通过。
怎么办?

[ 本帖最后由 Magicloud 于 2009-6-30 15:35 编辑 ]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

欢烬 2022-08-27 13:06:22

a <- widgetRun widget中widget的类型不能确定, 改成: a <- widgetRun widget::你的类型

淤浪 2022-08-27 13:06:09

1 main = do
2   mapM_ ((x, y, widget) -> do
3            a <- widgetRun widget
4            putStrLn $ show a
5         ) widgetList
6
7 widgetList :: (Widget w) => [(Integer, Integer, w)]
8 widgetList = []
9
10 class Widget w where
11   widgetRun :: w -> IO ()
---
% ghc --make tmp/test.hs
[1 of 1] Compiling Main             ( tmp/test.hs, /tmp/Main.o )

tmp/test.hs:3:16:
    Ambiguous type variable `t' in the constraint:
      `Widget t' arising from a use of `widgetRun' at tmp/test.hs:3:16-31
    Probable fix: add a type signature that fixes these type variable(s)

酒与心事 2022-08-27 13:05:47

原帖由 Magicloud 于 2009-7-2 14:28 发表

那就郁闷了……想用class表现几个data的共性,怎么办呢?

看不到完整的代码,就不可能知道问题出在什么地方。

赏烟花じ飞满天 2022-08-27 12:58:55

原帖由 MMMIX 于 2009-7-1 17:35 发表

在 widgetList 的类型声明中,w 需要更多的限定。

那就郁闷了……想用class表现几个data的共性,怎么办呢?

指尖微凉心微凉 2022-08-27 10:43:30

原帖由 Magicloud 于 2009-7-1 15:44 发表
27   prs_ <- foldM (prs_ (x, y, widget) -> do
28                    pid <- forkProcess $ W.widgetRun widget
29                    return $ M.insert pid (x, y, fst $ W.widgetNew widget, snd $ W.widgetNew widget) prs_
30                 ) M.empty widgetList
...
84 widgetList :: (W.Widget w) => [(Integer, Integer, w)]
85 widgetList = []

在 widgetList 的类型声明中,w 需要更多的限定。

在你怀里撒娇 2022-08-26 15:37:14

Main.hs:28:40:
    Ambiguous type variable `t' in the constraint:
      `W.Widget t'
        arising from a use of `W.widgetRun' at Main.hs:28:40-57
    Probable fix: add a type signature that fixes these type variable(s)
---
25 main = do
26   prs <- newEmptyMVar
27   prs_ <- foldM (prs_ (x, y, widget) -> do
28                    pid <- forkProcess $ W.widgetRun widget
29                    return $ M.insert pid (x, y, fst $ W.widgetNew widget, snd $ W.widgetNew widget) prs_
30                 ) M.empty widgetList
...
84 widgetList :: (W.Widget w) => [(Integer, Integer, w)]
85 widgetList = []
...
1 module Widget where
2
3 class Widget w where
4   widgetNew :: w -> (Integer, Integer)
5   widgetRun :: w -> IO ()

很快妥协 2022-08-26 02:39:38

原帖由 Magicloud 于 2009-7-1 10:27 发表
基本代码就是类似的了,但A的instance有多个。当wList可能返回[]时,编译不通过。

我把你顶楼的代码补全后,可以编译通过。

错误是funA不能确定参数类型(这时候mapM_根本不会执行呀!)。

完整的代码和编译错误信息,直接拷贝粘贴。

晨光如昨 2022-08-25 16:07:26

基本代码就是类似的了,但A的instance有多个。当wList可能返回[]时,编译不通过。错误是funA不能确定参数类型(这时候mapM_根本不会执行呀!)。

百合的盛世恋 2022-08-23 01:19:46

原帖由 Magicloud 于 2009-6-30 14:57 发表
haskell是静态类型,所以会有这样一个问题
class A t where
  funA :: t -> IO ()
instance A W where
  funA w = do
    bluh w
    return ()
...
wList :: (A -> a) => [a]
...
main = do
  mapM_ (funA) wList
最后一行编译无法通过。

出了什么错误?给个完整的例子看看。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文