[Haskell-cafe] Trick to have existential type work in this case?

Albert Y. C. Lai trebla at vex.net
Thu Apr 15 18:27:18 UTC 2021

On 2021-04-15 8:54 a.m., YueCompl via Haskell-Cafe wrote:
> -- * Things not working
> managedArrayAsSeries::SomeManagedArray->IODynamic
> managedArrayAsSeries (SomeManagedArrayma)=do
> vec <-do
> SomeArraycap fp <-arrayAtTheMoment ma
> return $VS.unsafeFromForeignPtr0 fp cap
> letlen =return $VS.length vec
> rs i =return $vec VS.!i
> return $toDyn $Serieslen rs

That means you have this code fragment:

arrayAtTheMoment ma
\(SomeArray cap fp) -> return $ VS.unsafeFromForeignPtr0 fp cap

That means you have this function:

\(SomeArray cap fp) -> return $ VS.unsafeFromForeignPtr0 fp cap

Now you are violating the 1st restriction at

There are two solutions.

1st solution: One more existential type.

data Vec = forall a. (Typeable a, VS.Storable a) => Vec (VS.Vector a)

   Vec vec <- do
     SomeArray cap fp <- arrayAtTheMoment ma
     return (Vec (VS.unsafeFromForeignPtr0 fp cap))

2nd solution: CPS transform.

{-# language RankNTypes #-}
{-# language BlockArguments #-}

withSomeArray :: SomeArray
               -> (forall a. (Typeable a, VS.Storable a) => Int -> ForeignPtr a -> r)
               -> r
withSomeArray (SomeArray i p) f = f i p

sa <- arrayAtTheMoment ma
withSomeArray sa \cap fp -> do
   let vec = VS.unsafeFromForeignPtr0 fp cap
       -- or if you prefer: vec <- return (VS.unsafeFromForeignPtr0 fp cap)
       len = return (VS.length vec)
       rs i = return (vec VS.! i)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20210415/d131f5dc/attachment.html>

More information about the Haskell-Cafe mailing list