Using a stack allocated tuple as an enviroment

54 views
Skip to first unread message

aditya siram

unread,
Dec 10, 2017, 9:06:28 AM12/10/17
to ats-lang-users
Hi,
I'm trying to pass and mutate a stack allocated tuple in a string processing function:

fn length_last
 
{l:nat | l > 1 }
 
(
    s
: string l
 
): (int, char) =
  let
   
var env = (0,s.head())
    implement stream_vt_foreach$fwork
<charNZ><(int,charNZ)>(c,env) =
      env
:= (env.0 + 1, c)
    val _
= stream_vt_foreach_env(
              streamize_string_char
(s.tail()),
              env
           
)
 
in
   env
 
end

 The stack allocated 'env' should finally have the length of the string and the last character after 'stream_vt_foreach_env' is run. Instead I get compile errors like:
length_last.dats:15:13: the dynamic expression should be a left-value but it is not.

length_last
.dats:11:3: the linear dynamic variable [env$view$5548(-1)] is preserved but with an incompatible type.

length_last
.dats:11:3: mismatch of static terms (tyleq):

The actual term is: S2Etop(knd=0; [1])
[1]: S2EVar(5260)
The needed term is: S2Etyrec(flt0; npf=-1; 0=S2Etop(knd=0; S2Etop(knd=0; [1])), 1=S2Etop(knd=0; S2Etop(knd=0; [2])))
[1]: g1int_int_t0ype(int_kind, 0)
[2]: charNZ

length_last
.dats:11:3: mismatch of static terms (tyleq):

The actual term is: S2Eat(S2Etop(knd=0; [1]); [2])
[1]: S2EVar(5260)
[2]: env
The needed term is: S2Eat(S2Etyrec(flt0; npf=-1; 0=S2Etop(knd=0; S2Etop(knd=0; [1])), 1=S2Etop(knd=0; S2Etop(knd=0; [2]))); [3])
[1]: g1int_int_t0ype(int_kind, 0)
[2]: charNZ
[3]: env
patsopt
(TRANS3): there are [2] errors in total.
exit(ATS): uncaught exception: _2home_2deech_2Downloads_2ATS_2ATS2_2src_2pats_error_2esats__FatalErrorExn(1025)

Is the problem that I'm passing a stack allocated tuple as the environment?

Thanks!

Hongwei Xi

unread,
Dec 10, 2017, 9:41:18 AM12/10/17
to ats-lan...@googlegroups.com
This is the best I could come up with.

Mixing templates with dependent types almost always
causes problems.

(* ****** ****** *)
//
#include
"share/atspre_staload.hats"
//

fn
length_last
{l:nat | l > 1 }
(
 s: string l
) : (int, char) = let
  typedef tenv = (int, char)
  var env: tenv = (0,s.head())
  implement
  stream_vt_foreach$fwork<char><tenv>(c,env) = env := (env.0 + 1, c)
  val-~stream_vt_nil() =
    stream_vt_foreach_env<char><tenv>(streamize_string_char(s.tail()), env)
in
  env
end

implement
main0() = let
val
tup =
length_last("abcde")
in
println! ("(", tup.0, ", ", tup.1, ")")
end // end of [main0]


--
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+unsubscribe@googlegroups.com.
To post to this group, send email to ats-lang-users@googlegroups.com.
Visit this group at https://groups.google.com/group/ats-lang-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/ats-lang-users/012ab377-9bd9-401d-b738-b9a3efbfd841%40googlegroups.com.

aditya siram

unread,
Dec 10, 2017, 9:52:37 AM12/10/17
to ats-lang-users
Oh, thanks!

So if I wanted to return '(int, charNZ)' instead I'd need some kind of 'praxi' asserting that it's not null?
To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-user...@googlegroups.com.
To post to this group, send email to ats-lan...@googlegroups.com.

Hongwei Xi

unread,
Dec 10, 2017, 9:58:43 AM12/10/17
to ats-lan...@googlegroups.com
I changed char to charNZ, and it actually worked!

To unsubscribe from this group and stop receiving emails from it, send an email to ats-lang-users+unsubscribe@googlegroups.com.
To post to this group, send email to ats-lang-users@googlegroups.com.

aditya siram

unread,
Dec 10, 2017, 10:12:41 AM12/10/17
to ats-lang-users
Ah, very nice. So the moral seems to be to fully annotate all template functions.
Reply all
Reply to author
Forward
0 new messages