Jan de Muijnck-Hughes / My Postings / Decidably Uninformative

Decidably Uninformative

Tags: Idris Dependent-Types Tdd Musing

Table of Contents

Dependently typed theorem proving is about using the power of dependent types to provide guarantees over various properties of software programs and mathematical constructs. In dependently typed programming we use dependent types to construct and better reason about the properties of our software programs.

While the former requires that we construct proofs, the latter requires that we construct a usable system. In this post I want to discuss how proving and construction can get in the way of each other and that when programming with dependent types we must recognise and tread carefully the fine line between theorem proving and programming.

A Helper

To help with our example we need a little helper data structure that I like to use. This is a reformulation of a list of dependent pairs, and is analogous to the forall list quantifier.

namespace ListD
  |||
  data ListD : (a  : Type)
            -> (e  : a -> Type)
            -> (as : List a)
            -> Type
    where
      Empty : ListD a e Nil

      Extend : (head : e v)
            -> (tail : ListD a e vs)
            -> ListD a e (v::vs)

Setting

Our motivating example will be examining the stopping condition for a simple declarative function-less specificiation language with linear-types. Within our language a specification can end iff all linear types have been used.

The precise terms, their semantics, and how usage types are represented is irrelevant for this dicsussion. What is important is how we represent the context and check for the stopping condition.

namespace Setting

Usage types

We encode our usage annotations using the following enumerated type.

  data Usage = Consumed | Free | Unrestricted

  notSameCU : (Consumed = Unrestricted) -> Void
  notSameCU Refl impossible

  notSameFU : (Free = Unrestricted) -> Void
  notSameFU Refl impossible

  notSameCF : (Consumed = Free) -> Void
  notSameCF Refl impossible

  DecEq Usage where
    decEq Consumed Consumed = Yes Refl
    decEq Consumed Free = No (notSameCF)
    decEq Consumed Unrestricted = No (notSameCU)
    decEq Free Consumed = No (negEqSym notSameCF)
    decEq Free Free = Yes Refl
    decEq Free Unrestricted = No (notSameFU)
    decEq Unrestricted Consumed = No (negEqSym notSameCU)
    decEq Unrestricted Free = No (negEqSym notSameFU)
    decEq Unrestricted Unrestricted = Yes Refl

Types

Types will be A or B types.

  data Ty = A | B

  notSameAB : (A = B) -> Void
  notSameAB Refl impossible

  DecEq Ty where
    decEq A A = Yes Refl
    decEq A B = No notSameAB
    decEq B A = No (negEqSym notSameAB)
    decEq B B = Yes Refl

Context

We simply describe the context as a list of name type usage pairings.

  Context : Type
  Context = List (String, Pair Ty Usage)

The Problem: Accounting for freedom.

Our problem is that we want to ensure that our type-level stopping condition requires that all bound terms in our typing context have been used.

We first see how using a typically straight forward approach leads to a valid program that is not informative for describing what is wrong.

Rather trivially we can represent this as a deciable test on the context.

Stating that Items are used.

First, we describe a predicate to describe in an entry in our context is used:

data ItemIsUsed : (item : Pair String (Pair Ty Usage)) -> Type where

If the item is linear it must be ‘consumed’.

  Linear : ItemIsUsed (label, (ty, Consumed))

If the item has unrestricted usage, it has been used.

  Unbound : ItemIsUsed (label, (ty, Unrestricted))

With ItemIsUsed we can show that a Free variable cannot have neen used:

isAmerican : ItemIsUsed (label, ty, Free) -> Void
isAmerican Linear impossible
isAmerican Unbound impossible

We can use this ‘fact’ to write a decision procedure that given an item in our context tells us if it has been used or not.

itemIsUsed : (item : Pair String (Pair Ty Usage)) -> Dec (ItemIsUsed item)
itemIsUsed (label, (ty, use)) with (use)
  itemIsUsed (label, (ty, use)) | Consumed = Yes Linear
  itemIsUsed (label, (ty, use)) | Free = No (isAmerican)
  itemIsUsed (label, (ty, use)) | Unrestricted = Yes Unbound

Stating that contexts have been used.

Using ListD we can then provide quantification over a context stating that ALL items have been used.

AllUsed : (ctxt : Context) -> Type
AllUsed = ListD (Pair String (Pair Ty Usage)) ItemIsUsed

The decision procedure to check if a context has been used is straightforward.

If the head is free then the context has not all been used.

headIsAmerican : (contra : ItemIsUsed x -> Void)
              -> ListD (String, Ty, Usage) ItemIsUsed (x :: xs)
              -> Void
headIsAmerican contra (Extend head tail) = contra head

If the rest of the context has a free item then the context has not all been used.

tailIsAmerican : (contra : ListD (String, Ty, Usage) ItemIsUsed xs -> Void)
              -> ListD (String, Ty, Usage) ItemIsUsed (x :: xs)
              -> Void
tailIsAmerican contra (Extend head tail) = contra tail

Simply, a context has been used if all items have been used, otherwise there is too much freedom.

allUsed : (ctxt : Context) -> Dec (AllUsed ctxt)
allUsed [] = Yes Empty
allUsed (x :: xs) with (itemIsUsed x)
  allUsed (x :: xs) | (Yes prf) with (allUsed xs)
    allUsed (x :: xs) | (Yes prf) | (Yes prfRest) = Yes (Extend prf prfRest)
    allUsed (x :: xs) | (Yes prf) | (No contra) = No (tailIsAmerican contra)

  allUsed (x :: xs) | (No contra) = No ((headIsAmerican contra))

The Real Problem

The real problem here is how can we construct an error message stating which elements in the context are free!

A really simple solution is to perform a value level computation to examine the context and report on the free entries.

However, we can do something a bit more interestingdate: 2020-05-04-DecidablyUninformative.md

Decidably Informative

The Dec data type is a powerful tool in dependently typed programming/proving to represent decidable information. We have seen this with allUsed.

However a problem with Dec is that it is not informative in the negative case, only the positive.

We can prove the negative case, but not use that to construct error messages. For instance, with entries are free in our context.

Enter the DecInfo

DecInfo is variant of Dec to support construction of useful error messages for the negative case in a decidable procedure. There is a copy of this in my containers library.

data DecInfo : (descWrong : descType)
            -> (whyTrue   : trueType)
            -> Type
  where

Like Dec if we can prove something true then present evidence.

    Yes : (prf : whyTrue) -> DecInfo whyWrong whyTrue

If we can prove something false then allow us to provide something that helps describe why.

    No  : (desc : whyWrong)
       -> (contra : whyTrue -> Void)
       -> DecInfo whyWrong whyTrue

An Example

We illustrate DecInfo with the following example:

namespace DecInfoExample

Record where in the context the error occurred and report the name of the free variable.

  data Desc = ErrHere String | ErrThere Desc

Our new version of allUsed' is minimally modified, we add in the error reporting.

  allUsed' : (ctxt : Context) -> DecInfo Desc (AllUsed ctxt)
  allUsed' Nil = Yes Empty
  allUsed' (x :: xs) with (itemIsUsed x)
    allUsed' (x :: xs) | (Yes prfHead) with (allUsed' xs)
      allUsed' (x :: xs) | (Yes prfHead) | (Yes prfTail) = Yes (Extend prfHead prfTail)
      allUsed' (x :: xs) | (Yes prfHead) | (No desc contra) = No (ErrThere desc) (tailIsAmerican contra)

    allUsed' (x :: xs) | (No contra) = No (ErrHere (fst x)) (headIsAmerican contra)

Two examples:

Here we show some examples.

The first demonstrates DecInfo in action showing the result of calling allUsed' on a context with a free variable.

  example : allUsed' [("a", (A, Unrestricted)), ("b", (B, Free))] = No (ErrThere (ErrHere "b"))  (tailIsAmerican (headIsAmerican DecidablyUninformative.isAmerican))
  example = Refl

Using this information, we can now use it to construct an error message containing the name of the free variable.

However, there is a problem. Both Dec and DecInfo are greedy and will return at the first sign of trouble.

With two free variables in the context proof can only be constructed that the first element si free.

  example1 : allUsed'  [("a", (A, Free)), ("b", (B, Free))] = No (ErrHere "a") (headIsAmerican DecidablyUninformative.isAmerican)
  example1 = Refl

  example1' : allUsed'  [("a", (A, Free)), ("b", (B, Free))] = No (ErrThere (ErrHere "b")) (tailIsAmerican (headIsAmerican DecidablyUninformative.isAmerican))
  example1' = ?rhs

Expanding ?rhs will fail to find a solution.

Impact

The Impact here is that one will only discover each free variable in the context in turn as and when you fix the problems. While this might be acceptable to some, to others it is not. Surely we can report each an every free item in the context in a more formal way.

First we must recognise some freedom:

Freedom

namespace RecognisingFreedom

  data ItemIsFree : (item : Pair String (Pair Ty Usage)) -> Type where
    IsFree : (label : String) -> ItemIsFree (label, (ty,Free))

  itemIsUsed' : ItemIsFree (label, ty, Consumed) -> Void
  itemIsUsed' (IsFree _) impossible

  itemIsUnbound : ItemIsFree (label, ty, Unrestricted) -> Void
  itemIsUnbound (IsFree _ ) impossible

  itemIsFree : (item : Pair String (Pair Ty Usage)) -> Dec (ItemIsFree item)
  itemIsFree (label, (ty,use)) with (use)
    itemIsFree (label, (ty,use)) | Consumed = No (itemIsUsed')
    itemIsFree (label, (ty,use)) | Free = Yes (IsFree label)
    itemIsFree (label, (ty,use)) | Unrestricted = No (itemIsUnbound)

Separating the Wheat from the Chaff.

Order preserving encodings (thinnings) are a cool technique to keep account of items at the type-level.

In their simplest form we can use a thinning-like structure to show that items in our context will either be free or be used. We show this as follows.

ShowState records if items in the context are free or not. Here we have used a simple list structure, perhaps a better one will be a vector with an envariant that the length of free and used is equal to the length of the context.

data ShowState : (free : Context)
              -> (used : Context)
              -> (ctxt : Context)
              -> Type
  where

Empty contexts are okay.

    Empty : ShowState Nil Nil Nil

If an item in our context is free, and we have proof of said freedom, then add the entry to the list of free entries.

    MovFree : (prf  : ItemIsFree  item)
           -> (rest : ShowState           frees  used          ctxt)
           -> ShowState          (item :: frees) used (item :: ctxt)

If an item in our context is not free, and we have proof of said usage, then add the entry to the list of used entries.

    MovUsed : (prf  : ItemIsUsed        item)
           -> (rest : ShowState  frees (item:: used)          ctxt)
           -> ShowState          frees         used  (item :: ctxt)

A problem with this approach is that we require distinct proofs to show freedom and usage. When writing the corresponding function to construct this view then we do not have a way to show what to do in the negative case. That is, if something is not free then we must test if it is used if it si not used then we must test if it is free and if it is not free then we must test if it has been used ad infinitum.

A better approach would be to go back to our original statement about entry usage and forumlate our thinning as one that keeps proofs that an item is free, and proofs of void representing that an item has been used.

We use this formulation to allow us to preserve the ‘positive proof’ that something is used, and thus retain information required for error reporting.

Our ShowState now becomes:

namespace Better

  data ShowState' : (free : Context)
                 -> (used : Context)
                 -> (ctxt : Context)
                 -> Type
    where
      Empty : ShowState' Nil Nil Nil

      MovUsed : (prf  : ItemIsFree item -> Void)
             -> (rest : ShowState'     frees        used           ctxt)
             -> ShowState'             frees (item::used) (item :: ctxt)

      MovFree : (prf  : ItemIsFree item)
             -> (rest : ShowState'               frees  used          ctxt)
             -> ShowState'              (item :: frees) used (item :: ctxt)

We use a wrapper data type TheState to remove the need for dependent pairs and type-level quantification of free and used entries. Further, TheState provides proof of the things that are free, and things that are used.

  data TheState : (ctxt : Context) -> Type where
    MkSt : (prfFree  : ListD (Pair String (Pair Ty Usage)) (ItemIsFree)       free)
        -> (prfUsed  : ListD (Pair String (Pair Ty Usage)) (Not . ItemIsFree) used)
        -> (theState : ShowState' free used ctxt)
        -> TheState ctxt

With TheState we can now construct a function to calculate the view and its proofs:

  calcState : (ctxt : Context) -> TheState ctxt
  calcState [] = MkSt Empty Empty Empty
  calcState (x :: xs) with (calcState xs)
    calcState (x :: xs) | (MkSt free used state) with (itemIsFree x)
      calcState (x :: xs) | (MkSt free used state) | (Yes prf)   = MkSt (Extend prf free) used (MovFree prf state)
      calcState (x :: xs) | (MkSt free used state) | (No contra) = MkSt free (Extend contra used) (MovUsed contra state)

and with this view use it to return a list of all names of free variables in our context. When using this function in production we can examine the output to determine if there are unused variables, and if so which ones, together with the knowledge that how we do so is robust.

  isFree : (ctxt : Context) -> List String
  isFree ctxt with (calcState ctxt)
    isFree ctxt | (MkSt free used state) = getNames free

      where
        getNames : ListD (Pair String (Pair Ty Usage)) (ItemIsFree) free' -> List String
        getNames Empty = Nil
        getNames (Extend (IsFree label) tail) = label :: getNames tail

As an example here is a context from before showing isFree in action.

  example : isFree [("a", (A, Unrestricted)), ("b", (B, Free))] = ["b"]
  example = Refl

  example1 : isFree [("a", (A, Free)), ("b", (B, Free))] = ["a", "b"]
  example1 = Refl

Coda

Programming with dependent types is cool and we can use those types to reason more precisely and informatively about our software programs. When we do theorem proving our proofs do not ‘fail’ once they are written; they are proofs of correctness.

However, and especially when dealing with user input, we need the ability to be able to provide informative error messages in our software programs. Decision procedures such as Dec and DecInfo are limited in what information they can present to construct error messages, and we need to think more carefully about how we use these tools in our code.

Hopefully, you may have learned something from this.

As a side note: If we examine the structure of TheState, and its use in calcState and isFree we can see that if is essentially a ‘verified’ version of filter.

Compare with this forumlation that I borrowed from a Frenchman.


Related Posts


← Mutual Avoidance | The Last Step →