# Multiple {DATS,SATS} directory

**URL:** <https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574>\
**Category:** Google Group\
**Created:** [September 12, 2014, 5:32pm UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574 "2014-09-12T17:32:05Z")\
**Posts on this page:** 18\
**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:** [September 12, 2014, 5:32pm UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/1 "2014-09-12T17:32:05Z")

</div>

Hi all,

May I use multiple {DATS,SATS} directory?

Now I’m trying to create an Arduino platform for ATS programming.

[https://github.com/fpiot/arduino-mega2560-ats](https://github.com/fpiot/arduino-mega2560-ats)

It has source code tree as following:

arduino-mega2560-ats  
|-- Makefile  
|-- Makefile.common  
|-- \_library  
| |-- DATS  
| `-- SATS |-- blink_ats | |-- DATS | |`– main.dats  
| `-- Makefile |-- lcd_ats | |-- DATS | | |-- lcd.dats | |`– main.dats  
| |-- Makefile  
| `-- SATS | |-- arduino.sats |`– lcd.sats  
`-- serial_ats |-- DATS | |-- hardware_serial.dats |`– main.dats  
|-- Makefile  
`-- SATS |-- arduino.sats`– hardware\_serial.sats

The {blink,lcd,serial}\_ats/ directories are projects of Arduino.  
However, they have own {DATS,SATS} directories.

How to put {arduino,hardware\_serial,lcd}.sats into  
arduino-mega2560-ats/\_library/SATS/,  
and {hardware\_serial,lcd}.dats into arduino-mega2560-ats/\_library/DATS/?  
Also, I would like to main.dats {arduino,hardware\_serial,lcd}\_ats/DATS/.

Thank’s,  
Kiwamu Okabe at METASEPI DESIGN

Want excitement?  
Manually upgrade your production database.  
When you want reliability, choose Perforce  
Perforce version control. Predictably reliable.  
[http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk](http://pubads.g.doubleclick.net/gampad/clk?id=157508191&iu=/4140/ostg.clktrk)  
ats-lang-users mailing list  
[ats-lan...@lists.sourceforge.net](mailto:ats-lan...@lists.sourceforge.net)  
[https://lists.sourceforge.net/lists/listinfo/ats-lang-users](https://lists.sourceforge.net/lists/listinfo/ats-lang-users)

---

<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:** [September 12, 2014, 7:05pm UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/2 "2014-09-12T19:05:56Z")

</div>

You may also need a ‘CATS’ directory.

The macdef code in your DATS-files should probably all be moved into  
SATS-files.

The functions in DATS-files can be written as templates. So you don’t need  
to generate libarduino.a.On Friday, September 12, 2014 2:53:59 PM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Sat, Sep 13, 2014 at 3:41 AM, gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > #define LIBARDUINO\_targetloc “/path/to/\_library”
> > 
> > staload “{$LIBARDUINO}/SATS/arduino.sats”
> 
> Ah, I know this now.  
> I think DATS files are compiled into “libarduino.a” file.
> 
> BTW, he other question: when we should compile SATS file?
> 
> ## Thank’s,
> 
> 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:** [September 13, 2014, 6:03am UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/3 "2014-09-13T06:03:11Z")

</div>

The C code in lcd.dats can be moved into CATS/lcd.cats.

BTW, is template really good for embedded application?

> Arduino Uno board only has 32kB ROM and 2kB RAM.  
> And lcd\_open function is large function.  
> I think space-efficient is very important in embedded application such  
> like Linux kernel.

You can control the use of templates.

Say you don’t want to inline lcd\_open. Then you can always do

fun lcd\_open\_ () = lcd\_open\<\> ()

And then use lcd\_open\_ instead.

Using templates can help you adjust/adapt your code must more easily.On Saturday, September 13, 2014 1:44:09 AM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Sat, Sep 13, 2014 at 1:38 PM, gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > You need to add the following line
> > 
> > staload \_ = “{$LIBARDUINO}/DATS/lcd.dats”
> 
> Thank’s! But… The lcd.dats file can’t include C language  
> function?
> 
> $ make  
> avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> -I…/…//\_dummy -c -o wiring\_digital.o wiring\_digital.c  
> patsopt -o DATS/main\_dats.c -d DATS/main.dats  
> avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> -I…/…//\_dummy -c -o DATS/main\_dats.o DATS/main\_dats.c  
> In file included from DATS/main\_dats.c:41:0:  
> /home/kiwamu/src/ATS-Postiats/prelude/CATS/filebas.cats: In function  
> ‘atspre\_fileref\_open\_exn’:  
> /home/kiwamu/src/ATS-Postiats/prelude/CATS/filebas.cats:177:8:  
> warning: assignment makes pointer from integer without a cast [enabled  
> by default]  
> filr = fopen((char\*)path, (char\*)fm) ;  
> ^  
> avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> -I…/…//\_dummy -Wl,-Map,main.map -o main.elf wiring\_digital.o  
> DATS/main\_dats.o  
> DATS/main\_dats.o: In function `main’:  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x4):
> 
> undefined reference to `PMVerr’  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x12):
> 
> undefined reference to `PMVerr’  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x20):
> 
> undefined reference to `PMVerr’  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x2e):
> 
> undefined reference to `PMVerr’  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x48):
> 
> undefined reference to `PMVerr’  
> DATS/main\_dats.o:/home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x5a):
> 
> more undefined references to `PMVerr' follow DATS/main_dats.o: In function `main’:  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x82):
> 
> undefined reference to `atspre\_uint8\_bit\_or’  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x8e):
> 
> undefined reference to `atspre\_uint8\_bit\_or’  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x98):
> 
> undefined reference to `PMVerr’  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0xa4):
> 
> undefined reference to `PMVerr’  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0xec):
> 
> undefined reference to `atspre\_uint8\_bit\_and’
> 
> > Templates in lcd.dats are “copied” into the file where they are used.  
> > So they do not need to be compiled. Just like .h files do not need to be  
> > compiled.
> 
> BTW, is template really good for embedded application?  
> Arduino Uno board only has 32kB ROM and 2kB RAM.  
> And lcd\_open function is large function.  
> I think space-efficient is very important in embedded application such  
> like Linux kernel.
> 
> ## Thank’s,
> 
> 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:** [September 13, 2014, 7:23am UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/4 "2014-09-13T07:23:40Z")

</div>

val lcd = $UN.castvwtp0 (addr@\_global\_lcd\_struct)

When using a cast function, please provide the type of the returned value:

val lcd = $UN.castvwtp0{…}(addr@\_global\_lcd\_struct)

What is …?On Saturday, September 13, 2014 2:58:50 AM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Sat, Sep 13, 2014 at 3:03 PM, gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > The C code in lcd.dats can be moved into CATS/lcd.cats.
> 
> Umm…
> 
> $ make  
> avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> -I…/…//\_dummy -c -o wiring\_digital.o wiring\_digital.c  
> patsopt -o DATS/main\_dats.c -d DATS/main.dats  
> avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> -I…/…//\_dummy -c -o DATS/main\_dats.o DATS/main\_dats.c  
> In file included from DATS/main\_dats.c:41:0:  
> /home/kiwamu/src/ATS-Postiats/prelude/CATS/filebas.cats: In function  
> ‘atspre\_fileref\_open\_exn’:  
> /home/kiwamu/src/ATS-Postiats/prelude/CATS/filebas.cats:177:8:  
> warning: assignment makes pointer from integer without a cast [enabled  
> by default]  
> filr = fopen((char\*)path, (char\*)fm) ;  
> ^  
> avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> -I…/…//\_dummy -Wl,-Map,main.map -o main.elf wiring\_digital.o  
> DATS/main\_dats.o  
> DATS/main\_dats.o: In function `main’:  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x4):
> 
> undefined reference to `PMVerr’  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x12):
> 
> undefined reference to `PMVerr’  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x20):
> 
> undefined reference to `PMVerr’  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x2e):
> 
> undefined reference to `PMVerr’  
> /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x48):
> 
> undefined reference to `PMVerr’  
> DATS/main\_dats.o:/home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x5a):
> 
> more undefined references to `PMVerr’ follow  
> collect2: error: ld returned 1 exit status  
> …/…[//Makefile.common:30](https://Makefile.common:30): recipe for target ‘main.elf’ failed  
> make: \*\*\* [main.elf] Error 1  
> $ grep -r PMVerr DATS/main\_dats.c|head -5  
> ATSINSstore(ATSSELfltrec(ATSderef(ATSPMVcastfn(LCD\_takeout\_struct,  
> atstkind\_type(atstype\_ptrk), ATSPMVrefarg0(ATSPMVcastfn(castvwtp0,  
> atstype\_boxed,  
> PMVerr(“/home/kiwamu/src/arduino-mega2560-ats/DATS/lcd.dats:  
> 1942(line=55, offs=30) – 1965(line=55, offs=53)”)))),  
> postiats\_tyrec\_1), postiats\_tyrec\_1, atslab\_\_rs\_pin),  
> ATSPMVcastfn(cast, atstkind\_t0ype(atstype\_uint8), arg0)) ;  
> ATSINSstore(ATSSELfltrec(ATSderef(ATSPMVcastfn(LCD\_takeout\_struct,  
> atstkind\_type(atstype\_ptrk), ATSPMVrefarg0(ATSPMVcastfn(castvwtp0,  
> atstype\_boxed,  
> PMVerr(“/home/kiwamu/src/arduino-mega2560-ats/DATS/lcd.dats:  
> 1942(line=55, offs=30) – 1965(line=55, offs=53)”)))),  
> postiats\_tyrec\_1), postiats\_tyrec\_1, atslab\_\_rw\_pin),  
> ATSPMVcastfn(cast, atstkind\_t0ype(atstype\_uint8), arg1)) ;  
> ATSINSstore(ATSSELfltrec(ATSderef(ATSPMVcastfn(LCD\_takeout\_struct,  
> atstkind\_type(atstype\_ptrk), ATSPMVrefarg0(ATSPMVcastfn(castvwtp0,  
> atstype\_boxed,  
> PMVerr(“/home/kiwamu/src/arduino-mega2560-ats/DATS/lcd.dats:  
> 1942(line=55, offs=30) – 1965(line=55, offs=53)”)))),  
> postiats\_tyrec\_1), postiats\_tyrec\_1, atslab\_\_enable\_pin),  
> ATSPMVcastfn(cast, atstkind\_t0ype(atstype\_uint8), arg2)) ;  
> ATSINSstore(ATSSELfltrec(ATSderef(ATSPMVcastfn(LCD\_takeout\_struct,  
> atstkind\_type(atstype\_ptrk), ATSPMVrefarg0(ATSPMVcastfn(castvwtp0,  
> atstype\_boxed,  
> PMVerr(“/home/kiwamu/src/arduino-mega2560-ats/DATS/lcd.dats:  
> 1942(line=55, offs=30) – 1965(line=55, offs=53)”)))),  
> postiats\_tyrec\_1), postiats\_tyrec\_1, atslab\_\_data\_pins), tmp356) ;  
> ATSINSmove\_void(tmp357,  
> pinMode(ATSSELfltrec(ATSderef(ATSPMVcastfn(LCD\_takeout\_struct,  
> atstkind\_type(atstype\_ptrk), ATSPMVrefarg0(ATSPMVcastfn(castvwtp0,  
> atstype\_boxed,  
> PMVerr(“/home/kiwamu/src/arduino-mega2560-ats/DATS/lcd.dats:  
> 1942(line=55, offs=30) – 1965(line=55, offs=53)”)))),  
> postiats\_tyrec\_1), postiats\_tyrec\_1, atslab\_\_rs\_pin),  
> ATSextval(OUTPUT))) ;
> 
> What is PMVerr?
> 
> ## 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:** [September 13, 2014, 6:52pm UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/5 "2014-09-13T18:52:35Z")

</div>

> > However, LCD\_struct becomes public API…

I see what you meant here. One possibility is to  
move LCD\_struct to lcd.cats, and then add the following  
line:

abst@ype LCD\_struct = $extype"LCD\_struct"

In lcd.dats, you can have

typedef LCD\_struct\_ = $extype\_struct"LCD\_struct" of  
{  
…  
}

Then use $UN.cast to go between LCD\_struct and LCD\_struct\_.On Saturday, September 13, 2014 2:18:13 PM UTC-4, gmhwxi wrote:

> It is good that it worked.
> 
> > > However, LCD\_struct becomes public API…
> 
> You could do something like this:
> 
> 1. Introduce lcd\_initize (!LCD0 \>\> LCD1, …)
> 
> where LCD0 means uninitialized LCD and LCD1 means initialized.
> 
> implement  
> {}(_tmp_)  
> lcd\_open (…) = let  
> val lcd = lcd\_get () in lcd\_initize (lcd, …)  
> end // end of [lcd\_open]
> 
> Both LCD0 and LCD1 are abstract.
> 
> In low-level programming, it is often a good practice to separate  
> resource-acquisition  
> (lcd\_get) and resource-initialization (lcd\_initize).
> 
> If you like, you can now allocate LCD on the call stack of main; or you  
> can have a lock  
> on the use of lcd\_get to avoid potential conflicts; or …
> 
> On Saturday, September 13, 2014 10:55:40 AM UTC-4, Kiwamu Okabe wrote:
> 
> > Hi Hongwei,
> > 
> > On Sat, Sep 13, 2014 at 4:43 PM, gmhwxi [gmh...@gmail.com](mailto:gmh...@gmail.com) wrote:
> > 
> > > I think it makes sense to introduce a template
> > > 
> > > fun{} lcd\_get (): LCD // or use LCD\_unintized
> > > 
> > > Use lcd\_get to implement lcd\_open. You can implement  
> > > lcd\_get in main.dats if you like.
> > 
> > Thank’s! It’s fixed and runnable.
> > 
> > [Use template and runnable · fpiot/arduino-ats@1ed48ae · GitHub](https://github.com/fpiot/arduino-mega2560-ats/commit/1ed48ae653a9f4430604a00c4bfb7f267f0e6070#diff-d286c2b60b4101c3f21181e0f66793e6R15)
> > 
> > ## However, LCD\_struct becomes public API…
> > 
> > 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:** [September 13, 2014, 6:33am UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/6 "2014-09-13T06:33:43Z")

</div>

> Now, lcd.{sats,dats} user is only one, as main.dats.  
> However, theother.dats may staload lcd.{sats,dats} in the future?  
> In the case, I think there are two lcd\_open instances.

Only if theother.dats uses the lcd\_open template. If it uses lcd\_open\_,  
then there is only one instance, which is in lcd.dats:

implement lcd\_open\_ () = lcd\_open\<\>On Saturday, September 13, 2014 2:27:22 AM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Sat, Sep 13, 2014 at 3:19 PM, gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > Maybe lcd\_open is not a good example to illustrate this point.
> 
> Umm… I think it’s a good example for embedded system.  
> In real programming, initialize function sometimes has many logics.  
> And also it should be library such like kernel module.
> 
> > lcd\_open\_ is also declared in lcd.sats.
> > 
> > If lcd\_open\_ suffices, then one just calls lcd\_open\_.
> 
> Now, lcd.{sats,dats} user is only one, as main.dats.  
> However, theother.dats may staload lcd.{sats,dats} in the future?  
> In the case, I think there are two lcd\_open instances.
> 
> > The lcd\_open template allows you to quickly build lcd\_open2\_ and  
> > lcd\_open3\_,  
> > which are similar to lcd\_open\_ but with some small changes.
> 
> Of course, the template is good, if there are the variations.
> 
> ## Thank’s,
> 
> 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:** [September 13, 2014, 4:38am UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/7 "2014-09-13T04:38:21Z")

</div>

You need to add the following line

staload \_ = “{$LIBARDUINO}/DATS/lcd.dats”

Templates in lcd.dats are “copied” into the file where they are used.  
So they do not need to be compiled. Just like .h files do not need to be  
compiled.On Saturday, September 13, 2014 12:11:34 AM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Sat, Sep 13, 2014 at 7:57 AM, gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > I need to take a closer look at your code. In general, short functions  
> > should almost always be templates, allowing aggressive inlining. Also,  
> > templates can give you many forms of programming freedom.
> 
> Umm… Not make sense.  
> I tried to use it.
> 
> [GitHub - fpiot/arduino-ats at feature/use-template](https://github.com/fpiot/arduino-mega2560-ats/tree/feature/use-template)
> 
> However, following error occurs.
> 
> $ cd arduino-mega2560-ats/demo/lcd\_ats  
> $ grep include DATS/main.dats  
> #include “share/atspre\_define.hats”  
> #include “share/atspre\_staload.hats”  
> $ make  
> avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> -I…/…//\_dummy -c -o wiring\_digital.o wiring\_digital.c  
> patsopt -o DATS/main\_dats.c -d DATS/main.dats  
> avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> -I…/…//\_dummy -c -o DATS/main\_dats.o DATS/main\_dats.c  
> In file included from DATS/main\_dats.c:41:0:  
> /home/kiwamu/src/ATS-Postiats/prelude/CATS/filebas.cats: In function  
> ‘atspre\_fileref\_open\_exn’:  
> /home/kiwamu/src/ATS-Postiats/prelude/CATS/filebas.cats:177:8:  
> warning: assignment makes pointer from integer without a cast [enabled  
> by default]  
> filr = fopen((char\*)path, (char\*)fm) ;  
> ^  
> In file included from DATS/main\_dats.c:15:0:  
> DATS/main\_dats.c: In function ‘mainats\_void\_0’:  
> DATS/main\_dats.c:343:19: error: ‘PMVtmpltcstmat’ undeclared (first use  
> in this function)  
> ATSINSmove(tmp25, PMVtmpltcstmat0(ATSPMVi0nt(8),  
> ATSPMVi0nt(13), ATSPMVi0nt(9), ATSPMVi0nt(4), ATSPMVi0nt(5),  
> ATSPMVi0nt(6), ATSPMVi0nt(7))) ;  
> ^
> 
> I’m confusing about the compiling process of template.  
> The “demo/lcd\_ats/DATS/main.dats” file calls template function lcd\_open{}.  
> The “SATS/lcd.sats” file has the declaration of lcd\_open{}.  
> The “DATS/lcd.dats” file has the definition of lcd\_open{}.
> 
> Why no need to compile lcs.dats file?
> 
> ## Thank’s,
> 
> 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:** [September 12, 2014, 6:41pm UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/8 "2014-09-12T18:41:14Z")

</div>

You can just put arduino.sats inside \_library/SATS.

To access it from blink\_ats/DATS, you can just do:

staload “./…/…/\_library/SATS/arduino.sats” // the first dot means to  
start from the current directory

Or you can do

#define LIBARDUINO\_targetloc “/path/to/\_library”

staload "{$LIBARDUINO}/SATS/arduino.sats"On Friday, September 12, 2014 1:32:05 PM UTC-4, gmhwxi wrote:

> ---------- Forwarded message ----------
> 
> Hi all,
> 
> May I use multiple {DATS,SATS} directory?
> 
> Now I’m trying to create an Arduino platform for ATS programming.
> 
> [GitHub - fpiot/arduino-ats: ATS programing on Arduino](https://github.com/fpiot/arduino-mega2560-ats)
> 
> It has source code tree as following:
> 
> arduino-mega2560-ats  
> |-- Makefile  
> |-- Makefile.common  
> |-- \_library  
> | |-- DATS  
> | `-- SATS |-- blink_ats | |-- DATS | | `– main.dats  
> | `-- Makefile |-- lcd_ats | |-- DATS | | |-- lcd.dats | | `– main.dats  
> | |-- Makefile  
> | `-- SATS | |-- arduino.sats | `– lcd.sats  
> `-- serial_ats |-- DATS | |-- hardware_serial.dats | `– main.dats  
> |-- Makefile  
> `-- SATS |-- arduino.sats `– hardware\_serial.sats
> 
> The {blink,lcd,serial}\_ats/ directories are projects of Arduino.  
> However, they have own {DATS,SATS} directories.
> 
> How to put {arduino,hardware\_serial,lcd}.sats into  
> arduino-mega2560-ats/\_library/SATS/,  
> and {hardware\_serial,lcd}.dats into arduino-mega2560-ats/\_library/DATS/?  
> Also, I would like to main.dats {arduino,hardware\_serial,lcd}\_ats/DATS/.
> 
> ## Thank’s,
> 
> 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:** [September 13, 2014, 6:19am UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/9 "2014-09-13T06:19:48Z")

</div>

Maybe lcd\_open is not a good example to illustrate this point.

lcd\_open\_ is also declared in lcd.sats.

If lcd\_open\_ suffices, then one just calls lcd\_open\_.

The lcd\_open template allows you to quickly build lcd\_open2\_ and lcd\_open3\_,  
which are similar to lcd\_open\_ but with some small changes.On Saturday, September 13, 2014 2:09:47 AM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Sat, Sep 13, 2014 at 3:03 PM, gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > You can control the use of templates.
> > 
> > Say you don’t want to inline lcd\_open. Then you can always do
> > 
> > fun lcd\_open\_ () = lcd\_open\<\> ()
> > 
> > And then use lcd\_open\_ instead.
> > 
> > Using templates can help you adjust/adapt your code must more easily.
> 
> Where should I write the “lcd\_open\_” definition?
> 
> Please imagine a Linux kernel module.  
> The module provides “lcd\_open” as a template.  
> The other modules depend on the “lcd\_open”.  
> How many “lcd\_open” instance in the kernel, if they use “lcd\_open\_” style?
> 
> ## Thank’s,
> 
> 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:** [September 12, 2014, 6:48pm UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/10 "2014-09-12T18:48:14Z")

</div>

To clean things up, I suggest the following structure:

arduino-mega2560-ats  
|-- Makefile  
|-- Makefile.common  
|-- SATS  
|-- ardunio.sats  
|–blink  
|–lcd  
|–serial  
|-- DATS  
|–blink  
|–lcd  
|–serial  
|— TEST/DEMO

---

<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:** [September 13, 2014, 6:26am UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/11 "2014-09-13T06:26:32Z")

</div>

Yes, a single and possibly BIG file of C code, which can enable gcc to do  
very aggressive optimizations.

By the way, the generated object code may not be a lot bigger. You can do  
some experiments to compare.On Saturday, September 13, 2014 2:20:11 AM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Sat, Sep 13, 2014 at 3:09 PM, Kiwamu Okabe \<kiw…@debian.or.jp \<javascript:\>\> wrote:
> 
> > Please imagine a Linux kernel module.  
> > The module provides “lcd\_open” as a template.  
> > The other modules depend on the “lcd\_open”.  
> > How many “lcd\_open” instance in the kernel, if they use “lcd\_open\_”  
> > style?
> 
> Or, should I write all DATS files other than main.dats in template style?  
> However, I think gcc will get single and BIG C language source file in  
> this case.
> 
> ## Thank’s,
> 
> 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:** [September 16, 2014, 2:16am UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/12 "2014-09-16T02:16:37Z")

</div>

I think this requires further investigation. I don’t see why using templates  
in your example would cause a large increase in the size of the generated  
object code. It would be very useful to know what code really got  
duplicated.

> > And it can’t control safe API…

I think it can. You can just move the following code into lcd.dats:

var \_global\_lcd\_struct: LCD\_struct  
implement lcd\_get\<\> () = $UN.castvwtp0 (addr@\_global\_lcd\_struct)On Monday, September 15, 2014 9:35:31 PM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Fri, 12 Sep 2014 23:26:32 -0700 (PDT) gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > By the way, the generated object code may not be a lot bigger. You can  
> > do  
> > some experiments to compare.
> 
> I compared them that are compiled with -Os option.
> 
> ## Not use template
> 
> $ size main.elf  
> text data bss dec hex filename  
> 3442 448 16 3906 f42 main.elf
> 
> ## Use template
> 
> $ size main.elf  
> text data bss dec hex filename  
> 6044 448 20 6512 1970 main.elf
> 
> Umm…  
> I think template system is not good for tiny embedded system.  
> And it can’t control safe API…
> 
> ## Thank’s,
> 
> Kiwamu Okabe \<kiw…@debian.or.jp \<javascript:\>\>

---

<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:** [September 12, 2014, 10:57pm UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/13 "2014-09-12T22:57:04Z")

</div>

> Hi Hongwei,

> > The macdef code in your DATS-files should probably all be moved into  
> > SATS-files.
> 
> Following?
> 
> $ grep macdef serial\_ats/DATS/hardware\_serial.dats | head -5  
> macdef hserial = $extval(cPtr0(hardware\_serial), “(&Serial)”)  
> macdef F\_CPU = $extval(ulint, “F\_CPU”)  
> macdef rx\_buffer = $extval(cPtr0(ring\_buffer), “&rx\_buffer”)  
> macdef tx\_buffer = $extval(cPtr0(ring\_buffer), “&tx\_buffer”)  
> macdef ADDR\_UBRRH = $extval(ptr, “&ADDR\_UBRRH”)
> 
> But they are only for using in hardware\_serial.dats, and not for  
> main.dats…  
> I think they are private and not public.

I see, Then they should stay in the DATS file.

> > The functions in DATS-files can be written as templates. So you don’t  
> > need  
> > to generate libarduino.a.
> 
> I should change all functions into template function?  
> Now I compile DATS files as following.
> 
> $ pwd  
> /home/kiwamu/src/arduino-mega2560-ats/lcd\_ats  
> $ make  
> $ find . -name “\*.o”  
> ./wiring\_analog.o  
> ./wiring\_digital.o  
> ./DATS/lcd\_dats.o  
> ./DATS/main\_dats.o
> 
> In the case, libarduino.a is a bad idea?

I need to take a closer look at your code. In general, short functions  
should almost always be templates, allowing aggressive inlining. Also,  
templates can give you many forms of programming freedom.

---

<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:** [September 16, 2014, 3:54am UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/14 "2014-09-16T03:54:56Z")

</div>

Is your new code in this directory:

> **[GitHub - fpiot/arduino-ats: ATS programing on Arduino](https://github.com/fpiot/arduino-ats/)**
>
> ATS programing on Arduino. Contribute to fpiot/arduino-ats development by creating an account on GitHub.

I would like to give it a try.On Monday, September 15, 2014 11:37:11 PM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Mon, 15 Sep 2014 19:16:37 -0700 (PDT) gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > I think this requires further investigation. I don’t see why using  
> > templates  
> > in your example would cause a large increase in the size of the  
> > generated  
> > object code. It would be very useful to know what code really got  
> > duplicated.
> 
> O.K.  
> I try more.
> 
> > > > And it can’t control safe API…
> > 
> > I think it can. You can just move the following code into lcd.dats:
> > 
> > var \_global\_lcd\_struct: LCD\_struct  
> > implement lcd\_get\<\> () = $UN.castvwtp0 (addr@\_global\_lcd\_struct)
> 
> I think I can’t that.
> 
> $ grep github …/…/.git/config  
> url = [git@github.com](mailto:git@github.com):fpiot/arduino-ats.git  
> $ git branch
> 
> - feature/use-template  
> master  
> $ git diff  
> diff --git a/DATS/lcd.dats b/DATS/lcd.dats  
> index ed3ee3b…f279be2 100644  
> — a/DATS/lcd.dats  
> +++ b/DATS/lcd.dats  
> @@ -95,6 +95,13 @@ in  
> lcd  
> end
> 
> +local
> 
> - var \_global\_lcd\_struct: LCD\_struct
> - implement lcd\_get\<\> () = $UN.castvwtp0 (addr@\_global\_lcd\_struct)  
> +in
> - implement lcd\_open\_ (rs, rw, enable, d0, d1, d2, d3) = lcd\_open (rs,  
> rw, enable, d0, d1, d2, d3)  
> +end
> - 
> 
> implement{} lcd\_close (lcd) = {  
> val () = $UN.castvwtp0(lcd) (\* Consume lcd \*)  
> }  
> diff --git a/demo/lcd\_ats/DATS/main.dats b/demo/lcd\_ats/DATS/main.dats  
> index 1ba72a6…b90361e 100644  
> — a/demo/lcd\_ats/DATS/main.dats  
> +++ b/demo/lcd\_ats/DATS/main.dats  
> @@ -12,13 +12,6 @@ staload UN = “prelude/SATS/unsafe.sats”
> 
> val g\_string = " ATS is a statically typed programming  
> language that unifies implementation with formal specification. It is  
> equipped with a h  
> ighly expressive type system rooted in the framework Applied Type System,  
> which gives the language its name. In particular, both dependent types and  
> linear typ  
> es are available in ATS. "
> 
> -local
> 
> - var \_global\_lcd\_struct: LCD\_struct
> - implement lcd\_get\<\> () = $UN.castvwtp0 (addr@\_global\_lcd\_struct)  
> -in
> - implement lcd\_open\_ (rs, rw, enable, d0, d1, d2, d3) = lcd\_open (rs,  
> rw, enable, d0, d1, d2, d3)  
> -end
> - 
> 
> implement main0 () = {  
> fun loop {n:int}{i:nat | i \< n}  
> (lcd: !LCD, str: string (n), pos: size\_t (i)): void = {  
> $ make  
> avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> -I…/…//\_dummy -c -o wiring\_digital.o wiring\_digital.c  
> patsopt -o DATS/main\_dats.c -d DATS/main.dats  
> avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> -I…/…//\_dummy -c -o DATS/main\_dats.o DATS/main\_dats.c  
> In file included from DATS/main\_dats.c:41:0:  
> /home/kiwamu/src/ATS-Postiats/prelude/CATS/filebas.cats: In function  
> ‘atspre\_fileref\_open\_exn’:  
> /home/kiwamu/src/ATS-Postiats/prelude/CATS/filebas.cats:177:8: warning:  
> assignment makes pointer from integer without a cast [enabled by default]  
> filr = fopen((char\*)path, (char\*)fm) ;  
> ^  
> avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> -I…/…//\_dummy -Wl,-Map,main.map -o main.elf wiring\_digital.o  
> DATS/main\_dats.o  
> DATS/main\_dats.o: In function `main': /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats_ccomp_runtime.c:(.text+0x20): undefined reference to `_057\_home\_057\_kiwamu\_057\_src\_057\_arduino\_055\_ats\_057\_SATS\_057\_lcd\_056\_sats\_\_lcd\_open_’
> 
> collect2: error: ld returned 1 exit status  
> …/…[//Makefile.common:30](https://Makefile.common:30): recipe for target ‘main.elf’ failed  
> make: \*\*\* [main.elf] Error 1
> 
> ## Thank’s,
> 
> Kiwamu Okabe \<kiw…@debian.or.jp \<javascript:\>\>

---

<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:** [September 13, 2014, 6:41am UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/15 "2014-09-13T06:41:23Z")

</div>

By providing a template-based library, you can give the freedom to the user:  
he or she can decide whether A or B or somewhere in between is good for his  
or her project.

When I program, I often do not use a library directly. I often build a thin  
layer of  
library code for my own project. In this thin layer, I have functions like  
lcd\_open\_,  
lcd\_open2\_, lcd\_open3\_ (if needed).On Saturday, September 13, 2014 2:34:22 AM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Sat, Sep 13, 2014 at 3:26 PM, gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > Yes, a single and possibly BIG file of C code, which can enable gcc to  
> > do  
> > very aggressive optimizations.
> 
> Yeah.  
> However, we will lose binary modularity, such like kernel module…
> 
> > By the way, the generated object code may not be a lot bigger. You can  
> > do  
> > some experiments to compare.
> 
> My problem is that we should choose only following:
> 
> A. BIG single C code style, using fully template  
> B. Many small C code style, using partly template
> 
> A style has no binary modularity. It’s a big rock.  
> B style has binary modularity. It can load by insmod or dlopen.
> 
> ## Thank’s,
> 
> 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:** [September 12, 2014, 7:27pm UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/16 "2014-09-12T19:27:55Z")

</div>

> > BTW, the other question: when we should compile SATS file?

For now, a SATS-file does not need to be compiled (This is a place where  
ATS2 differs from ATS).On Friday, September 12, 2014 2:53:59 PM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Sat, Sep 13, 2014 at 3:41 AM, gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > #define LIBARDUINO\_targetloc “/path/to/\_library”
> > 
> > staload “{$LIBARDUINO}/SATS/arduino.sats”
> 
> Ah, I know this now.  
> I think DATS files are compiled into “libarduino.a” file.
> 
> BTW, he other question: when we should compile SATS file?
> 
> ## Thank’s,
> 
> 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:** [September 13, 2014, 6:18pm UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/17 "2014-09-13T18:18:13Z")

</div>

It is good that it worked.

> > However, LCD\_struct becomes public API…

You could do something like this:

1. Introduce lcd\_initize (!LCD0 \>\> LCD1, …)

where LCD0 means uninitialized LCD and LCD1 means initialized.

implement  
{}(_tmp_)  
lcd\_open (…) = let  
val lcd = lcd\_get () in lcd\_initize (lcd, …)  
end // end of [lcd\_open]

Both LCD0 and LCD1 are abstract.

In low-level programming, it is often a good practice to separate  
resource-acquisition  
(lcd\_get) and resource-initialization (lcd\_initize).

If you like, you can now allocate LCD on the call stack of main; or you can  
have a lock  
on the use of lcd\_get to avoid potential conflicts; or …On Saturday, September 13, 2014 10:55:40 AM UTC-4, Kiwamu Okabe wrote:

> Hi Hongwei,
> 
> On Sat, Sep 13, 2014 at 4:43 PM, gmhwxi \<[gmh...@gmail.com](mailto:gmh...@gmail.com) \<javascript:\>\> wrote:
> 
> > I think it makes sense to introduce a template
> > 
> > fun{} lcd\_get (): LCD // or use LCD\_unintized
> > 
> > Use lcd\_get to implement lcd\_open. You can implement  
> > lcd\_get in main.dats if you like.
> 
> Thank’s! It’s fixed and runnable.
> 
> [Use template and runnable · fpiot/arduino-ats@1ed48ae · GitHub](https://github.com/fpiot/arduino-mega2560-ats/commit/1ed48ae653a9f4430604a00c4bfb7f267f0e6070#diff-d286c2b60b4101c3f21181e0f66793e6R15)
> 
> ## However, LCD\_struct becomes public API…
> 
> 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:** [September 13, 2014, 7:43am UTC](https://discourse.ats-lang.org/t/multiple-dats-sats-directory/574/18 "2014-09-13T07:43:26Z")

</div>

After seeing the following code:

local var \_global\_lcd\_struct: LCD\_struct in …  
end

I think it makes sense to introduce a template

fun{} lcd\_get (): LCD // or use LCD\_unintized

Use lcd\_get to implement lcd\_open. You can implement  
lcd\_get in main.dats if you like.On Saturday, September 13, 2014 3:23:41 AM UTC-4, gmhwxi wrote:

> val lcd = $UN.castvwtp0 (addr@\_global\_lcd\_struct)
> 
> When using a cast function, please provide the type of the returned value:
> 
> val lcd = $UN.castvwtp0{…}(addr@\_global\_lcd\_struct)
> 
> What is …?
> 
> On Saturday, September 13, 2014 2:58:50 AM UTC-4, Kiwamu Okabe wrote:
> 
> > Hi Hongwei,
> > 
> > On Sat, Sep 13, 2014 at 3:03 PM, gmhwxi [gmh...@gmail.com](mailto:gmh...@gmail.com) wrote:
> > 
> > > The C code in lcd.dats can be moved into CATS/lcd.cats.
> > 
> > Umm…
> > 
> > $ make  
> > avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> > -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> > -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> > -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> > -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> > -I…/…//\_dummy -c -o wiring\_digital.o wiring\_digital.c  
> > patsopt -o DATS/main\_dats.c -d DATS/main.dats  
> > avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> > -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> > -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> > -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> > -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> > -I…/…//\_dummy -c -o DATS/main\_dats.o DATS/main\_dats.c  
> > In file included from DATS/main\_dats.c:41:0:  
> > /home/kiwamu/src/ATS-Postiats/prelude/CATS/filebas.cats: In function  
> > ‘atspre\_fileref\_open\_exn’:  
> > /home/kiwamu/src/ATS-Postiats/prelude/CATS/filebas.cats:177:8:  
> > warning: assignment makes pointer from integer without a cast [enabled  
> > by default]  
> > filr = fopen((char\*)path, (char\*)fm) ;  
> > ^  
> > avr-gcc -g -Wall -Os -mmcu=atmega2560 -DF\_CPU=16000000UL -std=c99  
> > -D\_XOPEN\_SOURCE -D\_ATS\_CCOMP\_EXCEPTION\_NONE -D\_ATSTYPE\_VAR\_SIZE=0X000F  
> > -Wno-unused-variable -Wno-unused-label -Wno-unused-but-set-variable  
> > -Wno-implicit-function-declaration -I. -I/home/kiwamu/src/ATS-Postiats  
> > -I/home/kiwamu/src/ATS-Postiats/ccomp/runtime -I…/…//\_arduino  
> > -I…/…//\_dummy -Wl,-Map,main.map -o main.elf wiring\_digital.o  
> > DATS/main\_dats.o  
> > DATS/main\_dats.o: In function `main’:  
> > /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x4):
> > 
> > undefined reference to `PMVerr’  
> > /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x12):
> > 
> > undefined reference to `PMVerr’  
> > /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x20):
> > 
> > undefined reference to `PMVerr’  
> > /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x2e):
> > 
> > undefined reference to `PMVerr’  
> > /home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x48):
> > 
> > undefined reference to `PMVerr’  
> > DATS/main\_dats.o:/home/kiwamu/src/ATS-Postiats/ccomp/runtime/pats\_ccomp\_runtime.c:(.text+0x5a):
> > 
> > more undefined references to `PMVerr’ follow  
> > collect2: error: ld returned 1 exit status  
> > …/…[//Makefile.common:30](https://Makefile.common:30): recipe for target ‘main.elf’ failed  
> > make: \*\*\* [main.elf] Error 1  
> > $ grep -r PMVerr DATS/main\_dats.c|head -5  
> > ATSINSstore(ATSSELfltrec(ATSderef(ATSPMVcastfn(LCD\_takeout\_struct,  
> > atstkind\_type(atstype\_ptrk), ATSPMVrefarg0(ATSPMVcastfn(castvwtp0,  
> > atstype\_boxed,  
> > PMVerr(“/home/kiwamu/src/arduino-mega2560-ats/DATS/lcd.dats:  
> > 1942(line=55, offs=30) – 1965(line=55, offs=53)”)))),  
> > postiats\_tyrec\_1), postiats\_tyrec\_1, atslab\_\_rs\_pin),  
> > ATSPMVcastfn(cast, atstkind\_t0ype(atstype\_uint8), arg0)) ;  
> > ATSINSstore(ATSSELfltrec(ATSderef(ATSPMVcastfn(LCD\_takeout\_struct,  
> > atstkind\_type(atstype\_ptrk), ATSPMVrefarg0(ATSPMVcastfn(castvwtp0,  
> > atstype\_boxed,  
> > PMVerr(“/home/kiwamu/src/arduino-mega2560-ats/DATS/lcd.dats:  
> > 1942(line=55, offs=30) – 1965(line=55, offs=53)”)))),  
> > postiats\_tyrec\_1), postiats\_tyrec\_1, atslab\_\_rw\_pin),  
> > ATSPMVcastfn(cast, atstkind\_t0ype(atstype\_uint8), arg1)) ;  
> > ATSINSstore(ATSSELfltrec(ATSderef(ATSPMVcastfn(LCD\_takeout\_struct,  
> > atstkind\_type(atstype\_ptrk), ATSPMVrefarg0(ATSPMVcastfn(castvwtp0,  
> > atstype\_boxed,  
> > PMVerr(“/home/kiwamu/src/arduino-mega2560-ats/DATS/lcd.dats:  
> > 1942(line=55, offs=30) – 1965(line=55, offs=53)”)))),  
> > postiats\_tyrec\_1), postiats\_tyrec\_1, atslab\_\_enable\_pin),  
> > ATSPMVcastfn(cast, atstkind\_t0ype(atstype\_uint8), arg2)) ;  
> > ATSINSstore(ATSSELfltrec(ATSderef(ATSPMVcastfn(LCD\_takeout\_struct,  
> > atstkind\_type(atstype\_ptrk), ATSPMVrefarg0(ATSPMVcastfn(castvwtp0,  
> > atstype\_boxed,  
> > PMVerr(“/home/kiwamu/src/arduino-mega2560-ats/DATS/lcd.dats:  
> > 1942(line=55, offs=30) – 1965(line=55, offs=53)”)))),  
> > postiats\_tyrec\_1), postiats\_tyrec\_1, atslab\_\_data\_pins), tmp356) ;  
> > ATSINSmove\_void(tmp357,  
> > pinMode(ATSSELfltrec(ATSderef(ATSPMVcastfn(LCD\_takeout\_struct,  
> > atstkind\_type(atstype\_ptrk), ATSPMVrefarg0(ATSPMVcastfn(castvwtp0,  
> > atstype\_boxed,  
> > PMVerr(“/home/kiwamu/src/arduino-mega2560-ats/DATS/lcd.dats:  
> > 1942(line=55, offs=30) – 1965(line=55, offs=53)”)))),  
> > postiats\_tyrec\_1), postiats\_tyrec\_1, atslab\_\_rs\_pin),  
> > ATSextval(OUTPUT))) ;
> > 
> > What is PMVerr?
> > 
> > ## Regards,
> > 
> > Kiwamu Okabe at METASEPI DESIGN
