3 ms·
I think this might depend on the language you're writing in. Historically, at least, it's pretty verbose to define a data type in Python compared to languages
by kragen 1y ago
I think this might depend on the language you're writing in.
Historically, at least, it's pretty verbose to define a data type in Python compared to languages that are more designed for writing compilers. Consider these definitions from my prototype Bicicleta interpreter, which is written in ML, specifically OCaml:
type methods = NoDefs
(* name, body, is_positional ... *)
| Definition of string * bicexpr * bool * methods
and bicexpr = Name of string
| Call of bicexpr * string
| Literal of string option * methods
| Derivation of bicexpr * string option * methods
| StringConstant of string
| Integer of int
| Float of float
| NativeMethod of (lookup -> bicobj)
Those ten lines of code would be ten classes in Python with an average of 1.6 attributes each. Using dataclasses or attrs, that would be 36 lines of code, and then (if you're doing it the OO way) every function that I defined on one of these OCaml types becomes a method implemented in each class implementing a particular protocol, with a copy of its argument signature in every class. (If you used namedtuple instead, it's no less code, but you write it on less lines.) So, for example, this function on bicexprs
let rec freevars = function
Name n -> stringset [n]
| Integer _ | StringConstant _ | Float _ -> stringset ["prog"]
| NativeMethod _ -> stringset []
| Literal (Some selfname, methods) ->
StringSet.diff (freevars_methods methods) (stringset [selfname])
| Literal (None, methods) -> freevars_methods methods
| Derivation(object_, self, methods) ->
StringSet.union (freevars object_) (freevars (Literal(self, methods)))
| Call(object_, _) -> freevars object_
becomes six to eight method definitions in the different classes. (You can cut it down to six if you define an abstract base class for the constant classes.) And Literal.freevars needs an if-then-else. So that's another 20 lines of code.
Python does support pattern-matching now, so functions like this might not be any more verbose than the ML version if you program them the same way instead of in the OO fashion. I haven't tried using Python pattern-matching, so I don't really know.
In general, though, Python is more verbose than ML-family languages for this kind of thing by a factor of about 2–4, and that's before you count the test code you need in Python to get the kind of confidence in correctness that ML's type-checking gives you with no extra code. To my knowledge, Mypy doesn't do the kinds of pattern-matching-exhaustiveness checks that ML compilers do.
I've sometimes "cheated" by trying to write code like this in Python using regular tuples rather than named tuples. You can definitely make it work, but it's a real pain to debug.
Quoting Andy Chu from https://andychu.net/projects/ https://andychu.net/projects/:
> Python is not the right language for [implementing] languages. I will use OCaml for subsequent projects like this.
Python does have GC and dynamic dispatch, though, and those count for a lot.
- kragen 1y agoEli Bendersky's blogpost on the Expression Problem: https://news.ycombinator.com/item?id=45155877 https://news.ycombinator.com/item?id=45155877
- sevensor 1y agoI thought this was a good read, and it’s clearly a problem in an OO language, but I struggled to see how it’s an actual problem in a functional language. Maybe I need to go read that paper he referenced by the Racket guys.
- naasking 1y agoThe example in the article was very clear I thought: try to extend an interpreter with a new primitive (function call in the article) without changing the original interpreter's source code. I'm an FP language, you can't extend the core AST without changing the source code for eval.
- kragen 1y agoIn my example above, to add a new variant type to bicexpr, you need to edit roughly every function that pattern-matches on bicexprs, which is a good fraction of the Bicicleta interpreter codebase.
- sevensor 1y agoAh, I see. I’ve written that code where you have to add one case to a whole bunch of pattern matches. It’s not terrible; I rather like that everything that does the same thing is in the same place. But I have wondered if there was a better way. Clearly I need to go read the article again.
- kragen 1y agoYeah, I share your assessment that it's not unmanageable. But it does add friction. I suspect that there are only two real solutions: - program source code that isn't a linear document but instead can be viewed in multiple different ways: by operation or by operand type, for example. Then you would use the view that best suits the modification you were doing at the time. Smalltalk, for example, does support browsing either all the implementors of a method selector or all the methods of a class. (But in both cases you normally only see one method at a time.) - Alternating levels of hierarchy. Maybe if you have 63 operations on 63 types, you could arrange it so that you can add a 64th operation or a 64th type by editing only 8 places instead of 63.