From a26253c1efe45459b46edd4d06a4ee03d99eb6dd Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 15 Apr 2021 08:09:59 +0200 Subject: (coerceTo $ StrVal 1) ::Maybe Bool -- still doesn't work --- src/MyLib.hs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'src/MyLib.hs') diff --git a/src/MyLib.hs b/src/MyLib.hs index 6539412..864f99e 100644 --- a/src/MyLib.hs +++ b/src/MyLib.hs @@ -1,13 +1,10 @@ -module MyLib (someFunc) where +module BizExpr (repl, eval) where import Data.Maybe (fromMaybe) import Text.Read (readMaybe) --import Data.Text as T -someFunc :: IO () -someFunc = putStrLn "someFunc" - repl :: IO () repl = getLine >>= putStrLn . maybe "Failed to evaluate expression" show . eval >> repl @@ -30,11 +27,17 @@ instance CoerceTo Integer where coerceFrom = IntVal instance CoerceTo Bool where + coerceTo (BoolVal v) = Just v coerceTo (IntVal v) = Just $ v /= 0 coerceTo (StrVal "True") = Just True coerceTo (StrVal "true") = Just True - coerceTo (BoolVal v) = Just v - coerceTo _ = Just False + --coerceTo v@(StrVal _) = coerceTo . IntVal =<< (coerceTo v :: Maybe Integer) :: Maybe Bool + coerceTo v@(StrVal x) = case (readMaybe x :: Maybe Integer) of + Nothing -> Just False + Just n -> Just $ True--n /= 0 +-- case coerceTo v :: Maybe Integer of +-- Nothing -> Just False +-- Just n -> coerceTo (IntVal n) coerceFrom = BoolVal -- cgit v1.2.3