6

Here's a syntax for a dependently-typed lambda calculus.

data TermI a = Var a
             | App (TermI a) (TermC a)  -- when type-checking an application, infer the type of the function and then check that its argument matches the domain
             | Star  -- the type of types
             | Pi (Type a) (Scope () Type a)  -- The range of a pi-type is allowed to refer to the argument's value
             | Ann (TermC a) (Type a)  -- embed a checkable term by declaring its type
             deriving (Functor, Foldable, Traversable)

data TermC a = Inf (TermI a)  -- embed an inferrable term
             | Lam (Scope () TermC a)
             deriving (Functor, Foldable, Traversable)

type Type = TermC  -- types are values in a dependent type system

(I more-or-less lifted this from Simply Easy.) The type system is bidirectional, splitting terms into those whose type can be inferred from the typing context, and those which can only be checked against a goal type. This is useful in dependent type systems because in general lambda terms will have no principal type.

Anyway, I've got stuck trying to define a Monad instance for this syntax:

instance Monad TermI where
    return = Var
    Var x >>= f = f x
    App fun arg >>= f = App (fun >>= f) (arg >>= Inf . f)  -- embed the substituted TermI into TermC using Inf
    Star >>= _ = Star
    Pi domain range >>= f = Pi (domain >>= Inf . f) (range >>>= Inf . f)
    Ann term ty >>= f = Ann (term >>= Inf . f) (ty >>= Inf . f)

instance Monad TermC where
    return = Inf . return
    Lam body >>= f = Lam (body >>>= f)
    Inf term >>= f = Inf (term >>= _)

To fill the hole in the last line of TermC's instance, I need something of type a -> TermI b but f has a type of a -> TermC b. I can't embed the resulting TermC into TermI using the Ann constructor because I don't know the type of the TermC.

Is this datatype incompatible with bound's model? Or is there a trick I can use to make the Monad instance go?

6
  • If you run (abusing the notation) (var 0 (var 1)) [\x -> x], you'll get (\x -> x) (var 1), which doesn't have a syntactic representation in your type system. Note that both substs in the paper receive inferrable terms and there is no subst : TermC -> TermC -> TermC. It's not necessarily to have bidirectional type system in order to define a bidirectional type checker, so you can just collapse these mutually recursive data types into a single Term. Aug 20, 2016 at 19:17
  • 1
    bound doesn't support mutual data, but (as said above) you can do your type checker with a single data definition. The indexed version of bound can do mutual data (for example), but it doesn't exist as published library. Aug 21, 2016 at 9:58
  • Throwing everything into a single datatype is not without downsides: it lets you write a Lam without an annotation, which should not be syntactically valid. I suppose I could require a type in the Lam constructor (Lam (Scope () Term a) Type), but then you get superfluous annotations for nested lambdas, and you have to support an additional construct for annotating other terms. Aug 21, 2016 at 11:28
  • If bound doesn't do the trick, you may look into the more typey paper bound simplifies. I don't know if it's the right sort of typey, but it's worth checking.
    – dfeuer
    Aug 21, 2016 at 17:27
  • @dfeuer D'you mean de Bruijn Notation as a Nested Datatype? As far as I can tell, the main difference between bound and that paper is that bound's Var has an extra parameter to account for the type of variables bound in a particular scope. (If anything bound is the more typey of the two!) Aug 21, 2016 at 19:28

1 Answer 1

2

It's quite simply impossible to do: TermC is not a monad. Substitution puts terms in place of variables. For this to make sense, the terms need to be able to fit, i.e. to be similar enough so that the resulting term still has good properties. Here it means that its type must be inferrable. TermC won't do.

You can either implement:

 substI :: TermI a -> (a -> TermI b) -> TermI b
 substC :: TermC a -> (a -> TermI b) -> TermC b

and have

 instance Monad TermI where
   return = Var
   bind   = substI
1
  • This kinda works - you get the Monad instance - but it ends up ugly. You can't use most of bound's combinators (including >>>= - you have to implement subst by fmapping under the Scope) because they require a Monad instance for Scope's second parameter. So you end up reimplementing most of bound. Aug 22, 2016 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.