First, let me say that lambdas are one part of ATS that I'm not super 
experienced in myself.  However, if you haven't been there already, I'd 
recommend reading the chapters on linear & stack-allocated closures in the 
" A Tutorial on Programming Features in ATS " here 
<http://ats-lang.sourceforge.net/DOCUMENT/ATS2TUTORIAL/HTML/c1220.html> & 
here <http://ats-lang.sourceforge.net/DOCUMENT/ATS2TUTORIAL/HTML/c1267.html> 
respectively.

Now, to answer your questions:

   1. (Why is it failing to compile?)  Your lambda doesn't see "acc", the 
   easiest way to solve this is to *pass "acc" to your lambda via reference* 
   (aka use the "&" operator in the function argument.)
      1. Side note: You don't need to say "x: !int", *because "int" is not 
      a linear type*, you can leave it "x: int" without the "!" part.
      2. (Is it possible to avoid the UNSAFE cast?)  The page I linked to 
   you above about linear closure says that yes, there is a way, but that it's 
   cumbersome to use.  That's all I know, I've not seen any example code.
   3. (Can I do this with a stack allocated closure?)  Yes, and in fact 
   it's probably better to use a stack allocated closure in this instance 
   because you are not keeping the closure around to use with multiple 
   functions.  Were you storing this closure in a struct for example, then a 
   linear closure should be used.

Let me know if you have any other questions, and good luck!

On Monday, October 4, 2021 at 11:19:34 PM UTC-4 mark.l....@gmail.com wrote:

> Hi all,
>
> I'm trying to understand higher-order programming in ATS better, but I 
> keep running into errors when my lambdas use linear resources.  Here's a 
> small example that doesn't compile:
>
>     extern fn {a,b:vt@ype} map_vt {n:nat} {e:eff} (
>       l: !list_vt(a, n), f: !(!a) -<cloptr,e> b
>     ):<!wrt,e> list_vt(b, n)
>
>     fn prefix_sums {n:nat} (l: !list_vt(int, n)): list_vt(int, n) =
>       let
>         var acc: int = 0
>         val f = lam(x: !int): int =<cloptr,!wrt> (acc := acc + x; acc)
>         val result = map_vt(l, f)
>         val () = cloptr_free{void}($UNSAFE.castvwtp0(f))
>       in result end
>
> Compiling this gives an error saying that proof search for the view at 
> 'acc' failed.
>
> So, my questions are:
> 1. Why is this failing to compile, and how can I fix it? (Ideally without 
> threading through an 'env' view parameter to all the closures.)
> 2. Is there a way to avoid the UNSAFE cast?
> 3. Would there be a way to do it with a stack allocated closure?
>
> Thanks!
>
> -Mark
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"ats-lang-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ats-lang-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ats-lang-users/aee5f45c-e423-40ba-b471-3344c280f64dn%40googlegroups.com.

Reply via email to