1

One can easily Church-encode pairs like that:

Definition prod (X Y:Set) : Set := forall (Z : Set), (X -> Y -> Z) -> Z.

Definition pair (X Y:Set)(x:X)(y:Y) : prod X Y := fun Z xy => xy x y.

Definition pair_rec (X Y Z:Set)(p:X->Y->Z) (xy : prod X Y) : Z := xy Z p.

Definition fst (X Y:Set)(xy:prod X Y) : X := pair_rec X Y X (fun x y => x) xy.

Definition snd (X Y:Set)(xy:prod X Y) : Y := pair_rec X Y Y (fun x y => y) xy.

It is then tempting to generalize it to dependent pairs like that:

Definition dprod (X:Set)(Y:X->Set) : Set :=
forall (Z : Set), (forall (x:X),Y x->Z)->Z.

Definition dpair (X:Set)(Y:X->Set)(x:X)(y:Y x) : dprod X Y :=
fun Z xy => xy x y.

Definition dpair_rec (X:Set)(Y:X->Set)(Z:Set)(p:forall (x:X),Y x->Z)
(xy : dprod X Y) : Z := xy Z p.

Definition dfst (X:Set)(Y:X->Set)(xy:dprod X Y) : X :=
dpair_rec X Y X (fun x y => x) xy.

Definition dsnd (X:Set)(Y:X->Set)(xy:dprod X Y) : Y (dfst X Y xy) :=
dpair_rec X Y (Y (dfst X Y xy)) (fun x y => y) xy.

However the last definition fails with the error message:

In environment
X : Set
Y : X -> Set
xy : dprod X Y
x : X
y : Y x
The term "y" has type "Y x"
while it is expected to have type 
"Y (dfst X Y xy)".

I understand the problem here. But what is the solution? In other words, how to Church-encode dependent pairs?

chepner
  • 497,756
  • 71
  • 530
  • 681
Bob
  • 1,713
  • 10
  • 23

2 Answers2

3

First of all, let’s get the terminology right.

What you are calling dprod is actually a “dependent sum”, while a “dependent product” is this thing that you might be tempted to call a “dependent function”. The reason for this is that dependent functions generalise regular products, you only have to cleverly use Bool:

prod : Set -> Set -> Set
prod A B = (b : Bool) -> case b of { True -> A; False -> B; }
{-
The type-theoretic notation would be:
prod A B = Π Bool (\b -> case b of { True -> A; False -> B; })
-}

mkPair : (A B : Set) -> A -> B -> prod A B
mkPair A B x y = \b -> case b of { True -> x; False -> y; }

elimProd : (A B Z : Set) -> (A -> B -> Z) -> prod A B -> Z
elimProd A B Z f p = f (p True) (p False)

In the same spirit, dependent pairs (usually denoted Σ) generalise regular sums:

sum : Set -> Set -> Set
sum A B = Σ Bool (\b -> case b of { True -> A; False -> B; })

mkLeft : (A B : Set) -> A -> sum A B
mkLeft A B x = (True, x)

mkRight : (A B : Set) -> B -> sum A B
mkRight A B y = (False, y)

elimSum : (A B Z : Set) -> (A -> Z) -> (B -> Z) -> sum A B -> Z
elimSum A B Z f _ (True, x) = f x
elimSum A B Z _ g (False, y) = g y

It might be confusing, but, on the other hand, Π A (\_ -> B) is the type of regular functions, while Σ A (\_ -> B) is the type of regular pairs (see, for example, here).

So, once again:

  • Dependent product = type of dependent functions
  • Dependent sum = type of dependent pairs

Your question can be rephrased in the following way:

Does there exist a Church-encoding for dependent sums via dependent products?

This has been already asked before on Math.StackExchange, and here is an answer that gives essentially the same encoding as yours.

However, reading the comments to this answer, you will notice that it apparently lacks the expected induction principle. There is also a similar question but regarding Church-encoding for natural numbers, and this answer (in particular the comments) sort of explains why Coq or Agda is not enough to derive the induction principle, you need additional assumptions, such as parametricity. There is yet another similar question on MathOverflow and while the answers do not give a definite “yes” or “no” for the specific case of Agda/Coq, they imply that it is likely impossible.

Lastly, I have to mention, that, as with many other type-theoretic questions nowadays, apparently HoTT is the answer. To quote the beginning of the blog post by Mike Shulman:

In this post I will argue that, improving on previous work of Awodey-Frey-Speight, (higher) inductive types can be defined using impredicative encodings with their full dependent induction principles — in particular, eliminating into all type families without any truncation hypotheses — in ordinary (impredicative) Book HoTT without any further bells or whistles.

(Although the (impredicative) encoding you will get can hardly be called Church encoding.)

kirelagin
  • 13,248
  • 2
  • 42
  • 57
  • The [answer](https://math.stackexchange.com/a/673003/80660) you mention does not appear to be correct. See my comment there. – Bob Mar 17 '19 at 15:34
  • Looks pretty well-typed to me (although, I admit, I didn’t check it myself _thoroughly_). – kirelagin Mar 17 '19 at 16:28
  • Well, it is definitely not, as you finally agreed there. Thus your answer here does not answer my question. – Bob Mar 17 '19 at 17:07
  • It actually does: it is impossible in Agda / Coq. – kirelagin Mar 17 '19 at 18:28
  • 1
    What we learned from that other question being wrong it that even such a simple function as `proj2` requires the eliminator to be dependent, so you can’t have `proj2` in Agda/Coq. – kirelagin Mar 17 '19 at 18:29
  • Indeed, I agree. – Bob Mar 17 '19 at 18:39
0

There is no way to Church-encode dependent pairs in Coq or Agda.

Ok, when we think of homogenous tuples AxA then this can also be understood as a function 2 -> A. This also works for heterogenous tuples like AxB using dependent functions Pi x:2.if x then A else B. However the next logical step is Sigma x:A.B x, which have no good representations as functions (unless we accept very dependent functions which in my mind goes against the spirit of type theory). For this reason it seems to me that the generalisation from -> to Pi and from x to Sigma is the primary one, and the fact that tuples can be represented as functions is secondary. -- Dr Thorsten Altenkirch, somewhere on the Agda mailing list

The very dependent functions encoding that Thorsten mentions can be found here (note that this is not valid Agda, just Agda-like syntax of an "insanely dependent" type theory).

effectfully
  • 12,325
  • 2
  • 17
  • 40
  • There is nothing that looks like a Church encoding [there](https://github.com/UlfNorell/insane/blob/master/Sigma.agda). By the way, Church encoding of a dependent type is possible: See [here](https://stackoverflow.com/questions/55081273/church-encoding-for-dependent-types-from-coq-to-haskell) for instance. – Bob Mar 17 '19 at 12:25
  • 3
    "There is nothing that looks like a Church encoding there" -- correct, it's another encoding. "Church encoding of a dependent type is possible" - no it's not, what you have at the link is an indexed recursor, but not an eliminator. That is to say, you do not have an induction principle with Church-encoded dependent types, meaning that those encoded types are not isomorphic to their non-encoded counterparts, i.e. this is not a true encoding. – effectfully Mar 17 '19 at 13:41
  • I disagree. Like with Church numerals (See this [answer](https://cstheory.stackexchange.com/questions/30923/why-its-impossible-to-declare-an-induction-principle-for-church-numerals/30924#30924)), you get an induction principle if you assume parametricity. – Bob Mar 17 '19 at 13:56
  • 2
    @Bob, true, but I was speaking in the context of vanilla type theories (Coq or Agda) and I'm not sure if you still can call an encoding that makes use of parametricity Church encoding. – effectfully Mar 17 '19 at 15:12
  • There must be a misunderstanding here. Since Church numerals do not come with an induction principle, do you claim that Church numerals are not a Church encoding? – Bob Mar 17 '19 at 15:16
  • @Bob, technically speaking, the Church numerals are the only thing that constitutes Church-encoded numbers. But you can only get induction for encoded (not Church-encoded) numbers if you use a representation that is rather distinct from Church encoding (even though, Church encoding is a part of it). See the "5.3 Induction for the Natural Numbers" section of [Internalizing Relational Parametricity in the Extensional Calculus of Constructions](https://www.cl.cam.ac.uk/~nk480/final-csl-internalizing-parametricity.pdf). – effectfully Mar 17 '19 at 15:27
  • I have some code [here](https://github.com/effectfully/random-stuff/blob/master/Lists/elimChurchList.agda) that shows how parametricity can be used in order to get the induction principle of lists. In observational type theory postulated equalities do not break canonicity, so we can have eliminators in OTT with proper computational properties (but I was unable to make it work, because types are huge, because OTT's equality of functions is a fancy one). – effectfully Mar 17 '19 at 15:31