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. Get rid of Maybes in complex types (Baa) 2. Re: Get rid of Maybes in complex types (Imants Cekusins) 3. Incomplete patterns (Jona Ekenberg) 4. Re: Incomplete patterns (Sylvain Henry) ---------------------------------------------------------------------- Message: 1 Date: Thu, 6 Jul 2017 11:12:55 +0300 From: Baa <aqua...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] Get rid of Maybes in complex types Message-ID: <20170706111255.1d6695b8@Pavel> Content-Type: text/plain; charset=US-ASCII Hello Dear List! Consider, I retrieve from external source some data. Internally it's represented as some complex type with `Maybe` fields, even more, some of fields are record types and have `Maybe` fields too. They are Maybe's because some information in this data can be missing (user error or it not very valuable and can be skipped): data A = A { a1 :: Maybe B ... } data B = B { b1 :: Maybe C ... } I retrieve it from network, files, i.e. external world, then I validate it, report errors of some missing fields, fix another one (which can be fixed, for example, replace Nothing with `Just default_value` or even I can fix `Just wrong` to `Just right`, etc, etc). After all of this, I know that I have "clean" data, so all my complex types now have `Just right_value` fields. But I need to process them as optional, with possible Nothing case! To avoid it I must create copies of `A`, `B`, etc, where `a1`, `b1` will be `B`, `C`, not `Maybe B`, `Maybe C`. Sure, it's not a case. After processing and filtering, I create, for example, some resulting objects: data Result { a :: A -- not Maybe! ... } And even more: `a::A` in `Result` (I know it, after filtering) will not contain Nothings, only `Just right_values`s. But each function which consumes `A` must do something with possible Nothing values even after filtering and fixing of `A`s. I have, for example, function: createResults :: [A] -> [Result] createResults alst = ... case of (a1 theA) -> Just right_value -> ... Nothing -> logError undefined -- can not happen Fun here is: that it happens (I found bug in my filtering code with this `undefined`). But now I thought about it: what is the idiomatic way to solve such situation? When you need to have: - COMPLEX type WITH Maybes - the same type WITHOUT Maybes Alternative is to keep this Maybes to the very end of processing, what I don't like. Or to have types copies, which is more terrible, sure. PS. I threw IOs away to show only the crux of the problem. --- Cheers, Paul ------------------------------ Message: 2 Date: Thu, 6 Jul 2017 11:24:39 +0300 From: Imants Cekusins <ima...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Get rid of Maybes in complex types Message-ID: <CAP1qinZ2ydUksEJDkFK=qtfeh45_jl_g70wbc-2mm3yrz_i...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Identity http://hackage.haskell.org/package/mtl-2.2.1/docs/Control-Monad-Identity.html may work: data A m = A { a1 :: m B } data B m = B { b1 :: m C ... } m: Maybe or Identity - any good? On 6 July 2017 at 11:12, Baa <aqua...@gmail.com> wrote: > Hello Dear List! > > Consider, I retrieve from external source some data. Internally it's > represented as some complex type with `Maybe` fields, even more, some > of fields are record types and have `Maybe` fields too. They are > Maybe's because some information in this data can be missing (user > error or it not very valuable and can be skipped): > > data A = A { > a1 :: Maybe B > ... } > data B = B { > b1 :: Maybe C > ... } > > I retrieve it from network, files, i.e. external world, then I validate > it, report errors of some missing fields, fix another one (which can be > fixed, for example, replace Nothing with `Just default_value` or even I > can fix `Just wrong` to `Just right`, etc, etc). After all of this, I > know that I have "clean" data, so all my complex types now have `Just > right_value` fields. But I need to process them as optional, with > possible Nothing case! To avoid it I must create copies of `A`, `B`, > etc, where `a1`, `b1` will be `B`, `C`, not `Maybe B`, `Maybe C`. Sure, > it's not a case. > > After processing and filtering, I create, for example, some resulting > objects: > > data Result { > a :: A -- not Maybe! > ... } > > And even more: `a::A` in `Result` (I know it, after filtering) will not > contain Nothings, only `Just right_values`s. > > But each function which consumes `A` must do something with possible > Nothing values even after filtering and fixing of `A`s. > > I have, for example, function: > > createResults :: [A] -> [Result] > createResults alst = > ... > case of (a1 theA) -> > Just right_value -> ... > Nothing -> > logError > undefined -- can not happen > > Fun here is: that it happens (I found bug in my filtering > code with this `undefined`). But now I thought about it: what is the > idiomatic way to solve such situation? When you need to have: > > - COMPLEX type WITH Maybes > - the same type WITHOUT Maybes > > Alternative is to keep this Maybes to the very end of processing, what I > don't like. Or to have types copies, which is more terrible, sure. > > PS. I threw IOs away to show only the crux of the problem. > > --- > Cheers, > Paul > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170706/fa31ea1d/attachment-0001.html> ------------------------------ Message: 3 Date: Thu, 6 Jul 2017 11:12:04 +0200 From: Jona Ekenberg <saik...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: [Haskell-beginners] Incomplete patterns Message-ID: <calveeueldexztzoflx-wy6fvwn-oc5bgxs6wxftg7qdbsvx...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hello, I am currently going through the CIS194 spring 13 course, since it has been recommended by several haskellions. I find the exercises extremely valuable, and the reading material is succinct (in a good way). I just finished the exercises for lesson two, and while everything works, I get this warning: Lektion02.hs:(38,1)-(44,21): warning: [-Wincomplete-patterns] … Pattern match(es) are non-exhaustive In an equation for ‘insert’: Patterns not matched: (LogMessage _ _ _) (Node _ (LogMessage _ _ _) _) I understand that there is a pattern I'm not covering, but I can't really figure out which one. Here is my function: insert :: LogMessage -> MessageTree -> MessageTree insert (Unknown _) mt = mt insert m Leaf = Node Leaf m Leaf insert m@(LogMessage _ t _) (Node left value@(LogMessage _ t' _) right) | t <= t' = Node (insert m left) value right | t > t' = Node left value (insert m right) insert m@(LogMessage _ _ _) (Node left (Unknown _) right) = Node left m right (here's a pastebin with intact indentation: http://lpaste.net/356716) I am not sure on how I should progress to find out which pattern I'm missing. Any help is appreciated. Kind regards, Jona -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170706/7ef33e7c/attachment-0001.html> ------------------------------ Message: 4 Date: Thu, 6 Jul 2017 11:21:35 +0200 From: Sylvain Henry <sylv...@haskus.fr> To: beginners@haskell.org Subject: Re: [Haskell-beginners] Incomplete patterns Message-ID: <203988f4-5c4c-e7f1-5d5a-ed5555ec5...@haskus.fr> Content-Type: text/plain; charset="utf-8"; Format="flowed" Hi, In the following code: insert m@(LogMessage _ t _) (Node left value@(LogMessage _ t' _) right) | t <= t' = Node (insert m left) value right | t > t' = Node left value (insert m right) GHC can't detect that both guards (t <= t') and (t > t') cover all cases. The usual way to deal with this is to use "otherwise" (which is always true): insert m@(LogMessage _ t _) (Node left value@(LogMessage _ t' _) right) | t <= t' = Node (insert m left) value right | otherwise = Node left value (insert m right) Regards, Sylvain On 06/07/2017 11:12, Jona Ekenberg wrote: > Hello, > > I am currently going through the CIS194 spring 13 course, since it has > been recommended by several haskellions. I find the exercises > extremely valuable, and the reading material is succinct (in a good way). > > I just finished the exercises for lesson two, and while everything > works, I get this warning: > Lektion02.hs:(38,1)-(44,21): warning: [-Wincomplete-patterns] … > Pattern match(es) are non-exhaustive > In an equation for ‘insert’: > Patterns not matched: > (LogMessage _ _ _) (Node _ (LogMessage _ _ _) _) > > I understand that there is a pattern I'm not covering, but I can't > really figure out which one. Here is my function: > insert :: LogMessage -> MessageTree -> MessageTree > insert (Unknown _) mt = mt > insert m Leaf = Node Leaf m Leaf > insert m@(LogMessage _ t _) (Node left value@(LogMessage _ t' _) right) > | t <= t' = Node (insert m left) value right > | t > t' = Node left value (insert m right) > insert m@(LogMessage _ _ _) (Node left (Unknown _) right) > = Node left m right > > (here's a pastebin with intact indentation: http://lpaste.net/356716) > > I am not sure on how I should progress to find out which pattern I'm > missing. > Any help is appreciated. > > Kind regards, > Jona > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170706/f36852cf/attachment.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 109, Issue 5 *****************************************