Why this code segmentation fault (core dumped)

Any idea why this code (given below) seg faults when run from console .

#include “share/atspre_define.hats”
#include "share/atspre_staload.hats"
staload
UN = “prelude/SATS/unsafe.sats”

dataview slseg_v (a:t@ype+, int(len) , addr(beg) , addr(end)) =
| {l:addr} slseg_v_nil(a,0,l,l) of ()
| {n:nat} {l_fst : agz} {l_nxt :addr} {l_end : addr}
slseg_v_cons (a,n+1,l_fst,l_end) of
( (a,ptr l_nxt) @ l_fst , slseg_v(a , n , l_nxt , l_end))

viewdef sl_v (a:t@ype+ , n:int , l:addr) = slseg_v (a,n,l,null)

fun
make_nil_list {a:t@ype+} () : (sl_v (a,0,null) | ptr null) =
(slseg_v_nil () | $UN.cast{ptr null}(0))

fun
node_alloc{a:t@ype+}{m:addr} () :
[l:agz] ((a, ptr m)? @ l, mfree_gc_v (l) | ptr (l))
= ptr_alloc<(a,ptr m)> ()

//extern
fun
print_lst {a:t@ype} {n:nat} {l:addr} ..
(pf : !sl_v (a,n,l) | p : ptr l, printfn : a - void) : void =
if ptr1_is_null§ then let
prval slseg_v_nil () = pf
val () = println!(“done”) in pf := slseg_v_nil () end
else let
prval slseg_v_cons (pat , plst) = pf
val x = !p.0
val () = printfn (x)
val nxt_ptr = !p.1
val ()= println!(ptr1_is_null (nxt_ptr))

val () = println!("ok ")
val () = print_lst (plst | nxt_ptr, printfn)
in pf := slseg_v_cons (pat , plst) end

//extern
prfun
at2lst{a:t@ype} {n:nat} {l:agz} {m:addr} .<>.
(pat : (a,ptr m) @ l, plst : sl_v(a,n,m) ) : sl_v(a,n+1,l) =
slseg_v_cons(pat , plst)

val (pf_null_lst | null_p) = make_nil_list{int}()
val () = print_lst{int}{…}{…} (pf_null_lst | null_p , lam (x) => ())
val [l:addr] (pat,pfree | p) = node_alloc{int}()
val () = !p.1 := null_p
val () = !p.0 := 1908
prval pf_lst = at2lst{int}{…}{…}{…} (pat , pf_null_lst)

val () = print_lst{int}{…}{…} (pf_lst | p , lam (x) => () )

implement main0 () = {
(* val () = print(“hello\n”) )
(
val () = println!(“whatever”) )
(
val x = 123 )
(
val () = println!(“hello “,x,” hah”) )
(
val () = print_val (100) *)
val () = println!()

}

As this kind of error (templates implemented as polymorphic functions)
is very common among beginners, I have added a little bit safe guard: The
generated C code should now be stopped by gcc (or clang).On Friday, January 17, 2014 3:08:03 PM UTC-5, gmhwxi wrote:

Even after you fix the problem I mentioned above,
you still need to fix print_lst a bit to make it work. See
the code given below.

As I mentioned earlier, it can be quite challenging to write code
this way. For learning ATS concepts, this is okay. For using ATS, IMO,
this is not a productive way.

fun{a:t@ype}
node_alloc{m:addr} () :
[l:agz] ((a, ptr m)? @ l, mfree_gc_v (l) | ptr (l))
= ptr_alloc<(a,ptr m)> ()

//extern
fun{a:t@ype}
print_lst{n:nat} {l:addr} ..
(pf : !sl_v (a,n,l) | p : ptr l, printfn : a - void) : void =
if ptr1_is_null(p) then let
prval slseg_v_nil () = pf
val () = println!(“done”) in pf := slseg_v_nil () end
else let
prval
slseg_v_cons{…}{…}
{l_fst}{l_nxt}{l_end} (pat , plst) = pf
prval pat = pat: (a, ptr(l_nxt)) @ l_fst
val x = !p.0
val () = printfn (x)
val nxt_ptr = !p.1
val ()= println!(ptr1_is_null (nxt_ptr))

val () = println!("ok ")
val () = print_lst (plst | nxt_ptr, printfn)
in pf := slseg_v_cons (pat , plst) end

On Friday, January 17, 2014 2:04:02 PM UTC-5, gmhwxi wrote:

node_alloc should be a template (instead of polymorphic) function:

fun{a:t@ype} node_alloc …

The same about print_lst.

On Friday, January 17, 2014 3:04:38 AM UTC-5, chota singh wrote:

Any idea why this code (given below) seg faults when run from console .

#include “share/atspre_define.hats”
#include “share/atspre_staload.hats”
staload
UN = “prelude/SATS/unsafe.sats”

dataview slseg_v (a:t@ype+, int(len) , addr(beg) , addr(end)) =
| {l:addr} slseg_v_nil(a,0,l,l) of ()
| {n:nat} {l_fst : agz} {l_nxt :addr} {l_end : addr}
slseg_v_cons (a,n+1,l_fst,l_end) of
( (a,ptr l_nxt) @ l_fst , slseg_v(a , n , l_nxt , l_end))

viewdef sl_v (a:t@ype+ , n:int , l:addr) = slseg_v (a,n,l,null)

fun
make_nil_list {a:t@ype+} () : (sl_v (a,0,null) | ptr null) =
(slseg_v_nil () | $UN.cast{ptr null}(0))

fun
node_alloc{a:t@ype+}{m:addr} () :
[l:agz] ((a, ptr m)? @ l, mfree_gc_v (l) | ptr (l))
= ptr_alloc<(a,ptr m)> ()

//extern
fun
print_lst {a:t@ype} {n:nat} {l:addr} ..
(pf : !sl_v (a,n,l) | p : ptr l, printfn : a - void) : void =
if ptr1_is_null(p) then let
prval slseg_v_nil () = pf
val () = println!(“done”) in pf := slseg_v_nil () end
else let
prval slseg_v_cons (pat , plst) = pf
val x = !p.0
val () = printfn (x)
val nxt_ptr = !p.1
val ()= println!(ptr1_is_null (nxt_ptr))

val () = println!("ok ")
val () = print_lst (plst | nxt_ptr, printfn)
in pf := slseg_v_cons (pat , plst) end

//extern
prfun
at2lst{a:t@ype} {n:nat} {l:agz} {m:addr} .<>.
(pat : (a,ptr m) @ l, plst : sl_v(a,n,m) ) : sl_v(a,n+1,l) =
slseg_v_cons(pat , plst)

val (pf_null_lst | null_p) = make_nil_list{int}()
val () = print_lst{int}{…}{…} (pf_null_lst | null_p , lam (x) => ())
val [l:addr] (pat,pfree | p) = node_alloc{int}()
val () = !p.1 := null_p
val () = !p.0 := 1908
prval pf_lst = at2lst{int}{…}{…}{…} (pat , pf_null_lst)

val () = print_lst{int}{…}{…} (pf_lst | p , lam (x) => () )

implement main0 () = {
(* val () = print(“hello\n”) )
(
val () = println!(“whatever”) )
(
val x = 123 )
(
val () = println!(“hello “,x,” hah”) )
(
val () = print_val (100) *)
val () = println!()

}

Even after you fix the problem I mentioned above,
you still need to fix print_lst a bit to make it work. See
the code given below.

As I mentioned earlier, it can be quite challenging to write code
this way. For learning ATS concepts, this is okay. For using ATS, IMO,
this is not a productive way.

fun{a:t@ype}
node_alloc{m:addr} () :
[l:agz] ((a, ptr m)? @ l, mfree_gc_v (l) | ptr (l))
= ptr_alloc<(a,ptr m)> ()

//extern
fun{a:t@ype}
print_lst{n:nat} {l:addr} ..
(pf : !sl_v (a,n,l) | p : ptr l, printfn : a - void) : void =
if ptr1_is_null(p) then let
prval slseg_v_nil () = pf
val () = println!(“done”) in pf := slseg_v_nil () end
else let
prval
slseg_v_cons{…}{…}
{l_fst}{l_nxt}{l_end} (pat , plst) = pf
prval pat = pat: (a, ptr(l_nxt)) @ l_fst
val x = !p.0
val () = printfn (x)
val nxt_ptr = !p.1
val ()= println!(ptr1_is_null (nxt_ptr))

val () = println!("ok ")
val () = print_lst (plst | nxt_ptr, printfn)
in pf := slseg_v_cons (pat , plst) endOn Friday, January 17, 2014 2:04:02 PM UTC-5, gmhwxi wrote:

node_alloc should be a template (instead of polymorphic) function:

fun{a:t@ype} node_alloc …

The same about print_lst.

On Friday, January 17, 2014 3:04:38 AM UTC-5, chota singh wrote:

Any idea why this code (given below) seg faults when run from console .

#include “share/atspre_define.hats”
#include “share/atspre_staload.hats”
staload
UN = “prelude/SATS/unsafe.sats”

dataview slseg_v (a:t@ype+, int(len) , addr(beg) , addr(end)) =
| {l:addr} slseg_v_nil(a,0,l,l) of ()
| {n:nat} {l_fst : agz} {l_nxt :addr} {l_end : addr}
slseg_v_cons (a,n+1,l_fst,l_end) of
( (a,ptr l_nxt) @ l_fst , slseg_v(a , n , l_nxt , l_end))

viewdef sl_v (a:t@ype+ , n:int , l:addr) = slseg_v (a,n,l,null)

fun
make_nil_list {a:t@ype+} () : (sl_v (a,0,null) | ptr null) =
(slseg_v_nil () | $UN.cast{ptr null}(0))

fun
node_alloc{a:t@ype+}{m:addr} () :
[l:agz] ((a, ptr m)? @ l, mfree_gc_v (l) | ptr (l))
= ptr_alloc<(a,ptr m)> ()

//extern
fun
print_lst {a:t@ype} {n:nat} {l:addr} ..
(pf : !sl_v (a,n,l) | p : ptr l, printfn : a - void) : void =
if ptr1_is_null(p) then let
prval slseg_v_nil () = pf
val () = println!(“done”) in pf := slseg_v_nil () end
else let
prval slseg_v_cons (pat , plst) = pf
val x = !p.0
val () = printfn (x)
val nxt_ptr = !p.1
val ()= println!(ptr1_is_null (nxt_ptr))

val () = println!("ok ")
val () = print_lst (plst | nxt_ptr, printfn)
in pf := slseg_v_cons (pat , plst) end

//extern
prfun
at2lst{a:t@ype} {n:nat} {l:agz} {m:addr} .<>.
(pat : (a,ptr m) @ l, plst : sl_v(a,n,m) ) : sl_v(a,n+1,l) =
slseg_v_cons(pat , plst)

val (pf_null_lst | null_p) = make_nil_list{int}()
val () = print_lst{int}{…}{…} (pf_null_lst | null_p , lam (x) => ())
val [l:addr] (pat,pfree | p) = node_alloc{int}()
val () = !p.1 := null_p
val () = !p.0 := 1908
prval pf_lst = at2lst{int}{…}{…}{…} (pat , pf_null_lst)

val () = print_lst{int}{…}{…} (pf_lst | p , lam (x) => () )

implement main0 () = {
(* val () = print(“hello\n”) )
(
val () = println!(“whatever”) )
(
val x = 123 )
(
val () = println!(“hello “,x,” hah”) )
(
val () = print_val (100) *)
val () = println!()

}

node_alloc should be a template (instead of polymorphic) function:

fun{a:t@ype} node_alloc …

The same about print_lst.On Friday, January 17, 2014 3:04:38 AM UTC-5, chota singh wrote:

Any idea why this code (given below) seg faults when run from console .

#include “share/atspre_define.hats”
#include “share/atspre_staload.hats”
staload
UN = “prelude/SATS/unsafe.sats”

dataview slseg_v (a:t@ype+, int(len) , addr(beg) , addr(end)) =
| {l:addr} slseg_v_nil(a,0,l,l) of ()
| {n:nat} {l_fst : agz} {l_nxt :addr} {l_end : addr}
slseg_v_cons (a,n+1,l_fst,l_end) of
( (a,ptr l_nxt) @ l_fst , slseg_v(a , n , l_nxt , l_end))

viewdef sl_v (a:t@ype+ , n:int , l:addr) = slseg_v (a,n,l,null)

fun
make_nil_list {a:t@ype+} () : (sl_v (a,0,null) | ptr null) =
(slseg_v_nil () | $UN.cast{ptr null}(0))

fun
node_alloc{a:t@ype+}{m:addr} () :
[l:agz] ((a, ptr m)? @ l, mfree_gc_v (l) | ptr (l))
= ptr_alloc<(a,ptr m)> ()

//extern
fun
print_lst {a:t@ype} {n:nat} {l:addr} ..
(pf : !sl_v (a,n,l) | p : ptr l, printfn : a - void) : void =
if ptr1_is_null(p) then let
prval slseg_v_nil () = pf
val () = println!(“done”) in pf := slseg_v_nil () end
else let
prval slseg_v_cons (pat , plst) = pf
val x = !p.0
val () = printfn (x)
val nxt_ptr = !p.1
val ()= println!(ptr1_is_null (nxt_ptr))

val () = println!("ok ")
val () = print_lst (plst | nxt_ptr, printfn)
in pf := slseg_v_cons (pat , plst) end

//extern
prfun
at2lst{a:t@ype} {n:nat} {l:agz} {m:addr} .<>.
(pat : (a,ptr m) @ l, plst : sl_v(a,n,m) ) : sl_v(a,n+1,l) =
slseg_v_cons(pat , plst)

val (pf_null_lst | null_p) = make_nil_list{int}()
val () = print_lst{int}{…}{…} (pf_null_lst | null_p , lam (x) => ())
val [l:addr] (pat,pfree | p) = node_alloc{int}()
val () = !p.1 := null_p
val () = !p.0 := 1908
prval pf_lst = at2lst{int}{…}{…}{…} (pat , pf_null_lst)

val () = print_lst{int}{…}{…} (pf_lst | p , lam (x) => () )

implement main0 () = {
(* val () = print(“hello\n”) )
(
val () = println!(“whatever”) )
(
val x = 123 )
(
val () = println!(“hello “,x,” hah”) )
(
val () = print_val (100) *)
val () = println!()

}