<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">What a concise explanation and practical solutions! Thanks!<div class=""><br class=""></div><div class="">With the hint from <a href="https://www.reddit.com/user/bss03/" class="">https://www.reddit.com/user/bss03/</a> I've figured out the [1st solution](<a href="https://www.reddit.com/r/haskell/comments/mre9ha/trick_to_have_existential_type_work_in_this_case/guw5y80/?context=3" class="">https://www.reddit.com/r/haskell/comments/mre9ha/trick_to_have_existential_type_work_in_this_case/guw5y80/?context=3</a>).</div><div class=""><br class=""></div><div class="">And your 2nd solution really updated my knowledge about CPS, I used to assume some "continuation" always has to be passed around in CPS, now I know it can be used for nesting of scopes, and to have it naturally "return" back to outer scope.</div><div class=""><br class=""></div><div class="">Thanks with best regards!</div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 2021-04-16, at 02:27, Albert Y. C. Lai <<a href="mailto:trebla@vex.net" class="">trebla@vex.net</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" class="">
  
  <div class="">
    On 2021-04-15 8:54 a.m., YueCompl via Haskell-Cafe wrote:<br class="">
    <blockquote type="cite" cite="mid:679DF502-F8BF-42DE-8DFA-AA7226208A31@icloud.com" class="">
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" class="">
      <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 class="">That means you have this code fragment:</p>
    <pre class="">arrayAtTheMoment ma
>>=
\(SomeArray cap fp) -> return $ VS.unsafeFromForeignPtr0 fp cap
</pre><p class="">That means you have this function:</p>
    <pre class="">\(SomeArray cap fp) -> return $ VS.unsafeFromForeignPtr0 fp cap</pre><p class="">Now you are violating the 1st restriction at<br class="">
      <a moz-do-not-send="true" href="https://downloads.haskell.org/ghc/latest/docs/html/users_guide/exts/existential_quantification.html#restrictions" class="">https://downloads.haskell.org/ghc/latest/docs/html/users_guide/exts/existential_quantification.html#restrictions</a></p><p class="">There are two solutions.</p><p class="">1st solution: One more existential type.</p>
    <pre class="">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 class="">2nd solution: CPS transform.</p>
    <pre class="">{-# 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>
  </div>

_______________________________________________<br class="">Haskell-Cafe mailing list<br class="">To (un)subscribe, modify options or view archives go to:<br class=""><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe</a><br class="">Only members subscribed via the mailman list are allowed to post.</div></blockquote></div><br class=""></div></body></html>