Advertisement
Guest User

Untitled

a guest
Sep 16th, 2021
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. -- Environment to map values to var names in intepreter
  3. Type Env = [(String, Integer)]
  4.  
  5.  
  6. -- Some different type of error (types doesn't matter)
  7. data SomeError = A String' | B String''
  8.  
  9. -- Test monad :
  10. -- The test monad does some computation in input Env.
  11. -- Returns either SomeError, or a result of type a.
  12. -- Side effect in form of output is added to [String]
  13. newtype Test a = Test {runTest :: Env -> (Either SomeError a, [String]) }
  14.  
  15. instance Monad Test where
  16.    return a = Test $ const (Right a, [])
  17.    m >>= f = Test $ \e -> case runTest m e of
  18.                                (Left a, x) -> (Left a, x)
  19.                                (Right a, s) -> case runTest (f a) e of
  20.                                                     (Left a,x) -> (Left a,x)
  21.                                                     (Right a', s') -> (Right a', s++s')
  22.  
  23.    -- Monadic doesnt work
  24.    -- m >>= f = Test $ \e ->
  25.    --  runTest m e >>= \t ->
  26.    --  runTest (f (first t)) >>= \t' ->
  27.     --  return t'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement