Protected access using lightweight capabilities

The following is some SML code that implements the natural numbers using peano arithmetic in the SML type system. These numbers can be used to protect access to list functions. I haven't yet figured out if addition is possible, but I'm hoping that it is. It could be really handy!

signature SLIST =
sig
type 'a nat
type Z
type 'a S
val S : 'a nat -> 'a S nat
val P : 'a S nat -> 'a nat
val Z : unit -> Z nat
val zerop : 'a nat -> bool
val toInt : 'a nat -> int

type ('elt,'n) slist

val snil : unit -> ('elt, Z nat) slist
val scons : 'elt -> ('elt, 'n nat) slist -> ('elt, 'n S nat) slist
val :+: : 'elt * ('elt, 'n nat) slist -> ('elt, 'n S nat) slist
val shd : ('elt, 'n S nat) slist -> 'elt
val stl : ('elt, 'n S nat) slist -> ('elt, 'n nat) slist
val slen : ('elt, 'n S nat) slist -> int

end

structure SList :> SLIST =
struct
(* encode integer types *)
type 'a nat = int
type Z = unit
type 'a S = unit
fun S i = i+1;
fun P i = i-1;
fun Z () = 0
fun zerop 0 = true
| zerop _ = false
fun toInt d = d

type ('elt, 'n) slist = 'elt list * 'n

fun snil () = ([],0)

fun scons elt sl =
let val (l,i) = sl
in ((elt::l),S i)
end

infixr :+:
fun x :+: y = scons x y

fun shd sl =
let val (h::t,i) = sl
in h
end

fun stl sl  =
let val (h::t,i) = sl
in (t,P i)
end

fun slen sl =
let val (_,i) = sl
in i
end

end

open SList
infixr :+:

val mylist = 1 :+: 2 :+: 3 :+: snil();
val the_head = shd mylist;
val the_tail = stl mylist;
val the_next_head = shd the_tail;
(*
This doesn't even compile!
val head_of_nil = shd (snil())
*)

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…

Plotkin, the LGG and the MGU

Legend has it that a million years ago Plotkin was talking to his professor Popplestone, who said that unification (finding the most general unifier or the MGU) might have an interesting dual, and that Plotkin should find it. It turns out that the dual *is* interesting and it is known as the Least General Generalisation (LGG). Plotkin apparently described both the LGG for terms, and for clauses. I say apparently because I can't find his paper on-line.

The LGG for clauses is more complicated so we'll get back to it after we look at the LGG of terms. We can see how the MGU is related to the LGG by looking at a couple of examples and the above image. We use the prolog convention that function symbols start with lower case, and variables start with uppercase. The image above is organised as a DAG (Directed Acyclic Graph). DAGs are a very important structure in mathematics since DAGs are lattices.

Essentially what we have done is drawn an (incomplete) Hasse diagram f…