[Haskell-cafe] Strong duck typing / structural subtyping / type class aliases / ??? in Haskell

oleg at okmij.org oleg at okmij.org
Tue Sep 29 03:37:09 EDT 2009


Alp Mestan wrote:
> Indeed, OCaml has stuctural polymorphism, it's a wonderful feature.
>
> *# let f myobj = myobj#foo "Hi !";;
> val f : < foo : string -> 'a; .. > -> 'a = <fun>*

And Haskell has that too:

> -- This is how we define labels.
> data Field1 deriving Typeable; field1 = proxy::Proxy Field1
>
> -- This is how record selection looks like.
> foo f = f # field1

The inferred type of  foo is

	*OCamlTutorial> :t foo
	foo :: (HasField (Proxy Field1) r v) => r -> v

It doesn't seem too different from the OCaml's type; the type variable
r acts as a row type.

The quoted example is the first from many others described in
	http://darcs.haskell.org/OOHaskell/OCamlTutorial.hs

The file quotes at length OCaml's Object tutorial and then
demonstrates how the OCaml code can be written in Haskell.  When it
comes to objects, structural subtyping, width and depth subtyping,
etc., Haskell does not seem to miss match compared to OCaml. In
contrast, Haskell has a few advantages when it comes to coercions
(one does not have to specify the type to coerce to, as Haskell can
figure that out). The other files in that directory give many more
example of encoding C++, Eiffel, OCaml patterns.



More information about the Haskell-Cafe mailing list