Live data from Hacker News

Exotic Programming Ideas: Module Systems

stephendiehl.com

51–57 of 57 posts

Re: Exotic Programming Ideas: Module Systems

#51
post #49

Coming in without much OCaml experience, I don't really think this is a great demonstration of why this construct has value. I don't really want to read a long form description of the OCaml implementation of modules. I want a comparison to the languages he dismissed at the beginning of the article, and a discussion of why this feature has some value that isn't provided by those languages. Basically - This feels like…

It's absolutely a different approach to generics. Or, rather, that's the ringer. I want to say first: OCaml's take on modules is just a really nice way of doing namespacing as well. Secondly, generics depend upon (a) having a means to discuss functionality which abstracts over one or more types and certain behaviors those types must support, (b) having a means to bundle up one or more types along with some behaviors,…

I love this example. I have a feeling that I'll be borrowing it often in the future.

Re: Exotic Programming Ideas: Module Systems

#52
post #45
post #32

Earlier quoted context omitted.

This makes it seem like modules are strictly inferior, if you can't assign names to common requirements.

You can still give a name to the interface. However, interfaces apply to modules, not to types. In Ocaml you'd say "this module implements the Comparable interface" while in an OO languague you'd say "this type is a subtype of Comparable". Sorry for the confusion.

Ah, thanks for the clarification.

Re: Exotic Programming Ideas: Module Systems

#53
post #44

Earlier quoted context omitted.

Not possible, but an approximation: interface Mappable , T> { flatMap (f: (x: T) => Mappable ): Mappable ; } class Maybe implements Mappable , T> { x: T | undefined; public flatMap (f: (x: T) => Maybe ): Maybe { if (this.x) { return f(this.x); } return Maybe.nothing(); } }

Yes, in fact this reminds me of the HKT implementation[1] found in fp-ts[2][3] interface HKT { _URI: F _A: A } interface Mappable { map (f: (a: A) => B, fa: HKT ): HKT } where F is a unique identifier representing the type constructor and A its type parameter. [1]: https://www.cl.cam.ac.uk/~jdy22/papers/lightweight-higher-ki... [2]: https://github.com/gcanti/fp-ts [3]: https://gist.github.com/gcanti/2b455c5008c2e1674…

gcanti’s work in Flow and Typescript is amazing and I use it daily :)

Re: Exotic Programming Ideas: Module Systems

#54
post #49

Coming in without much OCaml experience, I don't really think this is a great demonstration of why this construct has value. I don't really want to read a long form description of the OCaml implementation of modules. I want a comparison to the languages he dismissed at the beginning of the article, and a discussion of why this feature has some value that isn't provided by those languages. Basically - This feels like…

It's absolutely a different approach to generics. Or, rather, that's the ringer. I want to say first: OCaml's take on modules is just a really nice way of doing namespacing as well. Secondly, generics depend upon (a) having a means to discuss functionality which abstracts over one or more types and certain behaviors those types must support, (b) having a means to bundle up one or more types along with some behaviors,…

All the languages you mention have parameterized types, so I don't see why anyone would be tempted to use subtyping rather than generics. The only reason I could see is wanting to parameterize at runtime, but it's not immediately obvious to me that graphs with runtime parameterized edge and nodes are something you'd want on a regular basis. Am I missing some subtlety?

Re: Exotic Programming Ideas: Module Systems

#55
post #54
post #49

Earlier quoted context omitted.

It's absolutely a different approach to generics. Or, rather, that's the ringer. I want to say first: OCaml's take on modules is just a really nice way of doing namespacing as well. Secondly, generics depend upon (a) having a means to discuss functionality which abstracts over one or more types and certain behaviors those types must support, (b) having a means to bundle up one or more types along with some behaviors,…

All the languages you mention have parameterized types, so I don't see why anyone would be tempted to use subtyping rather than generics. The only reason I could see is wanting to parameterize at runtime, but it's not immediately obvious to me that graphs with runtime parameterized edge and nodes are something you'd want on a regular basis. Am I missing some subtlety?

Parameterized types can help here a lot. I didn't want to speak to them too quickly so I blurred a few lines, but it's a good point.

Parametric types help with part (a) by allowing you to specify only part of the structure of your type. That can help enormously, though they also force some amount of concretion in your type which isn't always good. Ultimately, OCaml's module system is pointed in the direction of ad hoc polymorphism where you pass in behavior with your abstracted types.

Subtyping supports this passing of behavior as it lets you specify a whole space of types abstractly. In that way, it's a little more supportive of the pathway to ad hoc polymorphism.

Re: Exotic Programming Ideas: Module Systems

#56
post #46
post #40

Earlier quoted context omitted.

Seems like something covered by typeclasses in Haskell, right?

Seems like it, but typeclasses are inherently anti-modular, they provide a globally coherent unique instance. consider the Ord typeclass giving a single ordering for a specific type. To reverse the order you need to create a new type with a new instance of ord which reverses it. Where in a module system its perfectly fine to have multiple instances of Ord for a given type. One gives global consistency where the other…

When you put it like that, modules sound more powerful than typeclasses. Is there any situation where typeclasses are superior to modules?

Re: Exotic Programming Ideas: Module Systems

#57
post #56
post #46

Earlier quoted context omitted.

Seems like it, but typeclasses are inherently anti-modular, they provide a globally coherent unique instance. consider the Ord typeclass giving a single ordering for a specific type. To reverse the order you need to create a new type with a new instance of ord which reverses it. Where in a module system its perfectly fine to have multiple instances of Ord for a given type. One gives global consistency where the other…

When you put it like that, modules sound more powerful than typeclasses. Is there any situation where typeclasses are superior to modules?

I think the extra power comes with cognitive overhead, I.e. operators are now relative to some module, and you're stuck with that overhead whether or not you actually use the power or not.

Trying to stay neutral, I think its no suprise that some people prefer typeclasses...

Post reply on HN