Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a quoted string in the import syntax `import "cryptonite" Crypto.Hash` mean?

Tags:

haskell

As stated in the title. There is a codebase, where I have seen the following syntax

import       "cryptonite" Crypto.Hash
             (Context, Digest, SHA256, hash, hashFinalize, hashInit,
             hashUpdate)

This syntax doesn't seem to be mentioned on haskell wiki on imports.

What does the "cryptonite" string do here?

Where does this syntax come from?

Is is part of Haskell2010 and if it is so where in the language report is it mentioned?

like image 695
typetetris Avatar asked Aug 08 '18 09:08

typetetris


1 Answers

This is extra syntax that is supported when using the PackageImports extension:

With the PackageImports extension, GHC allows import declarations to be qualified by the package name that the module is intended to be imported from. For example:

import "network" Network.Socket

would import the module Network.Socket from the package network (any version). This may be used to disambiguate an import when the same module is available from multiple packages, or is present in both the current package being built and an external package.

The special package name this can be used to refer to the current package being built.

It occasionally happens that two packages export a module with the same name. For example both hashmap and unordered-containers export Data.HashSet. If both packages are installed, we want to disambiguate between the different packages. With this type of import, the author thus specifies that the Crypto.Hash module of the cryptonite needs to be used.

This is to to the best of my knowledge not standard Haskell (in the sense that other Haskell compilers do not have to support this, it seems not specified in the import statement section of the Haskell 2010 report), but a Glasgow Haskell compiler extension. Of course other compilers can support this as well, but a compiler without this extension, can still rightfully call itself a "Haskell compiler". In order to activate this extension, you need to compile with the -XPackageImports extension:

ghc -XPackageImports main.hs

This is a dynamic flag, and thus can be specified in the "pragma" of a Haskell source file as well.

like image 94
Willem Van Onsem Avatar answered Sep 22 '22 01:09

Willem Van Onsem