Newest at the top
2025-01-31 01:43:29 +0100 | <dminuoso> | (Assuming you use `try` of course) |
2025-01-31 01:43:15 +0100 | <dminuoso> | You can also use `ParsecT e s IO`, then you can suddenly do `liftIO (putStrLn "Hello world")` in the middle of your parser. Note, that because megaparsec will do backtracking this will have some interesting/strange behavior... which is non-determinism in fact. |
2025-01-31 01:42:13 +0100 | <dminuoso> | type Parsec e s = ParsecT e s Identity |
2025-01-31 01:42:06 +0100 | <dminuoso> | https://hackage.haskell.org/package/megaparsec-9.7.0/docs/Text-Megaparsec.html#t:Parsec |
2025-01-31 01:41:46 +0100 | <dminuoso> | If you dont want IO, you can just layer it over `Identity`, which is sort of the "does nothing monad", giving you just the parsing effects. |
2025-01-31 01:41:25 +0100 | merijn | (~merijn@host-vr.cgnat-g.v4.dfn.nl) merijn |
2025-01-31 01:41:14 +0100 | <dminuoso> | The reason you can do this, is because all this parsing effect is in a transformer ParsecT which you can layer over any other monad. |
2025-01-31 01:40:55 +0100 | <dminuoso> | If you use it together with IO, you can interleave your parser with IO - say print statements or database calls |
2025-01-31 01:40:29 +0100 | <dminuoso> | euouae: Your megaparsec parser can run either in pure mode, or it can run together with IO. |
2025-01-31 01:40:04 +0100 | <dminuoso> | euouae: Lets pick a more concrete examplee: |
2025-01-31 01:39:17 +0100 | <dminuoso> | ANd you can layer it over IO to have state, non-determinism, exceptions and real world nuclear missile effects. |
2025-01-31 01:39:00 +0100 | <dminuoso> | And if you add ListT (which adds non-determinism), you could have non-determinism, state and exceptions |
2025-01-31 01:38:35 +0100 | <dminuoso> | Them being transformers means you can layer them ontop of each other, giving you a Monad that has both state *and* exceptions. |
2025-01-31 01:38:33 +0100 | <geekosaur> | so, StateT doesn't just model state; it adds state to something. It transforms something else by adding state to it |
2025-01-31 01:38:20 +0100 | <dminuoso> | euouae: StateT allows you to drag some state around, while ExceptT allows you to throw exceptions. |
2025-01-31 01:37:55 +0100 | <dminuoso> | THat may or may not make any sense to you. |
2025-01-31 01:37:53 +0100 | <euouae> | With a State I know I have a 'state processor' and an 'output value' but what is the transformer for? |
2025-01-31 01:37:49 +0100 | <dminuoso> | euouae: If we think of Monads not as "state" but as some generalized idea of effects, then transformers lets us combine different effects. |
2025-01-31 01:37:24 +0100 | <euouae> | but what is transformer? |
2025-01-31 01:37:19 +0100 | <euouae> | yup. thank you |
2025-01-31 01:37:03 +0100 | <dminuoso> | euouae: Anyhow. Do try to build the state intuition around `>>=` and `pure` - and then test it in the future. |
2025-01-31 01:36:12 +0100 | <dminuoso> | Just like we teach newtonian physics in school, despite general relativity being more accurate. |
2025-01-31 01:35:44 +0100 | <dminuoso> | euouae: I dont want discredit this particular intuition, because I think it *is* a good starting point |
2025-01-31 01:35:25 +0100 | <hololeap> | IO being (State RealWorld a) is more of a mental model |
2025-01-31 01:35:22 +0100 | <dminuoso> | Or you would need to do some more gymnastics. |
2025-01-31 01:35:03 +0100 | <dminuoso> | euouae: There's a bunch of things you can do in IO that... isnt quite about changing the real world. |
2025-01-31 01:34:55 +0100 | <euouae> | Is IO not a state |
2025-01-31 01:34:34 +0100 | <euouae> | I'm not sure what you mean by proving it wrong |
2025-01-31 01:34:33 +0100 | <hololeap> | I can't remember how to do 'unmtl' in #haskell |
2025-01-31 01:33:43 +0100 | <dminuoso> | Given that you could just `launchMissiles` and change the state of the real world with IO. |
2025-01-31 01:33:18 +0100 | <dminuoso> | euouae: And perhaps, for IO the state notion is not too shabby either as a first try by the way, if we think of the "real world" being the state. This is an intuition I can easily prove wrong, but I think I would rob you of some valuable learning process as monochrom as pointed out earlier. |
2025-01-31 01:31:25 +0100 | <hololeap> | it stands for "transformer" |
2025-01-31 01:31:12 +0100 | <euouae> | I haven't seen any examples either. I know a monad goes there, but State has it to be Identity |
2025-01-31 01:31:04 +0100 | merijn | (~merijn@host-vr.cgnat-g.v4.dfn.nl) (Ping timeout: 272 seconds) |
2025-01-31 01:31:02 +0100 | <dminuoso> | (Internally it uses a different representation for performance reasons, miond you) |
2025-01-31 01:30:59 +0100 | <euouae> | What is the role of T in StateT? I haven't quite grasped that |
2025-01-31 01:30:37 +0100 | <dminuoso> | euouae: In fact, megaparsec under the hood could be thought of StateT and ExceptT wired together - thats state looks so fitting, because it *does* use a state monad internally. |
2025-01-31 01:29:10 +0100 | <dminuoso> | But its not a good general intuition, it just does not work for list. |
2025-01-31 01:28:50 +0100 | <dminuoso> | euouae: Your intuitoin is absolutely great for megaparsec. |
2025-01-31 01:28:49 +0100 | <euouae> | anyway sorry for wasting your time... just thoughts |
2025-01-31 01:28:40 +0100 | <euouae> | As long as I can get somewhere with megaparsec :P |
2025-01-31 01:28:25 +0100 | <euouae> | You want to content that it's not matching List. I agree, but at the same time, I am not too afraid to stretch my imagination and pretend it does |
2025-01-31 01:28:08 +0100 | <euouae> | Like I said, it's matching some usual applications of Monad like the parser combinators |
2025-01-31 01:27:56 +0100 | <dminuoso> | These two functions, *together with some laws* are what Monad is. Nothing more, nothing less. |
2025-01-31 01:27:36 +0100 | <dminuoso> | If you cannot put your "state" into relationship with that interface, your intuition has nothing to do with Monad, but with something else. |
2025-01-31 01:27:11 +0100 | <yahb2> | (>>=) :: Monad m => m a -> (a -> m b) -> m b |
2025-01-31 01:27:11 +0100 | <dminuoso> | % :t (>>=) |
2025-01-31 01:27:06 +0100 | <yahb2> | pure :: Applicative f => a -> f a |
2025-01-31 01:27:05 +0100 | <dminuoso> | % :t pure |
2025-01-31 01:27:03 +0100 | <dminuoso> | euouae: If we want to derive meaning of Monad, we must constrain to its interface |