I have a simple length-indexed vector type and an append
function over length-indexed vectors:
{-# LANGUAGE GADTs #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
module LengthIndexedList where
data Zero
data Succ a
type family Plus (a :: *) (b :: *) :: *
type instance Plus Zero b = b
type instance Plus (Succ a) b = Succ (Plus a b)
data Vec :: * -> * -> * where
VNil :: Vec a Zero
VCons :: a -> Vec a n -> Vec a (Succ n)
-- If you remove the following type annotation, type inference
-- fails.
-- append :: Vec a n1 -> Vec a n2 -> Vec a (Plus n1 n2)
append v1 v2 = case v1 of
VNil -> v2
(VCons x xs) -> VCons x (append xs v2)
Compilation fails as GHC cannot infer a type for the append
function. I understand that type inference is tricky in presence of GADTs and Type Families partly due to polymorphic recursion. Nevertheless, as per Vytiniotis et al's JFP paper GHC7's type inference should work in presence of "type classes + GADTs + type families". In this context, I have two questions:
- Why isn't type inference working for the above example (I am using GHC7)?
- What is a non-trivial example involving GADTs and Type Functions (like the
append
above) for which GHC can infer types?