# How to trace chain of pointers with checking NULL?

**URL:** <https://discourse.ats-lang.org/t/how-to-trace-chain-of-pointers-with-checking-null/500>\
**Category:** Google Group\
**Created:** [June 25, 2015, 3:54pm UTC](https://discourse.ats-lang.org/t/how-to-trace-chain-of-pointers-with-checking-null/500 "2015-06-25T15:54:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [June 25, 2015, 3:54pm UTC](https://discourse.ats-lang.org/t/how-to-trace-chain-of-pointers-with-checking-null/500/1 "2015-06-25T15:54:17Z")

</div>

In this case, you need to use ‘topize’ to turn a function into a pointer:

val \_ =  
if b  
then let  
prval () = opt\_unsome (f)  
val res = f (get\_dentry\_p ())  
prval () = topize(f)  
in  
res  
end // then  
else let  
prval () = opt\_unnone (f)  
in 0 end // elseOn Thu, Jun 25, 2015 at 11:12 AM, Kiwamu Okabe [kiw...@debian.or.jp](mailto:kiw...@debian.or.jp) wrote:

> Hi all,
> 
> Now I’m trying to trace chain of pointers with checking NULL.
> 
> [https://github.com/jats-ug/practice-ats/blob/dd46010122b1705fd2d0aa9323172695924adcff/pointer\_chain/main.dats](https://github.com/jats-ug/practice-ats/blob/dd46010122b1705fd2d0aa9323172695924adcff/pointer_chain/main.dats)
> 
> [https://github.com/jats-ug/practice-ats/blob/dd46010122b1705fd2d0aa9323172695924adcff/pointer\_chain/dentry.c](https://github.com/jats-ug/practice-ats/blob/dd46010122b1705fd2d0aa9323172695924adcff/pointer_chain/dentry.c)
> 
> [https://github.com/jats-ug/practice-ats/blob/dd46010122b1705fd2d0aa9323172695924adcff/pointer\_chain/dentry.h](https://github.com/jats-ug/practice-ats/blob/dd46010122b1705fd2d0aa9323172695924adcff/pointer_chain/dentry.h)
> 
> But the code has following error.
> 
> /home/kiwamu/src/practice-ats/pointer\_chain/main.dats: 980(line=42,  
> offs=21) – 1056(line=43, offs=47): error(3): mismatch of static terms  
> (tyleq):  
> The actual term is: S2Efun(FUN; lin=0; eff=S2EFFset(1); npf=-1;  
> S2Ecst(dentry\_t\_p); S2Ecst(int))  
> The needed term is: S2Ecst(ptr\_type)
> 
> I think it’s caused by cast envless function.  
> I would like to check if the envless function is NULL.
> 
> How to trace chain of pointers with checking NULL?
> 
> ## Thank’s,
> 
> Kiwamu Okabe at METASEPI DESIGN
> 
> –  
> 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-user...@googlegroups.com](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [http://groups.google.com/group/ats-lang-users](http://groups.google.com/group/ats-lang-users).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/ats-lang-users/CAEvX6d%3DdFk%3DA9GYY7ZePy2gtGWMqnZgyaDmwbqnXYOndgeLDAQ%40mail.gmail.com](https://groups.google.com/d/msgid/ats-lang-users/CAEvX6d%3DdFk%3DA9GYY7ZePy2gtGWMqnZgyaDmwbqnXYOndgeLDAQ%40mail.gmail.com)  
> .

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [June 29, 2015, 3:34pm UTC](https://discourse.ats-lang.org/t/how-to-trace-chain-of-pointers-with-checking-null/500/2 "2015-06-29T15:34:55Z")

</div>

> > extern fun get\_dentry\_p (): [l:addr] (dentry\_t@l | ptr(l)) = “mac#”

I assume that ‘get\_dentry\_p’ is implemented in dentry.c. You need to  
make its C-interface available in ATS in order to call it in ATS.On Monday, June 29, 2015 at 5:54:03 AM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Sat, Jun 27, 2015 at 2:55 AM, Hongwei Xi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > > > s\_op= {l:addr}(super\_operations\_t@l | ptr(l))
> > 
> > This is likely a mistake. I think you need:
> > 
> > s\_op= [l:addr](super\_operations\_t@l | ptr(l))
> 
> Thank’s. It’s my mistake.
> 
> [https://github.com/jats-ug/practice-ats/blob/eadbc1861341b927bc8de80776067dc779135b9b/pointer\_chain2/main.dats](https://github.com/jats-ug/practice-ats/blob/eadbc1861341b927bc8de80776067dc779135b9b/pointer_chain2/main.dats)
> 
> However, my code has compile error at GCC side.
> 
> $ patscc main.dats dentry.c  
> main\_dats.c: In function ‘mainats\_void\_0’:  
> main\_dats.c:304:1: warning: implicit declaration of function  
> ‘get\_dentry\_p’ [-Wimplicit-function-declaration]  
> ATSINSmove(tmp1, get\_dentry\_p()) ;  
> ^  
> In file included from main\_dats.c:15:0:  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_instrset.h:260:35:  
> warning: assignment makes pointer from integer without a cast  
> #define ATSINSmove(tmp, val) (tmp = val)  
> ^  
> main\_dats.c:304:1: note: in expansion of macro ‘ATSINSmove’  
> ATSINSmove(tmp1, get\_dentry\_p()) ;  
> ^  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_instrset.h:203:29:  
> error: dereferencing pointer to incomplete type  
> #define ATSderef(pmv, hit) (_(hit_)pmv)  
> ^  
> –snip–
> 
> Doesn’t ATS compiler support this style?
> 
> ## Regards,
> 
> Kiwamu Okabe at METASEPI DESIGN

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [June 26, 2015, 3:26pm UTC](https://discourse.ats-lang.org/t/how-to-trace-chain-of-pointers-with-checking-null/500/3 "2015-06-26T15:26:44Z")

</div>

Why not the following interface:

fun{s.f:t0ype}  
tracestruct(…)On Fri, Jun 26, 2015 at 5:09 AM, Kiwamu Okabe [kiw...@debian.or.jp](mailto:kiw...@debian.or.jp) wrote:

> Hi Hongwei,
> 
> Finally, I get following code using template.  
> It may be useful for me to rewrite Linux kernel.
> 
> [https://github.com/jats-ug/practice-ats/blob/469f888704ed6d4e5b743fdf36ecbbb36dd54f02/pointer\_chain/main.dats#L25](https://github.com/jats-ug/practice-ats/blob/469f888704ed6d4e5b743fdf36ecbbb36dd54f02/pointer_chain/main.dats#L25)
> 
> fun{s,f:type}  
> tracestruct (from: s, trace: (s → f), function: &f? \>\> opt (f, b)):  
> #[b:bool] bool(b) = let  
> val f = trace from  
> in  
> if $UN.cast{ptr}(f) = the\_null\_ptr then let  
> prval () = opt\_none{f}(function)  
> in  
> false  
> end  
> else let  
> val () = function := f  
> prval () = opt\_some{f}(function)  
> in  
> true  
> end  
> end
> 
> ## Thank’s,
> 
> Kiwamu Okabe
> 
> –  
> 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-user...@googlegroups.com](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [http://groups.google.com/group/ats-lang-users](http://groups.google.com/group/ats-lang-users).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/ats-lang-users/CAEvX6dnA1C5V%3DE8FQm4uUQ4Z1oA0qcB0D%2ByOCp%3D6iGe-DG-y9A%40mail.gmail.com](https://groups.google.com/d/msgid/ats-lang-users/CAEvX6dnA1C5V%3DE8FQm4uUQ4Z1oA0qcB0D%2ByOCp%3D6iGe-DG-y9A%40mail.gmail.com)  
> .

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [June 29, 2015, 3:21pm UTC](https://discourse.ats-lang.org/t/how-to-trace-chain-of-pointers-with-checking-null/500/4 "2015-06-29T15:21:35Z")

</div>

If ‘s’ and ‘f’ are supposed to be function types, then you should  
use ‘s,f:type’. In any case, ‘type’ is treated as a subsort of ‘t@ype’.On Monday, June 29, 2015 at 5:48:32 AM UTC-4, Kiwamu Okabe wrote:

> On Sat, Jun 27, 2015 at 12:26 AM, Hongwei Xi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > Why not the following interface:
> > 
> > fun{s.f:t0ype}  
> > tracestruct(…)
> 
> Thank’s, I didn’t know that envless function is “t@ype”.  
> How to find out that? $showtype say the envless function is…
> 
> $ make  
> /home/kiwamu/src/ATS-Postiats/bin/patscc main.dats dentry.c  
> **SHOWTYPE[UP]**(/home/kiwamu/src/practice-ats/pointer\_chain/main.dats:  
> 1173(line=47, offs=33) – 1174(line=47, offs=34)): S2Efun(FUN; lin=0;  
> eff=S2EFFset(1); npf=-1; S2Ecst(dentry\_t\_p); S2Ecst(int)):  
> S2RTbas(S2RTBASimp(0; type))
> 
> Which string say that envless function is “t@ype”?
> 
> Also, why doesn’t occur error on type checking while envless function is  
> used as “type”? My code with envless function as “type” has no error on  
> both compile time and run tine.
> 
> ## Regards,
> 
> Kiwamu Okabe at METASEPI DESIGN

---

<div class="post-metadata">

**Author:** ![gmhwxi](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gmhwxi](https://discourse.ats-lang.org/u/gmhwxi)\
**Post date:** [June 26, 2015, 5:55pm UTC](https://discourse.ats-lang.org/t/how-to-trace-chain-of-pointers-with-checking-null/500/5 "2015-06-26T17:55:43Z")

</div>

> > s\_op= {l:addr}(super\_operations\_t@l | ptr(l))

This is likely a mistake. I think you need:

s\_op= [l:addr](super\_operations\_t@l | ptr(l))On Fri, Jun 26, 2015 at 8:49 AM, Kiwamu Okabe [kiw...@debian.or.jp](mailto:kiw...@debian.or.jp) wrote:

> Hi all,
> 
> Can’t ATS compiler allow following style?
> 
> [https://github.com/jats-ug/practice-ats/blob/b46f993a94874e353876f2b76a4a20cf9548b9fe/pointer\_chain2/main.dats](https://github.com/jats-ug/practice-ats/blob/b46f993a94874e353876f2b76a4a20cf9548b9fe/pointer_chain2/main.dats)
> 
> typedef f\_statfs = ptr → int
> 
> vtypedef super\_operations\_t = $extype\_struct"struct super\_operations" of {  
> statfs= f\_statfs  
> }  
> vtypedef super\_block\_t = $extype\_struct"struct super\_block" of {  
> s\_op= {l:addr}(super\_operations\_t@l | ptr(l))  
> }  
> vtypedef dentry\_t = $extype\_struct"struct dentry" of {  
> d\_sb= {l:addr}(super\_block\_t@l | ptr(l))  
> }
> 
> extern fun get\_dentry\_p (): [l:addr] (dentry\_t@l | ptr(l)) = “mac#”  
> extern fun get\_dentry\_p\_null (): [l:addr] (dentry\_t@l | ptr(l)) = “mac#”
> 
> implement main0 () = {  
> val (pfdentry | dentry) = get\_dentry\_p ()  
> val (pfsb | sb) = dentry-\>d\_sb  
> val (pfsop | sop) = sb-\>s\_op  
> val \_ = sop-\>statfs dentry  
> val \_ = $UN.castvwtp0{ptr}((pfsop | sop))  
> val \_ = $UN.castvwtp0{ptr}((pfsb | sb))  
> val \_ = $UN.castvwtp0{ptr}((pfdentry | dentry))  
> }
> 
> ## Thank’s,
> 
> Kiwamu Okabe at METASEPI DESIGN
> 
> –  
> 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-user...@googlegroups.com](mailto:ats-lang-user...@googlegroups.com).  
> To post to this group, send email to [ats-lan...@googlegroups.com](mailto:ats-lan...@googlegroups.com).  
> Visit this group at [http://groups.google.com/group/ats-lang-users](http://groups.google.com/group/ats-lang-users).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/ats-lang-users/CAEvX6dkFkEJq0Gvwdj67W7KVtsh1Mqp-\_bAnYibf0Zm7oFVcyg%40mail.gmail.com](https://groups.google.com/d/msgid/ats-lang-users/CAEvX6dkFkEJq0Gvwdj67W7KVtsh1Mqp-_bAnYibf0Zm7oFVcyg%40mail.gmail.com)  
> .
