Questions tagged [church-encoding]

Questions about the Church encoding, a way to represent data using functions, and the Boehm-Berarducci encoding, a transposition of it to a typed setting. For questions primarily about the related Scott and Mogensen-Scott encodings, there is [scott-encoding].

Related Tags: , .

93 questions
4
votes
1 answer

How to create a type instance of class in haskell?

I'm a newbie in Haskell. I'm looking if there's any way to create an instance of type of a class. Is there any way to get this code working without using data or newtype? type N = ∀n. (n -> n) -> n -> n instance Printable N where print ::…
4
votes
1 answer

How to implement Church encoding division in haskell?

I'm a beginner in haskell, and trying to implement the Church encoding for natural numbers, as explained in this guide. I'd like to implement a division between two church numerals. {-# LANGUAGE RankNTypes #-} import Unsafe.Coerce y :: (a -> a) ->…
dimid
  • 7,285
  • 1
  • 46
  • 85
4
votes
2 answers

How to implement Church Numerals using Java 1.8

I'm trying to implement Church Numerals in Java 1.8. My first attempt was: import java.util.function.UnaryOperator; @FunctionalInterface public interface ChurchNumeral { public static ChurchNumeral valueOf(int n) { if (n < 0) { throw…
Ellen Spertus
  • 6,576
  • 9
  • 50
  • 101
4
votes
1 answer

Is it possible to implement addition on typed Church numerals using iterated incrementation?

I can't find a way to define addition as repeated incrementation, despite this being possible in an untyped language. Here is my code: {-# LANGUAGE RankNTypes #-} type Church = forall a . (a -> a) -> (a -> a) zero :: Church zero = \f -> id inc ::…
hugomg
  • 68,213
  • 24
  • 160
  • 246
3
votes
1 answer

About Church encoded lists in Haskell

Various optimisation problems, like this one, led to Church encoded lists as a way to enable stream fusion, i.e the compiler's elimination of intermediate results (e.g. lists). Here's the definition that was used successfully in the optimisation…
mcmayer
  • 1,931
  • 12
  • 22
3
votes
1 answer

Church Numerals in F#

I have been trying to implement church numerals in F#. They were briefly introduced in a course at college and I may have gone down the rabbit hole a bit since then. I have working Predecessor, Successor, Add and Operations but I can't get subtract…
3
votes
1 answer

Church naturals, exponentiation function and type checking

I have a definition of the natural numbers in lambda calculus as follow, which was my main goal. -- Apply a function n times on x apply = \f -> \n -> \x -> foldr ($) x $ replicate n f -- Church numbers churchZero = \f -> id churchOne = \f -> \x ->…
David Lilue
  • 597
  • 2
  • 14
3
votes
0 answers

Can I implement an heterogeneous list based on an existential with Church encoding and Rank-N types?

In my attempt to understand existential types I've read that Church encoding along with the Rank-N-types extension would be sufficient to encode them in Haskell without existential quantification. I found this straightforward example: type Obj =…
user6445533
3
votes
1 answer

Are Church encoded sum types a proper alternative in an untyped language?

I have been struggling for quite some time with the idea of sum types in Javascript. The language includes neither native sum types nor pattern matching. While you can mimic sum types with plain old Javascript Objects and the prototype system and…
3
votes
1 answer

Formulas that work with Church numerals

The wikipedia entry on lambda calculus defines some formulas that work with Church numerals like SUCC := λn.λf.λx.f (n f x) In Churches paper where he first defines his lambda calculus, he says that ..a function of two variables whose value, when…
user7816390
3
votes
1 answer

can't deduce the numeral representation (church encoding) of a lambda expression λx.λy.x(xy)

I have a lambda expression: λx.λy.x(xy), and I'm supposed to infer the integer representation of it. I've read a lot about Church encodings and Church numerals specifically but I can't find what number is. Can you explain it to me in a way a 3 year…
matos416
  • 85
  • 4
3
votes
1 answer

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

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…
3
votes
1 answer

Adding church numerals using lambda functions in python

I'm trying to learn python and CS on my own using a course online that is based off SICP. I understand the basics of church numerals, but I am having trouble on adding church numerals using lambda functions in python. This is my code below for…
Andrew
  • 33
  • 1
  • 3
2
votes
1 answer

Expressing Church Numerals with Boost.Bind

Church numerals can be expressed in C++0x (C++11?) using the new lambda parts of the language using something like this: typedef function F; static const F id = [=](int x) { return x; }; function church(unsigned int i) { if(i ==…
PaulH
  • 7,759
  • 8
  • 66
  • 143
2
votes
0 answers

How to prove that the Church encoding, forall r. (F r -> r) -> r, gives an initial algebra of the functor F?

The well-known Church encoding of natural numbers can be generalized to use an arbitrary functor F. The result is the type, call it C, defined by data C = Cfix { run :: forall r. (F r -> r) -> r } Here and below, for simplicity, we will assume…