|
|
|
|
|
by andolanra
4073 days ago
|
|
There are very much ways of expressing that computation in SML—or in Haskell or other languages—but not in the language of modules. Haskell typeclasses would have a similar problem. Consider the following pseudocode: class Map m where {- some implementation -}
instance Map HashMap where {- ... -}
instance Map TreeMap where {- ... -}
mkMap :: Int -> {- ??? -}
mkMap size | size > threshold = newHashMap
| otherwise = newTreeMap
What is the type of mkMap? It won't actually type-check in Haskell. It can't be (Map m) => m, because it needs to have a concrete type. We could do some kind of type-level work, relect the size variable into the type level, and have a type family that decides whether the resulting type is a HashMap or a TreeMap—but that has the same problem as the OCaml example, where we're using a different language (the language of type-level computation) to talk about something that'd be simple with value-level programming.One place where you could write this easily is in a dynamically typed language: def mkMap(size):
if size > threshold:
return HashMap()
else:
return TreeMap()
That is exactly why this kind of research is interesting: we'd like to capture the flexibility of idioms that dynamic languages can sometimes afford us, with the extra safety and security guarantees of a static type system! |
|