Mark Tarver
1/24/2016 6:13:00 PM
On Saturday, January 23, 2016 at 10:12:21 AM UTC, Jim Newton wrote:
> I was discussing this week whether the Python language is sufficiently flexible to implement
> a CL-like type calculus. It looks like it almost is, but since I'm not a Python expert, I can't say
> whether it's impossible.
>
> I did a few searches to see if anyone had done this already and didn't find anything.
>
> Does someone know of an attempt, either successful or unsuccessful, or is any comp.lang.lisp
> reader familiar enough with CL and Python to comment on this possibility?
>
> I'm also happy to explain why I want to do this if anyone is interested in knowing.
>
> Here's what I've found out so far: (sorry that my Python-speak is not correct and I mix up some terms)
>
> There is a type in Python called "type", and I (the programmer) can create a subclass of it named combo_type,
> and then proceed to make or_type, and_type, not_type and empty_type subclasses of combo_type.
> The methods corresponding to the CL typep and subtypep are available can be overwritten on these classes. E.g., if A is an instance of or_type (corresponding to (or int string), one would like to ask
> are int and (and A string) subclass/superclass of A.
>
> The thing that seems to be missing is the following.
>
> A class knows is superclasses in Python but apparently not its subclasses.
> This might be related to the feature that classes can be local to lexical scopes, not really sure the motivation, that's just my guess.
>
> In order to figure out whether the intersection of two types is empty, the algorithm needs to trace down the
> type lattice from both types in question to find whether the paths intersect before arriving at the empty type.
> This sounds impossible to me in Python.
>
> I'm happy to hear anyone's thoughts.
Several years ago Shen was implemented in Python independently by two programmers - a Python version by Ramil Farkshatov is available from www.shenlanguage.org. A CL version is also available there too. The Shen type system is programmable to a high degree. I've implemented a compiler from ML style algebraic datatypes definitions in 60 lines. You might look at Ramil's work.
Mark