### System F is Amazing - Part II

As I mentioned in my last post System-F is amazing. I haven't written in a very long time because I have been busy ensuring that I graduate. I passed my viva finally! So now that I'm back in the real world I thought I'd talk a bit about things that I found while I was in my cave. I'll put my dissertation up with some notes when I've completed the revisions.

Those who have dealt with proof assistants based on type theories (Such as Agda and Coq) might have noticed that we often require types to have a positivity restriction. This restriction essentially states that you can't have non-positive occurrences of the recursive type variable. As I described earlier, System F avoids this whole positivity restriction by forcing the programmer to demonstrate that constructors themselves (as a Church encoding) can be implemented, avoiding the problem of uninhabited types being inadvertently asserted.

So this means that in some sense positivity is probably the same thing as saying that there is an algorithmic method of translation of a data-type into System-F. I suspect (but can not yet prove) it also means that we should be allowed to write down datatypes as long as we can show that the inductive or coinductive types are inhabited by a Church encoding! If we could also do this trick for the Calculus of Constructions it might give a tricky way to increase the number of (co)inductive types we are allowed to write down.

Now, I'll write down an example of what I'm talking about in System-F, embedded in the propositional fragment of Coq (which is suitably impredicative), so that you can see a very straightforwardly non-positive type which has a Church encoding. This example of the LamMu type came from Andreas Abel's paper on sized types (which one I forget!) where he demonstrates that his system allows the definition.

In the very final entry we attempt to enter LamMu into the Coq Inductive type framework and watch it fail (for reasons of positivity violation). The original type definition gives us three constructors, each of which is proved in turn below by a "constructor".

```Definition LamMu := forall (X : Prop),   (Nat -> X) ->   (Nat -> List X -> X) ->   ((forall (Y : Prop), ((X -> Y) -> Y)) -> X) -> X.Definition var : Nat -> LamMu :=   fun (n : Nat) =>     fun (X : Prop)       (v : Nat -> X)       (f : Nat -> List X -> X)       (m : (forall (Y : Prop), ((X -> Y) -> Y)) -> X) =>       v n.Definition func : Nat -> List LamMu -> LamMu :=   fun (n : Nat) =>     fun (t : List LamMu) =>       fun (X : Prop)         (v : Nat -> X)         (f : Nat -> List X -> X)         (m : (forall (Y : Prop), ((X -> Y) -> Y)) -> X) =>         f n (t (List X) (nil X) (fun (x : LamMu) (y : List X) => cons (x X v f m) y)).Definition mu : (forall (Y : Prop), (LamMu -> Y) -> Y) -> LamMu. Proof.  unfold LamMu.  refine     (fun (zi : (forall (Y : Prop), (LamMu -> Y) -> Y)) =>       fun (X : Prop)         (v : Nat -> X)         (f : Nat -> List X -> X)         (m : (forall (Y : Prop), ((X -> Y) -> Y)) -> X) =>         m (fun (Y : Prop) (g : X -> Y) =>           g (zi X (fun e : LamMu => e X v f m)))).Defined.Inductive LamMu2 : Type :=| Var : Nat -> LamMu2| Fun : Nat -> LamMu2 -> LamMu2| Mu : ((forall Y, ((LamMu2 -> Y) -> Y)) -> LamMu2) -> LamMu2 ```

### Managing state in Prolog monadically, using DCGs.

Prolog is a beautiful language which makes a lot of irritating rudimentary rule application and search easy. I have found it is particularly nice when trying to deal with compilers which involve rule based transformation from a source language L to a target language L'.

However, the management of these rules generally requires keeping track of a context, and this context has to be explicitly threaded through the entire application, which involves a lot of irritating and error prone sequence variables. This often leads to your code looking something a bit like this:

compile(seq(a,b),(ResultA,ResultB),S0,S2) :- compile(a,ResultA,S0,S1), compile(b,ResultB,S1,S2).
While not the worst thing, I've found it irritating and ugly, and I've made a lot of mistakes with incorrectly sequenced variables. It's much easier to see sequence made explicitly textually in the code.

While they were not designed for this task, but rather for parsing, DCGs turn out to be a convenient …

### Automated Deduction and Functional Programming

I just got "ML for the working programmer" in the mail a few days ago,
and worked through it at a breakneck pace since receiving it. It
turns out that a lot of the stuff from the "Total Functional
Programming" book is also in this one. Paulson goes through the use
of structural recursion and extends the results by showing techniques
for proving a large class of programs to be terminating. Work with
co-algebras and bi-simulation didn't quite make it in, except for a
type: D := D → D which is the type of programs in the untyped lambda
calculus, and hence liable to lead one into trouble.

I have to say that having finished the book, this is the single most