Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Could anyone explain what this means? (Haskell wiki entry on monads) (umptious) 2. Re: Could anyone explain what this means? (Haskell wiki entry on monads) (Gesh) 3. Re: Learning Monads with 'setjmp' and 'longjmp' like actions (Gesh) 4. Re: regex and Unicode (Brian Sammon) ---------------------------------------------------------------------- Message: 1 Date: Tue, 30 Aug 2016 20:47:43 +0100 From: umptious <umpti...@gmail.com> To: "beginners@haskell.org" <beginners@haskell.org> Subject: [Haskell-beginners] Could anyone explain what this means? (Haskell wiki entry on monads) Message-ID: <cae20bnvuyqurwawz8xokbaptgy_zedz6ynrmhkdhifumxnj...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" >>Each monad, or computation type<< That seems to be saying that monad and "computation type" are synonyms. But surely this is just bad writing? A monad is a CT, but a CT doesn't have to be a monad? And what is the point of adding the verbiage of "computation type" in what will be a massive sentence anyway? >>provides means, subject to *Monad Laws*,<< I think it's expected that monads will follow monad laws and provide means to do something.. >>> to *(a)* *create* a description of computation action that will produce (a.k.a. "return") a given Haskell value<<< This is just bizarre. The monad can create a description of a computation action? What does that mean - a comment is a description - does it mean that? And does it mean the description will do the returning or that the computation action will? And why say "means" instead of something more specific and meaningful (for example, I'm guessing the means might be a function..) -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160830/109b4b7f/attachment-0001.html> ------------------------------ Message: 2 Date: Wed, 31 Aug 2016 01:56:08 +0300 From: Gesh <g...@gesh.uni.cx> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Could anyone explain what this means? (Haskell wiki entry on monads) Message-ID: <17cbf2f9-fe40-747a-9251-cfd5e7101...@gesh.uni.cx> Content-Type: text/plain; charset=utf-8; format=flowed I agree with you that the phrasing could be better. On 2016-08-30 22:47, umptious wrote: > > >>Each monad, or computation type<< > > That seems to be saying that monad and "computation type" are > synonyms. But surely this is just bad writing? A monad is a CT, but a > CT doesn't have to be a monad? And what is the point of adding the > verbiage of "computation type" in what will be a massive sentence anyway? What they seem to be trying to convey here is that each monad represents a type of computation. E.g. the list monad represents nondeterministic computation, the Maybe monad represents failable computation, the Either monad represents computations that may throw exceptions, etc. Basically, the point here is to present the weird and abstract "monad" in terms of the more familiar and intuitive notion of "computation". The word "type" here refers to the intuitive sense, and not the type-theoretic/programmer's sense IIUC. > >>provides means, subject to /*Monad Laws*/,<< > > I think it's expected that monads will follow monad laws and provide > means to do something.. That's clear post priori. That is, obviously, if there are "Foo Laws", then we'd expect Foos to satisfy them. If it was clear to you at the onset, good for you! You're well on your way to developing the mindset used in many libraries (e.g. pipes/conduit, lens, etc.) which is to approach any new concept with the question "What laws must it satisfy?" I assume the second half of your remark is pure snark. (Statement necessary due to difficulty distinguishing snark from questions) > >>> to /*(a)*/ /create/ a description of computation action that will > produce (a.k.a. "return") a given Haskell value<<< > > This is just bizarre. The monad can create a description of a > computation action? What does that mean - a comment is a description - > does it mean that? And does it mean the description will do the > returning or that the computation action will? And why say "means" > instead of something more specific and meaningful (for example, I'm > guessing the means might be a function..) An example would make it easier to see what is meant, in my opinion. Consider the definitions > data State s a = Return a | Get (s -> State s a) | Set s (State s a) > instance Functor (State s) where > fmap f (Return a) = Return a > fmap f (Get t) = Get (\s -> fmap f (t s)) > fmap f (Set s n) = Set s (fmap f n) > instance Monad (State s) where > return = Return > Return a >>= f = f a > m >>= f = fmap (>>= f) m > get = Get Return > set s = Set s (Return ()) > modify f = get >>= \s -> set (f s) > runState (Return a) _ = a > runState (Get t) s = runState (t s) > runState (Set s n) _ = runState n s With these definitions in hand, we can write programs such as: > sum = runState go 0 > where go [] = get > go (x:xs) = modify (+x) >> go xs Note, however, that `State` only *describes* a stateful computation. We could do other things with the value returned by `go` (e.g., count the amount of modifications to the global state). In other words, the point is that monads can be seen as describing a program in a language with semantics wildly different from Haskell's, where at the end of the day we interpret the program using whichever interpreter is most useful to us. Hence, despite Haskell being pure and functional, we can write code that makes use of e.g. pointer manipulation, randomness, nondeterminism, etc. In fact, the fact that in Haskell we have `main :: IO a` means that the value of `main` is the "program text" of some imperative program that gets passed to GHC's runtime system, which is capable of interpreting this imperative program. It is in this sense, if I'm not mistaken, that Simon Peyton-Jones refers to Haskell as the "world's finest imperative programming language"[0]. I hope this will reduce confusion, rather than create it. Gesh [0] - https://www.microsoft.com/en-us/research/wp-content/uploads/2016/07/mark.pdf ------------------------------ Message: 3 Date: Wed, 31 Aug 2016 06:03:46 +0300 From: Gesh <g...@gesh.uni.cx> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Learning Monads with 'setjmp' and 'longjmp' like actions Message-ID: <5a15efaa-2acd-432c-f344-78fb8ebb9...@gesh.uni.cx> Content-Type: text/plain; charset=utf-8; format=flowed On 2016-07-24 20:17, Michael Roth wrote: > Hi, > > I'm really trying hard to understand monads but I guess my brain is > trapped in the imperative world. > > Below what I have done so far. But now I would like to implement > something like this: > > do > foo > (label, jumped) <- setjmp > bar > longjmp label > > But I'm stuck. How can I implement 'setjmp' and 'longjmp' with my monad? > I'm not able to grasp how can I design 'setjmp' and somehow create a > label to use in 'longjmp'. > Maybe my monad structure is not appropriate? > > > Code follows: > --------------------------------------------------------------- > > data World = SomeWorld > deriving (Eq, Show) > > data ProgramResult a = > Result { world :: World, result :: a } > | Continue { world :: World, continue :: Program a } > | Stopped { world :: World } Firstly, you are correct in noting that `setjmp`/`longjmp` aren't implementable in your monad. Your monad is a variation on the following one: > data Lazy a = Value a | Thunk (() -> Lazy a) with the obvious instances. In essence, given that your `World` type is opaque, I'm treating it as isomorphic to (). Given that, we have `ProgramResult a ~ Lazy (Maybe a)`. In contrast, `setjmp`/`longjmp` are *much* more powerful than that. Any monad in which one can implement them can be used to implement `callCC`. One can embed the `Cont` monad into such monads (writing the code to do this is left as an exercise). However, one can embed any monad into `Cont`[0], from which it should be intuitively obvious that your monad is not up to the task. Moreover, usually `setjmp`/`longjmp` is not what you want. Derek Elkins explains this much better than I can[1], but in brief the problem with `setjmp` is that the continuation that it corresponds to contains its own use, which means you need to test whether the current execution of the code occurs before the first call to `longjmp`. The alternative he recommends is `callCC`, which is equivalent in power to `setjmp`/`longjmp` but whose usage is simpler. Hope this helps, despite the original question being over a month old. Gesh [0] - https://www.schoolofhaskell.com/school/advanced-haskell/the-mother-of-all-monads [1] - http://lambda-the-ultimate.org/node/3611#comment-51086 P.S. An untested implementation of `setjmp`/`longjmp` in `Cont`: > throw :: r -> Cont r b > throw e = Cont $ \_ -> e > setjmp :: Cont r (Either (a -> Cont r b) a) > setjmp = Cont $ \c-> c (Left (throw . c . Right)) > longjmp :: (a -> Cont r b) -> a -> Cont r b > longjmp c v = c v ------------------------------ Message: 4 Date: Tue, 30 Aug 2016 23:16:57 -0400 From: Brian Sammon <haskell-beginn...@brisammon.fastmail.fm> To: beginners@haskell.org Subject: Re: [Haskell-beginners] regex and Unicode Message-ID: <20160830231657.c9d628dfa538d8e795ab5...@brisammon.fastmail.fm> Content-Type: text/plain; charset=US-ASCII On Sun, 28 Aug 2016 15:11:24 -0400 Brian Sammon <haskell-beginn...@brisammon.fastmail.fm> wrote: > I tried to write a program using Text.Regex.PCRE to search through a UTF8- > encoded document. It appears that the presence of non-breaking-space > characters (Charpoint 160) triggers some weird behavior in my program. Well switching my code to use Text.RegexPR-based searches rather than Text.Regex.PCRE made the problem go away. Text.Regex.PCRE seems to be unmaintained, so I guess I shouldn't be surprised that I had problems with it. ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 98, Issue 24 *****************************************