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?
(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 bothsubst
s in the paper receive inferrable terms and there is nosubst : 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 singleTerm
.bound
doesn't support mutual data, but (as said above) you can do your type checker with a single data definition. The indexed version ofbound
can do mutual data (for example), but it doesn't exist as published library.Lam
without an annotation, which should not be syntactically valid. I suppose I could require a type in theLam
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.bound
doesn't do the trick, you may look into the more typey paperbound
simplifies. I don't know if it's the right sort of typey, but it's worth checking.bound
and that paper is thatbound
'sVar
has an extra parameter to account for the type of variables bound in a particular scope. (If anythingbound
is the more typey of the two!)