Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 如何调用不带实例参数的函数?_Haskell - Fatal编程技术网

Haskell 如何调用不带实例参数的函数?

Haskell 如何调用不带实例参数的函数?,haskell,Haskell,假设您有一个类: class AClass a where func:: Int instance AClass SomeTree where func = 0 instance AClass Double where func = 1 如何调用函数func?neutral::Int,应该可以,因为信息都是从输入类型和输出类型收集的。没有不带参数的函数func这里只是一个Int。像任何其他Int一样使用它。更大的问题是,您不能将func定义为在某种程度上不涉及a的类型。@TVS

假设您有一个类:

class AClass a where
  func:: Int

instance AClass SomeTree where
  func = 0

instance AClass Double where
  func = 1

如何调用函数func?

neutral::Int
,应该可以,因为信息都是从输入类型和输出类型收集的。没有不带参数的函数
func
这里只是一个
Int
。像任何其他
Int
一样使用它。更大的问题是,您不能将
func
定义为在某种程度上不涉及
a
的类型。@TVSuchty:然后您可以使用
TypeApplications
func@Double
。您可以将
{-\LANGUAGE TypeApplications}
添加到您的文件中,它启用了一个新的
@
符号来提供类型参数。然后,您可以编写
func@SomeTree
,这将产生0,或者
func@Double
,这将产生1。请看,您能否提供一个更详细的示例,说明您正在尝试执行的操作?条形类型约束有何神奇之处?谢谢你的回答<代码>∀ a.正是您在函数中隐含的内容,如
sum::Num a=>[a]->a
,它实际上是
sum::∀ A.数值a=>[a]->a
。区别在于显式
(也称为
forall
)将
a
引入作用域,使其可以在函数体中使用,如这里的
func@a
{-# LANGUAGE AllowAmbiguousTypes, TypeApplications #-}

class AClass a where
  func :: Int

instance AClass SomeTree where
  func = 0

instance AClass Double where
  func = 1

foo :: Int
foo = func @SomeTree + func @Double

{-# LANGUAGE ScopedTypeVariables, UnicodeSyntax #-}

bar :: ∀ a . AClass a => a -> Int
bar _ = func @a