GHC's DataKinds extension allows for defining types that are parametrized by some data. Is there some possible way to use these these parameters on the RHS of a function definition? For example, in Agda I could extract the length of a vector directly from its type without computing on its constructors:
length : Vec A n -> Nat
length {n = n} _ = n
Is there a possible way to do this in Haskell?
The reason I am interested in this is because I think it would be helpful for a project I'm doing, which is a custom Haskell library for calling Java in a type-safe way (as safe as it is possible to achieve). I thought instead of having a single type representing all Java objects I could have a type parametrized by a Java class name as a string (e.g. JObject "java.math.BigDecimal"), so that instances of different classes in Java would also have different types in their representations in Haskell. And if what I am asking here is possible, then it would allow for automatically computing the JNI type-signature string from the given Haskell type of a method, so that I could write something like
method <- findMethod "toString" :: IO (JMethod (JObject "java.math.BigDecimal") (IO JString))
In Haskell, types are erased, they do not exist at run time.
DataKindslets you use data constructors in types, but they are still erased. In a function of typeforall n. Vec A n -> Nat, it's not possible to returnnbecause it will be erased.For
nto not be erased, you would have a typeVec A nwhich depends on a run-time valuen, which is what dependent types are about.Dependent types are not a native feature of Haskell (yet). However, it is possible to emulate them using singletons. The type of
lengthwould then beforall n. SingI n => Vec A n -> Nat, where theSingIconstraint provides a run-time reflection ofn.