I have a little problem with Data Types in Haskell, I think I should post first some code to help to understand the problem
helper :: (MonadMask a, MonadIO a, Functor a) => Expr -> String -> a (Either InterpreterError Int)
helper x y = ( getEval ( mkCodeString x y ) )
-- Creates Code String
mkCodeString :: (Show a) => a -> String -> String
mkCodeString x y = unpack (replace (pack "Const ") (pack "") (replace (pack "\"") (pack "") (replace (pack "Add") (pack y) (pack (show x) ) ) ) )
-- Calculates String
getEval :: (MonadMask m, MonadIO m, Functor m) => [Char] -> m (Either InterpreterError Int)
getEval str = (runInterpreter (setImports ["Prelude"] >> interpret str (as ::Int)))
-- | A test expression.
testexpression1 :: Expr
testexpression1 = 3 + (4 + 5)
-- | A test expression.
testexpression2 :: Expr
testexpression2 = (3 + 4) + 5
-- | A test expression.
testexpression3 :: Expr
testexpression3 = 2 + 5 + 5
I use the helper Function like this "helper testexpression3 "(+)" and it returns me the value "Right 12" with the typ "Either InterpreterError Int", but I only want to have the "Int" value "12"
I tried the function -> "getValue (Right x) = x" but I dont get that Int value. After some time of testing I think it is a problem with the Monads I've used.
If I test the typ of the helper function like this: ":t (helper testexpression1 "(+)")" I'll get that: "(... :: (Functor a, MonadIO a, MonadMask a) => a (Either InterpreterError Int)"
How can I make something like that working: write "getValue (helper testexpression1 "(+)")" and get "12" :: Int
I'll know that the code makes no sence, but its a homework and I wanted to try some things with haskell.Hope you have some more Ideas than I am.
And Sorry for my bad English, I have began to learn English, but I am just starting and Thank you for every Idea and everything.
Edit, here is what was missing on code:
import Test.HUnit (runTestTT,Test(TestLabel,TestList),(~?))
import Data.Function (on)
import Language.Haskell.Interpreter -- Hint package
import Data.Text
import Data.Text.Encoding
import Data.ByteString (ByteString)
import Control.Monad.Catch
-- | A very simple data type for expressions.
data Expr = Const Int | Add Expr Expr deriving Show
-- | 'Expression' is an instance of 'Num'. You will get warnings because
-- many required methods are not implemented.
instance Num Expr where
fromInteger = Const . fromInteger
(+) = Add
-- | Equality of 'Expr's modulo associativity.
instance Eq Expr where
(==) x1 x2 = True --(helper x1 "(+)") == (helper x2 "(+)") && (helper x1 "(*)") == (helper x2 "(*)")
That functions are also in the file ... everything else I have in my file are some Testcases I have created for me.
MonadMask,Expr,runInterpreter) that are not shown. Please post the minimal amount of code that compiles and demonstrates the problem you're having. - cdkhelperas if it wasEither a b, but it is(MonadMask m, MonadIO m, Functor m) => m (Either a b), so you have to unwrap the monad layer before you can pattern match onLeftorRightconstructors. - cdkmain:main = do { result <- helper testexpression1 "(+)"; case result of { Right x -> print x; Left err -> putStrLn "There was an error!" } }, but feel free to reformat that into multiple lines. The short answer is that you're restricted to being insideIOat some point, and you can't escape fromIOin Haskell (except forunsafePerformIO, which you might guess by its name is not safe for every day use, it causes very strange problems and exists mainly for low level operations in advanced code, do not use it unless you know what you're doing!). - bheklilrMaybeandEither. In fact, most (if not all) monads that are not built on top ofIOare simply that, a data structure. Executing a "monadic" action is just building up a data structure for that monad.IOis a special monad, it's one that you don't have access to its constructors and it truly is impure, you can get different values out each time you run it (e.g. reading from a file). Any monad that is just a data structure can usually be "unwrapped" to get a final result, meaning it's just deconstructed to get some value out. - bheklilr