Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.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_Types_Typeclass_Generic Type Argument - Fatal编程技术网

类型别名声明中的Haskell类型类

类型别名声明中的Haskell类型类,haskell,types,typeclass,generic-type-argument,Haskell,Types,Typeclass,Generic Type Argument,我正试图开发一个模块,通过接收函数作为参数对一组状态执行不同的算法。我定义了不同类型的别名,使其更易于实现;理解这一点的一个简单示例是: -- Function that expands a state type Expand = State -> State -- Function that evaluates a state type Eval = State -> Float -- Group of states type Tree = [State] 然后我意识到我想要St

我正试图开发一个模块,通过接收函数作为参数对一组状态执行不同的算法。我定义了不同类型的别名,使其更易于实现;理解这一点的一个简单示例是:

-- Function that expands a state
type Expand = State -> State
-- Function that evaluates a state
type Eval = State -> Float
-- Group of states
type Tree = [State]
然后我意识到我想要
State
成为一个可平等类型的别名:我真的不在乎用户的状态是
(Int,Int)
Float
,或者
String
只要他能够实现
Expand
Eval
功能,并且我能够比较两种不同的状态


如何泛化类型
State
来实现这一点?我脑海中直观的想法是类似于
type State=(Eq a)=>a
的东西,但如果范围中没有
a
,这显然是不可能的。有没有一种简单的方法来声明将
状态
视为黑盒的函数?

您需要将泛型状态作为类型参数

type Expand s = s -> s
type Eval s   = s -> Float
type Tree s   = [s]
不可能完全隐藏它,因为例如
(Int,Int)
状态的
展开
字符串
状态的类型完全不同

至于
Eq
约束。通常,您将仅在需要约束的特定函数中局部包含该约束