How do I write the qualified name of a symbol in Haskell?

六眼飞鱼酱① 提交于 2020-01-02 00:01:18

问题


I've got a name clash between two different Haskell modules that want to use the same infix operator (<*>). The Haskell 98 report says that

modid.varsym

is permitted, but I can't get it to work. In their entirety here are Test.hs:

module Test
where

import qualified Test2 as T

three = T.<*>

and Test2.hs:

module Test2
where
(<*>) = 3

But trying to compile results in an error message:

Test.hs:6:12: parse error on input `T.<*>'

I tried T.(<*>) but that doesn't work either.

How can I refer to a symbolic name defined in a module imported by import qualified?


回答1:


try

three = (T.<*>)

It's weird to define an infix operator as an integer. Let's consider \\ (the set difference operator):

import qualified Data.List as L

foo = [1..5] L.\\ [1..3] -- evaluates to [4,5]
diff = (L.\\)

As you can see above, L.\\ is a qualified infix operator; and it still works as an infix operator. To use it as a value, you put parentheses around the whole thing.




回答2:


Remember that we import symbols wrapped parens. E.g.

import T ((<*>))

so importing qualified is the same:

import qualified T as Q

main = print (Q.<*>)


来源:https://stackoverflow.com/questions/741184/how-do-i-write-the-qualified-name-of-a-symbol-in-haskell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!