
Redundancy and Power - maxwell
http://www.paulgraham.com/redund.html
======
jganetsk
_Unfortunately, Joel is so provincial that he doesn't know how to spell
"dynamic typing", spelling it "variants"._

Paul Graham is so provincial, he doesn't know that "variants" is one of the
most powerful features of static typing.

[http://en.wikibooks.org/wiki/Haskell/Type_Declarations#data_...](http://en.wikibooks.org/wiki/Haskell/Type_Declarations#data_for_making_your_own_types)

[http://caml.inria.fr/pub/docs/manual-
ocaml/manual003.html#s:...](http://caml.inria.fr/pub/docs/manual-
ocaml/manual003.html#s:tut-recvariants)

~~~
cchooper
Not quite the same meaning of 'variant'.

~~~
jganetsk
Oh, I see... we're talking about Visual Basic: "Dim X as Variant".

Well then, Paul Graham is right. That is a provincial spelling of "dynamic
typing".

Incidentally, I don't think variants in the Hindley-Milner sense of the word
get nearly enough love from hackers. And, in fact, they _do_ correspond to
dynamic typing.

data Dyn = I Integer | S String | D Double | F Dyn -> Dyn | ...

And now you can use your statically typed language as a dynamically typed one.

~~~
cchooper
But the language will still be statically typed, as the correct resolution of
a Dyn into an Integer, String etc, will still have to be ensured at compile
time.

~~~
jganetsk
No, it will be ensured at run time, as all unboxing happens through pattern
matches (which, btw, is EXACTLY what happens with a dynamically typed
language).

For example, I can define...

    
    
      (+) :: Dyn -> Dyn -> Dyn
      (+) (I x) (I y) = I (x + y) -- integer addition
      (+) (S x) (S y) = S (x ++ y) -- string concat
    

So I can now use the + operator on variables of type Dyn. If a value has the
wrong tag, then a pattern matching exception is thrown. Once I get enough
primitive operations defined, I will never really have to manually unbox a
value of Dyn type again.

The one difficulty is emulating objects, because we would not have good syntax
for arbitrary method calls. We could take a Smalltalk-like or Erlang-like
approach, where the object takes message... the message being of type Dyn.

We should really look at dynamically typed languages as subsets of statically
typed languages. In other words, they are not untyped, they are _unityped_. In
other words, statically typed, but with exactly one type. This is the
relationship between typed and untyped lambda calculus (untyped lambda
calculus is actually typed, where every term has type Mu X. X -> X)

~~~
cchooper
This is getting into details of semantics, but I'm not sure if your example is
really shows that the _language_ supports dynamic typing. It's more like an
example of implementing dynamic typing in a static language. After all, you're
not using the language's type system to do run-time type checking. You're
using run-time pattern matching as a substitute.

Of course, I'm just being pernickety, but that's how I am :)

