11 ms·
x = random.choice([1, "foo"]) this is given as an example of why Python could never be strongly typed. Honest question: isn't this solved using Either in Hask
by carlob 13y ago
x = random.choice([1, "foo"])
this is given as an example of why Python could never be strongly typed.
Honest question: isn't this solved using Either in Haskell?
- mattfenwick 13y agos/strong/static/
- carlob 13y agoYou're right, but Haskell is also statically typed and can be compiled, so my question still holds.
- untothebreach 13y agoRather than saying that Haskell solves this with the `Either` type, I believe it is more accurate to say that in this case, the return value of `random.choice` would be a "sum type." But the actual type would be dependent on the value passed to `random.choice`.
- exDM69 13y agoNo, using the Either type wouldn't be the same at all. Yes, you can do choice [Left "foo", Right 42] (from Data.Arbitrary, used by QuickCheck) but it's not really the same thing. The two elements in the list both have the type Either String Int, unlike the Python example. Further, an Either can have only two possible kinds of values (but no-one prevents you from writing data Either3 a b c = Left a | Middle b | Right c). You can achieve something like this with type classes, though. This is somewhat similar to requiring all the elements to fullfill an interface, kind of like List<Comparable> in Java-like languages. With existential quantification (there are other options) you can make a list where every element in a list can be of a different type as long as they derive from the same type class (like Eq, Ord, etc). The type class is still a constraint that Python doesn't have but it makes static typing possible. See the heterogenous collection link above.
- rapala 13y agohttp://www.haskell.org/haskellwiki/Heterogenous_collections http://www.haskell.org/haskellwiki/Heterogenous_collections lists some solutions
- samth 13y agoUsing Either in Haskell would require changing the program a little to add injections. But that exact program (modulo syntax) can be written in Typed Racket: #lang typed/racket (: choose : (All (A) (Listof A) -> A)) (define (choose e) (list-ref e (random (length e)))) (define x (choose (list 1 "foo"))) Unfortunately, people tend to assume that "type systems" and "the type system in Java/ML/Haskell" have the same level of expressiveness.
- sitkack 13y agoI just realized that all programs have types and type systems. Some dynamically typed programs have hairy n-dimensional fractal type systems that no human would ever figure out (usually from bad code). The inferred languages just have this dude that watches your code and says, "yeah, I understand what is going on, your stuff is internally consistent"
- samth 13y agoWell, I don't think that's correct -- it would make the term "type system" meaningless. In particular, lots of programs have bugs that type systems can catch. For more on whether all programs have types, I wrote this: https://medium.com/p/8a3b4bedf68c https://medium.com/p/8a3b4bedf68c
- sitkack 13y agoI don't think it makes the standard definition of "type system" meaningless. Maybe what I am referring to above is a "type space". I agree with some of what you said in the medium link. Python does have a type system, structural and dynamic strong. The way the plus operator works was a mistake, Lua is superior in this regard. Take a look at the type inferencing that Shedskin and RPython in PyPy can do. If there wasn't a type system, how are those 'static strong' programs created? The programer and the program has a type system where the language may not.
- samth 13y ago