Skip to main content

Formation Rules for Strictly Positive Types

For my thesis I've needed to use strictly positive types to avoid the problems which can occur with types of the form: νD. D → D, which were discussed briefly in my post System F is Amazing.

In order to ensure types are positive they should be restricted in some way.  It is therefore convenient to have formation rules for types which ensure that we can't make bad types. My starting point was a wonderful paper by Andreas Abel and Thorsten Altenkirch [1]

Specifically, this paper describes interleaved inductive types, which is close to what I want.   However, I also want to be able to have polymorphism using ∀.  Since I'm interested in becoming more familiar with Agda (a language in which I'm still a novice) I've written the following as a prototype of my type formation rules, as an experiment.

First, we have to set up the appropriate libraries.

module StrictlyPositive where 

open import Data.Nat
open import Data.Bool 
open import Data.List 
open import Data.Unit using ()
open import Relation.Nullary.Core
open import Data.Sum 

The first data type we define is a context of type variables. Since we are going to use de Bruijn indices, we can use a trick and say that the type variable context is really just a bound on the number of free variables. Since our types are not themselves typed (as our theory is impredicative) we are justified in doing this as the only information needed formation is that the variable is in a context.

Ctx : Set
Ctx = 

We then define an ∈ relation which is true whenever a variable is below the bound.

data __ :   Ctx  Set where 
  bounded :  {n Δ}  suc n  Δ  n  Δ

It turns out to be convenient to define lemmas which perform inversions on data-types in Agda. In Coq, I would generally just use a tactic to perform the inversion. However, there are advantages to Agda's way of doing things, one of which is that it obviates a lot of playing with equational reasoning. So far I've yet to be forced to learn how equational reasoning works in Agda since I haven't had to use it. I'm a little frightened that things work so well!

inInversion :  {n Δ}  n  Δ  suc n  Δ
inInversion (bounded p) = p

Next we prove that ∈ is decidable, which is pretty straight forward since it is defined using ≤ on natural numbers.

inctx :  n Δ  Dec (n  Δ)
inctx n Δ with (suc n) ? Δ
inctx n Δ | no p = no (λ x  p (inInversion x))
inctx n Δ | yes q  = yes (bounded q)

Next we define the syntax for types. We have ① for unit, ι which creates a type variable, η which creates a fixed-point type variable, ⇒ which is the function type, × which is the type of a pair, ⊕ which is the type of an injection into the left or right, μ which makes a fixed point, and Π which is the type of a type abstraction.

data Ty : Set where
   : Ty
  ι :   Ty
  η :   Ty
  __ : Ty  Ty  Ty
  _×_ : Ty  Ty  Ty 
  __ : Ty  Ty  Ty
  μ : Ty  Ty
  Π : Ty  Ty

Next we demonstrate the formation rules. Here we use two contexts. One is for ι variables, the other for η. The reason for segregating them is that we can allow ι variables to be placed more freely, while η variables need to be tightly controlled to ensure strictly positive types. The restriction manifests itself in the rule for ⇒, which allows formation with a type to the left of the arrow, only if the context for η vars is empty. The reader will notice that every time we go under a Π binder, the first context is increased in size, and every time we go under a μ, we increase the second. When we check that variables are in context, we refer to these segregated contexts with our two different type variables.

data _·__ : Ctx  Ctx  Ty  Set where 
     UnitValid :  {Δ Δ+}  
       Δ · Δ+  
     VarValid :  {n Δ Δ+}  
       (n  Δ)  
       Δ · Δ+  (ι n) 
     FixValid :  {n Δ Δ+}  
       (n  Δ+)  
       Δ · Δ+  (η n) 
     ImpValid :  {Δ Δ+ α β}  
       Δ · 0  α  
       Δ · Δ+  β  
       Δ · Δ+  (α  β)
     AndValid :  {Δ Δ+ α β}  
       Δ · Δ+  α  
       Δ · Δ+  β  
       Δ · Δ+  (α × β)
     OrValid :  {Δ Δ+ α β}  
       Δ · Δ+  α  
       Δ · Δ+  β  
       Δ · Δ+  (α  β)
     MuValid :  {Δ Δ+ α}  
       Δ · (suc Δ+)  α  
       Δ · Δ+  μ α
     AllValid :  {Δ Δ+ α}  
       (suc Δ) · Δ+  α  
       Δ · Δ+  Π α

Again, we find it convenient to define a number of inversion rules, this type on type formation.

varValidInversion :  {Δ Δ+ n}  Δ · Δ+  ι n  n  Δ
varValidInversion {Δ} {Δ+} {n} (VarValid p) = p

fixValidInversion :  {Δ Δ+ n}  Δ · Δ+  η n  n  Δ+
fixValidInversion {Δ} {Δ+} {n} (FixValid p) = p

orValidInversionL :  {Δ Δ+ α β}  Δ · Δ+  (α  β)  (Δ · Δ+  α)
orValidInversionL {Δ} {Δ+} {α} {β} (OrValid p q) = p

orValidInversionR :  {Δ Δ+ α β}  Δ · Δ+  (α  β)  (Δ · Δ+  β)
orValidInversionR {Δ} {Δ+} {α} {β} (OrValid p q) = q

impValidInversionL :  {Δ Δ+ α β}  Δ · Δ+  (α  β)  (Δ · 0  α)
impValidInversionL {Δ} {Δ+} {α} {β} (ImpValid p q) = p

impValidInversionR :  {Δ Δ+ α β}  Δ · Δ+  (α  β)  (Δ · Δ+  β)
impValidInversionR {Δ} {Δ+} {α} {β} (ImpValid p q) = q

andValidInversionL :  {Δ Δ+ α β}  Δ · Δ+  (α × β)  (Δ · Δ+  α)
andValidInversionL {Δ} {Δ+} {α} {β} (AndValid p q) = p

andValidInversionR :  {Δ Δ+ α β}  Δ · Δ+  (α × β)  (Δ · Δ+  β)
andValidInversionR {Δ} {Δ+} {α} {β} (AndValid p q) = q

allValidInversion :  {Δ Δ+ α}  Δ · Δ+  Π α  ((suc Δ) · Δ+  α)
allValidInversion {Δ} {Δ+} {α} (AllValid p) = p

muValidInversion :  {Δ Δ+ α}  Δ · Δ+  μ α  (Δ · (suc Δ+)  α)
muValidInversion {Δ} {Δ+} {α} (MuValid p) = p

Now we would like to show that we can demonstrate whether or not a type is valid or not. This program will construct a proof of the validity of a given type, or reject it with a proof that one can not be constructed. Intermediate computations which attempt to construct subproofs or proofs that no subproof exists are done using with. This allows us to further pattern much, just as we would do with parameters of functions in Haskell. The notation (λ { (VarValid q) p q }) is especially cool. It is a pattern matching lambda, which allows us to destruct the argument with each possible case. In this case, because of the type, there is exactly one case which allows us to prove that this type can not be valid.

isValid :  Δ Δ+ α  Dec (Δ · Δ+  α)
isValid Δ Δ+        = yes UnitValid
isValid Δ Δ+ (ι n)   with (inctx n Δ)
isValid Δ Δ+ (ι n)   | yes p = yes (VarValid p)
isValid Δ Δ+ (ι n)   | no p  = no (λ { (VarValid q)  p q })
isValid Δ Δ+ (η n)   with (inctx n Δ+)
isValid Δ Δ+ (η n)   | yes p = yes (FixValid p)
isValid Δ Δ+ (η n)   | no p  = no (λ { (FixValid q)  p q })
isValid Δ Δ+ (α  β) with isValid Δ 0 α
isValid Δ Δ+ (α  β) | yes p with isValid Δ Δ+ β
isValid Δ Δ+ (α  β) | yes p | yes q = yes (ImpValid p q) 
isValid Δ Δ+ (α  β) | yes p | no q  = no (λ x  q (impValidInversionR x))
isValid Δ Δ+ (α  β) | no p  = no (λ x  p (impValidInversionL x))
isValid Δ Δ+ (α × β) with isValid Δ Δ+ α
isValid Δ Δ+ (α × β) | yes p with isValid Δ Δ+ β
isValid Δ Δ+ (α × β) | yes p | yes q = yes (AndValid p q) 
isValid Δ Δ+ (α × β) | yes p | no q  = no (λ x  q (andValidInversionR x))
isValid Δ Δ+ (α × β) | no p  = no (λ x  p (andValidInversionL x))
isValid Δ Δ+ (α  β) with isValid Δ Δ+ α
isValid Δ Δ+ (α  β) | yes p with isValid Δ Δ+ β
isValid Δ Δ+ (α  β) | yes p | yes q = yes (OrValid p q) 
isValid Δ Δ+ (α  β) | yes p | no q  = no (λ x  q (orValidInversionR x))
isValid Δ Δ+ (α  β) | no p  = no (λ x  p (orValidInversionL x))
isValid Δ Δ+ (Π α)   with isValid (suc Δ) Δ+ α
isValid Δ Δ+ (Π α)   | yes p = yes (AllValid p) 
isValid Δ Δ+ (Π α)   | no p  = no (λ x  p (allValidInversion x))
isValid Δ Δ+ (μ α)   with isValid Δ (suc Δ+) α
isValid Δ Δ+ (μ α)   | yes p = yes (MuValid p) 
isValid Δ Δ+ (μ α)   | no p  = no (λ x  p (muValidInversion x))

Finally we give an example of some terms which can be typed, and some which can not. When there is no inhabitant of a pattern in Agda, we can use (). Agda is smart enough to know that there is no sense in a right-hand-side of a pattern which has no inhabitants because the type is indexed in such a way that inversion would yield no cases. It's very clever!

idTy : Ty
idTy = Π ((ι zero)  (ι zero))

idValid : 0 · 0  idTy 
idValid = AllValid
            (ImpValid (VarValid (bounded (ss zn)))
             (VarValid (bounded (ss zn))))

listTy : Ty
listTy = Π (μ (  ((ι 0) × (η 0))))

listValid : 0 · 0  listTy
listValid = AllValid
              (MuValid
               (OrValid UnitValid
                (AndValid (VarValid (bounded (ss zn)))
                 (FixValid (bounded (ss zn))))))


notPos : Ty 
notPos = μ ((ι 0)  (ι 0))

notPosInvalid : ¬ (0 · 0  notPos)
notPosInvalid x with muValidInversion x
notPosInvalid x | p with impValidInversionL p
notPosInvalid x | p | q with varValidInversion q
notPosInvalid x | p | q | bounded ()

dodgy : Ty
dodgy = μ (μ ((ι 1)  (ι 0)))

dodgyInvalid :  ¬ (0 · 0  dodgy)
dodgyInvalid x with muValidInversion x
dodgyInvalid x | p with muValidInversion p 
dodgyInvalid x | p | q with impValidInversionL q
dodgyInvalid x | p | q | r with varValidInversion r 
dodgyInvalid x | p | q | r | bounded ()

isThisDodgy : Ty 
isThisDodgy = Π (μ (μ ((((ι 0)  (η 1))  (η 0)))))

isThisDodgyValid : ¬ (0 · 0  isThisDodgy)
isThisDodgyValid x with allValidInversion x
isThisDodgyValid x | p with muValidInversion p
isThisDodgyValid x | p | q with muValidInversion q
isThisDodgyValid x | p | q | r with impValidInversionL r
isThisDodgyValid x | p | q | r | s with impValidInversionR s
isThisDodgyValid x | p | q | r | s | t with fixValidInversion t
isThisDodgyValid x | p | q | r | s | t | bounded ()



[1] A Predicative Strong Normalisation Proof for a λ-calculus with Interleaving Inductive Types

Comments

Popular posts from this blog

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 conveni

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

Teagrey

I was ironing my shirt today, which I almost never do. Because of this I don't have an ironing board so I tried to make a make-shift ironing board on my floor using a towel and some books. I grabbed the heaviest books on the nearest shelf, which happened to be Organic Chemistry, Stalingrad and an annotated study bible containing the old and new testament. As I pulled out the bible, a flower fell out which had been there for over 17 years now. I know that because it was put there by my first wife, Daniel, who killed herself in April about 17 years ago. It fell from Thessalonians to which it had been opened partially. Highlighted was the passage: "Ye are all sons of the light and sons of the day." I guess the passage gave her solace. Daniel was a complicated woman. She had serious mental health issues which plagued her for her entire life. None of them were her fault. She was dealt an absolutely awful hand in life, some truly nasty cards. She had some considerable c