Arduino programing of ML-style in ATS Kiwamu Okabe

Hongwei Xi

METASEPI DESIGN [email protected]

Boston University [email protected]

Abstract Functional programming languages often require a large run-time environment supported by the underlying OS to ensure various forms of safety during code execution. For instance, memory safety is usually achieved through systematic garbage collection (GC), which may not be available for embedded programming or should even be avoided on purpose due its adverse effect on predictability. In this talk, we demonstrate that programming in ATS, a language with a functional core of ML-style, can be effectively employed to construct programs running on Arduino Uno (of 2KB SRAM and 8-bit CPU).

1.

Introduction

Functional programming (FP) is gain popularity steadily. In general, FP is associated with the need for a big run-time environment supported by the underlying OS. For instance, functional programming languages (FPLs) like SML, OCaml, and Haskell fall into this category. This need makes it difficult to directly employ these languages in embedded programming where resources are far more limited and constraints (resource-wise and performance-wise) are stricter. As an example, memory safety in FP is usually achieved through systematic garbage collection (GC), which may not be available for embedded programming or should even be avoided on purpose due its adverse effect on predictability. There have already been many attempts to address the issue of applying FP to embedded programming. One proposed approach is to have the run-time needed for FP supported by a tiny OS (running on a VM) [1]. Of course, this approach is not applicable if the underlying device is still too limited for the tiny OS. Another approach is to build a domain-specific language (DSL) based on a host language of functional style (e.g., Haskell) [2] and rely on the type system of the host language to ensure type-safety. However, a DSL often lacks flexibility, and can incur a high cost in terms of programming productivity. Yet another approach is to directly employ a general purpose FPL in the construction of programs running on bare metal hardware [3], making trade-offs between safety with flexibility. This is the approach we take here as well. In this talk, we introduce ATS [4] as a FPL for constructing programs running on Arduino Uno [5] (of 2 KB SRAM and 8bit CPU core). We give a concrete example showing that higherorder functions (of ML-style) can be supported in this rather limited environment. We also present some measurement to show that binaries generated from ATS source are very close (in terms of size) to those generated from the C counterpart.

2.

particular, dependent types (of DML-style) and linear types are supported in ATS. For instance, we can use dependent types to ensure statically (that is, at compile-time) the absence of out-ofbounds array subscripting at run-time; we can use linear types to prevent resources from being leaked; etc. Binaries generated from ATS source tend to be very compact and have minimal dependency on POSIX API. For programming Arduino boards, we can use ATS to construct code that does not rely on GC or any use of malloc/free. While this means that we have to stay away features that do need GC or malloc/free, we can still make use of higher-order functions (by stack-allocating closurefunctions) and many other programming features of ML-style (e.g., pattern matching, loops based on tail-recursion).

ATS programming language

ATS is a programming language equipped with a highly expressive type system rooted in the framework Applied Type System [4]. In

3. Arduino Uno board

Figure 1. Arduino Uno board Figure 1 shows an Arduino Uno board that has following summary of specification: • Architecture: AVR (8-bit Harvard architecture) • Flash Memory: 32 KB • SRAM: 2 KB

Also the board has many pins that can connect the other function boards (that is, shields). Examples of shields include LCD screen, Ethernet, WiFi, Motor control. Clearly, it is difficult for any applications running on the board to use GC due to its tiny memory (2 KB). Using (customized) malloc/free may be possible but certainly requires great caution.

4. Compile flow for Arduino application ATS can be readily used as a front-end to C. Figure 2 outlines a cross-compilation environment for the AVR architecture. The ATS compiler (patsopt) translates an ATS source file (e.g. main.dats) into an C source file (e.g. main_dats.c). The C code is then compiled by a C compiler (avr-gcc) into an object file (e.g. main_dats.o). Finally a linker (avr-ld) combines several object files (including those generated from ATS source) into a single executable (e.g. main.elf).

15 v a l ( ) = p i n M o d e ( LED , O U T P U T ) 16 val () = (fix f(): void = > (fadein(); f()))() 17 }

The function analogWrite controls a given LED using pulse width modulation (PWM), taking as its second argument a natural number less than 256 (that indicates brightness). The function fadein gradually increases the brightness of LED by calling analogWrite. It is implemented with a call to a higher-order function int_foreach_clo; given 256 and fwork, int_foreach_clo calls fwork on natural numbers from 0 until 255, inclusive. Note that fwork is a closure-function allocated in the frame of fadein. In particular, there is no dynamic memory allocation involved. The fix-expression implements a non-terminating loop for calling fadein(0) repeatedly.

6.

Binary size efficiency of the ATS code Example 01 02 03C 04 05 06A 06B

Figure 2. Compile flow for Arduino application

5.

Arduino programming in ATS

In this section, we translate a sample Arduino program (written in C) into ATS. The following code is taken from Example 04 [6], which gradually fade in an LED: Listing 1. C code to fade an LED in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

#define LED 9 #define DELAY_MS 10 int main() { int i; init(); p i n M o d e ( LED , O U T P U T ) ; while (1) { for (i = 0; i < 255; i++) { a n a l o g W r i t e ( LED , i ) ; delay(DELAY_MS); } } return 0; }

Listing 1 shows original code of C language. We can translate it into ATS as is given in listing 2. Listing 2. ATS code to fade an LED in 1 2 3 4 5 6 7 8 9 10 11 12 13 14

#define LED 9 #define DELAY_MS 10.0 typedef analog_w_t = natLt(256) implement main0 () = { fun fadein() = let var fwork = lam@ (n: analog_w_t) = > ( a n a l o g W r i t e ( LED , n ) ; d e l a y _ m s ( D E L A Y _ M S ) ) in / / type - e r r o r i f 2 5 6 c h a n g e s t o 2 5 5 o r 2 5 7 i n t _ f o r e a c h _ c l o (256 , f w o r k ) / / higher - o r d e r end // end of [fadein] val () = init ()

C 1145 byte 1135 byte 1203 byte 1447 byte 1635 byte 1431 byte 1421 byte

ATS 1203 byte 1173 byte 1231 byte 1493 byte 1571 byte 1477 byte 1467 byte

Table 1. Binary size efficiency on examples of the Arduino book In Table 1, we give a comparison between the binary size of the code generated from C programs and that of the code generated from the ATS counterparts of these C programs. In each of the presented case, the ATS version is only slightly larger (by fewer than 50 bytes) than the C version. This should be reasonable for practical use.

7. Conclusion We have given a brief presentation in support of the claim that the ATS programming language can be employed effectively for constructing programs running on bare metal hardware such as 8-bit Arduino. It is shown that closure-functions can be stack-allocated to support the use of higher-order functions in the absence of GC. There are many other features in ATS for supporting safe and efficient low-level programming that cannot be presented here due to space limitation. For instance, safe manual memory management can be based on linear types; template-based programming allows for (extreme) late-binding of funtion calls; etc.

References [1] Anil Madhavapeddy and David J. Scott. Unikernels: Rise of the virtual library operating system. Queue. [2] Patrick C. Hickey, Lee Pike, Trevor Elliott, James Bielman, and John Launchbury. Building embedded systems with embedded dsls. In Proceedings of the 19th ACM SIGPLAN International Conference on Functional Programming. [3] Kiwamu Okabe and Takayuki Muranushi. Systems demonstration: Writing netbsd sound drivers in haskell. In Proceedings of the 2014 ACM SIGPLAN Symposium on Haskell. [4] Hongwei Xi. Applied Type System (extended abstract). In postworkshop Proceedings of TYPES 2003. Springer-Verlag LNCS 3085. [5] Arduino Uno. http://arduino.cc/en/Main/ArduinoBoardUno. [6] Massimo Banzi. Getting Started with Arduino. O’Reilly, 2 edition, 2011.

Arduino programing of ML-style in ATS - ML Family Workshop

binaries generated from ATS source are very close (in terms of size) to those generated from the C counterpart. 2. ATS programming language. ATS is a programming language equipped with a highly expressive type system rooted in the framework Applied Type System [4]. In particular, dependent types (of DML-style) and ...

2MB Sizes 1 Downloads 248 Views

Recommend Documents

Mergeable Types - ML Family Workshop
systems with the ability to define and compose distributed ML computations around ... library on a single machine, this implementation behaves as expected.

Tierless Modules - The ML Family Workshop
Web, client/server, OCaml, ML, Eliom, functional, module. 1 INTRODUCTION. Traditional Web applications are composed of several dis- tinct tiers: Web pages ...

Ambiguous pattern variables - The ML Family Workshop
Jul 29, 2016 - Let us define .... where the Bi,k are binding sets, sets of variables found ... new rows bind to a different position. [Bi,1 ... Bi,l. | K(q1,...,qk) pi,2.

Relational Conversion for OCaml - ML Family Workshop
preters (Programming Pearl) // Proceedings of the 2012 Work- shop on Scheme and Functional Programming (Scheme '12). [5] Henk Barendregt. Lambda ...

Sundials/ML: interfacing with numerical solvers - ML Family Workshop
Sep 22, 2016 - 4. REFERENCES. [1] T. Bourke and M. Pouzet. Zélus: A synchronous language with ODEs. In HSCC, pages 113–118. ACM. Press, Apr. 2013.

Sundials/ML: interfacing with numerical solvers - ML Family Workshop
Sep 22, 2016 - [email protected]. Jun Inoue. National Institute of Advanced. Industrial Science and. Technology. [email protected]. Marc Pouzet. Univ. Pierre et Marie Curie. École normale supérieure,. PSL Research University. Inria Paris.

VOCAL – A Verified OCAml Library - ML Family Workshop
OCaml is the implementation language of systems used worldwide where stability, safety, and correctness are of ... An overview of JML tools and applications.

Relational Conversion for OCaml - The ML Family Workshop
St.Petersburg State University .... Logic in Computer Science (Vol. 2), 1992. [6] William E. ... Indiana University, Bloomington, IN, September 30, 2009. [7] Dmitry ...

Typer: An infix statically typed Lisp - The ML Family Workshop
Oxford, UK, September 2017 (ML'2017), 2 pages. ... the syntax of macro calls is just as exible as that of any other .... Conference on Functional Programming.

VOCAL – A Verified OCAml Library - ML Family Workshop
Libraries are the basic building blocks of any realistic programming project. It is thus of utmost .... verification of object-oriented programs. In 21st International ...

Polymorphism, subtyping and type inference in MLsub - ML Family ...
Sep 3, 2015 - Polymorphism, subtyping and type inference in. MLsub. Stephen Dolan and Alan Mycroft ... We have two tricks for getting around the difficulties: • Define types properly. • Only use half of them. 2 ... Any two types have a greatest c

Extracting from F* to C: a progress report - The ML Family Workshop
raphy (ECC) primitives, and on extracting this code to C. ... verification extract the code back to C. .... pointers are made up of a block identifier along with an.

Polymorphism, subtyping and type inference in MLsub - ML Family ...
Sep 3, 2015 - Polymorphism, subtyping and type inference in. MLsub. Stephen Dolan and Alan Mycroft ... We have two tricks for getting around the difficulties: • Define types properly. • Only use half of them. 2 ... Any two types have a greatest c

Extracting from F* to C: a progress report - The ML Family Workshop
sub-tree untouched. In short, hyperheaps provide framing guarantees. Each sub-tree is assigned a region-id (rid), and a hyperheap maps an rid to a heap.

UBS ATS
Mar 14, 2017 - information concerning UBS, nor is it intended to be a complete statement or ... described herein comply with all applicable laws, rules and regulations. ... provided investment banking, capital markets and/or other financial services

PROGRAMING IN 'C++.pdf
scope and lifetime. 5. EITHER. 2. ... (d) List various exceptions and give its uses in object. programming. 5. 5. ... PROGRAMING IN 'C++.pdf. PROGRAMING IN ...

GADTs and exhaustiveness: looking for the impossible - ML Family ...
... !env expected_ty) expected_ty k else k (mkpat Tpat_any expected_ty). | Ppat_or (sp1, sp2) -> (* or pattern *) if mode = Check then let state = save_state env in try type_pat sp1 expected_ty k with exn ->. 3The code is available through OCaml's Su

GADTs and exhaustiveness: looking for the impossible - ML Family ...
log's SLD resolution, for which counter-example genera- tion (i.e. construction of a witness term) is known to be only semi-decidable. Another way to see it is that ...

Billerica Public Schools Family Workshop ...
Mar 8, 2016 - This workshop is an introduction to Google Apps such as Google Docs, Slides, Calendar, and Gmail ... Parents will learn about how to use Aspen to it's greatest potential by reviewing settings, setting up .... home by explaining the posi

Page 1 Z 7654 ML ML LEAL ML ML 8_2m1L _22.13_ _BML _BML ...
S e e e cl S t L_l cl 1 o. TITLE: ñrch BLE v1.84. Design: v? 32. 31. 29. 28. || 27. 26. 25. 19. En „3 21. En ai 22. En „5 23. En ná 24. 123456789 ...