Unwrapping the Haskell State Monad -


in process of writing assignment university having ever-joyous fun of learning new haskell monads. yay!!!

i have function typechecks fine:

compile :: prog -> state varsstate string compile prog@(prog functions) =     s1 <- sequence (map (translate_func 0) [get_function prog name | name <- [func_name func | func <- functions]])     return $ trace ("here's program: \n" ++ show prog) $ concat $ s1 

but when other function:

maybe_compile_prog ::     maybeok prog -> string -> io () maybe_compile_prog (error msg) _ =     putstrln ("error: " ++ msg) maybe_compile_prog (ok prog) modulename =     s1 <- compile prog     writefile (modulename ++ ".m") ((header modulename) ++ s1) 

tries call it, blows @ line

s1 <- compile prog 

saying couldn't match expected type "io t0" actual type "state varsstate string".

i assume because maybe_compile_prog returns type io () expects unwrap io information? varsstate custom datatype have made use state monad/

however, if problem , assume is, don't know how transmit simple string maybe_compile_prog. really, that's want - give string maybe_compile_prog.

perhaps there's neat way unwrap state monad? perhaps it's possible rewrite "compile" takes in state monad information whilst runs, returns string (not wrapped in monad)?

please let me know if i'm missing information.

compile prog action in state varsstate monad, cannot use in io-do-block such. in do-block, lines must use same monad, in case io.

you need "run" compile action obtain result, 1 of

runstate :: state s -> s -> (a,s) evalstate :: state s -> s -> execstate :: state s -> s -> s 

depending on whether need

  • both, result , final state
  • only result
  • only final state

in case, want result, evalstate is.

for need provide initial state, like

maybe_compile_prog (ok prog) modulename =     let s1 = evalstate (compile prog) initialstate     writefile (modulename ++ ".m") ((header modulename) ++ s1) 

but provided initial state compile action same ok progs passed. if not right thing, pass initial state parameter too.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -