3 ms·
> I would like to see -- hint, I'm probably going to have to get my hands dirty! -- an implementation in either a dynamically typed language like Ruby/Python/Pe
by eric_bullington 10y ago
> I would like to see -- hint, I'm probably going to have to get my hands dirty! -- an implementation in either a dynamically typed language like Ruby/Python/Perl/PHP/Javascript/… or a non-functional language like C/C++/Obj-C/…
I understand where you're coming from. I'm working on a mini-ML in Python as a learning exercise for myself and possibly as a tutorial for others; will open source it soon.
As such, I've come across a few good resources:
check out Robert Small's Hindley-Milner in Python[0], as well as alehander42's Hermetic language in Python[1].
I also just found out about Hask, an implementation of many Haskell language features in Python. Looking at the source code, it's well-commented and clear, so I suspect I'll learn a lot from it as well [2].
Finally, even though it's not in a dynamic or non-functional language like you request, I highly recommend Andrej Bauer's Programming Language Zoo[3], which contains very simple and easy-to-understand implementations of various type systems in OCaml. Very elucidating.
0. http://smallshire.org.uk/sufficientlysmall/2010/04/11/a-hindley-milner-type-inference-implementation-in-python http://smallshire.org.uk/sufficientlysmall/2010/04/11/a-hind...
1. https://github.com/alehander42/hermetic https://github.com/alehander42/hermetic
2. https://github.com/billpmurphy/hask https://github.com/billpmurphy/hask
3. http://andrej.com/plzoo/ http://andrej.com/plzoo/
Edit: Oh, and one more resource that's been extremely helpful has been "Introduction to Functional Programming through Lambda Calculus" by Michaelson. Well worth the cost of the book.
- igravious 10y agoBrilliant, I'm attempting something very similar in Ruby :) Will check out those links. When I have something workable, I'll post it to Github as well and then we can see about creating some kind of umbrella structure? A related idea I have is that what is needed is some kind of grammar interchange format, or typing-relation interchange format -- kind of like JSON (or Amazon's ION) but for this sort of work. In some ways that is what S-expressions are, maybe there is no need to reinvent the wheel but my hunch is that a domain specific format is needed. Apologies if this is a bit vague, it's a hunch, and I'm going by intuition.
- eric_bullington 10y agoCheck out RAML: https://github.com/raml-org/raml-spec https://github.com/raml-org/raml-spec Maybe it will fit the bill? It can even encode sum types. But I'm not sure if that's what you're looking for. I'll ping you when I put my mini-ml in Python on Github.
- igravious 10y agoThat's not really what I mean. Take the file `parser.mly` from http://andrej.com/plzoo/html/miniml.html http://andrej.com/plzoo/html/miniml.html . See how it has it's own format for describing grammars. So does Yacc, so does X, Y, and Z. Is there an interchange format for parser generators?
- kragen 10y agoThere's BNF, and there's DFDL, and there's just plain lists of productions without the extra decorations in BNF, but basically no. The issue is that each parser generator in current wide use has its particular funky set of limitations that you have to hack around one way or another when you write the grammar for it: it can't handle left recursion, or right recursion, or ambiguity, except in certain special cases (maybe if it doesn't continue for more than one token, or two syntax-tree levels), or it can handle them but has potentially exponential runtime, or it just arbitrarily resolves them in a certain way which may be adequate for your needs or may require you to restructure the grammar, or whatever. And then there are lots of ways that our languages aren't quite context-free, and different parsing systems have different ways to shoehorn that into a basically context-free framework. I think it's maybe a solvable problem, but I haven't seen anyone try to solve it.
- igravious 10y agoHermetic is amusing. Hask is very like what I'm aiming towards, not PLZoo. I'm starting with a PEG library (https://kschiess.github.io/parslet/ https://kschiess.github.io/parslet/) and working my way up. I'd prefer to start with a GLL parser which could handle mildly context-sensitive grammars and work my way up but I think creating a PEG version as a proof of concept is the way to go first. Thanks for the book reference!