在haskell中使用y组合器

时间:2016-04-24 17:30:45

标签: haskell lambda-calculus combinators

我是haskell的初学者,并试图为自然数实现Church编码,如this guide中所述。 我使用了来自this answer的y组合子的定义,但不确定如何应用它。

我想在lambda演算中实现一个简单的函数,它计算[1..n]的总和,如所示here

{-# LANGUAGE RankNTypes #-}

import Unsafe.Coerce

y :: (a -> a) -> a
y = \f -> (\x -> f (unsafeCoerce x x)) (\x -> f (unsafeCoerce x x))

true = (\x y -> x)
false = (\x y -> y)

newtype Chur = Chr (forall a. (a -> a) -> (a -> a))

zer :: Chur
zer = Chr (\x y -> y)

suc :: Chur -> Chur
suc (Chr cn) = Chr (\h -> cn h . h)

ci :: Chur -> Integer
ci (Chr cn) = cn (+ 1) 0

ic :: Integer -> Chur
ic 0 = zer
ic n = suc $ ic (n - 1)


-- church pair
type Chp = (Chur -> Chur -> Chur) -> Chur

pair :: Chur -> Chur -> Chp
pair (Chr x) (Chr y)  f = f (Chr x) (Chr y)

ch_fst :: Chp -> Chur
ch_fst p = p true

ch_snd :: Chp -> Chur
ch_snd p = p false

next_pair :: Chp -> Chp
next_pair = (\p x -> x (suc (p true)) (p true))

n_pair :: Chur -> Chp -> Chp
n_pair (Chr n) p = n next_pair p

p0 = pair zer zer
pre :: Chur -> Chur
pre (Chr cn) = ch_snd $ n_pair (Chr cn) p0

iszero :: Chur -> (a->a->a)
iszero (Chr cn) = cn (\h -> false) true

unchr :: Chur -> ((a -> a) -> (a -> a))
unchr (Chr cn) = cn

ch_sum (Chr cn) = (\r -> iszero (Chr cn) zer (cn suc (r (pre (Chr cn)))))

到目前为止一切顺利,但如何将y应用于sum? 例如

n3 = ic 3
y ch_sum n3

导致类型不匹配:

<interactive>:168:3:
     Couldn't match type ‘(Chur -> Chur) -> Chur’ with ‘Chur’
     Expected type: ((Chur -> Chur) -> Chur) -> (Chur -> Chur) -> Chur
     Actual type: Chur -> (Chur -> Chur) -> Chur
     In the first argument of ‘y’, namely ‘ch_sum’
     In the expression: y ch_sum n3

<interactive>:168:10:
   Couldn't match expected type ‘Chur -> Chur’ with actual type ‘Chur’
   In the second argument of ‘y’, namely ‘n3’
   In the expression: y ch_sum n3

Y Combinator in Haskell提供了y组合子的定义,但没有解释如何使用它。

1 个答案:

答案 0 :(得分:3)

我已经介绍了函数ch_sum(显然添加了两个教会数字)来简化add :: Chur -> Chur -> Chur add (Chr cn1) (Chr cn2) = Chr (\h -> cn1 h . cn2 h) 的定义:

r

要使用定点组合器创建递归函数,您需要将其写为普通递归函数(使用递归语言),但作为最后一步添加显式 &#34;自&#34;参数作为第一个函数的参数(在这种情况下为r),而不是递归调用,你只需要调用&#34; self&#34; (ch_sum)。所以ch_sum :: (Chur -> Chur) -> Chur -> Chur ch_sum = \r n -> iszero n zer $ add n (r $ pre n) 可以写成

λ> let n3 = ic 3
λ> ci (y ch_sum n3)
6
λ> let n10 = ic 10
λ> ci (y ch_sum n10)
55

ghci中的几个测试:

from (
select itemname,count(action) boughtcount from data a 
join 
select distinct userid as id from data  where action='bought' b 
on a.userid=b.id 
where a.action='bought' group by name order by boughtcount desc limit 3) as t select t.itemname limit 1;