Skip to main content

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.
  unfold LamMu.
    (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)))).

Inductive LamMu2 : Type :=
| Var : Nat -> LamMu2
| Fun : Nat -> LamMu2 -> LamMu2
| Mu : ((forall Y, ((LamMu2 -> Y) -> Y)) -> LamMu2) -> LamMu2


Popular posts from this blog

Generating etags automatically when needed

Have you ever wanted M-. (the emacs command which finds the definition of the term under the cursor) to just "do the right thing" and go to the most current definition site, but were in a language that didn't have an inferior process set-up to query about source locations correctly (as is done in lisp, ocaml and some other languages with sophisticated emacs interfaces)?

Well, fret no more. Here is an approach that will let you save the appropriate files and regenerate your TAGS file automatically when things change assuring that M-. takes you to the appropriate place.

You will have to reset the tags-table-list or set it when you first use M-. and you'll want to change the language given to find and etags in the 'create-prolog-tags function (as you're probably not using prolog), but otherwise it shouldn't require much customisation.

And finally, you will need to run etags once manually, or run 'M-x create-prolog-tags' in order to get the initia…

Decidable Equality in Agda

So I've been playing with typing various things in System-F which previously I had left with auxiliary well-formedness conditions. This includes substitutions and contexts, both of which are interesting to have well typed versions of. Since I've been learning Agda, it seemed sensible to carry out this work in that language, as there is nothing like a problem to help you learn a language.

In the course of proving properties, I ran into the age old problem of showing that equivalence is decidable between two objects. In this particular case, I need to be able to show the decidability of equality over types in System F in order to have formation rules for variable contexts. We'd like a context Γ to have (x:A) only if (x:B) does not occur in Γ when (A ≠ B). For us to have statements about whether two types are equal or not, we're going to need to be able to decide if that's true using a terminating procedure.

And so we arrive at our story. In Coq, equality is som…

Formalisation of Tables in a Dependent Language

I've had an idea kicking about in my head for a while of making query plans explicit in SQL in such a way that one can be assured that the query plan corresponds to the SQL statement desired. The idea is something like a Curry-Howard in a relational setting. One could infer the plan from the SQL, the SQL from the plan, or do a sort of "type-checking" to make sure that the plan corresponds to the SQL.

The devil is always in the details however. When I started looking at the primitives that I would need, it turns out that the low level table joining operations are actually not that far from primitive SQL statement themselves. I decided to go ahead and formalise some of what would be necessary in Agda in order get a better feel for the types of objects I would need and the laws which would be required to demonstrate that a plan corresponded with a statement.

Dependent types are very powerful and give you plenty of rope to hang yourself. It's always something of…