Haskell Import error: Not in scope - haskell

I have written this code:
import GHC.Float
next :: GHC.Float -> GHC.Float-> GHC.Float
next n x = (x + n / x) / 2
And I am getting the following error:
numerical.hs:3:9:
Not in scope: type constructor or class `GHC.Float'
numerical.hs:3:22:
Not in scope: type constructor or class `GHC.Float'
numerical.hs:3:34:
Not in scope: type constructor or class `GHC.Float'
The module imports without any problem, so I'm not sure if I'm referring to it with the wrong name or if the standard Float module is the same as the IEEE GHC.Float one and there's no need to explicitly import it.
I tried doing an import GHC.Float as Fl with no success--got the same type error on Fl.
I'm just starting Haskell (obviously), so any help is appreciated!

You don't have to import GHC.Float manually, you can just write Float, like so
next :: Float -> Float -> Float
next n x = (x + n / x) / 2
GHC implicitly imports a module called Prelude in every source file you have. Prelude includes a lot of handy types, functions, and other things that are used as the "built-ins" of the language. Types like Int, Float, Maybe, IO, and functions like head, +, /, and more.
You can test to see if a floating point number is an IEEE floating point with the function isIEEE from the GHC.Float module:
import GHC.Float
main = do
putStr "1.0 is an IEEE floating point: "
print $ isIEEE (1.0 :: Float)
If you run this, it will print True
I should have also mentioned that the reason why your code didn't compile earlier is because when you import a module with just import, everything from it comes into scope. You can force it to be qualified by using import qualified, here's a few examples:
import GHC.Float -- Everything now in scope
import qualified Data.Maybe -- Have to use full name
import qualified Data.List as L -- aliased to L
main = do
-- Don't have to type GHC.Float.isIEEE
print $ isIEEE (1.0 :: Float)
-- Have to use full name
print $ Data.Maybe.isJust $ Nothing
-- Uses aliased name
print $ L.sort [1, 4, 2, 5, 3]

Related

Construct a dependent type out of a list in Haskell

I want to write a library in Haskell for Galois fields. A Galois field is defined by its irreducible polynomial. Galois field elements can only be added if they have the same Galois field. I want to lift the polynomial into the type of my Galois field, so that for example a Galois field with the polynomial [1, 2, 3] has a different type than a Galois Field with the polynomial [2, 0, 1]. This way i could assure that only Galois field elements with the same Galois field can be added. Is this possible?
My polynomial data type looks like this:
newtype Polynomial a = Polynomial [a]
My Galois field data type look like this:
data GF irr a = GF {
irreducible :: irr
, q :: PrimePower
}
So i want a constructor that takes a polynomial (for example (Polynomial [2, 0, 1])) and gives me a Galois field of the type GF (Polynomial Int) ([2, 0, 1]).
I know that [2, 0, 1] is not a valid type but i saw that with Data.Singletons it is possible to create types like
(SCons STrue (SCons SFalse SNil))
for [True, False], but i do not know how to construct types likes these from my list [2, 0, 1] and how the constructor would look like.
As Luke already commented, [2, 0, 1] is actually a valid type.
Prelude> :set -XDataKinds -XPolyKinds
Prelude> data A x = A deriving Show
Prelude> A :: A [2,0,1]
A
where the number literals are actually type-level Nat literals, and [...] is a lifted version of the list value-constructor to the type kind. This can be made explicit by writing it with “prime-quote syntax”
Prelude> A :: A '[2, 0, 1]
A
...so, this task is actually pretty trivial. You can just use
{-# LANGUAGE DataKinds, KindSignatures #-}
import GHC.TypeLits (Nat)
newtype Polynomial a = Polynomial [a]
data GF (irr :: Polynomial Nat) = GF {q :: PrimePower}
As also said by Luke, keep in mind though that type-level calculations don't work as well as they do in full dependently-typed languages. If you really want to do proof stuff with this, you should consider switching to Idris, Agda or Coq.
It seems that the "lifted" value will be used merely as a tag. For cases in which we only want it to work as a tag, but it's difficult to lift the required value to the type level with DataKinds, a possible alternative consists in attaching to the value a "ghostly" type tag conjured by means of a polymorphic incantation. Consider this helper module:
{-# LANGUAGE RankNTypes #-}
module Named (Named,forgetName,name) where
newtype Named n a = Named a -- don't export the constructor
forgetName :: Named n a -> a
forgetName (Named a) = a
name :: a -> ( forall name. Named name a -> r ) -> r
name x f = f ( Named x ) -- inside the callback, "x" has a "name" type tag attached
And this other module which depends on it:
module GF (Polynomial(..),GF,stuff,makeGF,addGF) where
import Named
newtype Polynomial a = Polynomial [a]
data GF a = GF { -- dont' export the constructor
_stuff :: Int -- don't export the bare field
}
stuff :: GF a -> Int
stuff (GF x) = x
makeGF :: Named ghost (Polynomial Int) -> Int -> GF ghost
makeGF _ = GF
addGF :: Named ghost (Polynomial Int) -> GF ghost -> GF ghost -> GF ghost
addGF _ x1 x2 = GF (stuff x1 + stuff x2)
I would be impossible for clients of this module to sum two GF values with different ghostly tags. Their only way of creating ghostly tags is through name, and they don't have the means of re-tagging either the Named values or the GF ones—we carefully hid constructors and bare fields to prevent that. So this would compile:
module Main where
import Named
import GF
main :: IO ()
main = print $
name (Polynomial [2::Int,3,4]) $ \ghost ->
let x1 = makeGF ghost 3
x2 = makeGF ghost 4
in stuff (addGF ghost x1 x2)
But this wouldn't:
main :: IO ()
main = print $
name (Polynomial [2::Int,3,4]) $ \ghost1 ->
name (Polynomial [3::Int]) $ \ghost2 ->
let x1 = makeGF ghost1 3
x2 = makeGF ghost2 4
in stuff (addGF ghost1 x1 x2)

Import a type family that is an operator in Haskell

GHC.TypeNats exports type family of the following signature:
type family (m :: Nat) + (n :: Nat) :: Nat
How can I import it explicitly? import GHC.TypeNats((+)) does not work, because it says that GHC.TypeNats does not export (+)...
Everything compiles okay when I import whole module implicitly, but this really is not what I want to have in my code.
I am using GHC 8.6.5
From the manual:
There is now some potential ambiguity in import and export lists; for example if you write import M( (+) ) do you mean the function (+) or the type constructor (+)? The default is the former, but with ExplicitNamespaces (which is implied by TypeOperators) GHC allows you to specify the latter by preceding it with the keyword type, thus:
import M( type (+) )

Hide a constructor but not the type on import

I've got an internal module I'd like to provide an external API for
module Positive.Internal where
newtype Positive a = Positive { getPositive :: a }
deriving (Eq, Ord)
-- smart constructor
toPositive :: (Num a, Ord a) => a -> Maybe (Positive a)
toPositive a | a <= 0 = Nothing
| otherwise = Just $ Positive a
-- ...
I want to hide the dumb constructor, and replace it with a unidirectional
pattern so users can still pattern match values, they just have to use the smart constructor to use new values.
Since I want the pattern and the dumb constructor to use the same name, I need to hide the dumb constructor to prevent namespace clashes.
However, since the dumb constructor and the type share names, it's a little tricky to import the everything BUT the dumb constructor.
Currently I'm doing this, which works ok:
{-# LANGUAGE PatternSynonyms #-}
module Positive
( module Positive.Internal, pattern Positive
) where
import Positive.Internal (Positive())
import Positive.Internal hiding (Positive)
import qualified Positive.Internal as Internal
pattern Positive :: a -> Positive a
pattern Positive a <- Internal.Positive a
I could simplify my imports by just using the qualified import, but I'm curious.
Is there a way to, in a single import statement, import all of Positive.Internal except the dumb constructor?
I tried hiding (Positive(Positive)), but that hid both the type and the dumb constructor. I've poked about the wiki, but I haven't noticed any way to differentiate between constructors and types in hiding lists.
Correct me if I am wrong, but I am almost certain this is what you are looking for:
{-# LANGUAGE PatternSynonyms #-}
module Positive
( module Positive.Internal, pattern Positive, foo
) where
import Positive.Internal hiding (pattern Positive)
import qualified Positive.Internal as Internal (pattern Positive)
pattern Positive :: a -> Positive a
pattern Positive a <- Internal.Positive a
foo :: Positive Int
foo = Internal.Positive 5
Internal module stays the same way as it is defined so far. And for the sake of example:
module Negative where
import Positive
bar :: Maybe Int
bar = getPositive <$> toPositive 6
Let's double check in GHCi:
Prelude> :load Negative
[1 of 3] Compiling Positive.Internal ( Positive/Internal.hs, interpreted )
[2 of 3] Compiling Positive ( Positive.hs, interpreted )
[3 of 3] Compiling Negative ( Negative.hs, interpreted )
Ok, modules loaded: Negative, Positive, Positive.Internal.
*Negative> bar
Just 6
*Negative> getPositive foo
5
*Negative> :i Positive
newtype Positive a = Positive.Internal.Positive {getPositive :: a}
-- Defined at Positive/Internal.hs:3:1
instance [safe] Ord a => Ord (Positive a)
-- Defined at Positive/Internal.hs:4:17
instance [safe] Eq a => Eq (Positive a)
-- Defined at Positive/Internal.hs:4:13
*Negative> :t Positive
<interactive>:1:1: error:
• non-bidirectional pattern synonym ‘Positive’ used in an expression
• In the expression: Positive

Import a data constructor without importing the type

numeric-prelude does this thing where every data type is named T and every type-class is named C. For the sake of... consistency, I suppose I'll play along:
{-# LANGUAGE NoImplicitPrelude #-}
module Number.SqrtRatio (T(..), ratioPart) where
import qualified Number.Ratio as Ratio
import Number.Ratio ((:%))
import qualified Algebra.Ring as Ring
import NumericPrelude.Base
-- | A number whose square is rational, canonicalized as a rational
-- times the square root of a squarefree integer.
data T x = T {
numerator :: !x,
denominator :: !x,
rootNum :: !x
} deriving (Eq, Show)
ratioPart :: T x -> Ratio.T x
ratioPart (T n d _) = n :% d
fromRatio :: (Ring.C x) => Ratio.T x -> T x
fromRatio (n :% d) = T n d Ring.one
ghc is not impressed:
Number/SqrtRatio.hs:5:22:
In module ‘Number.Ratio’:
‘(:%)’ is a data constructor of ‘T’
To import it use
‘import’ Number.Ratio( T( (:%) ) )
or
‘import’ Number.Ratio( T(..) )
Sure thing buddy, I can comply:
{-# LANGUAGE NoImplicitPrelude #-}
module Number.SqrtRatio (T, ratioPart) where
import qualified Number.Ratio as Ratio
import Number.Ratio (T((:%)))
-- newly added ^
...but this also ends up importing Ratio.T, which conflicts with my T!
ratioPart :: T x -> Ratio.T x
{- ^-- Ambiguous occurrence ‘T’
It could refer to either ‘Number.SqrtRatio.T’,
defined at Number/SqrtRatio.hs:11:1
or ‘Number.Ratio.T’,
imported from ‘Number.Ratio’ at Number/SqrtRatio.hs:5:22-28
-}
Alright, so how about import Number.Ratio (T((:%))) hiding T?
Number/SqrtRatio.hs:5:31: parse error on input ‘hiding’
I'm at a bit of a loss, gaise. :/
Turns out there is a proper way to do this:
{-# LANGUAGE NoImplicitPrelude, PatternSynonyms #-}
module Number.SqrtRatio (T(..), ratioPart) where
import qualified Number.Ratio as Ratio
import Number.Ratio (pattern (:%))
Note that I've used the -XPatternSynonyms extension not to actually define any pattern synonym, just to enable the pattern keyword so it's clear I want to import the value constructor :% alone.
My current solution, discovered moments before posting:
Give up on trying to import (:%).
Keep the qualified import.
Change :% to Ratio.:% everywhere (patterns and expressions).
Result:
{-# LANGUAGE NoImplicitPrelude #-}
module Number.SqrtRatio (T(..), ratioPart) where
import qualified Number.Ratio as Ratio
import qualified Algebra.Ring as Ring
import NumericPrelude.Base
-- | A number whose square is rational, canonicalized as a rational
-- times the square root of a squarefree integer.
data T x = T {
numerator :: !x,
denominator :: !x,
rootNum :: !x
} deriving (Eq, Show)
ratioPart :: T x -> Ratio.T x
ratioPart (T n d _) = n Ratio.:% d
fromRatio :: (Ring.C x) => Ratio.T x -> T x
fromRatio (n Ratio.:% d) = T n d Ring.one
Ugly.

Haskell "import qualified" and "Not in scope: data constructor"

I have an import like this:
import qualified Bioinformatics.DNA as DNA
from an other file looking like this:
data DNA = A | C | G | T
deriving (Eq, Ord, Show)
And in this function in the module RNA where:
module Bioinformatics.RNA
( RNA
, fromDna
) where
import qualified Bioinformatics.DNA as DNA
data RNA = A | C | G | U
deriving (Eq, Ord, Show)
fromDna :: DNA.DNA -> RNA
fromDna DNA.A = A
fromDna DNA.C = C
fromDna DNA.G = G
fromDna DNA.T = U
I receive errors:
/home/thibaud/code/bioinformatics/src/Bioinformatics/RNA.hs:46:9:
Not in scope: data constructor ‘DNA.A’
/home/thibaud/code/bioinformatics/src/Bioinformatics/RNA.hs:47:9:
Not in scope: data constructor ‘DNA.C’
/home/thibaud/code/bioinformatics/src/Bioinformatics/RNA.hs:48:9:
Not in scope: data constructor ‘DNA.G’
/home/thibaud/code/bioinformatics/src/Bioinformatics/RNA.hs:49:9:
Not in scope: data constructor ‘DNA.T’
Do you have an idea why?
Thanks
Use (..) to import all the constructors of a data type.
import qualified Bioinformatics.DNA as DNA (DNA(..), A, C, G, T)
Code Breakdown
EDIT: Let's break down your code a little since its not quite idiomatic.
import qualified Bioinformatics.DNA as DNA (DNA(..), A, C, G, T)
That's already an odd one. Typically people import things qualified or selectively, not both. Try just:
import qualified Bioinfomatics.DNA as DNA
So lets keep the quantification and drop the explicit list of symbols.
Now for the use, you have:
fromDna :: DNA.DNA -> RNA
fromDna DNA.A = A
fromDna DNA.C = C
fromDna DNA.G = G
fromDna DNA.T = U
The claim that this function converts from DNA to RNA. Note your questino never presented an RNA type or constructors - are those in your code somewhere? The remaining error from the code you posted is with respect to RNA, consider something like:
import qualified Bioinformatics.RNA as RNA
fromDna :: DNA.DNA -> RNA.RNA
fromDna DNA.A = RNA.A
fromDna DNA.C = RNA.C
fromDna DNA.G = RNA.G
fromDna DNA.T = RNA.U
Let's imagine we need a Geometry.Shape module for operations on a shapes. First, we'll make a folder called Geometry. Mind the capital G. In it, we'll place the Shape.hs file. Here's what the file will contain:
module Geometry.Shape
( Shape(Circle,Rectangle)
, Point
) where
data Point = Point Float Float deriving (Show)
data Shape = Circle Point Float | Rectangle Point Point deriving (Show)
Notice that when defining a point, we used the same name for the data type and the value constructor.
So let's assume that we need the Geometry module for various geometric calculations. We will create the Geometry.hs file and place it at the same level as the Geometry directory:
module Geometry (area) where
import qualified Geometry.Shape as Shape
area :: Shape.Shape -> Float
area (Shape.Circle _ r) = pi * r ^ 2
area (Shape.Rectangle (Shape.Point x1 y1) (Shape.Point x2 y2)) =
(abs $ x2 - x1) * (abs $ y2 - y1)
The file structure should now look like this:
.
├── Geometry
│   └── Shape.hs
└── Geometry.hs
Well, let's check our area function. First we need to open GHCi in the same directory as the Geometry.hs file.
$ ghci
GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help
Prelude>
Then let's load Geometry module:
Prelude> :l Geometry.hs
[1 of 2] Compiling Geometry.Shape ( Geometry/Shape.hs, interpreted )
[2 of 2] Compiling Geometry ( Geometry.hs, interpreted )
Geometry.hs:7:24: Not in scope: data constructor ‘Shape.Point’
Geometry.hs:7:44: Not in scope: data constructor ‘Shape.Point’
Failed, modules loaded: Geometry.Shape.
Oops! Something went wrong. But wait! Look again at the error message:
Not in scope: data constructor ‘Shape.Point’
Well, simply speaking, it means that the type constructor is not in the reachable scope. So let's fix it by changing the Shape.hs file as follows:
module Geometry.Shape
( Shape(Circle,Rectangle)
, Point(..)
) where
Notice we changed Point to Point(..). Reload module as follows:
Prelude> :r
[1 of 2] Compiling Geometry.Shape ( Geometry/Shape.hs, interpreted )
[2 of 2] Compiling Geometry ( Geometry.hs, interpreted )
Ok, modules loaded: Geometry, Geometry.Shape.
and test it:
Prelude> area (Shape.Circle (Shape.Point 0 0) 24)
1809.5574
Yay, it works! I want to draw your attention to the fact that we used (..) to import all the constructors of the Point data type. It's the same as writing Point (Point). Similarly, we could use Shape(..) instead of Shape(Circle,Rectangle). For more see: https://www.haskell.org/tutorial/modules.html
Not exporting the value constructors of a data types makes them more abstract in such a way that we hide their implementation. Also, whoever uses our module can't pattern match against the value constructors. This is why previously we get compilation error in the Geometry module.
I hope this example will help you understand the reason for such errors.

Resources