Skip to content

Commit cbc906c

Browse files
author
Steve Krouse
committed
## Fun, fustrating, little progress on killing primitives
Part of the problem doing work on the computer is that I'm too plugged in. Whenever I get stuck in my thinking, it's almost too easy to google around for related work. There are many benefits for struggling through the problem myself, not least of which is that it's quite fun -- I did it last week via pencil and paper and it was a blast. Today was 6 hours of FoC work. 0 hours of Dark work. Very productive! Tomorrow maybe I'll do 1.5-3.5 hours of Dark work, and then 3ish hours of work here. In particular, I'm excited to explore xstate, statecharts, David Piano, and maybe email him to invite to chat on the podcast.
1 parent a4e9b56 commit cbc906c

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed

notes/kill-primitives.md

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: Kill Primitives
3+
---
4+
5+
# Kill Primitives
6+
7+
* TOC
8+
{:toc}
9+
10+
11+
## Problem
12+
13+
### 1. Overloading
14+
15+
Let's say you want a function that inputs a number. If it's even, it returns the square. If it's odd, it returns the input number unchanged.
16+
17+
In APL, you'd implement this function as:
18+
19+
```apl
20+
f←{⍵*(2-2|⍵)}
21+
```
22+
[Link](http://tryapl.org/?a=f%u2190%7B%u2375*%282-2%7C%u2375%29%7D&run)
23+
24+
(In APL, `` is the input, `a*b` is a to the power of b, and `2|x` is x modulus 2.)
25+
26+
In JavaScript, you might implement this as:
27+
28+
```javascript
29+
function f(x) {
30+
if (x % 2 == 0) {
31+
return x^2
32+
} else {
33+
return x
34+
}
35+
}
36+
```
37+
38+
... hmmm not able to make the point I want here...
39+
40+
### 2. Booleans too general (fka "kill booleans")
41+
42+
What are the possible results of `a == b`?
43+
44+
`True` or `False`? Wrong!
45+
46+
It's `Equal` or `NotEqual`.
47+
48+
Of course, I remember in Java, you'd use the compareTo method and it was super annoying because I could never remember what -1 or 1 meant. Of course, that's what I'm getting at here. -1 and 1 don't mean what we mean. We mean `LeftBigger`, `Equal`, or `RightBigger`. In fact, we really shouldn't even need the call to compareTo to express this. Instead you could just express it as a series of case-statements... but now I see how nested this could all get...
49+
50+
And, of course, there's the open question of how to do boolean algebra in a world without booleans. For example if you want to express:
51+
52+
```javascript
53+
(a == b) && (c == d)
54+
```
55+
56+
Tricky... && and || and ! are super concise and powerful. How to replace them without getting overly nested?
57+
58+
It's important to realize that this whole idea is to increase verbosity, so it'll be more keystrokes than one or two, but that's not a problem in a solid IDE interface.
59+
60+
### 3. No if-statements, just pattern matching
61+
62+
It always annoyed me that Haskell has pattern matching and also if-statements. Is there a way to define types in such a way to rid ourselves of if-statements.
63+
64+
Example, I want to do the function from above. In order to do it in Haskell, I have to explain how integers come in two types: even or odd. I guess it could just be a function from int to Even or Odd.
65+
66+
```haskell
67+
f x = f' (intToEvenOrOdd x)
68+
where
69+
f' Even =
70+
f' Odd =
71+
intToEvenOrOdd x = ... need `if` here
72+
```
73+
74+
I guess the distinction between if-statement and pattern matching is that if's do computation and pattern matching takes advantage of the inherent typed data structure. (TODO is this right?)
75+
76+
Of course, I could represent this recursively without an if-statement by defining all numbers as S-expressions and recur until I get down to the bottom, but this would be tedius to implement and probably slow in runtime.
77+
78+
Except maybe this is the key... if we have *inspectable* representations for everything -- as opposed to some magic number implementor in the hardware -- maybe we could achieve "turtles all the way down" as well as the abolishion of if-statements.
79+
80+
Some posts relevant here...
81+
82+
* https://blog.deprogramandis.co.uk/2013/03/20/if-statements-considered-harmful-or-gotos-evil-twin-or-how-to-achieve-coding-happiness-using-null-objects/
83+
* http://michal.muskala.eu/2016/01/04/if-considered-harmful-or-is-it.html#fnref:nil-variable
84+
* https://haacked.com/archive/2013/11/08/death-to-the-if-statement.aspx/
85+
* http://michaelfeathers.typepad.com/michael_feathers_blog/2013/11/unconditional-programming.html
86+
87+
But the one that originally inspired this idea: https://francescocirillo.com/pages/anti-if-campaign
88+
89+
### 4. Phsyics units for numbers
90+
91+
Maybe the problem with the above examples is that they are too toy. The whole point of this essay is that we need more specific types, which is hard with abstract problems with numbers. Before I think of a more traditional programming-based example of this (TODO), I want to explore how this idea could be used to explore physics problems. I remember in physics class that I would allow the units in an expression to guide its meaning and what I could do to it (what I could multiply or divide it to get what.)
92+
93+
## Problems
94+
95+
### Types-first is hard
96+
97+
Part of what makes primitives fun and easy is that you can get started right away without having to worry about the interfaces and excessive formalism. Concrete than abstract, remember?
98+
99+
100+
## Related Work
101+
102+
Conal Ellot's denotation design. [This article was helpful in accessing it](https://lukepalmer.wordpress.com/2008/07/18/semantic-design/).
103+
104+
105+
106+
<script>
107+
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
108+
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
109+
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
110+
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
111+
ga('create', 'UA-103157758-1', 'auto');
112+
ga('send', 'pageview');
113+
</script>
114+
<script repoPath="stevekrouse/futureofcoding.org" type="text/javascript" src="/unbreakable-links/index.js"></script>
115+

0 commit comments

Comments
 (0)