[英]Data type with type constraint in Wadler - Essence of functional programming paper,
In Phil Wadler's paper : The essence of functional programming, Wadler describes the application of Monads using a simple interpreter program.在 Phil Wadler 的论文:函数式编程的本质,Wadler 用一个简单的解释器程序描述了 Monads 的应用。 The program is as shown below:
程序如下图所示:
A term is either a variable, a constant, a sum, a lambda expression, or an application.
项可以是变量、常量、总和、lambda 表达式或应用程序。 The following will serve as test data.
以下将作为测试数据。
term0 = (App (Lam "x" (Add (Var "x") (Var "x"))) (Add (Con 10) (Con 11)))
For our purposes, a monad is a triple
(M,unitM,bindM)
consisting of a type constructorM
and a pair of polymorphic functions.就我们的目的而言,monad 是一个三元组
(M,unitM,bindM)
由一个类型构造函数M
和一对多态函数组成。unitM :: a -> M a bindM :: M a -> (a -> M b) -> M b
Then the interpreter program is described as:然后解释器程序被描述为:
type Name = String
data Value = Wrong
| Num Int
| Fun (Value -> M Value)
I do not see how M Value
has been included here.我没有看到
M Value
是如何包含在这里的。 My understanding that Haskell does not allow type constraints on data type constructors?我的理解是 Haskell 不允许对数据类型构造函数进行类型约束?
Further details: The complete program is below:更多详情:完整的程序如下:
type Name = String
data Term = Var Name
| Con Int
| Add Term Term
| Lam Name Term
| App Term Term
data Value = Wrong
| Num Int
| Fun (Value -> M Value)
type Environment = [(Name, Value)]
interp :: Term -> Environment -> M Value
interp (Var x) e = lookup x e
interp (Con i) e = unitM (Num i)
interp (Add u v) e = interp u e `bindM` (\a ->
interp v e `bindM` (\b ->
add a b))
interp (Lam x v) e = unitM (Fun (\a -> interp v ((x,a):e)))
interp (App t u) e = interp t e `bindM` (\f ->
interp u e `bindM` (\a ->
apply f a))
lookup :: Name -> Environment -> M Value
lookup x [] = unitM Wrong
lookup x ((y,b):e) = if x==y then unitM b else lookup x e
add :: Value -> Value -> M Value
add (Num i) (Num j) = unitM (Num (i+j))
add a b = unitM Wrong
apply :: Value -> Value -> M Value
apply (Fun k) a = k a
apply f a = unitM Wrong
As can be seen interp (Lam xv) e = unitM (Fun (\\a -> interp v ((x,a):e)))
requires definition data Value = ... | Func (Value -> M Value)
可以看出
interp (Lam xv) e = unitM (Fun (\\a -> interp v ((x,a):e)))
需要定义data Value = ... | Func (Value -> M Value)
data Value = ... | Func (Value -> M Value)
I tried to implement interp (Lam xv)
by using data Value = ... | Func (Value -> Value)
我试图通过使用
data Value = ... | Func (Value -> Value)
来实现interp (Lam xv)
data Value = ... | Func (Value -> Value)
, but it did not seem possible to me. data Value = ... | Func (Value -> Value)
,但对我来说似乎不可能。
M is not a constraint, it is a type constructor. M 不是约束,它是一个类型构造函数。 So writing
M Value
is similar [Value]
, Maybe Value
etc. A constraint would be something like Monad m => m Value
, where m is a type variable, not a type constructor and Monad m
is the constraint (Monad is a type class).所以写
M Value
类似于[Value]
, Maybe Value
等。一个约束类似于Monad m => m Value
,其中 m 是一个类型变量,而不是一个类型构造函数,而Monad m
是约束(Monad 是一个类型类)。
In the paper, the definition of the type M value
has not been presented, and it is later shown that you can give it different definitions (the definitions would involve defining data M v = ...
and the functions bindM
and unitM
).在这篇论文中,没有给出类型
M value
的定义,后来表明你可以给它不同的定义(定义将涉及定义data M v = ...
以及函数bindM
和unitM
)。
Edit: If you wanted to have a constraint, you would change the definition of value like this:编辑:如果你想有一个约束,你可以像这样改变值的定义:
data Value m = Wrong
| Num Int
| Fun (Value m -> m (Value m))
And then have the constraint in the types of functions, eg:然后对函数类型进行约束,例如:
interp :: Monad m => Term -> Environment -> m (Value m)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.