Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing function with an alias in Haskell

Tags:

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.

like image 999
Andrew Wonnacott Avatar asked Sep 29 '12 19:09

Andrew Wonnacott


People also ask

How do I import a function into Haskell?

The syntax for importing modules in a Haskell script is import <module name>. This must be done before defining any functions, so imports are usually done at the top of the file. One script can, of course, import several modules. Just put each import statement into a separate line.

What is a qualified import in Haskell?

A qualified import allows using functions with the same name imported from several modules, e.g. map from the Prelude and map from Data.


1 Answers

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 import statement (as far as I know). The best you would be able to do is

import qualified Fruit as F (apple) banana = F.apple 

You 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 annotate banana with its desired type (probably that of apple), or disable the monomorphism restriction as

{-# LANGUAGE NoMonomorphismRestriction #-} import qualified Fruit as F (apple) banana = F.apple 

Otherwise, the inferred type of banana could 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,

example = return () 

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 message

Ambiguous type variable `m0' in the constraint:   (Monad m0) arising from a use of `return' Possible cause: the monomorphism restriction applied to the following:   example :: m0 () (bound at Test.hs:44:1) Probable fix: give these definition(s) an explicit type signature               or use -XNoMonomorphismRestriction In the expression: return () In an equation for `example': example = return () 

Now, if you provide enough information for GHC to infer which instance of Monad you are using, such as

example = return ()  main :: IO () main = example 

then GHC will give the following type

*Main> :t example example :: IO () 

since you told it that example will have the same type as main

like image 141
sabauma Avatar answered Sep 27 '22 16:09

sabauma