間接的な影響

I just realized that it is futile to drop hints to my wife about, say, a snack before dinner. The master play is to talk about snacks with my kids before dinner, and they will always find a way to deliver the goods.

anna_the_persuasive3

Evidence of real power: badass snacks.

Pure Declarations in Erlang

Over the last year or so I’ve gone back and forth in my mind and in discussions with other Erlangers about type systems in Erlang, or rather, I’ve been going back and forth about its lack of one and the way Dialyzer acts as our bandaid in this area. Types are useful enough that we need Dialyzer, but the pursuit of functional puritanism gets insane enough that its simply not worth it in a language intended for real-world production use, especially in the messy, massively concurrent, let-it-crash, side-effecty, message-centric world of Erlang.

But… types and pure functions are still really useful and setting a goal of making as much of a program as possible into provable, bounded, typed, pure functions tends to result in easy to understand, test and maintain code. So there is obviously some stress here.

What I would like to do is add a semantic that the compiler (or Dialyzer, but would prefer this be a compiler check, tbh) be aware of what functions are pure and which are not. The way I would do this is by using a different “arrow”, in particular the Prolog-style declaration indicator: :-

[Edit after further discussion…] What I would like to do is add a directive that Dialyzer can interpret according to a simply purity rule. Adding this to Dialyzer makes more sense than putting it in the compiler — Dialyzer is already concerned with checking; the compiler is already concerned with compiling.

The directive would be -pure(Name/Arity) (a compliment to -spec). The rule would be very simple: only guard-permissible BIFs and other pure functions are legal from within the body of a pure function. This is basically just an extension of the current guard rule (actually, I wonder why this version isn’t already the guard rule… other than the fact that unless something like this is implemented the compiler itself wouldn’t have any way of checking for purity, so currently it must blindly accept a handful of BIFs known to be pure and nothing else).

For example, here is a pure function in Erlang, but neither the compiler nor Dialyzer can currently know this:

-spec increment(integer()) -> integer().
increment(A) ->
    A + 1.

Here is the same function declared to be pure:

-pure(increment/1).
-spec increment(integer()) -> integer().
increment(A) ->
    A + 1.

Pretty simple change.

“ZOMG! The whold standard library!” And yes, this is true — the whole thing is out. Except that the most important bits of it (the data structures like lists, dict, maps, etc.) could be easily converted to pure functions with little more than changing -> to :- adding a single line to the definition.

Any pure function could be strongly typed and Dialyzer could adhere to strong types instead of looser “success types” in these cases. Some code that is currently written to take an input from a side-effecty function, pass it through a chain of non-returning and possible side-effecty functions as a way to process or act on the value, and ultimately then call some side-effecty final output function would instead change to a form where the side-effects are limited to a single function that does both the input and output, and all the processing in-between would be done in pure functions.

This makes code inherently more testable. In the first case any test of the code is essentially an integration test — as to really know how things will work requires knowing at least one step into side effects (and very often we litter our code with side-effects without a second thought, something prayer-style monadisms assist greatly with). In the second case, though, the majority of the program is pure and independently testable, with no passthrough chain of values that have to be checked. I would argue that in many cases such passthrough is either totally unnecessary, or when it really is beneficial passing through in functions is not as useful as passing through in processes — that is to say, that when transformational passthrough is desired it is easier to reason about an Erlang program as a series of signal transformations over a message stream than a chain of arbitrarily side-effecty function calls that collectively make a recursive tail-call (and that’s a whole different ball of wax, totally orthogonal to the issue of functional purity).

Consider what we can know about a basic receive loop:

loop(State) ->
  receive
    {process, Data} ->
        {ok, NewState} = do_process(Data, State),
        loop(NewState);
    {send_state, From} ->
        From ! State,
        loop(State);
    halt ->
        exit(normal);
    Message ->
        ok = log(unexpected, Unexpected),
        loop(State)
  end.

-spec do_process(term(), #state{}) -> {ok, #state{}} | {error, term()}.
do_process(Data, State) :-
    % Do purely functional stuff
    Result.

-spec log(category(), term()) -> ok.
log(Cat, Data) ->
    % Do side-effecty stuff
    ok.

We can see exactly what cases result in another iteration and which don’t. Compare that with this:

loop(State) ->
  receive
    {process, Data}     -> do_process(Data, State);
    {send_state, Asker} -> tell(Asker, State);
    quit                -> exit(normal);
    Message             -> handle_unexpected(Message, State)
  end.

do_process(Data, State) ->
    % Do stuff.
    % Mutually recursive tail call; no return type.
    loop(NewState).

tell(Asker, State) ->
    % Do stuff; another tail call...
    loop(State).

handle_unexpected(Message, State) ->
    ok = log(unexpected, Message),
    % Do whatever else; end with tail call to loop/1...
    loop(NewState).

I like the way the code lines up visually in the last version of loop/1, sure, but I can’t know nearly as much about it as a process. Both styles are common, but the former lends itself to readability and testing while the latter is a real mixed bag. Pure functions would keep us conscious of what we are doing and commit our minds in ways to the definite-return form of code where our pure functions and our side-effecty ones are clearly separated. Of course, anyone could also continue to write Erlang any old way they used to — this would just be one more tool to assist with breaking complexity down and adding some compile-time checking in large systems.

I would love to see this sort of thing happen within Erlang eventually, but I am pretty certain that its the sort of change that won’t happen if I don’t roll up my sleeves and do it myself. We’ve got bigger fish to fry, in my opinion, (and I’ve certainly got higher priorities personally right now!) but perhaps someday…

Rich Hickey: Simple Made Easy

This is an excellent presentation of the “soft tech talk” type given by Rich Hickey about the difference between a thing being “easy” in the sense that it is near to our experience and it actually being “simple” in the sense that understanding the idea does not force us to understand a lot of other ideas that are partially folded into it. His point is that we should be careful about the way we use the words “easy” and “simple” and be rather more specific than we tend to be, because this hides up a rather large and common class of complexity problems that we tend to gloss over within programmer society because we are merely familiar with a huge class of unnecessarily complicated constructs that have nothing to do with the business problems we are supposed to be solving.

Rich Hickey: Simple Made Easy

If only it were glaringly obvious to us when we were engaging in the glossing-over of non-essential complexity in architecture. I suppose one way of at least trying to become conscious of this is the frequency of use of a certain alarm-bell phrase: “just”. Ward Cunningham’s wiki actually has a whole page on it, along with another page that enumerates and discusses/argues a few other linguistic red flags of note.

Its a Small, Small World

A question on the Worldbuilding SE site about military contractors / mercenaries / “evil henchmen” / whatever caught my attention and I responded. A discussion started between myself and another guy who has contracted before in the same places, but on the tech side of the business instead of PSD. I’m preserving the conversation here because it illustrates the point I was trying to make in my answer, but doesn’t really fit within the answer and will certainly be deleted by the mods:


@zxq9 I’m impressed by your answer. I worked as a contractor as well, and ran a bar at night in one country (Middle East). Lots of the guys I worked with were former Ranger / SF. I did tech instead of PSD, but some jobs we were responsible for our own security, travel, etc. All the guys I saw ended up going back to the States (or the Philippines…) after contracts; nobody really hung out in theater after jobs. Are those little communities you’ve referred to really that common? Because I’ve yet to see people hang around after the fact.
hathead 15 mins ago

@hathead If you were ever in Baghdad you might remember that along the main E-W road between the traffic circle and Triple Canopy’s base Olympia was a mish-mashed neighborhood with a few shops and restaurants, and a couple of smaller contract company offices working out of houses. (Near the “Hot Tomato” restaurant that was always weird.) That neighborhood is exactly the sort of spot I’m talking about. Kabul has something similar. I think most of that may have been invisible to you if you didn’t work PSD or base def (and therefore have your own wheels) — but its there. Similar in Kampala.
zxq9 10 mins ago

@zxq9 – I remember the neighborhood but didn’t spend a lot of time out there. People bought alcohol there. I worked on Bes-maya, I guess you know where that is. My glory days were in Kabul, though. There we all lived on the economy and would pop into the embassy / base to do work, or speed out to Bagram. Kabul was always weird- you can get whatever you needed and half the guys at the bar were wanna-be journalists. Tourists even popped in sometimes. This neighborhood you’re talking about in Baghdad – it seemed like it was within the confines of base (checkpoint controlled). Was it not?
hathead 7 mins ago

@hathead I also forgot to mention… There are contracts in the southern Philippines, Indo, Malay, Cambodia and Thailand , too — and there are two SF association houses (the front half of which are really bars, like Garfield’s in AC and Tilac II in Pattaya). The neighborhoods near there are full of older SF guys who married locally and never left, and sometimes its easy to source logistics or meet the right folks to get a crew together locally. Pace of life is a bit slower there, but the shape of things is fairly similar — but not everyone wants to have anything to do with contracting.
zxq9 6 mins ago

@hathead The neighborhood wasn’t, but right next to it was the former finance minister’s house and apartments for his wives — which was turned into a business development center. The edge of it was right on the NW edge of the circle and had walls + a guard force from TC (which I was in charge of for 6 months once). That place gave the impression the neighborhood was controlled, but it was just that one facility. The volume of alcohol trade there always surprised me! A lot better than the “parts cleaner” thing the Poles had going on down in al Kut.
zxq9 3 mins ago   edit

@zxq9 – half the ex-Army guys I worked with had houses in the Philippines. You probably know some of the same guys. It’s not easy getting work as a tech with a PMC but I managed to find it with a big name (closed now, but you’d know them) and it was a lot of fun. Hanging out in Turkey now. It’s not hot enough yet that a worthwhile expat community has established. Yet.
hathead 2 mins ago

@zxq9 – The Europeans had fine alcohol, it was just a matter of finding it. They had a bar on Liberty that was wild. Panties stapled to the ceiling. I only saw it once; kept out of that sort of thing mostly. Did brew my own beer, though.
hathead 1 min ago

@hathead Likely do know some of the same folks. They’ll put this thread in chat or delete it pretty soon. My url is the same as my alias here: zxq9.com . Drop me a line. Turkey is an interesting place right now, too!
zxq9 46 secs ago

@zxq9 – I’ll drop you a line, and delete my previous chats if I’m still able.

The Yuan: Stealing from Piers to pay 保罗

So, indeed the Yuan was made into a reserve currency and the link-by-failure is already being established. It is interesting, though, that the bulk of the value transfer involved is coming from the Euro, not the Dollar or Yen. Linked by the IMF formally or not, though, if either the Euro or the Yuan fail over the mid-term the other will as well. The Chinese and Eurozone economies are intimately linked already, but were linked more by success than failure until now. That the failure of either is a very real possibility is too terrifying for the financial press to discuss, I think, and it is a political landmine public figures are trying very hard to avoid mentioning. It even seems that the made-up nature of Chinese government economic estimates isn’t even in the news much these days. You would think that little detail might enter into the discussion about adding a new currency to the IMF’s reserve currency group.

There is no longer a strong relationship among basic aspects of value assignment, legal ownership, practical control (that is, “real ownership”), vested business interest in terms of the performance value of concerns, and available goods and services in either the Eurozone or Chinese markets. While there are no recipes for economic success, there are several recipes for disaster (ask an economist about this — their responses tend to be as enlightening and humorous as they are depressing on reflection). A lack of correlation between various forms of utility values and assigned values is one of the disaster recipes. There is no easy way to fix this other than a kinetic re-establishment of property rights, and that means there is nothing left to do in the current situation than hope that when they do fail, they fail cleanly. But historically there is no such thing as a clean failure (in theory, of course, all sorts of lovely solutions exist).

Dropping an anvil on the overloaded camel’s back in the Eurozone or China would be rather easy at the moment, as both economies are in precarious situations. In fact, inducing a major market collapse would be so easy right now that failure is almost certain to come as the result of a deliberate action from an external player than by mere circumstance. The more players who realize this is true the more likely such an action becomes: why let a failure happen to you when you can be the one making it happen if the event is inevitable?

In describing the European and Chinese economic situations a financial analyst friend of mine used the phrase “poised to fail” (along with a lot of depressed-looking facepalming). When someone says that to a geopolitical analyst, though, ears perk up. There is always opportunity to be found in crisis, and sometimes when crisis is inevitable the best play is to be the cause of it yourself, because then you are the only one truly prepared. Consider the economic fallout of Russia’s invasion of Georgia in August 2008. A similar performance is absolutely not out of the question, nor is having some “terrorists” conveniently demonstrate the peaceful nature of some religion all over a ship in the Strait of Malacca at a perfectly horrible moment.

2007-2010 SPY chart

Rhetoric forces us to pretend that the August invasion of Georgia did not trigger a reassessment of risk in Eastern European carry trade loans, and instead believe that the already liquidated American subprime loans acted as a magical “contagion” that unfairly crushed the Eurozone. As if the European economies were not profoundly overleveraged and primed to implode.

There really isn’t anything to do about what is going on with the Yuan, really. This course was set about 20 years ago (yes, all the way back in 1995 — after the Cold War, after the first post-Tienanmen Square Five Year Plan was in action; as China started on its “Money is Good” -> “Expansion Above All” -> “Don’t Stop the Train” -> “WTO Rules? Screw the rules, I have money!” chain of policies). The general trend will continue, as none of the players seems to have any inkling of how to change the rules of the game — and the trend is of the end of a decades-long political and financial cycle. The way these stories end is never happy.

1984-2015 Money Base chart

Anyone who thinks that events since 2008 have been business as usual and that geopolitics plays no part in this because “its just a market hiccup” is deluded.

But!

Every end is a new beginning, and that’s what is really worth focusing on. That may sound like small comfort (and it is), but if you already know things are going to get worse before they get better, then at least you won’t find yourself sleeping in a bed of broken dreams. It is too soon to tell which way this Jenga tower is going to topple, but we are nearing the end of this round of the game.

China: Yuan Will Be a Reserve Currency, Come What May

EDIT: Indeed, it has been made a reserve currency, or at least it looks like announcements have already been made to pave the way.

The IMF is considering adding the Yuan to the group of reserve currencies. That would put it alongside the U.S. Dollar, the Japanese Yen, the English Pound, and the Euro in terms of “officially perceived” stash-your-value-here viability. As far as actual criteria for inclusion go, the Canadian Dollar, Australian Dollar, and very likely the Russian Ruble are probably actually closer to being genuine reserve currency material than the Chinese Yuan.

But… politics.

China is much closer to a total financial collapse and internal civil disruption* than recovery and stability in its current form. Long-term, of course, China will still be right where it is and the people there will still be Chinese (but there will eventually be far fewer of them, at least for a few generations). A Chinese collapse right now would be a major disaster for everyone. The commodity markets are depressed more than they have been for several decades (in relative terms, actually, I’m not sure that we actually have a post-WWII precedent for what is happening), energy is cheap, credit is massively overleveraged, and yet people aren’t buying enough stuff to keep the wheels spinning.

What does that have to do with the Yuan becoming a reserve currency? It does three things:

  • Gives China access to an external aggregate value device to prop up the yuan if necessary (links their economy to everyone else’s by failure, similar to the way subsidies can do this within a national economy). This effect is actually more a hoped-for psychological effect on the market than a tangible superpower China is being granted by the rays of a yellow sun.
  • Makes the Yuan a necessary holding for anyone trying to carry a balanced basket of reserve currencies (temporarily spikes demand for the Yuan).
  • Promotes an impression of stability in the Yuan (well-founded or not).

Why would the West agree to this? (And I say “the West” because, let’s face it, Washington and London are pretty much the ones who will be deciding.) Because if China were to fail right now it would be a severe annoyance for the U.S. and a complete disaster for Europe and Russia. Nobody really knows what the fallout of that would be, but it wouldn’t be pretty.

The Yuan will be made a reserve currency, whether it makes sense or not, and whether it actually fits in the reserve currency club by the standards and rules the IMF itself has laid out. These are scary times and nobody has any good levers to pull to “fix the economy” so national governments and central banks are pulling at straws because there is simply nothing left to try. All the control rods have been yanked out and tossed already, or shoved in and locked tightly; all the red buttons have been mashed; all the hyperbolic rhetorical devices have been so over-used at this point that the only thing that might actually influence market participants is a frank exposition about the truth rather than more “we’ll do whatever it takes!” and other gung-ho, “it’ll work this time” and “this is the lastest of the last rounds of QE, and this time it will really be the most effectivest of effective measures… I promise!” blather.

[* China is due for two painful corrections which will likely occur together, as they are linked. The first is a political correction; China’s geography does not lend itself to a central command economy. The second is a property-claims correction; when basic goods cannot be had at any price it means the entire system is so out of whack due to government interventions that only a hard reset can fix things. This will likely take the form of a civil war, but who knows. It could be gradual decline toward state failure followed by a logical and non-violent nation-wide roundtable discussion, or even a bloodless revolution coupled with a voluntary capitulation of material holdings by the power elites. But seriously, this has never happened in history and there is no reason to expect China’s inevitable transitions to occur independently of one another, or for either to be non-violent.]

Bad Art: Ahh! Beer! (Self Portrait)

This is the only self-portrait I’ve ever done. It is from a nice memory I have of taking a trip to Hokkaido with my then-girlfriend (now wife) between contract jobs. If you’ve never been, I strongly recommend Hokkaido in late summer — beautiful people, delicious food, lovely driving.

As with other images, these progress from raw images of the pencil sketch to some image work in gimp to blend lines, smudge things up, etc.

AhhBeer1-s

Raw sketch image.

After some chunky background removal…

AhhBeer2-s

Sketch after some chunky cropping.

After some closer line cropping…

AhhBeer3-s

Sketch after some more detail removal.

Touch up the outline just a bit more…

AhhBeer4-s

Sketch after some closer outline work.

Now to start on the line removal and smudging…

Sketch after some initial smudge work.

Sketch after some initial smudge work.

Forgot the darn letters. Working around written text is not easy sometimes.

Sketch after some more detail work on the letters.

Sketch after some more detail work on the letters.

Now to brighten things up a bit.

AhhBeerC3-s

Sketch after brightening.

And done!

This one came out better than some other attempts I’ve made — hence the only self-portrait that survives. I’ve never tried a real portrait-style sketch of myself. Its not like the world is missing out on anything.

Below are a bunch of in-progress screenshots of this image as I edited it. Nothing magical, its more a testament to the amount of time I had on my hands when I did this — I didn’t have a Wacom tablet or anything. Actually, I don’t think I ever had a mouse. If I remember correctly I did all this editing using a touchpad.

If I did have a Wacom tablet I would probably still draw some. But meh, the world is just chock full of supremely gifted and properly trained artists. I only did these because they were fun.

Screenshot-5 Screenshot-6 Screenshot-7 Screenshot-8 Screenshot-9 Screenshot-10 Screenshot-11 Screenshot-12

Bad Art: The Natsumi

I did a sketch and cleanup of a larger view of Natsumi. Hopefully she won’t get mad if she sees this (I’m married to her now!).

As in other images I touched up, I’ve put the intermediate stages here, beginning with the raw photo of the sketch, moving through stages of touch-up in gimp.

summersea1-s

Raw sketch image.

Make it look like the outline of an 8-bit game character…

Sketch with big chunks of the background removed.

Sketch with big chunks of the background removed.

A bit more detailed outline work…

summersea3-s

Sketch after detailed outline removal.

After getting a bit of the easy smudge work out of the way…

summersea4-s

Sketch after some early smudge work.

Now a bit more detail work…

summersea5-s

Sketch after some detail smudge work.

And some brightness/contrast work…

summersea6-s

The sketch after some brightening.

It appears I stopped here. I never did refine the hard pencil outlines. Meh.