I'm making a parser function for small strings that contain currency information.
I'm getting an error message about the regex syntax, =~
, that is unclear to me.
Here is my code:
{-# LANGUAGE DeriveGeneric, OverloadedStrings, FlexibleContexts, RankNTypes, KindSignatures, DataKinds #-}
import Data.Time
import Data.Text as T
import Text.Regex.PCRE
data Currency = USD | EUR | JPN deriving (Show, Generic)
-- -- Currency part
cparse :: Text -> Currency
cparse raw
| (raw =~ "^\\$" :: Bool) = USD
| (raw =~ "^¥" :: Bool) = JPN
| (raw =~ "^€" :: Bool) = EUR
| (raw =~ "USD$" :: Bool) = USD
| (raw =~ "JPN$" :: Bool) = JPN
| (raw =~ "EUR$" :: Bool) = EUR
| otherwise = USD
My tests for these conditions work as expected:
Prelude Text.Regex.PCRE> ("$12" =~ "^\\$" :: Bool)
True
Prelude Text.Regex.PCRE> ("¥12" =~ "^\\$" :: Bool)
False
Prelude Text.Regex.PCRE> ("¥12" =~ "^¥" :: Bool)
True
Prelude Text.Regex.PCRE> ("12USD" =~ "USD$" :: Bool)
True
Prelude Text.Regex.PCRE> ("12USF" =~ "USD$" :: Bool)
False
But when I try to define this function, here's my error:
<interactive>:14:8: error:
• No instance for (RegexLike Regex Text) arising from a use of ‘=~’
• In the expression: (raw =~ "^\\$" :: Bool)
In a stmt of a pattern guard for
an equation for ‘cparse’:
(raw =~ "^\\$" :: Bool)
In an equation for ‘cparse’:
cparse raw
| (raw =~ "^\\$" :: Bool) = USD
| (raw =~ "^¥" :: Bool) = JPN
| (raw =~ "^€" :: Bool) = EUR
| (raw =~ "USD$" :: Bool) = USD
| (raw =~ "JPN$" :: Bool) = JPN
| (raw =~ "EUR$" :: Bool) = EUR
| otherwise = USD
Since the regex itself works, I don't think I'm using the regex library incorrectly, even though that is what the error strongly says.
My hunch is something is wrong in the way I wrote my guard, but I can't tell what. What am I getting wrong?