Haskell: Why I can load this file in ghci but when I try to do the same in hugs I get a syntax error?

StackOverflow https://stackoverflow.com/questions/21506527

  •  05-10-2022
  •  | 
  •  

Frage

This is the file I am trying to load:

import Data.List (foldl')
import Text.Printf (printf)
import Data.Char (ord)

--data IntParsedStr = Int | ParsingError
--data ParsingError = ParsingError String

asInt :: String -> Either String Integer
asInt "" = Left "Empty string"
asInt xs@(x:xt) | x == '-' = either Left (Right . ((-1) *)) (asInt' xt)
                | otherwise = either Left Right (asInt' xs)
                where asInt' :: String -> Either String Integer
                      asInt' "" = Left "No digits"
                      asInt' xs = foldl' step (Right 0) xs
                                where step :: Either String Integer -> Char -> Either String Integer
                                      step (Left e) _ = Left e
                                      step (Right ac) c = either Left (Right . (10 * ac + ) . fromIntegral) (charAsInt c)

charAsInt :: Char -> Either String Int
charAsInt c | between (ord '0') (ord c) (ord '9') = Right $ ord c - ord '0'
            | otherwise = Left $ printf "'%c' is not a digit" c

checkPrecision str = either error ((str == ). show) (asInt str)

between :: Ord t => t -> t -> t -> Bool
between a b c = a <= b && b <= c

It loads without any problem in ghci but in hugs I get this error:

ERROR "folds.hs":17 - Syntax error in expression (unexpected `)')

Line 17 is the last in the definition of asInt function

Edit:

Hi! I recently founded that this is in fact a known hugs issue as said in this question where there is a link to the Hugs 98 Users Guide where says that

Legal expressions like (a+b+) and (a*b+) are rejected.

War es hilfreich?

Lösung

I believe this is a bug in Hugs, not a liberality of GHC. The Haskell 98 report (appropriate in the context of Hugs usage) says

Syntactic precedence rules apply to sections as follows. (op e) is legal if and only if (x op e) parses in the same way as (x op (e)); and similarly for (e op). For example, (*a+b) is syntactically invalid, but (+a*b) and (*(a+b)) are valid. Because (+) is left associative, (a+b+) is syntactically correct, but (+a+b) is not; the latter may legally be written as (+(a+b)).

I interpret that as allowing (10 * ac + ) since both (*) and (+) are left associative, and indeed (*) has higher precedence.

As pointed out in the comments, ((10 * ac) + ) is accepted by both, so is a workaround.

Interestingly, this isn't listed in the Hugs vs Haskell 98 page, so maybe Mark P. Jones reads this section of the report differently to me. Certainly I can forgive him for this; Gofer implemented constructor classes long before Haskell allowed them, and Hugs is still faster than GHCi at compilation, and still gives better error messages.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top