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
6
votes
2 answers

Converting this FreeT (explicitly recursive data type) function to work on FT (church encoding)

I'm using the FreeT type from the free library to write this function which "runs" an underlying StateT: runStateFree :: (Functor f, Monad m) => s -> FreeT f (StateT s m) a -> FreeT f m (a, s) runStateFree s0 (FreeT x) = FreeT $ do …
Justin L.
  • 13,510
  • 5
  • 48
  • 83
6
votes
2 answers

lambda calculus in scala

OK, so I'm trying to implement the basics of lambda calculus. Here it goes. My numbers: def zero[Z](s: Z => Z)(z: Z): Z = z def one[Z](s: Z => Z)(z: Z): Z = s(z) def two[Z](s: Z => Z)(z: Z): Z = s(s(z)) Partially (actually, non) applied version of…
Vadim Samokhin
  • 3,378
  • 4
  • 40
  • 68
6
votes
3 answers

Church numeral for addition

I am stuck up at the following step. It will be great if someone can help me out: 2 = λfx.f(f x) 3 = λfx.f(f(f x)) ADD = λm n f x. m f (n f x) My steps are: (λm n f x. m f (n f x)) (λf x.f(f(f x))) (λf x.f(f x)) -> ((λn f x. (λf x.f(f(f x))) f…
name_masked
  • 9,544
  • 41
  • 118
  • 172
5
votes
2 answers

Is it possible to create a type-level representation of generic ADTs?

Using Church encoding, it is possible to represent any arbitrary algebraic datatype without using the built-in ADT system. For example, Nat can be represented (example in Idris) as: -- Original type data Nat : Type where natSucc : Nat -> Nat …
5
votes
2 answers

Implement in Haskell the Church encoding of the pair for polymorphic λ-calculus/System F

I want to implement the Church encoding of the pair in polymorphic lambda calculus in Haskell. On page 77, section 8.3.3 of Peter Selinger's notes on lambda calculus, he gives a construction of the cartesian product of two types as A×B =…
5
votes
1 answer

Catamorphisms for Church-encoded lists

I want to be able to use cata from recursion-schemes package for lists in Church encoding. type ListC a = forall b. (a -> b -> b) -> b -> b I used a second rank type for convenience, but I don't care. Feel free to add a newtype, use GADTs, etc. if…
nponeccop
  • 13,527
  • 1
  • 44
  • 106
5
votes
1 answer

Is there any non-recursive term that folds over a scott-encoded list?

Suppose that I have a scott-encoded list such as: scott = (\ c n -> c 1 (\ c n -> c 2 (\ c n -> c 3 (\ c n -> n)))) I want a function that receives such kind of list and converts it to an actual list ([1,2,3]), except that such function can not be…
MaiaVictor
  • 51,090
  • 44
  • 144
  • 286
5
votes
3 answers

How to implement Binary numbers in Haskell

I have seen the following data constructor for Church numerals data Nat = Zero | Succ Nat deriving Show But this is unary numbers. How do we implement a data constructor for Binary numbers in Haskell in this way? I have tried this: data Bin = Zero…
Tem Pora
  • 2,043
  • 2
  • 24
  • 30
4
votes
1 answer

Operations on Church Lists in Haskell

I am referring to this question type Churchlist t u = (t->u->u)->u->u In lambda calculus, lists are encoded as following: [] := λc. λn. n [1,2,3] := λc. λn. c 1 (c 2 (c 3 n)) mapChurch :: (t->s) -> (Churchlist t u) -> (Churchlist s u) mapChurch f…
niklas
  • 2,887
  • 3
  • 38
  • 70
4
votes
2 answers

Is the Church numeral encoding of natural numbers unnecessarily complicated?

The Structure and Interpretation of Computer Programs book I've been reading presents Church numerals by defining zero and an increment function zero: λf. λx. x increment: λf. λx. f ((n f) x) This seemed pretty complicated to me and it took me a…
Peter Olson
  • 139,199
  • 49
  • 202
  • 242
4
votes
4 answers

How to make a function call itself n times

Let's say I have a function called f that takes an integer argument called x and returns an integer. I also have an integer n that says how many times the function must call itself. So for example if my function call looks like this f(x) when n = 1,…
4
votes
1 answer

Church encoding for dependent types: from Coq to Haskell

In Coq I can define a Church encoding for lists of length n: Definition listn (A : Type) : nat -> Type := fun m => forall (X : nat -> Type), X 0 -> (forall m, A -> X m -> X (S m)) -> X m. Definition niln (A : Type) : listn A 0 := fun X n c =>…
Bob
  • 1,713
  • 10
  • 23
4
votes
2 answers

lambda calculus xor expression by true false

I am trying to understand xor in context on lambda calculus. I understand xor (Exclusive or) as boolean logic operation in https://en.wikipedia.org/wiki/Exclusive_or and the truth table of xor. But how why is it true as a xor…
echo
  • 767
  • 2
  • 9
  • 24
4
votes
2 answers

Does OCaml's type system prevent it from modeling Church numerals?

As a pass-time, I'm trying to implement all kinds of problems that were presented in a course (concerned with Lambda Calculus and various programming concepts) I took at the university. So, I'm trying to implement Church numerals and associated…
asafc
  • 357
  • 3
  • 21
4
votes
2 answers

Non-escaping error when implementing Church Numerals in Swift 3

I am attempting to implement Church Numerals in Swift 3. Currently, I have: func numToChurch(n: Int) -> ((Int) -> Int) -> Int { return { (f: (Int) -> Int) -> (Int) -> Int in return { (x : Int) -> Int in return…