News for hackers. Stuff that matters.
Re:Pyre: Fast Type Checking for Python by joshuamorton on Tuesday May 15, 2018 @01:58AM (#17071946)
>It's not really any differrent (again, if you dscount the privileged syntax) than using requests instead of the mess that is urllib, or any other instance of "that API is terrible, let's use a different library."

Sure, but all you've really done is defined a new type system distinct from that of JS or python [1]. I'll agree your type system is different from JS's. Again, the fact that you can write a new language, with different semantics, within JS is not and should not be surprising.

>Run-time checking is a requirement to avoid coercion (assuming you also don't have static checking)

Ish. Duck typing avoids both of these, unless you consider the extreme that the existence or lack of any specific method defines an interface, and every object implements some subset of these interfaces, but that's not a particularly useful abstraction.

That is to say `try: lhs.method(rhs.value), except: TypeError` avoids both coercion and type checking.

>I agree. And my argument as to why strong-typing is not a feature of python-the-language is that it doesn't provide any declarative way for me to extend that property to my own interfaces; if I have some higher-level interface that doesn't fall right out of the existing Python libraries' interfaces, I have to do all of the same kind of work that I did in the above snippet of Javascript.

Huh? I'm going to challenge you to give an example, because I don't think you'll be able to.

>I think the concept of strong vs. weak typing that you're describing is coherent, but only (a) as a property of libraries, not languages, which is my argument, or (b) you assert that the built-in syntax is central. I think the latter is defensible.

To be clear, (a) is a valid interpretation, but only if you consider the base `Object` type to be a library (or more broadly, the base type). This isn't a realistic assumption. As soon as you write you're own object, you've created a new language with similar syntax but distinct semantics. Basically yes, its absolutely possible to implement a strongly typed language within a weakly typed language. But its also possible to implement a statically typed language within a dynamically typed one. That doesn't mean that the outer language isn't weakly typed. It is. You're just able to implement a stricter DSL in it, which again.

As a similar example, you can write Java code where everything is declared as Object. This doesn't make Java dynamically typed. It does however make your bit of code not statically checked. Similarly, you can write stuff within JS that is, within a fence, strongly typed, ish. You have to stop using a lot of the language's built in syntax and methods, so are you really writing JS any more? The syntax is similar, but the semantics are not.

But there are a lot of languages with similar (or even identical) syntax and different semantics. Python2 and Python3 are a good example.

[1]: https://joshuamorton.github.io/2015/10/04/types/

Re:Pyre: Fast Type Checking for Python by zenhack on Tuesday May 15, 2018 @02:20PM (#17076305)
> But its also possible to implement a statically typed language within a dynamically typed one.

Not as a simple set of library functions that you import. You need to actually write an offline tool, because if the checking is done by library code then it's already too late -- the program is already running. Contrast the implementation above which is just a function, and still composes with the rest of the language without any assistance, and doesn't do anything that's terribly out of the ordinary for a function to do. Static typing is a language property, strong typing is a library property.

> As soon as you write you're own object, you've created a new language with similar syntax but distinct semantics

This seems like a uselessly broad criterion for what constitutes a language. If you're just using the same everyday mechanisms you do writing any program, I don't think you can claim you're using a different language, without diluting the meaning of the term beyond all utility.

> I'm going to challenge you to give an example, because I don't think you'll be able to.

The trivial example is keeping units straight. Merely having an attribute __mul__ does not not adequately capture the interface of multiplication in the presence of units. Unless I specifically write the kind of boilerplate with a bunch of checks like my JavaScript example, it will just silently do the wrong thing.

In some sense Python's object does satisfy every interface, with a default implementation of throw AttributeError/TypeError. This may seem trivial, but it is important in that overriding one of these isn't different from overriding valeOf, in order to get a different implementation for your object (one that throws).

Re:Pyre: Fast Type Checking for Python by joshuamorton on Tuesday May 15, 2018 @03:35PM (#17077077)
>Not as a simple set of library functions that you import. You need to actually write an offline tool, because if the checking is done by library code then it's already too late -- the program is already running. Contrast the implementation above which is just a function, and still composes with the rest of the language without any assistance, and doesn't do anything that's terribly out of the ordinary for a function to do. Static typing is a language property, strong typing is a library property.

You're, probably unintentionally, setting your requirements a bit differently. As an example, Tensorflow is a statically checked DSL within python. You create a tensorflow graph, and (unless you're using the relatively recent dynamic graph features), that graph is statically checked for validity. Python is the virtual machine in which Tensorflow is written, but within the dynamic python VM, Tensorflow is a statically checked language. Basically, what happens is

1. python runs, parses the tensorflow graph and creates an AST

2. the AST is statically checked by tensorflow. This happens before tensorflow really does anything else. Errors like tensor shape mismatches are raised here, instead of the first time you actually attempt to multiply tensors of mismatched shape. It doesn't use runtime information about the types, it isn't dynamic checking. Its very much static checking, but static checking that happens after python runs, but before tensorflow runs.

3. You execute the now checked graph. Errors can still appear here, but you have avoided some via static analysis.

Similarly, you can create a strong language in the VM of a weak language. That doesn't suddenly make the weak language strong.

Neither static typing nor strong typing is solely a property of the language or ecosystem. It comes as a combination of both. I personally like to keep this distinction clean by calling DSLs that break core properties of the parent language different languages, because that's what they are.

>If you're just using the same everyday mechanisms you do writing any program, I don't think you can claim you're using a different language, without diluting the meaning of the term beyond all utility.

When you define a new object system with distinct mechanics, you aren't using the same mechanisms you do to write any program. They're no longer compatible with the rest of the language.

>The trivial example is keeping units straight. Merely having an attribute __mul__ does not not adequately capture the interface of multiplication in the presence of units. Unless I specifically write the kind of boilerplate with a bunch of checks like my JavaScript example, it will just silently do the wrong thing.

This has nothing to do with strong or static typing. Its yet a third axis on which a language can vary. But to be clear, if you define things correctly, managing units like that doesn't actually require much boilerplate. You create a Unit interface and 7 BaseUnits impls for each of the SI base units. Then within the bounds of Unit * Unit (or Unit * number) multiplication, the interface does indeed correctly capture multiplication in the presence of units. There's never any need to do the casework like you're describing, and the more you go down this tangent the more it seems like you have misunderstandings about core Object Oriented principles.

    Meter = Unit(['m'], 1)
    ...
    Kilometer = Meter * 1000
    KiloNewton = Kilogram * Kilometer / Second ** 2
    f = KiloNewton(12)
    f.unit  # kg * m / s ** 2
    f.magnitude  # 1200
The only "cases" are handling Units, which have a magnitude and a unit, and non-unit numbers, which have only a magnitude. As long as the base Unit class knows how to do that, you have one bit of casework, which has two options:

    class Unit:
        def __mul__(self, other):
            if isinstance(other, Numeric):
                return super(self.units, self.magnitude * other)
            if other.units:
                return super(self.units + other.units, self.magnitude * other.magnitude)
            raise TypeError
I'm simplifying self.units + other.units, but that's mostly irrelevant.

The point here is that you're defining a new interface, Unit, which is able to do what it wants. But in a JS world, if I tried to multiply my Unit by 12, I'd lose the unit.

>In some sense Python's object does satisfy every interface, with a default implementation of throw AttributeError/TypeError. This may seem trivial, but it is important in that overriding one of these isn't different from overriding valeOf, in order to get a different implementation for your object (one that throws).

Well, no. If you're claiming that the action of saying "I explicitly do not satisfy this interface" is, in a sense, a way of satisfying an interface, we've reached the point where you're not interested in a dialogue and are instead only interested in arguing a wrongheaded perspective, and at this point a perspective that it seems you clearly recognize is wrongheaded, but wish to defend anyway.

The difference between overriding a method and overriding valueOf is that with a method, I control which specific interfaces I implement. With valueOf, its all or nothing. I cannot pick and choose which interfaces to implement. I've said this or a variant of it in nearly every post so far, and you've yet to respond to this point.

So I'll try it once more: In a strong language, each object controls which specific interfaces it implements. In a weak language, each object does not. Thus, in python, I choose individually if I want to be iterable or multipliable or a container. In JS, I do not get to decide those things. My object is a container, my object is a number (which means that it can be added and multiplied and such in ways I don't intend), and my object is a string all at once.

While the ability to overload specific operators is a symptom of Python being stronger (each operator is bound to an interface, instead of all of the operators being bound to the number interface which everything implicitly implements), it is a symptom, and not the cause.

Re:Pyre: Fast Type Checking for Python by zenhack on Sunday May 20, 2018 @07:16PM (#17115237)
> at this point a perspective that it seems you clearly recognize is wrongheaded, but wish to defend anyway.

I don't appreciate the accusation. At this point your own position is baffling to me as well, but I'm not going to make attacks; if it comes to that better to just walk away (I did this for a few days, because I was getting frustrated).

> Well, no. If you're claiming that the action of saying "I explicitly do not satisfy this interface" is, in a sense, a way of satisfying an interface

What I'm saying is that the built-in 'object' takes just this approach -- so if it does count as a way of satisfying an interface then (in both Python and JavaScript) every value satisfies every interface. If it doesn't, then user defined classes doing it isn't different from classes that ship with Python. I agree it's kindof weird to call that satisfying an interface; my claim is only that the explicit opting-out that `object` doesn't isn't any different than explicit opting-out in user code.

> The difference between overriding a method and overriding valueOf is that with a method, I control which specific interfaces I implement. With valueOf, its all or nothing. I cannot pick and choose which interfaces to implement. I've said this or a variant of it in nearly every post so far, and you've yet to respond to this point.

I similarly feel like I've been repeating myself with my response, which is not being heard:

This only matters if you're willing to treat the functions with special syntax (, / etc) as privileged. Otherwise `valueOf` is just another interface (representing coercion), and while it is definitely more poorly designed than the Python approach of having separate interfaces for each of ``, `/` etc, this only has any implications for a property of the language if you're willing to treat those interfaces (both `valueOf`/coercion and ``/multiplication, '+'/addition etc) as special. Some arbitrary function I write (in either language) doesn't (necessarily) use these interfaces. There are a fixed set of functions, imported by default, with special syntax, that use these interfaces (and of course code that calls those functions).

I think it absolutely is* defensible to claim the fact that these operations have specialized syntax makes them special in a way that's really "part of" the language, and if you take that as given, I think the rest of your argument is valid. But if you take those functions to be not really special, then behavior that only pertains to them doesn't inform questions about the language proper, just the libraries.

You've said that you agree the operator syntax is unimportant, but I'm having a hard time groking how else this is relevant.

I should also reiterate that this is a really nitty-gritty technical point I'm making; the ecosystem and libraries are the best thing about Python, so saying "it's just a library" should not be construed as "it's unimportant."

Re: Tensorflow, the resulting Tensorflow program/AST is statically typed, but it isn't a Python program, by any means. You can't put a Python while or for loop inside a Tensorflow program, call a python function, or basically do anything that's not encoded in the AST. You can use those when constructing the AST, but that's different. (Do correct me if I've misunderstood the design of Tensorflow). To use your own words

> [Tensorflow programs a]re no longer compatible with the rest of the language.

But this just isn't true of my examples where I forgo using javascript's built-in operators in favor of my own `mul`, `add` etc. functions. In the latter case, it's still JavaScript -- it can still be used with other JavaScript code in the full generality of any other functions I write.

I have some things to say about the Unit example as well, but I'm running late, so am going to stop here.

Re:Pyre: Fast Type Checking for Python by joshuamorton on Sunday May 20, 2018 @08:38PM (#17115498)
>so if it does count as a way of satisfying an interface then

Right, but it doesn't. Fullstop, end of discussion.

In python, if you don't satisfy an interface, your program immediately breaks. In javascript, it keeps on chugging, unless you do extra error handling.

Really that's all that needs to be said. You can completely ignore the rest of this post if you wish. Because there is nothing else in it that matters. That fact alone makes js weak and python strong. And note it is a property of the language, and not apis. I can absolutely write interfaces that opt out of the weakness or strongness, but that's also true of strong typing (I can write apis that only accept object, or take in go's interface {}, or whatever).

>You can't put a Python while or for loop inside a Tensorflow program, call a python function, or basically do anything that's not encoded in the AST.

This is incorrect. You can indeed have tf call out to python.

But more importantly, to use your own argument, what's a while loop but some specially favored syntax. While and for are really just operators (consider that for in python is used by fulfilling an interface exactly the same way one opts into * or +).

You seem to be arguing both sides here. That using a different set of operators makes you a different languages if those operators are looping constructs, but doesn't make you a different language if they are binary constructs.

>Re: Tensorflow, the resulting Tensorflow program/AST is statically typed, but it isn't a Python program

This is my point. Neither is your "my-own-object-system-built-ontop-of-js" a js program. Its functionally speaking a different language with different semantics. Yes, you can call out from one to the other, but you can also call out from python to C and C to java. And when you do that you lose some level of introspection because the different languages don't implement fully cross compatible internal APIs.

>it can still be used with other JavaScript code in the full generality of any other functions I write.

Well no it can't. You cannot, for example, write a binary function that is polymorphic over all possible combinations of YOURTYPE and int.