<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
On 2021-04-15 8:54 a.m., YueCompl via Haskell-Cafe wrote:<br>
<blockquote type="cite"
cite="mid:679DF502-F8BF-42DE-8DFA-AA7226208A31@icloud.com">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<div class="">
<div style="color: rgb(212, 190, 152); background-color: rgb(23, 24, 24); font-family: Menlo, Monaco, "Courier New", monospace; line-height: 18px; white-space: pre;" class="">
<div class=""><span style="color: #7e6b5f;" class="">-- * Things not working</span></div>
<div class=""><span style="color: #66a89d;" class="">managedArrayAsSeries</span><span style="color: #96b946;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">::</span><span style="color: #96b946;" class=""> </span><span style="color: #6b95c5;" class="">SomeManagedArray</span><span style="color: #96b946;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">-></span><span style="color: #96b946;" class=""> </span><span style="color: #6b95c5;" class="">IO</span><span style="color: #96b946;" class=""> </span><span style="color: #6b95c5;" class="">Dynamic</span></div><div class=""><span style="color: #d4bfa0;" class="">managedArrayAsSeries </span>(<span style="color: #cca143;" class="">SomeManagedArray</span><span style="color: #d4bfa0;" class=""> ma</span>)<span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;font-weight: bold;" class="">=</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;" class="">do</span></div><div class=""><span style="color: #d4bfa0;" class=""> vec </span><span style="color: #cc524b;font-weight: bold;" class=""><-</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;" class="">do</span></div><div class=""><span style="color: #d4bfa0;" class=""> </span><span style="color: #cca143;" class="">SomeArray</span><span style="color: #d4bfa0;" class=""> cap fp </span><span style="color: #cc524b;font-weight: bold;" class=""><-</span><span style="color: #d4bfa0;" class=""> arrayAtTheMoment ma</span></div><div class=""><span style="color: #d4bfa0;" class=""> return </span><span style="color: #a3d349;" class="">$</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #73a36c;" class="">VS.</span><span style="color: #d4bfa0;" class="">unsafeFromForeignPtr0 fp cap</span></div>
<div class=""><span style="color: #d4bfa0;" class=""> </span><span style="color: #cc524b;" class="">let</span><span style="color: #d4bfa0;" class=""> len </span><span style="color: #cc524b;font-weight: bold;" class="">=</span><span style="color: #d4bfa0;" class=""> return </span><span style="color: #a3d349;" class="">$</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #73a36c;" class="">VS.</span><span style="color: #d4bfa0;" class="">length vec</span></div><div class=""><span style="color: #d4bfa0;" class=""> rs i </span><span style="color: #cc524b;font-weight: bold;" class="">=</span><span style="color: #d4bfa0;" class=""> return </span><span style="color: #a3d349;" class="">$</span><span style="color: #d4bfa0;" class=""> vec </span><span style="color: #73a36c;" class="">VS.</span><span style="color: #a3d349;" class="">!</span><span style="color: #d4bfa0;" class=""> i</span></div><div class=""><span style="color: #d4bfa0;" class=""> return </span><span style="color: #a3d349;" class="">$</span><span style="color: #d4bfa0;" class=""> toDyn </span><span style="color: #a3d349;" class="">$</span><span style="color: #d4bfa0;" class=""> </span><span style="color: #cca143;" class="">Series</span><span style="color: #d4bfa0;" class=""> len rs</span></div>
</div>
</div>
</blockquote>
<p>That means you have this code fragment:</p>
<pre>arrayAtTheMoment ma
>>=
\(SomeArray cap fp) -> return $ VS.unsafeFromForeignPtr0 fp cap
</pre>
<p>That means you have this function:</p>
<pre>\(SomeArray cap fp) -> return $ VS.unsafeFromForeignPtr0 fp cap</pre>
<p>Now you are violating the 1st restriction at<br>
<a moz-do-not-send="true"
href="https://downloads.haskell.org/ghc/latest/docs/html/users_guide/exts/existential_quantification.html#restrictions">https://downloads.haskell.org/ghc/latest/docs/html/users_guide/exts/existential_quantification.html#restrictions</a></p>
<p>There are two solutions.</p>
<p>1st solution: One more existential type.</p>
<pre>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))
</pre>
<p>2nd solution: CPS transform.</p>
<pre>{-# 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)
etc.
</pre>
</body>
</html>