Bean Machine Retrospective, part 9
Fabulous adventures in coding
by ericlippert
1y ago
I wanted to implement concise “pattern matching” in Python, a language which unlike C#, F#, Scala, and so on, does not have any pattern matching built in. Logically a pattern is just a predicate: a function which takes a value and returns true if the value “matches” the pattern, false otherwise. The code for this episode is here. When I embarked on this I realized two things. First, that it might be nice for debugging purposes to have more information in the return value than just “true” or “false”; in particular, when a complex pattern fails to match, but it should match, then I’ve made a mis ..read more
Visit website
Bean Machine Retrospective, part 8
Fabulous adventures in coding
by ericlippert
1y ago
Before getting into the details of how my combinator-inspired source code transformation system works, I should say first, what is a general overview of the system? and second, why did I build it at all? In my experience, a typical compiler’s AST rewriter does not use a combinator-based approach. Roslyn for example used the visitor pattern almost exclusively to build rewriters, and that’s pretty common. The basic overview is: we use a functional programming approach to AST transformation. A pattern represents a function from AST to bool. Is this an AST node that we’re looking to transform? A ..read more
Visit website
Bean Machine Retrospective, part 7
Fabulous adventures in coding
by ericlippert
1y ago
How do we write a compiler in a typical general-purpose line-of-business OO programming language such as Python, C#, Java, and so on? Compilers are programs, so we could make the question more general: how do we write programs? The basic idea common to almost every widely used programming language is to use composition: Divide the problem into many sub-problems Write functions that each solve one or more sub-problems Compose a solution by writing functions that call other functions The details of how those functions are organized varies from language to language of course; functions are stor ..read more
Visit website
The names of birds, part 4
Fabulous adventures in coding
by ericlippert
1y ago
The European starling is a lovely looking bird, though territorial, noisy and aggressive up close. Unfortunately, they are very invasive in North America. Most of the hundreds of millions of European starlings now living in the Americas can be found fighting over my suet feeder in winter months. Last time we said that the kestrel obeys the identity Kxy=x for all birds x and y in the forest. The starling has a slightly more complicated identity: Sxyz=xz(yz). Again, recall that this is parenthesized ((Sx)y)z=(xz)(yz). Yes I know I used S for the “successor bird” in part 2; sorry for the confusio ..read more
Visit website
The names of birds, part 3
Fabulous adventures in coding
by ericlippert
1y ago
In the autumn of last year my friend Joan and I went on a little trip up to the Skagit valley north of Seattle to photograph birds of prey; I managed to get a blurry but recognizable shot of this charming little kestrel looking for field mice. It was easily identifiable in the field because kestrels are one of the very few birds of prey that can briefly hover while hunting, almost like a hummingbird. Well, back to combinatory logic and some puzzles from To Mock a Mockingbird. We’ve said that if there are birds x and y such that xy=y, then x is fond of y, and y is a fixpoint of x. Smullyan goe ..read more
Visit website
The names of birds, part 2
Fabulous adventures in coding
by ericlippert
1y ago
Reader “Joel” had an insightful comment on the first part of this series which I thought deserved a short episode of its own. Recall that we proved the theorem “if a compositional forest contains a mockingbird then every bird in the forest has a fixpoint“. An equivalent way to state that theorem is: “if a forest contains a bird without a fixpoint then either the forest is not compositional or it contains no mockingbird“. Joel’s question was whether there is a more “intuitive” way to understand why the mockingbird is so interesting that the presence of a bird without a fixpoint should prevent a ..read more
Visit website
Bean Machine Retrospective, part 5
Fabulous adventures in coding
by ericlippert
1y ago
Let’s take another look at the “hello world” example and think more carefully about what is actually going on: @random_variable def fairness(): return Beta(2,2) @random_variable def flip(n): return Bernoulli(fairness()) heads = tensor(1) tails = tensor(0) observations = { flip(0) : heads, ... flip(9): tails } queries = [ fairness() ] num_samples = 10000 results = BMGInference().infer(queries, observations, num_samples) samples = results[fairness()] There’s a lot going on here. Let’s start by clearing up what the returned values of the random variables are. It sure looks like fairness ..read more
Visit website
Beanstalk Retrospective, part 5
Fabulous adventures in coding
by ericlippert
1y ago
Let’s take another look at the “hello world” example and think more carefully about what is actually going on: @random_variable def fairness(): return Beta(2,2) @random_variable def flip(n): return Bernoulli(fairness()) heads = tensor(1) tails = tensor(0) observations = { flip(0) : heads, ... flip(9): tails } queries = [ fairness() ] num_samples = 10000 results = BMGInference().infer(queries, observations, num_samples) samples = results[fairness()] There’s a lot going on here. Let’s start by clearing up what the returned values of the random variables are. It sure looks like fairness ..read more
Visit website
I want toast
Fabulous adventures in coding
by ericlippert
1y ago
I’ll get back to Bean Machine and Beanstalk in the next episode; today, a brief diversion to discuss a general principle of language design and congratulate some of my former colleagues. Back when we were all at Waterloo, a bunch of us were sitting around the comfy lounge, eating C&D donuts and talking about possible future user interfaces for “smart appliances”; this was in the 1990s, long before ubiquitous voice recognition, compute and networking that we take for granted today. I think it was my friend Peter who pointed out that there is a “use vs mention” problem in voice-driven home a ..read more
Visit website
Bean Machine Retrospective, part 4
Fabulous adventures in coding
by ericlippert
1y ago
Did I actually build a compiler? Yes and no. Traditionally we think of a compiler as a program which takes as its input the text of a program written in one language (C#, say), and produces as its output an “equivalent” program written in another language (MSIL, say).  A more modern conception is: a compiler is a collection of code analysis services, and one of those services is translation. I made a great many assumptions when embarking on this project. Some of the more important ones were as follows: Assumption: we must implement everything necessary for a source-to-source translation ..read more
Visit website

Follow Fabulous adventures in coding on FeedSpot

Continue with Google
Continue with Apple
OR