possible fun template misunderstanding

45 views
Skip to first unread message

xor.r...@gmail.com

unread,
Jun 26, 2017, 1:51:51 PM6/26/17
to ats-lang-users
lately, i got interested in ats2. i wrote some trivia code and stumbled upon the following:

i would say
- [bp1.dats] output is wrong?
- [bp2.dats] should not compile, but does?
- [bp3.dats] is correct (this was my "fix" for bp1.dats)?

is it because of the actual template params not beeing used?? is this wanted behaviour or a bug?

especially bp2.dats looks extremely suspicous.

bp1.dats:


// patscc -DATS_MEMALLOC_LIBC -Ofast -flto -o bp bp.dats


#include "share/atspre_staload.hats"


staload UN
= "prelude/SATS/unsafe.sats"


absvt@ype be
= ptr
absvt@ype le
= ptr


typedef u16 = uint16


extern fun{env:vt0p} rd16 ( env: &env >> _ ) : u16


implmnt
{be} rd16 ( env ) = $UN.cast{u16}(23)
implmnt
{le} rd16 ( env ) = $UN.cast{u16}(42)


assume be
= @{a=int}
assume le
= @{b=int}


implmnt main0
( argc, argv ) : void =
    let val odd
= argc % 2 = 0
       
var env_be: be = @{a=0}
       
var env_le: le = @{b=0}
   
in
       
if odd
           
then (
               
print!("be: ")
             
; println!(rd16(env_be))
           
)
       
else (
           
print!("le: ")
         
; println!(rd16(env_le))
       
)
   
end
$ patscc
-DATS_MEMALLOC_LIBC -Ofast -flto -o bp1 bp1.dats
$
./bp1 x
be
: 42
$
./bp1 x y
le
: 42

bp2
.dats:


// patscc -DATS_MEMALLOC_LIBC -Ofast -flto -o bp bp.dats


#include "share/atspre_staload.hats"


staload UN
= "prelude/SATS/unsafe.sats"


absvt@ype be
= ptr
absvt@ype le
= ptr


typedef u16 = uint16


extern fun{env:vt0p} rd16 ( env: &env >> _ ) : u16


assume be
= @{a=int}
assume le
= @{b=int}


implmnt main0
( argc, argv ) : void =
    let val odd
= argc % 2 = 0
       
var env_be: be = @{a=0}
       
var env_le: le = @{b=0}
   
in
       
if odd
           
then let implmnt{be} rd16 ( env ) = $UN.cast{u16}(23) in (
               
print!("be: ")
             
; println!(rd16(env_be))
           
) end
       
else let implmnt{be} rd16 ( env ) = $UN.cast{u16}(42) in (
           
print!("le: ")
         
; println!(rd16(env_le))
       
) end
   
end
$ patscc
-DATS_MEMALLOC_LIBC -Ofast -flto -o bp2 bp2.dats
$
./bp2 x
be
: 23
$
./bp2 x y
le
: 42

bp3
.dats


// patscc -DATS_MEMALLOC_LIBC -Ofast -flto -o bp bp.dats


#include "share/atspre_staload.hats"


staload UN
= "prelude/SATS/unsafe.sats"


absvt@ype be
= ptr
absvt@ype le
= ptr


typedef u16 = uint16


extern fun{env:vt0p} rd16 ( env: &env >> _ ) : u16


assume be
= @{a=int}
assume le
= @{b=int}


implmnt main0
( argc, argv ) : void =
    let val odd
= argc % 2 = 0
       
var env_be: be = @{a=0}
       
var env_le: le = @{b=0}
   
in
       
if odd
           
then let implmnt{be} rd16 ( env ) = $UN.cast{u16}(23) in (
               
print!("be: ")
             
; println!(rd16(env_be))
           
) end
       
else let implmnt{le} rd16 ( env ) = $UN.cast{u16}(42) in (
           
print!("le: ")
         
; println!(rd16(env_le))
       
) end
   
end
$ patscc
-DATS_MEMALLOC_LIBC -Ofast -flto -o bp3 bp3.dats
$
./bp3 x
be
: 23
$
./bp3 x y
le
: 42






Hongwei Xi

unread,
Jun 26, 2017, 5:03:35 PM6/26/17
to ats-lan...@googlegroups.com
(*
bp1.dats:
*)


// patscc -DATS_MEMALLOC_LIBC -Ofast -flto -o bp bp.dats


#include "share/atspre_staload.hats"


staload UN = "prelude/SATS/unsafe.sats"


absvt@ype be = ptr
absvt@ype le = ptr


typedef u16 = uint16


extern fun{env:vt0p} rd16 ( env: &env >> _ ) : u16

The code for bp1 should be written as follows:

(*

implmnt{be} rd16 ( env ) = $UN.cast{u16}(23)
implmnt{le} rd16 ( env ) = $UN.cast{u16}(42)
*)
implmnt
rd16<be> ( env ) = $UN.cast{u16}(23)
implmnt
rd16<le> ( env ) = $UN.cast{u16}(42)



assume be = @{a=int}
assume le = @{b=int}


implmnt main0 ( argc, argv ) : void =
    let val odd = argc % 2 = 0
        var env_be: be = @{a=0}
        var env_le: le = @{b=0}
    in
        if odd
            then (
                print!("be: ")
              ; println!(rd16<be>(env_be))

            )
        else (
            print!("le: ")
          ; println!(rd16<le>(env_le))
        )
    end

I will take a look at bp2 and bp3 later.


--
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/30790728-dfee-41b0-81ce-adefce142058%40googlegroups.com.

Hongwei Xi

unread,
Jun 26, 2017, 5:08:41 PM6/26/17
to ats-lan...@googlegroups.com
Basically, the issue can be explained as follows:

If you write

implement{be} rd16(...) = ...

then 'be' is a bound variable, which can be instantiated
with any type.

What you need is to write

implement rd16<be> (...) = ...

where 'be' is treated as the abstract type you declared earlier.


To post to this group, send email to ats-lan...@googlegroups.com.

n and

unread,
Jun 27, 2017, 11:36:13 AM6/27/17
to ats-lan...@googlegroups.com
thanks! stupid me. i got confused... i thought when using implement{x} it would do the instantiation env <-| x. 
definitely need more practice ;)

You received this message because you are subscribed to a topic in the Google Groups "ats-lang-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ats-lang-users/NUtFyrpftlU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ats-lang-users+unsubscribe@googlegroups.com.
To post to this group, send email to ats-lang-users@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages