
Arduino-copilot makes it easy to use Haskell to program an Arduino - dwb
https://joeyh.name/blog/entry/announcing_arduino-copilot/
======
joeyh
Author here.. Just as a preview of the next release of this, I've been
teaching it how different pins of the arduino can be used. Some pins support
digital IO, some also PWM, some analog input pins also support digital IO, but
others don't.

So I can write a program like this and pick pins to use essentially at random:

    
    
        import Copilot.Arduino.Uno
        
        main :: IO ()
        main = arduino $ do
            pwm pin2 =: longer_and_longer
            delay =: constant 10
    

And without even needing to load it into a board, let alone hook up wires, the
compiler can tell me it won't work:

    
    
       demo2.hs:5:9: error:
        • This Pin does not support PWM
        • In the first argument of ‘(=:)’, namely ‘pwm pin2’
          In a stmt of a 'do' block: pwm pin2 =: longer_and_longer
    

This is implemented using type level lists and type level programming, and
user-defined type error messages.

------
wruza
When I first met arduino (and quickly more embedded boards afterwards), the
hard part was not C[++], nor a toolchain around it – I’m a C guy since ‘00. It
was the fact that this area has few de-facto standards on how things should
cooperate. Basic tutorials have no notion of event loops, you just for-loop
forever and poll pins. But once I’ve got few non-standard devices, like that
pressure sensor I got somewhere in the city, it all falls apart, because e.g.
the time required to talk to it via provided library consumes enough cycles
for your entire process to be late on physics. And vice versa, your logging is
not async and interferes with signal timeouts. You simply spend tens-thousands
of cycles spin-locking on i/o and there is no way to fix that without
rewriting “drivers” under some “os”. Idk if haskelling everything could help
with that.

I decided to just move on to high-mhz stm’s or even -pi variants, where a
whole different stratum of software lives (seemingly), but luckily project
folded for unrelated reasons. My conclusion was that if you’re a software
developer, don’t just go embedded, it is not what you used to and routes you
can take are limited.

Edit: obviously missing some details here, and it was 7 years ago, would like
if someone would elaborate on the state of the embedded world today.

------
kbumsik
I'm not familiar with Haskell and functional programming in general, so I'm
curious if these codes in the OP make sense to Haskell people? Every lines of
the codes are full of side effects (LEDs, buttons, delay...) and completely
procedural.

~~~
monoideism
Pardon the off-topic question, but I noticed you used the plural "codes"
instead of "code", which has typically been used in American English.
Traditionally, "codes" has been used in other dialects of English, but I've
noticed an increasing number of Americans using the plural form.

Is this a thing now? Are we saying "codes" instead of "code"?

~~~
kbumsik
Interesting question. I'm Korean and actually I was asking myself if "codes"
is correct English while typing. I don't know, but except for grammar "code"
seems to be countable to me.

Note that Korean grammar (and maybe some other languages) doesn't have strict
rules about plural forms.

Anyway "Codes" make sense to me when they refer "snippets".

edit: added some more thoughts.

~~~
monoideism
Yes, I think it's something some American developers have picked up from
others using "global" English. I heard the plural used a lot from devs in and
from the Indian subcontinent, and now, I'm starting to hear native American
devs use the plural form.

------
jonny383
Trying to keep an open mind here about this - but what does Haskell offer on
an _arduino_ aside from developer preference?

Surely C/C++ will be the better performer on such a tiny board... And usually
you want to squeeze every drop of performance out of tiny boards

~~~
dragonsh
You are right C is a good choice for arduino, but if Python can be used with
it, I believe Haskell can also be used in similar way.

It will bring many high level features like type safety, immutability, runtime
error free code and code that is easy to refactor, once the Haskell learning
curve is climbed. I like Haskell for its brevity and pseudo code like syntax
which really runs like Python with all the necessary safety.

Given Haskell share in the overall eco-system of languages, I don’t expect it
to have any significant impact. But this little steps are necessary to move
forward Haskell.

I think the biggest stumbling block Haskell need to overcome is to have good
documentation like Python. I believe if instead of just features, language
research Haskell community needs to put extraordinary efforts in
documentation. In spite of Haskell being an old language more than 50% of
packages on hackage do not have proper documentation and unless that do not go
up to 90% or more Haskell won’t be able to be used effectively by programmers
to solve computing problems. We all stands on giants of shoulders and for that
to happen need documentation. I believe people underestimated the power of
good documentation. It should be twice or thrice more efforts than the
language feature development itself.

~~~
ph2082
My 2020 goal is to learn Haskell, so beginner here.

Is there a way other people can contribute to documentation ? Also how to
generate documentation like found in Hoogle ?

~~~
chowells
The Haskell documentation generation tool is named haddock. It comes with GHC.
I've never run it standalone, so I don't know how involved that is. But I
frequently run it with the command "cabal haddock", which takes care of
telling it where all your source files are and where to drop the output.

~~~
ph2082
Thank for reply. I'll dig into it. Kind of documentation I am thinking about
not only description of what method do, but also include example of 'how to
use' it.

~~~
sjakobi
Doctests are often useful to briefly demonstrate how something works:
[http://hackage.haskell.org/package/doctest](http://hackage.haskell.org/package/doctest)

They are a bit finicky to set up though.

Another good resource: [http://yannesposito.com/Scratch/en/blog/Haskell-
Tutorials--a...](http://yannesposito.com/Scratch/en/blog/Haskell-Tutorials--
a-tutorial/)

------
gregwebs
This looks like a great system to simplify Arduino programming.

However, this runs your program in a loop. You can add a delay to reduce power
consumption, but it will reduce responsiveness. I am really looking for
interrupt-based Arduino programming.

------
ph2082
That looks pretty cool. Is there some comparison table exists about binary
size, memory usage etc against other languages?

