am hoping some Haskell experts can help clarify something.
Is it possible to define Nat in the usual way (via @dorchard Singleton types in Haskell)
data S n = Succ n
data Z = Zero
class Nat n
instance Nat Z
instance Nat n => Nat (S n)
(or some variant thereof) and then define a LessThan relation
such that forall n and m
LessThan Z (S Z)
LessThan n m => LessThan n (S m)
LessThan n m => LessThan (S n) (S m)
and then write a function with a type like:
foo :: exists n. (LessThan n m) => Nat m -> Nat n
foo (S n) = n
foo Z = foo Z
I explicitly want to use the "LessThan" in the output type for foo,
I realize that one could certainly write something like
foo :: Nat (S n) -> Nat n
but thats not what I'm after.
Thanks!
Ranjit.
foo :: exists n...– really? So you want to allowfooto return any type it likes, with the only constraint that it be "less thanm"? That's not possible in Haskell (not just like that), and rightly so. Or do you rather mean,foocan return any type the caller requests, as long as it's less thanm? - leftaroundaboutLessThanneeds to be in the type system too. It's safe to ignore type level programming until you're very confident with Haskell. - AndrewC