0

When I create a .hs file with the following code and load it to ghci using :l, the file loads, but I receive the error <interactive>:1:1: error: Variable not in scope: symbol if I ask for the type of symbol. I am aware that this means that I'm using a name which is not defined in the place in which I'm attempting to use it, but I cannot see what is wrong with the code:

module MyData
(MetricUnit(..),
 ImperialUnit(..),
 Measurement(..),
 convert)
 where

data MetricUnit = Meter | Liter | KiloGram
              deriving (Show, Eq)

data ImperialUnit = Yard
                    | Gallon
                    | Pound
                      deriving (Show, Eq)

data Measurement = MetricMeasurement Double MetricUnit
             | ImperialMeasurement Double ImperialUnit
               deriving (Show)

symbol :: MetricUnit -> String
symbol Meter = "m"
symbol Liter = "L"
symbol KiloGram = "kg"

convert (MetricMeasurement x u)
     | u==Meter    = ImperialMeasurement (1.0936*x) Yard
     | u==Liter    = ImperialMeasurement (0.2642*x) Gallon
     | u==KiloGram = ImperialMeasurement (2.2046*x) Pound

convert (ImperialMeasurement x u)
      | u==Yard   = MetricMeasurement (0.9144*x) Meter
      | u==Gallon = MetricMeasurement (3.7854*x) Liter
      | u==Pound  = MetricMeasurement (0.4536*x) KiloGram
6
  • If anyone is wondering the context to this question, is here gist.github.com/BinRoot/4771288 . Please include the entire environment/file that you are feeding ghci with. Commented Aug 6, 2020 at 21:35
  • Was is always convert, symbol), or did you add this to the code after this Answer by chi indicated you were missing it? Commented Aug 6, 2020 at 21:50
  • @Scratte I added this because Chi below had recommended it. It hasn't solved the problem. Commented Aug 6, 2020 at 21:51
  • @Edward.Lin I understand. But now, you've invalidated their Answer. I suggest you remove the , symbol part. You can add to the Question that you've used it as per an Answer, but you're still getting the same error. Commented Aug 6, 2020 at 21:51
  • @Scratte I'll take convert, symbol) out of my question. But adding convert, symbol) doesn't work, so can't be the solution. Commented Aug 6, 2020 at 21:55

4 Answers 4

1

When typing multi-line things in ghci (like type declarations, class definitions, deriving clauses on another line), you have to enter multi-line mode. You can enter :{ which will put you in a multi-line prompt, then enter your code, then enter :} on another line to exit the multi-line prompt. Then, it'll interpret your code as one chunk instead of many lines.

Sign up to request clarification or add additional context in comments.

Comments

1

I guess the issue is that your module starts in this way:

module MyData
    (MetricUnit(..),
     ImperialUnit(..),
     Measurement(..),
     convert)
where

hence, the function symbol is not exported by the module.

In GHCi, loading the module as ghci MyData.hs from the command line, or as :load MyData from the GHCi prompt should bypass the export list and give you access to everything.

You could also add symbol to the export list.

You are getting that error because (I guess) you are instead using another way to import your module that won't give you access to non-exported identifiers. Maybe you are importing that module though another one?

3 Comments

To add symbol to the export list would I simply have to substitute (..) next to `MetricUnit`` with (symbol), and add the definition of symbol after where?
@Edward.Lin Change convert) to convert, symbol)
I added convert, symbol) and still have the same problem.
0

I'll start with "can not reproduce":

% cat <<EOF > ap.hs
heredoc> data MetricUnit = Meter | Liter | KiloGram
       deriving (Show, Eq)

symbol :: MetricUnit -> String
symbol Meter = "m"
symbol Liter = "L"
symbol KiloGram = "kg"
heredoc> EOF
tommd@ovdak /tmp% ghci ap.hs
GHCi, version 8.6.4: http://www.haskell.org/ghc/  :? for help
Loaded package environment from /Users/tommd/.ghc/x86_64-darwin-8.6.4/environments/default
package flags have changed, resetting and loading new packages...
Loaded GHCi configuration from /Users/tommd/.ghci
[1 of 1] Compiling Main             ( ap.hs, interpreted )
Ok, one module loaded.
*Main>

So it loads fine.

Perhaps you are trying to define the code in GHCi instead of load it from a file? In the case see @Aplet123's answer about multi-lines. For an example of a broken use of GHCi, which you might be doing, consider:

*Main> foo :: Bool

<interactive>:1:1: error: Variable not in scope: foo :: Bool
*Main> foo = True

After the first line is entered, GHCi tries to show you foo (print (foo :: Bool)) and not having foo it will tell you foo is not in scope. Again, see the other answer.

3 Comments

No, I'm not defining it in GHCI. I'm using a file. And deriving is on a separate line. The code is from here gist.github.com/BinRoot/4771288. It loads but when I ask for the type of symbol I receive the scope error.
@Edward.Lin Your code looks fine. Did you successfully load it in GHCi? Can you evaluate Meter successfully, for instance?
@Chi Yes I did. And Meter evaluates successfully.
0

The problem was the result of there being another file with the same name.

The problem was resolved when I created a new file with a fresh file name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.