在Haskell中实现Lambda演算true和false

在Haskell中实现Lambda演算true和false,haskell,lambda-calculus,Haskell,Lambda Calculus,也许这是一个微不足道的问题,我只是没有正确地思考它。如果是,我很抱歉 我想在Haskell中实现lambda演算true和false函数,然后使用它们来实现if-then-else。这就是我所做的 true :: t1 -> t2 -> t1 true x y = x false :: t1 -> t2 -> t2 false x y = y -- ifThenElse :: (t2 -> t1 -> t) -> t2 -> t1 ->

也许这是一个微不足道的问题,我只是没有正确地思考它。如果是,我很抱歉

我想在Haskell中实现lambda演算true和false函数,然后使用它们来实现if-then-else。这就是我所做的

true :: t1 -> t2 -> t1
true x y =  x

false :: t1 -> t2 -> t2
false x y = y

-- ifThenElse :: (t2 -> t1 -> t) -> t2 -> t1 -> t
ifThenElse cond thenPart elsePart =  cond thenPart elsePart
注释掉的
ifthenels
类型是GHC生成的类型。我关心的是,该类型中的
t
应限制为
t1
t2
。如果其他可以完成此操作,我可以为
编写一个类型吗

您想要这些:

{-# LANGUAGE RankNTypes #-}
-- These are the only two values of this type...
true  :: l -> r -> Either l r
false :: l -> r -> Either l r
true  l _ = Left  l
false _ r = Right r

-- ...and they are the only possible arguments here.
ifThenElse :: (forall l r. l -> r -> Either l r) -> l -> r -> Either l r
ifThenElse = id
如果您想要不同的左、右类型,也没有办法

您想要这些:

{-# LANGUAGE RankNTypes #-}
-- These are the only two values of this type...
true  :: l -> r -> Either l r
false :: l -> r -> Either l r
true  l _ = Left  l
false _ r = Right r

-- ...and they are the only possible arguments here.
ifThenElse :: (forall l r. l -> r -> Either l r) -> l -> r -> Either l r
ifThenElse = id

如果您想要不同的左、右类型,
也没有办法。

请检查否,因为Haskell不知道
cond
应该是
true
false
。因为Haskell
cond
是一个泛型函数。如果其他函数是:(t->t->t)->t->t->t
,你可以编写它。我知道Haskell不知道
cond
必须是
true
false
。我在问如何要求它是那样的..打开
RankNTypes
并要求
如果其他
为所有t设置类型为
的条件。t->t->t
。检查upNo,因为Haskell不知道
cond
应该是
true
false
。因为Haskell
cond
是一个泛型函数。如果其他函数是:(t->t->t)->t->t->t
,你可以编写它。我知道Haskell不知道
cond
必须是
true
false
。我在问如何要求它是那样的..打开
RankNTypes
并要求
如果其他
为所有t设置类型为
的条件。t->t->t