From borgauf at gmail.com Sun Dec 12 05:14:03 2021 From: borgauf at gmail.com (Galaxy Being) Date: Sat, 11 Dec 2021 23:14:03 -0600 Subject: [Haskell-beginners] Non-exhaustive patterns Message-ID: This code myTakePM :: Int -> [a] -> [a] myTakePM 0 _ = [] myTakePM n (x:xs) = x : myTakePM (n-1) xs is bad because it allows myTakePM 4 [1,2,3] [1,2,3*** Exception: :(395,1)-(396,41): Non-exhaustive patterns in function myTakePM I knew it would not work, but why is it calling this essentially a partial function? How does it know this? Again, I expected an error, but what is this Non-exhaustive patterns in function myTakePM saying? Or, said another way, what exactly is non-exhaustive about this? -- ⨽ Lawrence Bottorff Grand Marais, MN, USA borgauf at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From pietro.gra at hotmail.it Sun Dec 12 05:21:34 2021 From: pietro.gra at hotmail.it (Pietro Grandinetti) Date: Sun, 12 Dec 2021 05:21:34 +0000 Subject: [Haskell-beginners] Non-exhaustive patterns In-Reply-To: References: Message-ID: Hello - I think the pattern for the empty list (as second argument) is missing. ________________________________ From: Beginners on behalf of Galaxy Being Sent: Sunday, December 12, 2021 6:14:03 AM To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: [Haskell-beginners] Non-exhaustive patterns This code myTakePM :: Int -> [a] -> [a] myTakePM 0 _ = [] myTakePM n (x:xs) = x : myTakePM (n-1) xs is bad because it allows myTakePM 4 [1,2,3] [1,2,3*** Exception: :(395,1)-(396,41): Non-exhaustive patterns in function myTakePM I knew it would not work, but why is it calling this essentially a partial function? How does it know this? Again, I expected an error, but what is this Non-exhaustive patterns in function myTakePM saying? Or, said another way, what exactly is non-exhaustive about this? -- ⨽ Lawrence Bottorff Grand Marais, MN, USA borgauf at gmail.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael.eugene.turner at gmail.com Sun Dec 12 10:57:03 2021 From: michael.eugene.turner at gmail.com (Michael Turner) Date: Sun, 12 Dec 2021 19:57:03 +0900 Subject: [Haskell-beginners] But I just want a function that returns stack depth! Message-ID: The following prints 1, as you'd expect: ----------------- import GHC.Stack foo :: HasCallStack => IO () foo = do print (length (getCallStack callStack)) main = foo ------------------- But when I make it this: ... l <- getCallStack callStack ; print (length l) I get all this: ------------------------------------------ ... • Couldn't match type ‘[]’ with ‘IO’ Expected type: IO ([Char], SrcLoc) Actual type: [([Char], SrcLoc)] • In a stmt of a 'do' block: l <- getCallStack callStack In the expression: do l <- getCallStack callStack print (length l) In an equation for ‘foo’: foo = do l <- getCallStack callStack print (length l) | 5 | l <- getCallStack callStack ; | ^^^^^^^^^^^^^^^^^^^^^^ -------------------------------------------- What am I not seeing? Regards, Michael Turner Executive Director Project Persephone 1-25-33 Takadanobaba Shinjuku-ku Tokyo 169-0075 Mobile: +81 (90) 5203-8682 turner at projectpersephone.org Understand - http://www.projectpersephone.org/ Join - http://www.facebook.com/groups/ProjectPersephone/ Donate - http://www.patreon.com/ProjectPersephone Volunteer - https://github.com/ProjectPersephone "Love does not consist in gazing at each other, but in looking outward together in the same direction." -- Antoine de Saint-Exupéry From yehoshuapw at gmail.com Sun Dec 12 11:46:28 2021 From: yehoshuapw at gmail.com (=?UTF-8?B?15nXlNeV16nXoiDXldec15o=?=) Date: Sun, 12 Dec 2021 13:46:28 +0200 Subject: [Haskell-beginners] But I just want a function that returns stack depth! In-Reply-To: References: Message-ID: have another look at https://hackage.haskell.org/packaggetCallStackgetCallStacke/base-4.16.0.0/docs/GHC-Stack.html#v:callStack You need ``` let l = getCallStack callStack print (length l) ``` the `getCallStack` does not return an "IO" value.. On Sun, Dec 12, 2021, 12:58 Michael Turner wrote: > The following prints 1, as you'd expect: > ----------------- > import GHC.Stack > > foo :: HasCallStack => IO () > foo = do > print (length (getCallStack callStack)) > > main = > foo > ------------------- > But when I make it this: > ... > l <- getCallStack callStack ; > print (length l) > > I get all this: > ------------------------------------------ > ... > • Couldn't match type ‘[]’ with ‘IO’ > Expected type: IO ([Char], SrcLoc) > Actual type: [([Char], SrcLoc)] > • In a stmt of a 'do' block: l <- getCallStack callStack > In the expression: > do l <- getCallStack callStack > print (length l) > In an equation for ‘foo’: > foo > = do l <- getCallStack callStack > print (length l) > | > 5 | l <- getCallStack callStack ; > | ^^^^^^^^^^^^^^^^^^^^^^ > -------------------------------------------- > > What am I not seeing? > > > Regards, > Michael Turner > Executive Director > Project Persephone > 1-25-33 Takadanobaba > > Shinjuku-ku Tokyo 169-0075 > Mobile: +81 (90) 5203-8682 > turner at projectpersephone.org > > Understand - http://www.projectpersephone.org/ > Join - http://www.facebook.com/groups/ProjectPersephone/ > Donate - http://www.patreon.com/ProjectPersephone > Volunteer - https://github.com/ProjectPersephone > > "Love does not consist in gazing at each other, but in looking outward > together in the same direction." -- Antoine de Saint-Exupéry > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael.eugene.turner at gmail.com Sun Dec 12 15:31:34 2021 From: michael.eugene.turner at gmail.com (Michael Turner) Date: Mon, 13 Dec 2021 00:31:34 +0900 Subject: [Haskell-beginners] Beginners Digest, Vol 161, Issue 1 In-Reply-To: References: Message-ID: Regards, Michael Turner Executive Director Project Persephone 1-25-33 Takadanobaba Shinjuku-ku Tokyo 169-0075 Mobile: +81 (90) 5203-8682 turner at projectpersephone.org Understand - http://www.projectpersephone.org/ Join - http://www.facebook.com/groups/ProjectPersephone/ Donate - http://www.patreon.com/ProjectPersephone Volunteer - https://github.com/ProjectPersephone "Love does not consist in gazing at each other, but in looking outward together in the same direction." -- Antoine de Saint-Exupéry On Sun, Dec 12, 2021 at 8:48 PM wrote: > have another look at > https://hackage.haskell.org/packaggetCallStackgetCallStacke/base-4.16.0.0/docs/GHC-Stack.html#v:callStack > I'd been looking at that already, over and over. Still not seeing it. But your proposed fix does work. Thank you. > let l = getCallStack callStack > print (length l) > ``` > the `getCallStack` does not return an "IO" value.. And yet it's it's somehow an IO value in "print (length (getCallStack callStack))"? I just don't get this. > On Sun, Dec 12, 2021, 12:58 Michael Turner > wrote: > > > The following prints 1, as you'd expect: > > ----------------- > > import GHC.Stack > > > > foo :: HasCallStack => IO () > > foo = do > > print (length (getCallStack callStack)) > > > > main = > > foo > > ------------------- > > But when I make it this: > > ... > > l <- getCallStack callStack ; > > print (length l) > > > > I get all this: > > ------------------------------------------ > > ... > > • Couldn't match type ‘[]’ with ‘IO’ > > Expected type: IO ([Char], SrcLoc) > > Actual type: [([Char], SrcLoc)] > > • In a stmt of a 'do' block: l <- getCallStack callStack > > In the expression: > > do l <- getCallStack callStack > > print (length l) > > In an equation for ‘foo’: > > foo > > = do l <- getCallStack callStack > > print (length l) > > | > > 5 | l <- getCallStack callStack ; > > | ^^^^^^^^^^^^^^^^^^^^^^ > > -------------------------------------------- > > > > What am I not seeing? > > > > > > Regards, > > Michael Turner > > Executive Director > > Project Persephone > > 1-25-33 Takadanobaba > > > > Shinjuku-ku Tokyo 169-0075 > > Mobile: +81 (90) 5203-8682 > > turner at projectpersephone.org > > > > Understand - http://www.projectpersephone.org/ > > Join - http://www.facebook.com/groups/ProjectPersephone/ > > Donate - http://www.patreon.com/ProjectPersephone > > Volunteer - https://github.com/ProjectPersephone > > > > "Love does not consist in gazing at each other, but in looking outward > > together in the same direction." -- Antoine de Saint-Exupéry From yehoshuapw at gmail.com Sun Dec 12 17:05:22 2021 From: yehoshuapw at gmail.com (=?UTF-8?B?15nXlNeV16nXoiDXldec15o=?=) Date: Sun, 12 Dec 2021 19:05:22 +0200 Subject: [Haskell-beginners] Beginners Digest, Vol 161, Issue 1 In-Reply-To: References: Message-ID: Notice: (from ghci) Prelude GHC.Stack> :t callStack callStack :: CallStack Prelude GHC.Stack> :t getCallStack getCallStack :: CallStack -> [([Char], SrcLoc)] Prelude GHC.Stack> :t getCallStack callStack getCallStack callStack :: [([Char], SrcLoc)] Prelude GHC.Stack> :t length (getCallStack callStack) length (getCallStack callStack) :: Int Prelude GHC.Stack> :t print (length (getCallStack callStack)) print (length (getCallStack callStack)) :: IO () Prelude GHC.Stack> :t print print :: Show a => a -> IO () the `print` function takes a pure value, and creates an IO value from it. (actually printing is an IO action. the value it prints is not) On Sun, Dec 12, 2021 at 5:32 PM Michael Turner < michael.eugene.turner at gmail.com> wrote: > Regards, > Michael Turner > Executive Director > Project Persephone > 1-25-33 Takadanobaba > > Shinjuku-ku Tokyo 169-0075 > Mobile: +81 (90) 5203-8682 > turner at projectpersephone.org > > Understand - http://www.projectpersephone.org/ > Join - http://www.facebook.com/groups/ProjectPersephone/ > Donate - http://www.patreon.com/ProjectPersephone > Volunteer - https://github.com/ProjectPersephone > > "Love does not consist in gazing at each other, but in looking outward > together in the same direction." -- Antoine de Saint-Exupéry > > > > > On Sun, Dec 12, 2021 at 8:48 PM wrote: > > > > have another look at > > > https://hackage.haskell.org/packaggetCallStackgetCallStacke/base-4.16.0.0/docs/GHC-Stack.html#v:callStack > > < > https://hackage.haskell.org/package/base-4.16.0.0/docs/GHC-Stack.html#v:callStack > > > > I'd been looking at that already, over and over. Still not seeing it. > But your proposed fix does work. Thank you. > > let l = getCallStack callStack > > print (length l) > > ``` > > the `getCallStack` does not return an "IO" value.. > > And yet it's it's somehow an IO value in "print (length (getCallStack > callStack))"? > > I just don't get this. > > > On Sun, Dec 12, 2021, 12:58 Michael Turner < > michael.eugene.turner at gmail.com> > > wrote: > > > > > The following prints 1, as you'd expect: > > > ----------------- > > > import GHC.Stack > > > > > > foo :: HasCallStack => IO () > > > foo = do > > > print (length (getCallStack callStack)) > > > > > > main = > > > foo > > > ------------------- > > > But when I make it this: > > > ... > > > l <- getCallStack callStack ; > > > print (length l) > > > > > > I get all this: > > > ------------------------------------------ > > > ... > > > • Couldn't match type ‘[]’ with ‘IO’ > > > Expected type: IO ([Char], SrcLoc) > > > Actual type: [([Char], SrcLoc)] > > > • In a stmt of a 'do' block: l <- getCallStack callStack > > > In the expression: > > > do l <- getCallStack callStack > > > print (length l) > > > In an equation for ‘foo’: > > > foo > > > = do l <- getCallStack callStack > > > print (length l) > > > | > > > 5 | l <- getCallStack callStack ; > > > | ^^^^^^^^^^^^^^^^^^^^^^ > > > -------------------------------------------- > > > > > > What am I not seeing? > > > > > > > > > Regards, > > > Michael Turner > > > Executive Director > > > Project Persephone > > > 1-25-33 Takadanobaba > > > > > > Shinjuku-ku Tokyo 169-0075 > > > Mobile: +81 (90) 5203-8682 > > > turner at projectpersephone.org > > > > > > Understand - http://www.projectpersephone.org/ > > > Join - http://www.facebook.com/groups/ProjectPersephone/ > > > Donate - http://www.patreon.com/ProjectPersephone > > > Volunteer - https://github.com/ProjectPersephone > > > > > > "Love does not consist in gazing at each other, but in looking outward > > > together in the same direction." -- Antoine de Saint-Exupéry > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -- -------------- next part -------------- An HTML attachment was scrubbed... URL: From yehoshuapw at gmail.com Sun Dec 12 17:09:17 2021 From: yehoshuapw at gmail.com (=?UTF-8?B?15nXlNeV16nXoiDXldec15o=?=) Date: Sun, 12 Dec 2021 19:09:17 +0200 Subject: [Haskell-beginners] Beginners Digest, Vol 161, Issue 1 In-Reply-To: References: Message-ID: The important thing here, is that print is a function from a "pure" value (any type implements the Show typeclass. (similar to trait in rust, or interface in some languages)) and creates a "IO" value, which can be thought of as a IO action. inside the IO monad, that just means running it. ‪On Sun, Dec 12, 2021 at 7:05 PM ‫יהושע ולך‬‎ wrote:‬ > Notice: (from ghci) > > Prelude GHC.Stack> :t callStack > callStack :: CallStack > Prelude GHC.Stack> :t getCallStack > getCallStack :: CallStack -> [([Char], SrcLoc)] > Prelude GHC.Stack> :t getCallStack callStack > getCallStack callStack :: [([Char], SrcLoc)] > Prelude GHC.Stack> :t length (getCallStack callStack) > length (getCallStack callStack) :: Int > > Prelude GHC.Stack> :t print (length (getCallStack callStack)) > print (length (getCallStack callStack)) :: IO () > > Prelude GHC.Stack> :t print > print :: Show a => a -> IO () > > > the `print` function takes a pure value, and creates an IO value from it. > (actually printing is an IO action. the value it prints is not) > > > > On Sun, Dec 12, 2021 at 5:32 PM Michael Turner < > michael.eugene.turner at gmail.com> wrote: > >> Regards, >> Michael Turner >> Executive Director >> Project Persephone >> 1-25-33 Takadanobaba >> >> Shinjuku-ku Tokyo 169-0075 >> Mobile: +81 (90) 5203-8682 >> turner at projectpersephone.org >> >> Understand - http://www.projectpersephone.org/ >> Join - http://www.facebook.com/groups/ProjectPersephone/ >> Donate - http://www.patreon.com/ProjectPersephone >> Volunteer - https://github.com/ProjectPersephone >> >> "Love does not consist in gazing at each other, but in looking outward >> together in the same direction." -- Antoine de Saint-Exupéry >> >> >> >> >> On Sun, Dec 12, 2021 at 8:48 PM wrote: >> >> >> > have another look at >> > >> https://hackage.haskell.org/packaggetCallStackgetCallStacke/base-4.16.0.0/docs/GHC-Stack.html#v:callStack >> > < >> https://hackage.haskell.org/package/base-4.16.0.0/docs/GHC-Stack.html#v:callStack >> > >> >> I'd been looking at that already, over and over. Still not seeing it. >> But your proposed fix does work. Thank you. >> > let l = getCallStack callStack >> > print (length l) >> > ``` >> > the `getCallStack` does not return an "IO" value.. >> >> And yet it's it's somehow an IO value in "print (length (getCallStack >> callStack))"? >> >> I just don't get this. >> >> > On Sun, Dec 12, 2021, 12:58 Michael Turner < >> michael.eugene.turner at gmail.com> >> > wrote: >> > >> > > The following prints 1, as you'd expect: >> > > ----------------- >> > > import GHC.Stack >> > > >> > > foo :: HasCallStack => IO () >> > > foo = do >> > > print (length (getCallStack callStack)) >> > > >> > > main = >> > > foo >> > > ------------------- >> > > But when I make it this: >> > > ... >> > > l <- getCallStack callStack ; >> > > print (length l) >> > > >> > > I get all this: >> > > ------------------------------------------ >> > > ... >> > > • Couldn't match type ‘[]’ with ‘IO’ >> > > Expected type: IO ([Char], SrcLoc) >> > > Actual type: [([Char], SrcLoc)] >> > > • In a stmt of a 'do' block: l <- getCallStack callStack >> > > In the expression: >> > > do l <- getCallStack callStack >> > > print (length l) >> > > In an equation for ‘foo’: >> > > foo >> > > = do l <- getCallStack callStack >> > > print (length l) >> > > | >> > > 5 | l <- getCallStack callStack ; >> > > | ^^^^^^^^^^^^^^^^^^^^^^ >> > > -------------------------------------------- >> > > >> > > What am I not seeing? >> > > >> > > >> > > Regards, >> > > Michael Turner >> > > Executive Director >> > > Project Persephone >> > > 1-25-33 Takadanobaba >> > > >> > > Shinjuku-ku Tokyo 169-0075 >> > > Mobile: +81 (90) 5203-8682 >> > > turner at projectpersephone.org >> > > >> > > Understand - http://www.projectpersephone.org/ >> > > Join - http://www.facebook.com/groups/ProjectPersephone/ >> > > Donate - http://www.patreon.com/ProjectPersephone >> > > Volunteer - https://github.com/ProjectPersephone >> > > >> > > "Love does not consist in gazing at each other, but in looking outward >> > > together in the same direction." -- Antoine de Saint-Exupéry >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > > > -- > > > -- -------------- next part -------------- An HTML attachment was scrubbed... URL: From yehoshuapw at gmail.com Sun Dec 12 17:11:25 2021 From: yehoshuapw at gmail.com (=?UTF-8?B?15nXlNeV16nXoiDXldec15o=?=) Date: Sun, 12 Dec 2021 19:11:25 +0200 Subject: [Haskell-beginners] Beginners Digest, Vol 161, Issue 1 In-Reply-To: References: Message-ID: http://learnyouahaskell.com/input-and-output might help. ‪On Sun, Dec 12, 2021 at 7:09 PM ‫יהושע ולך‬‎ wrote:‬ > The important thing here, is that print is a function from a "pure" value > (any type implements the Show typeclass. (similar to trait in rust, or > interface in some languages)) > and creates a "IO" value, which can be thought of as a IO action. inside > the IO monad, that just means running it. > > > ‪On Sun, Dec 12, 2021 at 7:05 PM ‫יהושע ולך‬‎ > wrote:‬ > >> Notice: (from ghci) >> >> Prelude GHC.Stack> :t callStack >> callStack :: CallStack >> Prelude GHC.Stack> :t getCallStack >> getCallStack :: CallStack -> [([Char], SrcLoc)] >> Prelude GHC.Stack> :t getCallStack callStack >> getCallStack callStack :: [([Char], SrcLoc)] >> Prelude GHC.Stack> :t length (getCallStack callStack) >> length (getCallStack callStack) :: Int >> >> Prelude GHC.Stack> :t print (length (getCallStack callStack)) >> print (length (getCallStack callStack)) :: IO () >> >> Prelude GHC.Stack> :t print >> print :: Show a => a -> IO () >> >> >> the `print` function takes a pure value, and creates an IO value from it. >> (actually printing is an IO action. the value it prints is not) >> >> >> >> On Sun, Dec 12, 2021 at 5:32 PM Michael Turner < >> michael.eugene.turner at gmail.com> wrote: >> >>> Regards, >>> Michael Turner >>> Executive Director >>> Project Persephone >>> 1-25-33 Takadanobaba >>> >>> Shinjuku-ku Tokyo 169-0075 >>> Mobile: +81 (90) 5203-8682 >>> turner at projectpersephone.org >>> >>> Understand - http://www.projectpersephone.org/ >>> Join - http://www.facebook.com/groups/ProjectPersephone/ >>> Donate - http://www.patreon.com/ProjectPersephone >>> Volunteer - https://github.com/ProjectPersephone >>> >>> "Love does not consist in gazing at each other, but in looking outward >>> together in the same direction." -- Antoine de Saint-Exupéry >>> >>> >>> >>> >>> On Sun, Dec 12, 2021 at 8:48 PM wrote: >>> >>> >>> > have another look at >>> > >>> https://hackage.haskell.org/packaggetCallStackgetCallStacke/base-4.16.0.0/docs/GHC-Stack.html#v:callStack >>> > < >>> https://hackage.haskell.org/package/base-4.16.0.0/docs/GHC-Stack.html#v:callStack >>> > >>> >>> I'd been looking at that already, over and over. Still not seeing it. >>> But your proposed fix does work. Thank you. >>> > let l = getCallStack callStack >>> > print (length l) >>> > ``` >>> > the `getCallStack` does not return an "IO" value.. >>> >>> And yet it's it's somehow an IO value in "print (length (getCallStack >>> callStack))"? >>> >>> I just don't get this. >>> >>> > On Sun, Dec 12, 2021, 12:58 Michael Turner < >>> michael.eugene.turner at gmail.com> >>> > wrote: >>> > >>> > > The following prints 1, as you'd expect: >>> > > ----------------- >>> > > import GHC.Stack >>> > > >>> > > foo :: HasCallStack => IO () >>> > > foo = do >>> > > print (length (getCallStack callStack)) >>> > > >>> > > main = >>> > > foo >>> > > ------------------- >>> > > But when I make it this: >>> > > ... >>> > > l <- getCallStack callStack ; >>> > > print (length l) >>> > > >>> > > I get all this: >>> > > ------------------------------------------ >>> > > ... >>> > > • Couldn't match type ‘[]’ with ‘IO’ >>> > > Expected type: IO ([Char], SrcLoc) >>> > > Actual type: [([Char], SrcLoc)] >>> > > • In a stmt of a 'do' block: l <- getCallStack callStack >>> > > In the expression: >>> > > do l <- getCallStack callStack >>> > > print (length l) >>> > > In an equation for ‘foo’: >>> > > foo >>> > > = do l <- getCallStack callStack >>> > > print (length l) >>> > > | >>> > > 5 | l <- getCallStack callStack ; >>> > > | ^^^^^^^^^^^^^^^^^^^^^^ >>> > > -------------------------------------------- >>> > > >>> > > What am I not seeing? >>> > > >>> > > >>> > > Regards, >>> > > Michael Turner >>> > > Executive Director >>> > > Project Persephone >>> > > 1-25-33 Takadanobaba >>> > > >>> > > Shinjuku-ku Tokyo 169-0075 >>> > > Mobile: +81 (90) 5203-8682 >>> > > turner at projectpersephone.org >>> > > >>> > > Understand - http://www.projectpersephone.org/ >>> > > Join - http://www.facebook.com/groups/ProjectPersephone/ >>> > > Donate - http://www.patreon.com/ProjectPersephone >>> > > Volunteer - https://github.com/ProjectPersephone >>> > > >>> > > "Love does not consist in gazing at each other, but in looking >>> outward >>> > > together in the same direction." -- Antoine de Saint-Exupéry >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>> >> >> >> -- >> >> >> > > -- > > > -- -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeffbrown.the at gmail.com Sun Dec 12 19:08:49 2021 From: jeffbrown.the at gmail.com (Jeffrey Brown) Date: Sun, 12 Dec 2021 14:08:49 -0500 Subject: [Haskell-beginners] Non-exhaustive patterns In-Reply-To: References: Message-ID: The function as written assumes that if the first argument is not zero, then the second argument is nonempty. But if you try to take5 from a list of length 2 that way, you'll eventually be asking for `myTake 3 []`, which there's no way to evaluate. On Sun, Dec 12, 2021 at 12:14 AM Galaxy Being wrote: > This code > > myTakePM :: Int -> [a] -> [a] > myTakePM 0 _ = [] > myTakePM n (x:xs) = x : myTakePM (n-1) xs > > is bad because it allows > > myTakePM 4 [1,2,3] > [1,2,3*** Exception: :(395,1)-(396,41): Non-exhaustive > patterns in function myTakePM > > I knew it would not work, but why is it calling this essentially a partial > function? How does it know this? Again, I expected an error, but what is > this Non-exhaustive patterns in function myTakePM saying? Or, said > another way, what exactly is non-exhaustive about this? > -- > ⨽ > Lawrence Bottorff > Grand Marais, MN, USA > borgauf at gmail.com > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -- Jeff Brown | Jeffrey Benjamin Brown LinkedIn | Github | Twitter | Facebook -------------- next part -------------- An HTML attachment was scrubbed... URL: From coot at coot.me Tue Dec 28 18:34:53 2021 From: coot at coot.me (coot at coot.me) Date: Tue, 28 Dec 2021 18:34:53 +0000 Subject: [Haskell-beginners] Monad question In-Reply-To: References: <20210912105738.egomltlkquv5le5l@gmail.com> Message-ID: Hi Mike, An applicative instance of `Maybe` is enough to write the convert function you're looking for. Applicative is very useful when there are no dependencies between different branches, like when converting `Add`. But anyway, converting between applicative and monadic operations is a good exercise, which I'll leave for you. It's always possible to convert from applicative to monadic form, but the more useful conversion is from monadic to applicative (which is not always possible): ``` convert :: Expr (Maybe a) -> Maybe (Expr a) convert (Var a) = Var <$> a convert (Add a b) = Add <$> convert a <*> convert b ``` Best regards, Marcin Sent with ProtonMail Secure Email. ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐ On Thursday, November 25th, 2021 at 18:10, mike h wrote: > Hi, > > This isn’t homework! I’ve been staring at this for several hours - and that usually works. > > I also tried typed holes to no avail. I thinks it is quite simple really but I’ve gone past seeing it! > > I have > > data Expr a = Var a | Add (Expr a) (Expr a) > > and would like to write > > convert :: Expr (Maybe a) -> Maybe (Expr a) > > which returns Nothing if there is an occurrence of Nothing inside the > > input expression e, otherwise it returns Just e', where e' > > is a new expression where the internal values of type a are not wrapped in Just. > > You should use the functionality of the Maybe monad to implement > > the convert function. > > Thanks > > Mike > > Beginners mailing list > > Beginners at haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 509 bytes Desc: OpenPGP digital signature URL: From sgf.dma at gmail.com Thu Dec 30 13:10:01 2021 From: sgf.dma at gmail.com (Dmitriy Matrosov) Date: Thu, 30 Dec 2021 16:10:01 +0300 Subject: [Haskell-beginners] Coercing existential according to type-level Maybe Message-ID: Hi. I've tried to write a function to coerce existential value according to type stored in type-level 'Maybe' (i.e. 'Just or 'Nothing). > {-# LANGUAGE DataKinds #-} > {-# LANGUAGE RankNTypes #-} > {-# LANGUAGE KindSignatures #-} > {-# LANGUAGE PolyKinds #-} > {-# LANGUAGE TypeFamilies #-} > {-# LANGUAGE GADTs #-} > > import Data.Kind > import Data.Proxy > import Unsafe.Coerce > > class FromTypeMaybe k where > type ToType k > fromTypeMaybe :: (a -> ToType k) -> Proxy k -> a -> Maybe (ToType k) > > instance forall (t :: Type). FromTypeMaybe ('Nothing :: Maybe t) where > type ToType 'Nothing = t > fromTypeMaybe f _ x = Nothing > > instance forall (t :: Type). FromTypeMaybe ('Just t :: Maybe Type) where > type ToType ('Just t) = t > fromTypeMaybe f _ x = Just (f x) > > data Any where > Any :: a -> Any > > unAny :: Any -> t > unAny (Any x) = unsafeCoerce x This works as far as i can see *Main> fromTypeMaybe unAny (Proxy @('Just Int)) (Any 3) Just 3 *Main> fromTypeMaybe unAny (Proxy @'Nothing) undefined Nothing but i don't understand how 'Nothing instance works: type kind kind? vvv vvvvvv vvv instance forall (t :: Type). FromTypeMaybe ('Nothing :: Maybe t) where type ToType 'Nothing = t ^^^ type (in case alignment breaks: variable 't' in forall is type-variable with kind 'Type', but in "Maybe t" in instance head it's used as kind-variable. And then in associated type-family the same variable 't' is used as type-variable again). If i try to write "'Nothing"'s kind as 'Maybe Type' (in the same manner as 'Just has) instance forall (t :: Type). FromTypeMaybe ('Nothing :: Maybe Type) where i get an error: 1.lhs:21:3: error: • Type variable ‘t’ is mentioned in the RHS, but not bound on the LHS of the family instance • In the type instance declaration for ‘ToType’ In the instance declaration for ‘FromTypeMaybe ('Nothing :: Maybe Type)’ | 21 | > instance forall (t :: Type). FromTypeMaybe ('Nothing :: Maybe Type) where | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^... Well, i'm not sure, that understand why 'forall' in instance head may not be used as family's LHS, but.. that's probably ok. On the other hand, if i try to write 'Just instance using type-variable 't' as kind-variable (in the same manner as 'Nothing has): instance forall (t :: Type). FromTypeMaybe ('Just t :: Maybe t) where i get an error too: 1.lhs:25:47: error: • Expected kind ‘Maybe t’, but ‘'Just t’ has kind ‘Maybe *’ • In the first argument of ‘FromTypeMaybe’, namely ‘('Just t :: Maybe t)’ In the instance declaration for ‘FromTypeMaybe ('Just t :: Maybe t)’ | 25 | > instance forall (t :: Type). FromTypeMaybe ('Just t :: Maybe t) where Well, that's also probably expected, because though i've declared type-variable 't' with kind 'Type' in forall, due to 'DataKinds' extension type 't' is also promoted to kind 't' and, thus, by using ('Just t :: Maybe t) i say, that ('Just t) should have kind 'Maybe t', not 'Maybe Type', which it really has. But if that's so, how working 'Nothing instance instance forall (t :: Type). FromTypeMaybe ('Nothing :: Maybe t) where may work at all? It has the same problem as 'Just instance above: type-variable 't' is promoted to kind 't' and 'Nothing will have kind 'Maybe t' instead of 'Maybe Type' ? From ky3 at atamo.com Thu Dec 30 14:40:47 2021 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Thu, 30 Dec 2021 22:40:47 +0800 Subject: [Haskell-beginners] Coercing existential according to type-level Maybe In-Reply-To: References: Message-ID: Hi Dmitriy, Would you like to re-ask over at the haskell cafe mailing list? The beginners list focuses on topics at the level of LYAH. Great for splaining what a monad is, not so good at coercing existentials via a type-level Maybe. Best, Kim-Ee On Thu, Dec 30, 2021 at 9:10 PM Dmitriy Matrosov wrote: > Hi. > > I've tried to write a function to coerce existential value according to > type > stored in type-level 'Maybe' (i.e. 'Just or 'Nothing). > > > {-# LANGUAGE DataKinds #-} > > {-# LANGUAGE RankNTypes #-} > > {-# LANGUAGE KindSignatures #-} > > {-# LANGUAGE PolyKinds #-} > > {-# LANGUAGE TypeFamilies #-} > > {-# LANGUAGE GADTs #-} > > > > import Data.Kind > > import Data.Proxy > > import Unsafe.Coerce > > > > class FromTypeMaybe k where > > type ToType k > > fromTypeMaybe :: (a -> ToType k) -> Proxy k -> a -> Maybe (ToType k) > > > > instance forall (t :: Type). FromTypeMaybe ('Nothing :: Maybe t) where > > type ToType 'Nothing = t > > fromTypeMaybe f _ x = Nothing > > > > instance forall (t :: Type). FromTypeMaybe ('Just t :: Maybe Type) where > > type ToType ('Just t) = t > > fromTypeMaybe f _ x = Just (f x) > > > > data Any where > > Any :: a -> Any > > > > unAny :: Any -> t > > unAny (Any x) = unsafeCoerce x > > This works as far as i can see > > *Main> fromTypeMaybe unAny (Proxy @('Just Int)) (Any 3) > Just 3 > *Main> fromTypeMaybe unAny (Proxy @'Nothing) undefined > Nothing > > but i don't understand how 'Nothing instance works: > > type kind kind? > vvv vvvvvv vvv > instance forall (t :: Type). FromTypeMaybe ('Nothing :: Maybe t) where > type ToType 'Nothing = t > ^^^ > type > > (in case alignment breaks: variable 't' in forall is type-variable with > kind > 'Type', but in "Maybe t" in instance head it's used as kind-variable. And > then > in associated type-family the same variable 't' is used as type-variable > again). > > If i try to write "'Nothing"'s kind as 'Maybe Type' (in the same manner as > 'Just has) > > instance forall (t :: Type). FromTypeMaybe ('Nothing :: Maybe Type) > where > > i get an error: > > 1.lhs:21:3: error: > • Type variable ‘t’ is mentioned in the RHS, > but not bound on the LHS of the family instance > • In the type instance declaration for ‘ToType’ > In the instance declaration for > ‘FromTypeMaybe ('Nothing :: Maybe Type)’ > | > 21 | > instance forall (t :: Type). FromTypeMaybe ('Nothing :: > Maybe Type) where > | > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^... > > Well, i'm not sure, that understand why 'forall' in instance head may not > be > used as family's LHS, but.. that's probably ok. On the other hand, if i > try > to write 'Just instance using type-variable 't' as kind-variable (in the > same > manner as 'Nothing has): > > instance forall (t :: Type). FromTypeMaybe ('Just t :: Maybe t) where > > i get an error too: > > 1.lhs:25:47: error: > • Expected kind ‘Maybe t’, but ‘'Just t’ has kind ‘Maybe *’ > • In the first argument of ‘FromTypeMaybe’, namely > ‘('Just t :: Maybe t)’ > In the instance declaration for > ‘FromTypeMaybe ('Just t :: Maybe t)’ > | > 25 | > instance forall (t :: Type). FromTypeMaybe ('Just t :: Maybe t) > where > > Well, that's also probably expected, because though i've declared > type-variable 't' with kind 'Type' in forall, due to 'DataKinds' extension > type 't' is also promoted to kind 't' and, thus, by using ('Just t :: > Maybe t) > i say, that ('Just t) should have kind 'Maybe t', not 'Maybe Type', which > it > really has. > > But if that's so, how working 'Nothing instance > > instance forall (t :: Type). FromTypeMaybe ('Nothing :: Maybe t) where > > may work at all? It has the same problem as 'Just instance above: > type-variable 't' is promoted to kind 't' and 'Nothing will have kind > 'Maybe > t' instead of 'Maybe Type' ? > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -- -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: