Haskell in Termux in Android 10 in LG Q60

So I decided to bite the bullet and learn Haskell.

I am VERY late to learn Haskell. Other languages have incorporated pretty much every useful Haskell feature and I used most of them already. Anything I learn from this will probably be purely philosophical.

Since it will be purely philosophical, I don’t want to do this while sitting at a desk, but rather on my pocket supercomputer that I keep on my person at all times. 

My phone (a humble LGQ60 with Android 10) already has Termux in it. Termux is a terminal emulator that runs on Android.  It is a package manager and Bash interpreter that connects to the internet. I have successfully used it for small scale C/C++, JS and Python development.

If you are going to install Termux just for this tutorial, run “pkg update” in it before you do anything else and update the core libs.

The official way of managing Haskell toolchain is GHCup. You get it and the rest of the toolchain by curling a script. Alas, it didn’t work for me.

I decided to get an older version from the package manager itself, which installed nicely enough. It did crash immediately with a segfault upon execution though. So, this also didn’t work for me.

So I looked around the internet and found a Termux-specific build of a much older interpreter called HUGS. It is small enough that I can compile it from sourcecode, which means I can deal with problems locally on the phone itself. (see above: I already have gcc and make)

I saw the following instructions in the readme file:

$ termux-chroot
$ mkdir src
$ cd src
$ git clone https://github.com/trenttobler/android-termux-hugs.git
$ cd android-termux-hugs
$ make install

Do I really need root to run a secondary interpreter? I went ahead and did it anyway and found that binaries are saved under /usr/local/(bin|lib) folders. I can’t access them without root, since they don’t belong to my personal user, which is bad for doing stuff on a phone. I think I can just change the build script and do it without root privileges. I went ahead and edited the relevant path names in the Make files.

As you can see I use VIM in a shell on a phone to edit a makefile.

Next time all that crap will be dumped into a folder under my home directory ( henceforth referred to tersely as “~”)

I went ahead and moved all build artifacts to new locations manually – no need to compile all that code again right now.

$ cd /usr/local
$ cp bin/* ~/local/bin/.
$ cp -r lib/. ~/local/lib/.

I may delete the originals later if everything works out fine without root.

Now I forgot to take a screenshot but I also learned that the lib folder has to be directly under ~/   OR you should change the HUGS path to ~/local/ in HUGS config. Also don’t forget to add ~/local/bin/ to your bash PATH variable. You should finally refresh the PATH variable to use it in the running session. I just restarted the app.

Type hugs in prompt to start the REPL.

If you do what HUGS says and execute 😕 in the REPL, you will see a list of available commands.

So HUGS seems to have a :cd command for changing directory but no command for printing out where you are OR listing out what’s in the folder. 🤦

I’ll need to fix this. Hum the tune of ‘sound of silence’ as you read on.

hello darkness my old friend
I’ve come to write c code again
because some core feature is missing
shells without fs apis are annoying
and the vision that is planted in my brain
still remains
whispered in the sound of posix compliance

I go back to the github repo where I found this to read the source code.

First lets see where :cd is implemented. By performing a dumb string search in the github repo I find it immediately.

Well hello there…

It seems if I am on __SYMBIAN32__ I can use pwd, but not on Termux?
This isn’t even funny; why would this only work on symbian?

For those who are not old enough: Symbian was an embedded phone firmware for the pre-iPhone era.

The printDir() is a super thin wrapper for the OS api…

So I decided to take stock of what this file additionally does and noticed this.
This looks promising. Would a function called shell escape let me use shell commands in REPL?

So a quick google search found me this.

Shell escape :![command]

A :!cmd command can be used to execute the system command cmd without leaving the Hugs interpreter. For example, :!ls (or :!dir on DOS machines) can be used to list the contents of the current directory. For convenience, the :! command can be abbreviated to a single ! character.

from offical docs

Why isn’t this explained in the help text in the first place? I went and looked at the source code for the help text. It’s not even maliciously hidden; it just doesn’t exist.

OK I don’t need to write C with system calls. I can just use ! to get what I want. !pwd and !ls just work.

🎉 CRISIS AVERTED 🎉

Since it will probably work, I will go and read http://learnyouahaskell.com/

 If I figure out a good way of explaining what a monad is I’ll write a new blog about it.

Fixing A Lighter as a Software Person

Warning: I actually have a M. Sc. in Mechatronics Engineering, so I actually know what I am doing here. Unless you have the kind of qualification or education that has instilled a primal fear of Li-Po batteries deep in your soul, you should NOT do this kind of work. 

A couple weeks ago, I bought an electric lighter. It has a USB-C charging port. When you press a button on the side, it creates two sparks in an X shape. It’s really nifty. Yet, to my dismay, it stopped working recently. I decided to open it up and poke around.

Maybe I can fix it?

It is held together by a small screw at the bottom. I took it out with a thin screwdriver and the internals slid out without any resistance. 

This is what the internals look like.

 The silver-colored thing at the bottom is a Li-Po cell. If you mistreat it, it will rain fire and brimstone all over your desk (This is a deep-seated fear that I have). 

The white box in the middle should be a switch and/or relay. It probably regulates the alternating rhythm of the crossed sparks so that they don’t interfere with each other (This is a guess). 

The flow of energy in nature always follows the path of least resistance. If we tried to strike sparks at the same time, they would appear between the leads on the same (short) side because they are closer to each other and offer less resistance. If we are to strike a diagonal spark, the neighbouring leads must be neutral. 

 To get crossed sparks, we need to turn the leads on and off very rapidly. A frequency slightly more than 24 Hz is probably ok. Even if the sparks don’t coexist, human vision will make them appear. If the frequency is high enough that the leads turn off and on again before the spark collapses, you could have concurrent sparks. 

The black plastic things on the right hand side are integrated circuits. The one near the battery must be  the charge controller for the battery, the other two must be amplifiers for the spark plugs (This is an educated guess; I have seen similar PCB boards before.) 

The weird contraption upper left hand side with the spring and copper plate? I have no idea.

I’m touching a detached cable with my finger.

 As you can see, there is a cable that isn’t connected anywhere else. A first principle in electrical design is the necessity of connecting cables unless they are supposed to be RF antennas. And it has some solder smeared at the end so it must have been attached somewhere before. There is a cable of the same gauge connected to the contraption, and the contraption does not seem to connect to anything else. I assume that the cable is supposed be attached to the contraption somehow. 

You see… if I shut off the cover, that black lever pushes the spring of the contraption inside and breaks a possible electrical contact. It could be a security feature to prevent the thing from going off in your pocket. I am now convinced that I need to connect the floating cable to the contraption. 

It worked when I touched the copper plate with the floating cable and pressed the button. I can’t show you a video because I don’t have enough hands to record it at the same time. Through trial and error, I found a possible solution to follow. 

I was feeling especially daring, so I took apart the contraption to figure out where the cable was originally attached. There is a screw to hold the copper plate in place, a spring, and a piston head to make sure that spring moves in only one direction.  

Well, the piston head is obviously a plastic part that has a thin conductive coating. It is a terrible idea in general and nigh impossible to solder. So I took a closer look at the spring. Sadly, my phone can’t photograph the spring: it is just too small. BUT… It has some discoloration that is visible to the naked eye. And the discoloration suspiciously looks like solder. So, someone decided to solder a thin conducting wire to a tiny moving spring. 🤦 No wonder it detached. Such a moving thin solder will always break faster than its better designed neighbors. 

 

Meanwhile, BEHOLD the tiny friend that decided to drop by my desk. It looks like it’s a kind of weevil. He later disappeared under the printer there. 

Now I don’t want to repeat the mistake and solder the wire back onto the spring. It will become detached again. Also, I am not sure if I can do it properly in the first place. I am not a crisp undergrad anymore and I have shaky hands. 

Instead, I will drill a hole at the bottom of the contraption, poke the wire in there, and hope that the pressure from the spring pushes it down and holds it in contact. 

As you can see, I already own a hand-sized drill, a mandrel, and a 1mm drill bit. 

The blurry protrusion is the really thin drill bit

This material (which I assume is polyethylene) shall offer no resistance to my mighty drill bit!!! 

I was afraid for a second that maybe the mighty drill bit was too mighty, and I would just destroy that pocket, but it actually turned out OK, as seen in the picture above. I should have drilled a bit lower; I’ll keep it in mind for the next time this thing decides to break down. 

So, I poked the wire inside, reassembled the contraption as well as I could, and it looks like a solid OK.

This is also how I mostly go about troubleshooting projects for clients: make some investigations, invent some trial and error, rely on first principles to guide me through ambiguity, and keep costs and feasibility in mind when coming up with a hack solution. 

It also works now.

I’m really glad that I could save the 10€ space-age lighter without pulling out a soldering iron.

 

A UML diagram is a map

A UML diagram is a map.

A map is a simplified representation of complicated or hard to observe reality. A map should be simple enough to be digestible, yet it should also contain enough information to be useful for decision-making.

The current oldest surviving map is from Bronze age. It is a piece of rock that depicts the region where it was found. (click here for a diagram) It is obviously the kind of map you draw to take stock of things. To keep things under record. It doesn’t cover a large enough area to be useful for planning movement.

The oldest known maps that show not only where the cartographer lived, but also included territories of trade partners and potential military targets are from the roman empire. Based on our knowledge of how roman empire functioned it is safe to assume that they were actively used for planning.

Correlation does not mean causation, but I still think there is something to be said about how the “where are we standing?” map is older than the “where are we going?” map. It makes intuitive sense to determine where you are before you plot a course to some other place. If your imaginary path has a

Mind you, this distinction is entirely subjective. Both are maps in the exact same way, the only difference is the intention of the user.

Now it should be apparent where I am going with this in terms of UML diagrams.

Waterfall people originally intended UML to be a planning tool and a programming language. An expensive software engineer would design the system with UML then cheaper coders would write source code (probably in C++) according to UML. This two tiered workflow proved to be cumbersome.

UML is a human to human protocol. And THOSE are open to all sorts of corruption. If a programmer is being threatened with being fired on the spot, they will absolutely not care about the correctness of UML in a hypothetical future. Suchh practical constraints frequently cause operators to sideline UML and directly change source code. In time UML turns into stale documentation.

Most programmers have experienced the “stale documentation” version of UML and developed knee jerk reactions against anyone who comes bearing UML tools. But in doing so also miss an opportunity.

Remember, a map does not have to indicate where you are going, it is also useful if it shows you where you are. So lets assume we change the intention behind why we use UML. There are multiple tools that will read your code and create an UML diagram from it. Wouldn’t it be useful to have a single page overview of all your classes? As long as you don’t take it too seriously it will be fine.