In Haskell, I write import Fruit or import Fruit (apple) and can access apple or Fruit.apple.
In Python, I can write from Fruit import apple for apple or import Fruit for Fruit.apple.
I think can also write import Fruit.apple as banana in Python to reference the same function as banana.
How, in Haskell can I do this? import Fruit as Vegetable in either language can rename Fruit, but I want to rename apple.
This is a nice property Python has because its “dictionaries all the way down,” so to speak.
Haskell allows you to assign aliases to modules, but there is no way to alias functions
from the
importstatement (as far as I know). The best you would be able to do isYou could put this in its own module and export the values you want, hiding the details of all this, but that seems like a lot of work for something so simple.
As commented below by hammar, the monomorphism restriction could cause trouble with the inferred type
of
banana. To be safe, you should either annotatebananawith its desired type (probably that ofapple), or disable the monomorphism restriction asOtherwise, the inferred type of
bananacould be less polymorphic than desired.The Monomorphism restriction attempts to assign a concrete instance of a type class for each top level function (this is done for performance reasons). Consider,
This function should have type
Monad m => m (), but due to the monomorphism restriction, there is not enough information about which Monad instance should be used, so you get the following messageNow, if you provide enough information for GHC to infer which instance of Monad you are using, such as
then GHC will give the following type
since you told it that
examplewill have the same type asmain