church-encoding

Closures and universal quantification

断了今生、忘了曾经 提交于 2019-12-02 20:58:00
I've been trying to work out how to implement Church-encoded data types in Scala. It seems that it requires rank-n types since you would need a first-class const function of type forAll a. a -> (forAll b. b -> b) . However, I was able to encode pairs thusly: import scalaz._ trait Compose[F[_],G[_]] { type Apply = F[G[A]] } trait Closure[F[_],G[_]] { def apply[B](f: F[B]): G[B] } def pair[A,B](a: A, b: B) = new Closure[Compose[({type f[x] = A => x})#f, ({type f[x] = B => x})#f]#Apply, Id] { def apply[C](f: A => B => C) = f(a)(b) } For lists, I was able to encode cons : def cons[A](x: A) = {

Swift higher order function (Church pair aka cons) with generic parameter types not accepting input parameter types

℡╲_俬逩灬. 提交于 2019-12-01 08:50:16
I was messing around with the functional programming in Swift 2.1, trying to implement the Church encoding pair/cons function ( cons = λx λy λf f x y in untyped lambda calculus ), which I had read couldn't be done in earlier versions of Swift. With generics it looks like func cons<S,T,U>(x:S,_ y:T) -> ((S,T) -> U) -> U { return { (f:((S,T) -> U)) -> U in return f(x,y)} } cons(1,2) //error: cannot invoke 'cons' with an argument list of type '(Int, Int)' //note: expected an argument list of type '(S, T)' which doesn't work, and gives an error I cannot understand (surely parameter list of type

Swift higher order function (Church pair aka cons) with generic parameter types not accepting input parameter types

故事扮演 提交于 2019-12-01 06:18:57
问题 I was messing around with the functional programming in Swift 2.1, trying to implement the Church encoding pair/cons function (cons = λx λy λf f x y in untyped lambda calculus), which I had read couldn't be done in earlier versions of Swift. With generics it looks like func cons<S,T,U>(x:S,_ y:T) -> ((S,T) -> U) -> U { return { (f:((S,T) -> U)) -> U in return f(x,y)} } cons(1,2) //error: cannot invoke 'cons' with an argument list of type '(Int, Int)' //note: expected an argument list of type

Why are difference lists not an instance of foldable?

天涯浪子 提交于 2019-11-30 19:26:31
The dlist package contains the DList data type, which has lots of instances, but not Foldable or Traversable . In my mind, these are two of the most "list-like" type classes. Is there a performance reason that DList is not an instance of these classes? Also, the package does implement foldr and unfoldr , but none of the other folding functions. DList a is a newtype wrapper around [a] -> [a] , which has an a in a contravariant position, so it cannot implement Foldable or Traversable , or even Functor directly. The only way to implement them is to convert to and from regular lists (see the foldr

Why are difference lists not an instance of foldable?

僤鯓⒐⒋嵵緔 提交于 2019-11-30 16:55:19
问题 The dlist package contains the DList data type, which has lots of instances, but not Foldable or Traversable . In my mind, these are two of the most "list-like" type classes. Is there a performance reason that DList is not an instance of these classes? Also, the package does implement foldr and unfoldr , but none of the other folding functions. 回答1: DList a is a newtype wrapper around [a] -> [a] , which has an a in a contravariant position, so it cannot implement Foldable or Traversable , or