Using haskell's singletons, how can I write `fromList :: [a] -> Vec a n`?

后端 未结 2 1884
离开以前
离开以前 2020-12-19 14:11

As part of my journey in understanding singletons I have tried to bridge the gap between compile time safety, and lifting runtime values into that dependent typ

相关标签:
2条回答
  • 2020-12-19 14:41

    You can do this with an existentially quantified type variable, as in @Alec’s answer, or equivalently by rewriting in continuation-passing style. The trick is to give fromList a continuation (function) that’s polymorphic in the size of the Vec; then, within the continuation, you have access to a type variable representing the size:

    data Vec n a where
      Nil :: Vec Z a
      Cons :: a -> Vec n a -> Vec (S n) a
    
    deriving instance (Show a) => Show (Vec n a)
    
    fromList :: [a] -> (forall n. Vec n a -> r) -> r
    fromList [] k = k Nil
    fromList (x : xs) k = fromList xs $ \ xs' -> k (Cons x xs')
    
    -- fromList [1, 2, 3] show == "Cons 1 (Cons 2 (Cons 3 Nil))"
    

    You can’t know the actual value of n, because it’s not available at compile time.

    If you replace your Nat with the one from GHC.TypeLits, I think you can get a KnownNat constraint for n by creating a SomeNat from the runtime length using fromJust (someNatVal (fromIntegral (length xs))), then get back the actual length value at runtime with natVal. I’m not really familiar with how to do it, and it might require the ghc-typelits-natnormalise plugin, but it might be a starting point.

    0 讨论(0)
  • 2020-12-19 14:49

    This is not possible using Haskell because Haskell does not yet have full dependent types (although GHC might in the future). Notice that

    fromList :: [a] -> Vec a n
    

    Has both a and n quantified universally, which means that the user should be able to pick their n and get back a Vec of the right size. That makes no sense! The trick is that n is not really for the user to choose - it has to be the length of the input list. (For the same reason, fromList :: Integer -> [a] -> Vec a n would not be any more useful - the size hint has to be something type-level.)

    Looking to a dependently typed language like Idris, you can define

    fromList : (l : List elem) -> Vec (length l) elem
    

    And in fact they define this in the standard library.

    So, what can you do? Short of saying that Vec has the length equal to the size of the input list (which requires lifting "length of the input list" to the type level), you can say it has some length.

    data SomeVec a where { SomeVec :: Vec a n -> SomeVec a }
    
    list2SomeVec :: [a] -> SomeVec a
    list2SomeVec [] = SomeVec Nil
    list2SomeVec (x:xs) = case list2SomeVec xs of
                            SomeVec ys -> SomeVec (x `Cons` ys)
    

    That isn't spectacularly useful, but it is better than nothing.

    0 讨论(0)
提交回复
热议问题