From laiboonh at gmail.com Sat Oct 1 04:54:26 2016 From: laiboonh at gmail.com (Lai Boon Hui) Date: Sat, 1 Oct 2016 12:54:26 +0800 Subject: [Haskell-beginners] Just wanted to share some GHCI macros with fellow beginners Message-ID: Hi fellow beginners, wanted to share some macros you can add to your ghci config file so that you don't have to keep switching between ghci and command line. In my case i use Git a lot :def pwd (\_-> System.Directory.getCurrentDirectory >>= print >> return "") :def gitA (\_ -> System.Process.rawSystem "git" ["add", "-A"] >>= print >> return "") :def gitC (\m -> System.Process.rawSystem "git" ["commit", "-am", m] >>= print >> return "") :def gitP (\_ -> System.Process.rawSystem "git" ["push"] >>= print >> return "") -- Best Regards, Boon Hui -------------- next part -------------- An HTML attachment was scrubbed... URL: From andrew.bernard at gmail.com Sat Oct 1 10:42:47 2016 From: andrew.bernard at gmail.com (Andrew Bernard) Date: Sat, 1 Oct 2016 20:42:47 +1000 Subject: [Haskell-beginners] Just wanted to share some GHCI macros with fellow beginners In-Reply-To: References: Message-ID: Hi Boon, What's wrong with these? > :!pwd > :!git Andrew On 1 October 2016 at 14:54, Lai Boon Hui wrote: > Hi fellow beginners, > > wanted to share some macros you can add to your ghci config file so that > you don't have to keep switching between ghci and command line. In my case > i use Git a lot > > :def pwd (\_-> System.Directory.getCurrentDirectory >>= print >> return "") > :def gitA (\_ -> System.Process.rawSystem "git" ["add", "-A"] >>= print >> return "") > :def gitC (\m -> System.Process.rawSystem "git" ["commit", "-am", m] >>= print >> return "") > :def gitP (\_ -> System.Process.rawSystem "git" ["push"] >>= print >> return "") > > > -- > Best Regards, > Boon Hui > > _______________________________________________ > 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 patrick.browne at dit.ie Sun Oct 2 14:54:07 2016 From: patrick.browne at dit.ie (PATRICK BROWNE) Date: Sun, 2 Oct 2016 15:54:07 +0100 Subject: [Haskell-beginners] Problems with lifting code Message-ID: Hi, I am trying, without sucess, to run the code below from [1] (Section 4.3). Obviouly I am missing something. I would like to keep as close to the original code as possible. Any help would be apreciated. Regards, Pat [1] http://publik.tuwien.ac.at/files/pub-geo_2321.pdf data MyNumbers a = MyNum a deriving Show class (Floating a) => Numbers a where sqr :: a -> a -- Not sure if i need these -- instance Num (MyNumbers a) where -- instance Fractional (MyNumbers a) where class Lifts b a where lift0 :: a -> b a lift1 :: (a -> a) -> b a -> b a instance Lifts MyNumbers a where lift0 x = MyNum x lift1 o x = MyNum (o x) -- ** Couldn't match expected type `a' with actual type `MyNumbers a' ** instance (Floating (MyNumbers Float)) => Numbers (MyNumbers Float) where sqr x = lift1 sqr x -- ** could not deduce (Numbers Float) arising from a use of `sqr' ** -- This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. www.dit.ie Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. www.dit.ie Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman -------------- next part -------------- An HTML attachment was scrubbed... URL: From fa-ml at ariis.it Sun Oct 2 15:38:24 2016 From: fa-ml at ariis.it (Francesco Ariis) Date: Sun, 2 Oct 2016 17:38:24 +0200 Subject: [Haskell-beginners] Problems with lifting code In-Reply-To: References: Message-ID: <20161002153824.GA18282@casa.casa> On Sun, Oct 02, 2016 at 03:54:07PM +0100, PATRICK BROWNE wrote: > Hi, > I am trying, without sucess, to run the code below from [1] (Section 4.3). > Obviouly I am missing something. > I would like to keep as close to the original code as possible. > Any help would be apreciated. > Regards, > Pat > [1] http://publik.tuwien.ac.at/files/pub-geo_2321.pdf > I get different errors from yours (missing extension, Illegal instance declaration). Can you paste the exact file you are loading with ghci (because you are using ghci, right?)? From imantc at gmail.com Sun Oct 2 15:57:54 2016 From: imantc at gmail.com (Imants Cekusins) Date: Sun, 2 Oct 2016 17:57:54 +0200 Subject: [Haskell-beginners] Problems with lifting code In-Reply-To: <20161002153824.GA18282@casa.casa> References: <20161002153824.GA18282@casa.casa> Message-ID: this works: {-# LANGUAGE MultiParamTypeClasses,FlexibleInstances #-} module Lift where newtype MyNumbers a = MyNum a deriving Show class Numbers a where sqr :: a -> a class Lifts b a where lift0 :: a -> b a lift1 :: (a -> a) -> b a -> b a instance Lifts MyNumbers a where lift0 x = MyNum x lift1 o (MyNum x) = MyNum (o x) instance Numbers Float where sqr x = x * x instance Numbers (MyNumbers Float) where sqr x = lift1 sqr x ​ -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick.browne at dit.ie Sun Oct 2 16:03:25 2016 From: patrick.browne at dit.ie (PATRICK BROWNE) Date: Sun, 2 Oct 2016 17:03:25 +0100 Subject: [Haskell-beginners] Problems with lifting code In-Reply-To: <20161002153824.GA18282@casa.casa> References: <20161002153824.GA18282@casa.casa> Message-ID: Yes, I am running ghci 8.0.1. Below, I have added the language pragmas suggested by the compiler Regards, Pat . {-# LANGUAGE MultiParamTypeClasses,FlexibleInstances,FlexibleContexts,UndecidableInstances #-} data MyNumbers a = MyNum a deriving Show class (Floating a) => Numbers a where sqr :: a -> a class Lifts b a where lift0 :: a -> b a lift1 :: (a -> a) -> b a -> b a instance Lifts MyNumbers a where lift0 x = MyNum x lift1 o x = MyNum (o x) instance (Floating (MyNumbers Float)) => Numbers (MyNumbers Float) where sqr x = lift1 sqr x On 2 October 2016 at 16:38, Francesco Ariis wrote: > On Sun, Oct 02, 2016 at 03:54:07PM +0100, PATRICK BROWNE wrote: > > Hi, > > I am trying, without sucess, to run the code below from [1] (Section > 4.3). > > Obviouly I am missing something. > > I would like to keep as close to the original code as possible. > > Any help would be apreciated. > > Regards, > > Pat > > [1] http://publik.tuwien.ac.at/files/pub-geo_2321.pdf > > > > I get different errors from yours (missing extension, Illegal > instance declaration). Can you paste the exact file you are > loading with ghci (because you are using ghci, right?)? > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -- This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. www.dit.ie Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. www.dit.ie Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick.browne at dit.ie Sun Oct 2 16:15:57 2016 From: patrick.browne at dit.ie (PATRICK BROWNE) Date: Sun, 2 Oct 2016 17:15:57 +0100 Subject: [Haskell-beginners] Problems with lifting code In-Reply-To: References: <20161002153824.GA18282@casa.casa> Message-ID: So it seems that (1) lift1 o x = MyNum (o x) Needed to be rewritten to (2) lift1 o (MyNum x) = MyNum (o x) I am not sure why the constructor is used on the LHS of (2) Thanks for all your help. Pat On 2 October 2016 at 17:03, PATRICK BROWNE wrote: > Yes, I am running ghci 8.0.1. > Below, I have added the language pragmas suggested by the compiler > Regards, > Pat > . > {-# LANGUAGE MultiParamTypeClasses,FlexibleInstances,FlexibleContexts,UndecidableInstances > #-} > > data MyNumbers a = MyNum a deriving Show > > class (Floating a) => Numbers a where > sqr :: a -> a > > > class Lifts b a where > lift0 :: a -> b a > lift1 :: (a -> a) -> b a -> b a > > instance Lifts MyNumbers a where > lift0 x = MyNum x > lift1 o x = MyNum (o x) > > instance (Floating (MyNumbers Float)) => Numbers (MyNumbers Float) where > sqr x = lift1 sqr x > > On 2 October 2016 at 16:38, Francesco Ariis wrote: > >> On Sun, Oct 02, 2016 at 03:54:07PM +0100, PATRICK BROWNE wrote: >> > Hi, >> > I am trying, without sucess, to run the code below from [1] (Section >> 4.3). >> > Obviouly I am missing something. >> > I would like to keep as close to the original code as possible. >> > Any help would be apreciated. >> > Regards, >> > Pat >> > [1] http://publik.tuwien.ac.at/files/pub-geo_2321.pdf >> > >> >> I get different errors from yours (missing extension, Illegal >> instance declaration). Can you paste the exact file you are >> loading with ghci (because you are using ghci, right?)? >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > > -- This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. www.dit.ie Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. www.dit.ie Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman -------------- next part -------------- An HTML attachment was scrubbed... URL: From imantc at gmail.com Sun Oct 2 16:20:48 2016 From: imantc at gmail.com (Imants Cekusins) Date: Sun, 2 Oct 2016 18:20:48 +0200 Subject: [Haskell-beginners] Problems with lifting code In-Reply-To: References: <20161002153824.GA18282@casa.casa> Message-ID: > I am not sure why the constructor is used on the LHS of (2) lift1 :: (a -> a) -> b a -> b a lift1 o (MyNum x) = MyNum (o x) 2nd arg to lift1 is b a o expects a by adding ctor (MyNum a), we get to a lift1 o (b a) = ... without ctor MyNum, x is b a, not a ​ -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwyang at aranetworks.com Thu Oct 6 06:39:37 2016 From: cwyang at aranetworks.com (Chul-Woong Yang) Date: Thu, 6 Oct 2016 15:39:37 +0900 Subject: [Haskell-beginners] how to enter EOF in emacs haskell mode In-Reply-To: References: Message-ID: Putting following code to .emacs solves the problem. -- (defun haskell-send-eof () "send eof to interactive buffer" (interactive) (process-send-eof (haskell-process-process (haskell-interactive-process)))) (add-hook 'haskell-interactive-mode-hook (lambda () (local-set-key (kbd "C-c C-d") 'haskell-send-eof))) 2016-09-15 1:17 GMT+09:00 Chul-Woong Yang : > Yes, I've tried comint-send-eof with no success: >> Current buffer has no process > > haskell-mode's comint-interface is deprecated. > No success with googling and browsing of documentions on haskell-mode. > > 2016-09-15 0:46 GMT+09:00 Norbert Melzer : >> >> So have you tried `M-x comint-send-eof RET` then? If that works, it is >> just a matter of binding it to whatever you want. >> >> Chul-Woong Yang schrieb am Mi., 14. Sep. 2016 um >> 17:06 Uhr: >>> >>> No. Usual `C-c C-d' (comint-send-eof) does not work in haskell-mode's >>> interactive buffer. >>> It is not defined, emacs responds as follows: >>> >>> > C-c C-d is undefined >>> >>> 2016-09-14 20:01 GMT+09:00 Shrivats : >>>> >>>> Hi, >>>> >>>> If it's anything like other interactive modes, does >>>> C-c C-d do the job of signalling EOF? >>>> >>>> Shrivats >>>> >>>> >>>> On Sep 14, 2016 15:53, "Chul-Woong Yang" wrote: >>>> >>>> Hi, all. >>>> >>>> I use emacs haskell mode for editing and testing simple programs. >>>> I have no success in finding out how to enter EOF to haskell program >>>> in emacs "Interactive-Haskell" buffer. >>>> So I am unable to test haskell program which reads from stdin. :-( >>>> >>>> Any help would be appreciated deeply. >>>> >>>> Regards, >>>> Chul-Woong Yang >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>>> >>>> >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > From clever97 at gmail.com Thu Oct 6 15:17:56 2016 From: clever97 at gmail.com (Cleverson Casarin Uliana) Date: Thu, 6 Oct 2016 12:17:56 -0300 Subject: [Haskell-beginners] Basic sound playing on Windows? Message-ID: Hello all, is it easy to play/stop sound wave files on Windows? For now I'd like just playing and stopping them assynchronously. Do I need to install any package besides Haskell Platform? Thanks, Cleverson From t_gass at gmx.de Sat Oct 8 13:19:35 2016 From: t_gass at gmx.de (Tilmann) Date: Sat, 8 Oct 2016 15:19:35 +0200 Subject: [Haskell-beginners] Basic sound playing on Windows? In-Reply-To: References: Message-ID: <417e430b-14d5-1bda-217d-1df8e512831d@gmx.de> I used ALUT on OSX and it worked perfectly. Not used it on windows yet, but according to the documentation it's supported. Have a look here for how to use it on windows: https://hackage.haskell.org/package/OpenAL and here for some examples: https://github.com/haskell-openal/ALUT/tree/master/examples/Basic Best, Tilmann Am 06.10.16 um 17:17 schrieb Cleverson Casarin Uliana: > Hello all, is it easy to play/stop sound wave files on Windows? For > now I'd like just playing and stopping them assynchronously. Do I need > to install any package besides Haskell Platform? > > Thanks, > Cleverson > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners From clever97 at gmail.com Sat Oct 8 13:54:54 2016 From: clever97 at gmail.com (Cleverson Casarin Uliana) Date: Sat, 8 Oct 2016 10:54:54 -0300 Subject: [Haskell-beginners] Basic sound playing on Windows? In-Reply-To: <417e430b-14d5-1bda-217d-1df8e512831d@gmx.de> References: <417e430b-14d5-1bda-217d-1df8e512831d@gmx.de> Message-ID: <46d55c3c-0c5c-e1e1-c413-1406ec12a56c@gmail.com> Thank you Tilmann, it's quite good. Greetings, Cleverson From laiboonh at gmail.com Sun Oct 9 02:27:22 2016 From: laiboonh at gmail.com (Lai Boon Hui) Date: Sun, 9 Oct 2016 10:27:22 +0800 Subject: [Haskell-beginners] Lazy evaluation, trying to find out when its done Message-ID: Hi all, I understand that the take method will evaluate the value inside the cons cell whereas length will just evaluate the spine or structure of the list λ> let y = "abc" Prelude| y :: [Char] λ> :sprint y y = _ λ> take 1 y "a" it :: [Char] λ> :sprint y y = 'a' : _ λ> Well and good but why doesn't the same work on a list of Nums?? λ> let x = [1,2,3] Prelude| x :: Num t => [t] λ> :sprint x x = _ λ> take 1 x [1] it :: Num a => [a] λ> :sprint x x = _ λ> I expected to see x = 1 : _ -- Best Regards, Boon Hui -------------- next part -------------- An HTML attachment was scrubbed... URL: From amindfv at gmail.com Sun Oct 9 04:34:31 2016 From: amindfv at gmail.com (Tom Murphy) Date: Sun, 9 Oct 2016 00:34:31 -0400 Subject: [Haskell-beginners] Lazy evaluation, trying to find out when its done In-Reply-To: References: Message-ID: <20161009043431.GB29336@air.home> Maybe this will help answer some questions and raise others: . let x = [1,2,3] . take 1 x [1] . :sprint x x = _ . let x = [1,2,3] :: [Int] . take 1 x [1] . :sprint x x = [1,2,3] Tom On Sun, Oct 09, 2016 at 10:27:22AM +0800, Lai Boon Hui wrote: > Hi all, > > I understand that the take method will evaluate the value inside the cons > cell whereas length will just evaluate the spine or structure of the list > > λ> let y = "abc" > Prelude| > y :: [Char] > λ> :sprint y > y = _ > λ> take 1 y > "a" > it :: [Char] > λ> :sprint y > y = 'a' : _ > λ> > > Well and good but why doesn't the same work on a list of Nums?? > > λ> let x = [1,2,3] > Prelude| > x :: Num t => [t] > λ> :sprint x > x = _ > λ> take 1 x > [1] > it :: Num a => [a] > λ> :sprint x > x = _ > λ> > > I expected to see x = 1 : _ > > -- > Best Regards, > Boon Hui > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners From laiboonh at gmail.com Sun Oct 9 12:32:54 2016 From: laiboonh at gmail.com (Lai Boon Hui) Date: Sun, 9 Oct 2016 20:32:54 +0800 Subject: [Haskell-beginners] Beginners Digest, Vol 100, Issue 7 In-Reply-To: References: Message-ID: Hi Tom, this indeed led to more questions. I believe that was happening because x is not a list of concrete types. In determining the concrete type of the list GHCI evaluated all values hence λ> let foo = [1,2,3] :: [Int] Prelude| foo :: [Int] λ> :sprint foo foo = [1,2,3] I tried on other concrete types like [Char] and String...seems like [Char] is not entire the same as String after all λ> let x = ['a','b','c'] Prelude| x :: [Char] λ> let y = "abc" Prelude| y :: [Char] λ> :sprint x x = "abc" λ> :sprint y y = _ On Sun, Oct 9, 2016 at 8:00 PM, wrote: > Send Beginners mailing list submissions to > beginners at 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-request at haskell.org > > You can reach the person managing the list at > beginners-owner at haskell.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Beginners digest..." > > > Today's Topics: > > 1. Re: Basic sound playing on Windows? (Tilmann) > 2. Re: Basic sound playing on Windows? (Cleverson Casarin Uliana) > 3. Lazy evaluation, trying to find out when its done (Lai Boon Hui) > 4. Re: Lazy evaluation, trying to find out when its done > (Tom Murphy) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Sat, 8 Oct 2016 15:19:35 +0200 > From: Tilmann > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: Re: [Haskell-beginners] Basic sound playing on Windows? > Message-ID: <417e430b-14d5-1bda-217d-1df8e512831d at gmx.de> > Content-Type: text/plain; charset=utf-8; format=flowed > > I used ALUT on OSX and it worked perfectly. Not used it on windows yet, > but according to the documentation it's supported. > > > Have a look here for how to use it on windows: > > https://hackage.haskell.org/package/OpenAL > > > and here for some examples: > > https://github.com/haskell-openal/ALUT/tree/master/examples/Basic > > > Best, > > Tilmann > > > > > Am 06.10.16 um 17:17 schrieb Cleverson Casarin Uliana: > > Hello all, is it easy to play/stop sound wave files on Windows? For > > now I'd like just playing and stopping them assynchronously. Do I need > > to install any package besides Haskell Platform? > > > > Thanks, > > Cleverson > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > ------------------------------ > > Message: 2 > Date: Sat, 8 Oct 2016 10:54:54 -0300 > From: Cleverson Casarin Uliana > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: Re: [Haskell-beginners] Basic sound playing on Windows? > Message-ID: <46d55c3c-0c5c-e1e1-c413-1406ec12a56c at gmail.com> > Content-Type: text/plain; charset=utf-8; format=flowed > > Thank you Tilmann, it's quite good. > > Greetings, > Cleverson > > > ------------------------------ > > Message: 3 > Date: Sun, 9 Oct 2016 10:27:22 +0800 > From: Lai Boon Hui > To: beginners at haskell.org > Subject: [Haskell-beginners] Lazy evaluation, trying to find out when > its done > Message-ID: > gmail.com> > Content-Type: text/plain; charset="utf-8" > > Hi all, > > I understand that the take method will evaluate the value inside the cons > cell whereas length will just evaluate the spine or structure of the list > > λ> let y = "abc" > Prelude| > y :: [Char] > λ> :sprint y > y = _ > λ> take 1 y > "a" > it :: [Char] > λ> :sprint y > y = 'a' : _ > λ> > > Well and good but why doesn't the same work on a list of Nums?? > > λ> let x = [1,2,3] > Prelude| > x :: Num t => [t] > λ> :sprint x > x = _ > λ> take 1 x > [1] > it :: Num a => [a] > λ> :sprint x > x = _ > λ> > > I expected to see x = 1 : _ > > -- > Best Regards, > Boon Hui > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: attachments/20161009/7f08152b/attachment-0001.html> > > ------------------------------ > > Message: 4 > Date: Sun, 9 Oct 2016 00:34:31 -0400 > From: Tom Murphy > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: Re: [Haskell-beginners] Lazy evaluation, trying to find out > when its done > Message-ID: <20161009043431.GB29336 at air.home> > Content-Type: text/plain; charset=utf-8 > > Maybe this will help answer some questions and raise others: > > . let x = [1,2,3] > . take 1 x > [1] > . :sprint x > x = _ > . let x = [1,2,3] :: [Int] > . take 1 x > [1] > . :sprint x > x = [1,2,3] > > Tom > > > On Sun, Oct 09, 2016 at 10:27:22AM +0800, Lai Boon Hui wrote: > > Hi all, > > > > I understand that the take method will evaluate the value inside the cons > > cell whereas length will just evaluate the spine or structure of the list > > > > λ> let y = "abc" > > Prelude| > > y :: [Char] > > λ> :sprint y > > y = _ > > λ> take 1 y > > "a" > > it :: [Char] > > λ> :sprint y > > y = 'a' : _ > > λ> > > > > Well and good but why doesn't the same work on a list of Nums?? > > > > λ> let x = [1,2,3] > > Prelude| > > x :: Num t => [t] > > λ> :sprint x > > x = _ > > λ> take 1 x > > [1] > > it :: Num a => [a] > > λ> :sprint x > > x = _ > > λ> > > > > I expected to see x = 1 : _ > > > > -- > > Best Regards, > > Boon Hui > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > ------------------------------ > > End of Beginners Digest, Vol 100, Issue 7 > ***************************************** > -- Best Regards, Boon Hui -------------- next part -------------- An HTML attachment was scrubbed... URL: From laiboonh at gmail.com Mon Oct 10 05:15:17 2016 From: laiboonh at gmail.com (Lai Boon Hui) Date: Mon, 10 Oct 2016 13:15:17 +0800 Subject: [Haskell-beginners] Regarding defining cases exhaustively Message-ID: Hi all, I have this code myMaximumBy :: (a -> a -> Ordering) -> [a] -> a myMaximumBy _ [] = undefined myMaximumBy _ (x:[]) = x myMaximumBy f (x:xs) = case f x maxRestOfList of GT -> x _ -> maxRestOfList where maxRestOfList = myMaximumBy f xs whereby the case myMaximumBy _ [] will never happen because case myMaximumBy _ (x:[]) = x prevents that from happening. However i will still like to define it to keep -Wall warning from complaining. Is using undefined acceptable or is there a better way? -- Best Regards, Boon Hui -------------- next part -------------- An HTML attachment was scrubbed... URL: From raabe at froglogic.com Mon Oct 10 06:51:52 2016 From: raabe at froglogic.com (Frerich Raabe) Date: Mon, 10 Oct 2016 08:51:52 +0200 Subject: [Haskell-beginners] Regarding defining cases exhaustively In-Reply-To: References: Message-ID: <936a30b33b05cd6dfed8f3b11c73fa31@roundcube.froglogic.com> On 2016-10-10 07:15, Lai Boon Hui wrote: > I have this code > > myMaximumBy :: (a -> a -> Ordering) -> [a] -> a > myMaximumBy _ [] = undefined > myMaximumBy _ (x:[]) = x > myMaximumBy f (x:xs) = > case f x maxRestOfList of > GT -> x > _ -> maxRestOfList > where maxRestOfList = myMaximumBy f xs > > whereby the case myMaximumBy _ [] will never happen because case myMaximumBy > _ (x:[]) = x prevents that from happening. Somebody could call 'myMaximumBy' with an empty list, so as far as the compiler (and fellow programmers) are concerned, this case can certainly happen. > However i will still like to define it to keep -Wall warning from > complaining. > > Is using undefined acceptable or is there a better way? If you're fine with having a partial function (i.e. which is not defined for all possible inputs), then 'undefined' is okay. An alternative might be myMaximumBy _ [] = error "empty list" A nicer approach might be to make 'myMaximumBy' a total function though, i.e. give a plausible definition even for empty lists. So, for instance, you could declare it as myMaximumBy :: (a -> a -> Ordering) -> [a] -> Maybe a such that for empty lists, it yields 'Nothing'. -- Frerich Raabe - raabe at froglogic.com www.froglogic.com - Multi-Platform GUI Testing From laiboonh at gmail.com Wed Oct 12 04:23:41 2016 From: laiboonh at gmail.com (Lai Boon Hui) Date: Wed, 12 Oct 2016 12:23:41 +0800 Subject: [Haskell-beginners] Using scanl for Fibonacci sequence Message-ID: Hi All, i am not very sure how this can work fibs = 1 : scanl (+) 1 fibs Appreciate it if someone can guide me through by showing me a few steps of the function evaluation -- Best Regards, Boon Hui -------------- next part -------------- An HTML attachment was scrubbed... URL: From apoorv.ingle at gmail.com Wed Oct 12 22:24:22 2016 From: apoorv.ingle at gmail.com (Apoorv Ingle) Date: Wed, 12 Oct 2016 17:24:22 -0500 Subject: [Haskell-beginners] Using scanl for Fibonacci sequence In-Reply-To: References: Message-ID: Hi Boon, I guess it is difficult to comprehend what scanl exactly does here. If you see the definition of scanl from hackage scanl :: (b -> a -> b) -> b -> [a] -> [b] scanl is similar to foldl, but returns a list of successive reduced values from the left: scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, …] Think of calculating a running sum of the first 10 positive integers, it can be easily computed using scanl λ> scanl (+) 0 [1..10] [0,1,3,6,10,15,21,28,36,45,55] For fibonacci, you calculate the nth number by adding n-1th number and n-2th number. Lets try to unfold the definition of fibs fibs = 1 : scanl (+) 1 fibs now first element of fibs is by definition, ofcourse 1 (say x1) second element of fibs will be (the first element generated by scanl) 1 (i.e. z — say x2) Third element of fibs will be calculated as (the second element generated by scanl) z `f` x1 = 1 + 1 = 2 (say x3) Fourth element will be (and also the 3rd element generated by scanl) (z `f` x1) `f` x2 = (1 + 1) + 1 = 3 (say x4) Fifth element will be ((z `f` x1) `f` x2) `f` x3 = ((1 + 1) + 1) + 2 = 5 (say x5) and so on.. The elegance is of course achieved because of the lazy evaluation of the infinite list Hope this makes it some what clear. Regards, Apoorv > On 11-Oct-2016, at 23:23, Lai Boon Hui wrote: > > Hi All, > > i am not very sure how this can work > fibs = 1 : scanl (+) 1 fibs > > Appreciate it if someone can guide me through by showing me a few steps of the function evaluation > > -- > Best Regards, > Boon Hui > _______________________________________________ > 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 sumitraja at gmail.com Thu Oct 13 02:15:43 2016 From: sumitraja at gmail.com (Sumit Raja) Date: Thu, 13 Oct 2016 13:15:43 +1100 Subject: [Haskell-beginners] Monadic functions definitions for free monadic DSL Message-ID: Hello, I am trying to get my head around free monads by developing a simple network abstraction DSL. I've made good progress before adding TCP/IP semantics of accepting connections. I'm now stuck with the creation of monadic functions. I've defined the following: data NetworkActivity chan next = Accept chan next (chan -> next) | Send chan ByteString (Bool -> next) | Recv chan (ByteString -> next) | Close chan (() -> next) clse :: a -> Free (NetworkActivity a) Text clse chan = liftF (Close chan (const "Quit")) chatterServer :: a -> Free (NetworkActivity a) Text chatterServer svrchan = Free $ Accept svrchan (chatterServer svrchan) chatterLoop chatterLoop :: a -> Free (NetworkActivity a) Text chatterLoop chan = Free $ Recv chan $ \bs -> case BS.uncons bs of Nothing -> clse chan Just x -> if bs == "Bye" then Free $ Close chan (\_ -> Pure "Quit") else Free (Send chan bs (\_ -> chatterLoop chan)) This works fine with the interpretTCP interpreter below accepting multiple connections: interpretTCP :: Free (NetworkActivity TCPSocket) r -> IO r interpretTCP prg = case prg of Free (Accept serverSock svrLoop acceptProc) -> bracket (return serverSock) (\s-> interpretTCP (clse s)) (\s-> do (ss, sa) <- accept s forkIO $ do _ <- interpretTCP (acceptProc ss) return () interpretTCP svrLoop ) Free (Recv sock g) -> do bs <- receive sock 4096 mempty putStrLn (decodeUtf8 bs) interpretTCP (g bs) Free (Close sock g) -> do close sock putStrLn ("Server bye!" :: Text) interpretTCP (g ()) Pure r -> return r Free (Send sock pl g) -> do sent <- send sock pl mempty interpretTCP (g (sent > 0)) Where I'm stuck is defining the monadic version of accept and I'm beginning to think my original data type defined above may be wrong. As an initial step I've defined the following: recv :: a -> Free (NetworkActivity a) ByteString recv chan = liftF (Recv chan identity) sendit :: a -> ByteString -> Free (NetworkActivity a) Bool sendit chan pl = liftF (Send chan pl identity) mchatterServer :: a -> Free (NetworkActivity a) Text mchatterServer chan = Free $ Accept chan (mchatterServer chan) (\s -> return (identity s) >>= mchatterLoop) mchatterServer works as is, the interpreter accepts multiple connections. Similarly all good with recv and sendit. I am struggling with converting the Accept in mchatterServer into a function to use in the do syntax. The signature I think I should be using is acc :: a -> NetworkActivity a Text -> Free (NetworkActivity a) (NetworkActivity a Text) What I can't figure out is why it can't follow the pattern of recv and sendit above: acc chan next = liftF $ Accept chan next identity Which results in error on identity (using Protolude): Expected type: a -> NetworkActivity a Text Actual type: NetworkActivity a Text -> NetworkActivity a Text I can't really see how to get the types to line up and have now can't see through the type fog. What am I missing in my reasoning about the types? Help much appreciated! Thanks Sumit From toad3k at gmail.com Thu Oct 13 12:09:59 2016 From: toad3k at gmail.com (David McBride) Date: Thu, 13 Oct 2016 08:09:59 -0400 Subject: [Haskell-beginners] Monadic functions definitions for free monadic DSL In-Reply-To: References: Message-ID: I would really like to help you, but without your imports, packages, etc, it is really hard to interpret your program. Like where does decodeUtf8 come from, or receive, or TCPSocket? If they are functions you wrote, I don't need their code, the types would be sufficient. On Wed, Oct 12, 2016 at 10:15 PM, Sumit Raja wrote: > Hello, > > I am trying to get my head around free monads by developing a simple > network abstraction DSL. > I've made good progress before adding TCP/IP semantics of accepting > connections. I'm now stuck with the creation of monadic functions. > > I've defined the following: > > data NetworkActivity chan next = Accept chan next (chan -> next) | > Send chan ByteString (Bool -> next) | > Recv chan (ByteString -> next) | > Close chan (() -> next) > > clse :: a -> Free (NetworkActivity a) Text > clse chan = liftF (Close chan (const "Quit")) > > chatterServer :: a -> Free (NetworkActivity a) Text > chatterServer svrchan = Free $ Accept svrchan (chatterServer > svrchan) chatterLoop > > chatterLoop :: a -> Free (NetworkActivity a) Text > chatterLoop chan = Free $ Recv chan $ \bs -> case BS.uncons bs of > Nothing -> clse chan > Just x -> if bs == "Bye" then > Free $ Close chan (\_ -> Pure "Quit") > else > Free (Send chan bs (\_ -> chatterLoop chan)) > > This works fine with the interpretTCP interpreter below accepting > multiple connections: > > interpretTCP :: Free (NetworkActivity TCPSocket) r -> IO r > interpretTCP prg = case prg of > Free (Accept serverSock svrLoop acceptProc) -> bracket (return > serverSock) > (\s-> interpretTCP (clse s)) > (\s-> do > (ss, sa) <- accept s > forkIO $ do > _ <- interpretTCP (acceptProc ss) > return () > interpretTCP svrLoop > ) > Free (Recv sock g) -> do > bs <- receive sock 4096 mempty > putStrLn (decodeUtf8 bs) > interpretTCP (g bs) > Free (Close sock g) -> do > close sock > putStrLn ("Server bye!" :: Text) > interpretTCP (g ()) > Pure r -> return r > Free (Send sock pl g) -> do > sent <- send sock pl mempty > interpretTCP (g (sent > 0)) > > Where I'm stuck is defining the monadic version of accept and I'm > beginning to think my original > data type defined above may be wrong. As an initial step I've defined > the following: > > recv :: a -> Free (NetworkActivity a) ByteString > recv chan = liftF (Recv chan identity) > > sendit :: a -> ByteString -> Free (NetworkActivity a) Bool > sendit chan pl = liftF (Send chan pl identity) > > mchatterServer :: a -> Free (NetworkActivity a) Text > mchatterServer chan = Free $ Accept chan (mchatterServer chan) > (\s > -> return (identity s) >>= mchatterLoop) > > mchatterServer works as is, the interpreter accepts multiple > connections. Similarly all good with recv and sendit. > I am struggling with converting the Accept in mchatterServer into a > function to use in the do syntax. The signature I think I should be > using is > > acc :: a -> NetworkActivity a Text -> Free (NetworkActivity a) > (NetworkActivity a Text) > > What I can't figure out is why it can't follow the pattern of recv and > sendit above: > > acc chan next = liftF $ Accept chan next identity > > Which results in error on identity (using Protolude): > > Expected type: a -> NetworkActivity a Text > Actual type: NetworkActivity a Text -> NetworkActivity a Text > > I can't really see how to get the types to line up and have now can't > see through the type fog. What am I missing in my reasoning about the > types? > > Help much appreciated! > > Thanks > > Sumit > _______________________________________________ > 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 sumitraja at gmail.com Thu Oct 13 23:44:25 2016 From: sumitraja at gmail.com (Sumit Raja) Date: Fri, 14 Oct 2016 10:44:25 +1100 Subject: [Haskell-beginners] Monadic functions definitions for free monadic DSL Message-ID: > I would really like to help you, but without your imports, packages, etc, > it is really hard to interpret your program. Like where does decodeUtf8 > come from, or receive, or TCPSocket? If they are functions you wrote, I > don't need their code, the types would be sufficient. > Imports are: import Protolude import Control.Monad.Free import System.Socket import System.Socket.Family.Inet import System.Socket.Type.Stream import System.Socket.Protocol.TCP import Control.Exception ( bracket, catch ) import Data.ByteString as BS (uncons) decodeUtf8 :: ByteString -> Text encodeUtf8 :: Text -> ByteString I'm using the socket library for the actual networking (https://hackage.haskell.org/package/socket-0.6.0.1) type TCPSocket = Socket Inet Stream TCP receive :: Socket f t p -> Int -> MessageFlags -> IO ByteString Source send :: Socket f t p -> ByteString -> MessageFlags -> IO Int accept :: (Family f, Storable (SocketAddress f)) => Socket f t p -> IO (Socket f t p, SocketAddress f) If it helps the full source is at https://bitbucket.org/sumitraja/network-free/src/a4fcbc74c9e178e81d8b10b60d912b32c542b661/src/Lib.hs. Looking forward to your assistance. Thanks Sumit From yinbirmjin at gmail.com Fri Oct 14 08:39:56 2016 From: yinbirmjin at gmail.com (Birmjin In) Date: Fri, 14 Oct 2016 17:39:56 +0900 Subject: [Haskell-beginners] Why QuickCheck's Char value is limited to ASCII characters only? Message-ID: Hi, I found that the Arbitrary instance for the Char type generates only ASCII values while Char type represent Unicode characters. I can't figure out why it has such a limit. Not knowing this pitfall, one can misjudge the test results. Is this intended thing or just not being implemented yet? Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From traqueofziche at gmail.com Fri Oct 14 09:22:40 2016 From: traqueofziche at gmail.com (=?UTF-8?B?6bKN5Yev5paH?=) Date: Fri, 14 Oct 2016 02:22:40 -0700 Subject: [Haskell-beginners] Monadic functions definitions for free monadic DSL Message-ID: Hi, Although I don't really understand the contents of your code, I think the type error results from the fact that the 3rd field of the Accept constructor has type (chan -> next). In the context of 'acc', (chan :: a) and (next :: NetworkActivity a Text). I'm guessing the type error refers to when you used 'identity' (which I'm hoping is just 'id' from Prelude); its type gets inferred to be (NetworkActivity a Text -> NetworkActivity a text) instead of what it expected (chan -> next, i.e. a ->NetworkActivity a Text). Whether or not acc is the right type for your needs, I don't know. Hope that helps, toz P.S. I don't know if it's good practice, but I usually use type variables in data declarations consistently in other type signatures, e.g. since you declared NetworkActivity using 'chan' and 'next', in 'clse', it'd make more sense (to me) to use (clse :: chan -> Free (NetworkActivity chan) Text) since it seems that 'chan' as a word has some extra connotations as opposed to 'a', which when I read, I think it can be absolutely anything. > > ------------------------------ > > Message: 2 > Date: Thu, 13 Oct 2016 13:15:43 +1100 > From: Sumit Raja > To: beginners at haskell.org > Subject: [Haskell-beginners] Monadic functions definitions for free > monadic DSL > Message-ID: > gmail.com> > Content-Type: text/plain; charset=UTF-8 > > Hello, > > I am trying to get my head around free monads by developing a simple > network abstraction DSL. > I've made good progress before adding TCP/IP semantics of accepting > connections. I'm now stuck with the creation of monadic functions. > > I've defined the following: > > data NetworkActivity chan next = Accept chan next (chan -> next) | > Send chan ByteString (Bool -> next) | > Recv chan (ByteString -> next) | > Close chan (() -> next) > > clse :: a -> Free (NetworkActivity a) Text > clse chan = liftF (Close chan (const "Quit")) > > chatterServer :: a -> Free (NetworkActivity a) Text > chatterServer svrchan = Free $ Accept svrchan (chatterServer > svrchan) chatterLoop > > chatterLoop :: a -> Free (NetworkActivity a) Text > chatterLoop chan = Free $ Recv chan $ \bs -> case BS.uncons bs of > Nothing -> clse chan > Just x -> if bs == "Bye" then > Free $ Close chan (\_ -> Pure "Quit") > else > Free (Send chan bs (\_ -> chatterLoop chan)) > > This works fine with the interpretTCP interpreter below accepting > multiple connections: > > interpretTCP :: Free (NetworkActivity TCPSocket) r -> IO r > interpretTCP prg = case prg of > Free (Accept serverSock svrLoop acceptProc) -> bracket (return > serverSock) > (\s-> interpretTCP (clse s)) > (\s-> do > (ss, sa) <- accept s > forkIO $ do > _ <- interpretTCP (acceptProc ss) > return () > interpretTCP svrLoop > ) > Free (Recv sock g) -> do > bs <- receive sock 4096 mempty > putStrLn (decodeUtf8 bs) > interpretTCP (g bs) > Free (Close sock g) -> do > close sock > putStrLn ("Server bye!" :: Text) > interpretTCP (g ()) > Pure r -> return r > Free (Send sock pl g) -> do > sent <- send sock pl mempty > interpretTCP (g (sent > 0)) > > Where I'm stuck is defining the monadic version of accept and I'm > beginning to think my original > data type defined above may be wrong. As an initial step I've defined > the following: > > recv :: a -> Free (NetworkActivity a) ByteString > recv chan = liftF (Recv chan identity) > > sendit :: a -> ByteString -> Free (NetworkActivity a) Bool > sendit chan pl = liftF (Send chan pl identity) > > mchatterServer :: a -> Free (NetworkActivity a) Text > mchatterServer chan = Free $ Accept chan (mchatterServer chan) > (\s > -> return (identity s) >>= mchatterLoop) > > mchatterServer works as is, the interpreter accepts multiple > connections. Similarly all good with recv and sendit. > I am struggling with converting the Accept in mchatterServer into a > function to use in the do syntax. The signature I think I should be > using is > > acc :: a -> NetworkActivity a Text -> Free (NetworkActivity a) > (NetworkActivity a Text) > > What I can't figure out is why it can't follow the pattern of recv and > sendit above: > > acc chan next = liftF $ Accept chan next identity > > Which results in error on identity (using Protolude): > > Expected type: a -> NetworkActivity a Text > Actual type: NetworkActivity a Text -> NetworkActivity a Text > > I can't really see how to get the types to line up and have now can't > see through the type fog. What am I missing in my reasoning about the > types? > > Help much appreciated! > > Thanks > > Sumit > -------------- next part -------------- An HTML attachment was scrubbed... URL: From simon.jakobi at googlemail.com Fri Oct 14 11:12:54 2016 From: simon.jakobi at googlemail.com (Simon Jakobi) Date: Fri, 14 Oct 2016 13:12:54 +0200 Subject: [Haskell-beginners] Why QuickCheck's Char value is limited to ASCII characters only? In-Reply-To: References: Message-ID: You'll be interested in the discussion on this PR: https://github.com/nick8325/quickcheck/pull/119 2016-10-14 10:39 GMT+02:00 Birmjin In : > Hi, > > I found that the Arbitrary instance for the Char type generates only > ASCII values while Char type represent Unicode characters. > > I can't figure out why it has such a limit. Not knowing this pitfall, one > can misjudge the test results. > > Is this intended thing or just not being implemented yet? > > Thanks. > > _______________________________________________ > 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 toad3k at gmail.com Fri Oct 14 14:16:45 2016 From: toad3k at gmail.com (David McBride) Date: Fri, 14 Oct 2016 10:16:45 -0400 Subject: [Haskell-beginners] Monadic functions definitions for free monadic DSL In-Reply-To: References: Message-ID: I feel like you are close to a something, but the recursion makes it difficult. You need to think about what types you've given and what you need. The types of Send, Recv and Close make sense to me. Send takes a chan and a bytestring and returns bool. Recv takes a chan and returns a bytestring, close takes a chan and returns nothing. Accept takes a chan, and something? and returns a chan? I feel like if you can figure out what you actually want Accept to do, it will become clearer. Here's my attempt. Accept takes a chan, takes a procedure to loop on, a procedure to accept on, and then returns the server chan to continue the loop. I don't know if this is entirely right, but it type checks and hopefully it will give you some ideas. {-# LANGUAGE NoImplicitPrelude, DeriveFunctor, OverloadedStrings #-} module Lib where import Protolude import Control.Monad.Free import System.Socket import System.Socket.Family.Inet import System.Socket.Type.Stream import System.Socket.Protocol.TCP import Control.Exception ( bracket, catch ) import Data.ByteString as BS (uncons) data NetworkActivity chan next = Accept chan (Free (NetworkActivity chan) chan) (chan -> Free (NetworkActivity chan) Text) (chan -> next) | Send chan ByteString (Bool -> next) | Recv chan (ByteString -> next) | Close chan (() -> next) | Forked chan deriving Functor recv :: a -> Free (NetworkActivity a) ByteString recv chan = liftF (Recv chan identity) sendit :: a -> ByteString -> Free (NetworkActivity a) Bool sendit chan pl = liftF (Send chan pl identity) clse :: a -> Free (NetworkActivity a) Text clse chan = liftF (Close chan (const "Quit")) acc :: a -> Free (NetworkActivity a) a -> (a -> Free (NetworkActivity a) Text) -> Free (NetworkActivity a) a acc chan srv acc = liftF (Accept chan srv acc identity) mchatterServer :: a -> Free (NetworkActivity a) a mchatterServer chan = acc chan (mchatterServer chan) mchatterLoop mchatterLoop :: a -> Free (NetworkActivity a) Text mchatterLoop chan = do str <- recv chan case BS.uncons str of Nothing -> do msg <- clse chan Pure msg Just x -> if str == "Bye" then clse chan else do _ <- sendit chan str mchatterLoop chan interpretStdIO :: Free (NetworkActivity ()) r -> IO r interpretStdIO prg = case prg of Free (Accept sock _ _ g) -> interpretStdIO (g sock) Free (Recv _ g) -> do ln <- getLine interpretStdIO (g (encodeUtf8 ln)) Free (Close _ r) -> do putStrLn ("Server bye!" :: Text) interpretStdIO (r ()) Pure r -> return r Free (Send _ pl f) -> do putStrLn (decodeUtf8 pl) interpretStdIO (f True) type TCPSocket = Socket Inet Stream TCP tcpSock :: IO TCPSocket tcpSock = do s <- socket :: IO (Socket Inet Stream TCP) setSocketOption s (ReuseAddress True) bind s (SocketAddressInet inetAny 5000) listen s 5 return s interpretTCP :: Free (NetworkActivity TCPSocket) r -> IO r interpretTCP prg = case prg of Free (Accept serverSock svrLoop acceptProc g) -> bracket (return serverSock) (\s-> interpretTCP (clse s)) (\s-> do (ss, sa) <- accept s forkIO $ do _ <- interpretTCP (acceptProc ss) return () interpretTCP (g s) ) Free (Recv sock g) -> do bs <- receive sock 4096 mempty putStrLn (decodeUtf8 bs) interpretTCP (g bs) Free (Close sock g) -> do close sock putStrLn ("Server bye!" :: Text) interpretTCP (g ()) Pure r -> return r Free (Send sock pl g) -> do sent <- send sock pl mempty interpretTCP (g (sent > 0)) I feel like it should be able to be written without Free in the NetworkActivity datatype, but it will require some pattern matching on Free and maybe some liftF's that I couldn't quite figure out. On Wed, Oct 12, 2016 at 10:15 PM, Sumit Raja wrote: > Hello, > > I am trying to get my head around free monads by developing a simple > network abstraction DSL. > I've made good progress before adding TCP/IP semantics of accepting > connections. I'm now stuck with the creation of monadic functions. > > I've defined the following: > > data NetworkActivity chan next = Accept chan next (chan -> next) | > Send chan ByteString (Bool -> next) | > Recv chan (ByteString -> next) | > Close chan (() -> next) > > clse :: a -> Free (NetworkActivity a) Text > clse chan = liftF (Close chan (const "Quit")) > > chatterServer :: a -> Free (NetworkActivity a) Text > chatterServer svrchan = Free $ Accept svrchan (chatterServer > svrchan) chatterLoop > > chatterLoop :: a -> Free (NetworkActivity a) Text > chatterLoop chan = Free $ Recv chan $ \bs -> case BS.uncons bs of > Nothing -> clse chan > Just x -> if bs == "Bye" then > Free $ Close chan (\_ -> Pure "Quit") > else > Free (Send chan bs (\_ -> chatterLoop chan)) > > This works fine with the interpretTCP interpreter below accepting > multiple connections: > > interpretTCP :: Free (NetworkActivity TCPSocket) r -> IO r > interpretTCP prg = case prg of > Free (Accept serverSock svrLoop acceptProc) -> bracket (return > serverSock) > (\s-> interpretTCP (clse s)) > (\s-> do > (ss, sa) <- accept s > forkIO $ do > _ <- interpretTCP (acceptProc ss) > return () > interpretTCP svrLoop > ) > Free (Recv sock g) -> do > bs <- receive sock 4096 mempty > putStrLn (decodeUtf8 bs) > interpretTCP (g bs) > Free (Close sock g) -> do > close sock > putStrLn ("Server bye!" :: Text) > interpretTCP (g ()) > Pure r -> return r > Free (Send sock pl g) -> do > sent <- send sock pl mempty > interpretTCP (g (sent > 0)) > > Where I'm stuck is defining the monadic version of accept and I'm > beginning to think my original > data type defined above may be wrong. As an initial step I've defined > the following: > > recv :: a -> Free (NetworkActivity a) ByteString > recv chan = liftF (Recv chan identity) > > sendit :: a -> ByteString -> Free (NetworkActivity a) Bool > sendit chan pl = liftF (Send chan pl identity) > > mchatterServer :: a -> Free (NetworkActivity a) Text > mchatterServer chan = Free $ Accept chan (mchatterServer chan) > (\s > -> return (identity s) >>= mchatterLoop) > > mchatterServer works as is, the interpreter accepts multiple > connections. Similarly all good with recv and sendit. > I am struggling with converting the Accept in mchatterServer into a > function to use in the do syntax. The signature I think I should be > using is > > acc :: a -> NetworkActivity a Text -> Free (NetworkActivity a) > (NetworkActivity a Text) > > What I can't figure out is why it can't follow the pattern of recv and > sendit above: > > acc chan next = liftF $ Accept chan next identity > > Which results in error on identity (using Protolude): > > Expected type: a -> NetworkActivity a Text > Actual type: NetworkActivity a Text -> NetworkActivity a Text > > I can't really see how to get the types to line up and have now can't > see through the type fog. What am I missing in my reasoning about the > types? > > Help much appreciated! > > Thanks > > Sumit > _______________________________________________ > 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 cwyang at aranetworks.com Wed Oct 19 02:11:40 2016 From: cwyang at aranetworks.com (Chul-Woong Yang) Date: Wed, 19 Oct 2016 11:11:40 +0900 Subject: [Haskell-beginners] [Q] Bulding an Array of Tree Nodes Message-ID: Hi, all. I need to make graph/tree structure, the nodes of which are contained in _array_ to facilitate random access to that node. There is no problem in building an array of graph nodes from edge list. > -- Graphs > data Graph a = GNode a [Graph a] > mkGraph :: [(a,[Int])] -> Array Int (Graph a) > mkGraph table = table' > where n = length table > table' = listArray (0,n-1) $ > map(\(x,adjs) -> GNode x (map (table' !) adjs)) table However, I cannot build tree from the same input, if given edge list is undirected one. The leaf node's edge has a link to parent node when edge list is undirected, and I cannot find way to cut that parent edge in building tree. For example, think of following simple tree: A(0) / \ B(1) C(2) It's hard to build tree with following edge list: [('A',[1,2]), ('B',[0]), ('C',[0])] but it's easy when edge list is following: [('A',[1,2]), ('B',[]), ('C',[])] How can I build an array of tree nodes for the former? In other words, How can I build following mkTree function? data Tree a = Node { value :: a , parent :: Tree a , level :: Int , children :: [Tree a] } deriving Show mkTree :: [(a,[Int])] -> Array Int (Tree a) Any comments or helps will be deeply appreciated. Thank you. Chul-Woong Yang -------------- next part -------------- An HTML attachment was scrubbed... URL: From laiboonh at gmail.com Fri Oct 21 14:35:19 2016 From: laiboonh at gmail.com (Lai Boon Hui) Date: Fri, 21 Oct 2016 22:35:19 +0800 Subject: [Haskell-beginners] cabal repl issue Message-ID: Hi all, when i execute cabal repl i get this: :1:8: error: Not in scope: ‘System.Directory.getCurrentDirectory’ No module named ‘System.Directory’ is imported. because my ghci.conf has :def pwd (\_-> System.Directory.getCurrentDirectory >>= print >> return "") Does anyone know how i can include System.Directory somehow even though its not used in my cabal project? -- Best Regards, Boon Hui -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Fri Oct 21 14:56:55 2016 From: toad3k at gmail.com (David McBride) Date: Fri, 21 Oct 2016 10:56:55 -0400 Subject: [Haskell-beginners] cabal repl issue In-Reply-To: References: Message-ID: Just do this: :m +System.Directory :def pwd (\_ -> getCurrentDirectory >>= print >> return "") On Fri, Oct 21, 2016 at 10:35 AM, Lai Boon Hui wrote: > Hi all, > > when i execute cabal repl i get this: > > :1:8: error: > > Not in scope: ‘System.Directory.getCurrentDirectory’ > > No module named ‘System.Directory’ is imported. > because my ghci.conf has > :def pwd (\_-> System.Directory.getCurrentDirectory >>= print >> return > "") > > Does anyone know how i can include System.Directory somehow even though > its not used in my cabal project? > > -- > Best Regards, > Boon Hui > > _______________________________________________ > 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 sumitraja at gmail.com Sat Oct 22 01:14:15 2016 From: sumitraja at gmail.com (Sumit Raja) Date: Sat, 22 Oct 2016 12:14:15 +1100 Subject: [Haskell-beginners] Monadic functions definitions for free monadic DSL Message-ID: > I feel like if you can figure out what you actually want Accept to do, it > will become clearer. Here's my attempt. Accept takes a chan, takes a > procedure to loop on, a procedure to accept on, and then returns the server > chan to continue the loop. I don't know if this is entirely right, but it > type checks and hopefully it will give you some ideas. As you've said Accept needs refinement. I tried writing a interpretUDP which doesn't have an accept loop but does have a bind + listen. I suspect that accept needs to move into the interpretTCP somehow and the DSL needs to be Bind or similar. Bind makes more sense as well if I wanted to write a pipes or a chan based interpreter. Thanks for the rewrite and the pointers. -Sumit From ky3 at atamo.com Sat Oct 22 01:27:32 2016 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Sat, 22 Oct 2016 08:27:32 +0700 Subject: [Haskell-beginners] Monadic functions definitions for free monadic DSL In-Reply-To: References: Message-ID: Dear Sumit, You are right that there's something's fishy about the free monadic modeling of accept. The following parallel construction will prove instructive: The native effect: send :: chan -> ByteString -> IO Bool is modeled in the free monad by the constructor for the base functor Send :: chan -> ByteString -> (Bool -> next) -> NetworkActivity chan next which is the data wrapping used in the value level sendit :: chan -> ByteString -> Free (NetworkActivity chan) Bool sendit chan buf = liftF (Send chan buf identity) Analogously, the native accept :: chan -> IO chan is modeled by Accept :: chan -> (chan -> next) -> NetworkActivity chan next used in acc :: chan -> Free (NetworkActivity chan) chan acc chan = liftF (Accept chan identity) Except that you used a different constructor for the base functor. Not Accept :: chan -> (chan -> next) -> NetworkActivity chan next but Accept :: chan -> next -> (chan -> next) -> NetworkActivity chan next which is equivalent to Accept :: chan -> (Maybe chan -> next) -> NetworkActivity chan next The new free monadic term that substitutes for the native accept is the same like before acc chan = liftF (Accept chan identity) only with a different type acc :: chan -> Free (NetworkActivity chan) (Maybe chan) modeling a native accept :: chan -> IO (Maybe chan) Given a native API, its free monad encoding is entirely boilerplate. I wrote about the boilerplate process here (skip the sections that don't concern you): http://www.atamo.com/articles/free-monads-wont-detox-your-colon/ Best, Kim-Ee -- Kim-Ee On Fri, Oct 14, 2016 at 6:44 AM, Sumit Raja wrote: > > I would really like to help you, but without your imports, packages, etc, > > it is really hard to interpret your program. Like where does decodeUtf8 > > come from, or receive, or TCPSocket? If they are functions you wrote, I > > don't need their code, the types would be sufficient. > > > Imports are: > > import Protolude > import Control.Monad.Free > import System.Socket > import System.Socket.Family.Inet > import System.Socket.Type.Stream > import System.Socket.Protocol.TCP > import Control.Exception ( bracket, catch ) > import Data.ByteString as BS (uncons) > > decodeUtf8 :: ByteString -> Text > encodeUtf8 :: Text -> ByteString > > I'm using the socket library for the actual networking > (https://hackage.haskell.org/package/socket-0.6.0.1) > > type TCPSocket = Socket Inet Stream TCP > receive :: Socket f t p -> Int -> MessageFlags -> IO ByteString Source > send :: Socket f t p -> ByteString -> MessageFlags -> IO Int > accept :: (Family f, Storable (SocketAddress f)) => Socket f t p > -> IO (Socket f t p, SocketAddress f) > > If it helps the full source is at > https://bitbucket.org/sumitraja/network-free/src/ > a4fcbc74c9e178e81d8b10b60d912b32c542b661/src/Lib.hs. > > Looking forward to your assistance. > > Thanks > > Sumit > _______________________________________________ > 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 laiboonh at gmail.com Sat Oct 22 13:47:28 2016 From: laiboonh at gmail.com (Lai Boon Hui) Date: Sat, 22 Oct 2016 21:47:28 +0800 Subject: [Haskell-beginners] Beginners Digest, Vol 100, Issue 14 Message-ID: Hi David, Thanks for chipping in. I found the solution to be to include "directory" in cabal file "build-depends". I was confused because my stack ghci config didn't need :m + System.directory to work. But somehow cabal repl does not work without some kind of import. On Sat, Oct 22, 2016 at 8:00 PM, wrote: > Send Beginners mailing list submissions to > beginners at 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-request at haskell.org > > You can reach the person managing the list at > beginners-owner at haskell.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Beginners digest..." > > > Today's Topics: > > 1. cabal repl issue (Lai Boon Hui) > 2. Re: cabal repl issue (David McBride) > 3. Re: Monadic functions definitions for free monadic DSL > (Sumit Raja) > 4. Re: Monadic functions definitions for free monadic DSL > (Kim-Ee Yeoh) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Fri, 21 Oct 2016 22:35:19 +0800 > From: Lai Boon Hui > To: beginners at haskell.org > Subject: [Haskell-beginners] cabal repl issue > Message-ID: > gmail.com> > Content-Type: text/plain; charset="utf-8" > > Hi all, > > when i execute cabal repl i get this: > > :1:8: error: > > Not in scope: ‘System.Directory.getCurrentDirectory’ > > No module named ‘System.Directory’ is imported. > because my ghci.conf has > :def pwd (\_-> System.Directory.getCurrentDirectory >>= print >> return > "") > > Does anyone know how i can include System.Directory somehow even though its > not used in my cabal project? > > -- > Best Regards, > Boon Hui > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: attachments/20161021/81951b8d/attachment-0001.html> > > ------------------------------ > > Message: 2 > Date: Fri, 21 Oct 2016 10:56:55 -0400 > From: David McBride > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: Re: [Haskell-beginners] cabal repl issue > Message-ID: > gmail.com> > Content-Type: text/plain; charset="utf-8" > > Just do this: > > :m +System.Directory > :def pwd (\_ -> getCurrentDirectory >>= print >> return "") > > On Fri, Oct 21, 2016 at 10:35 AM, Lai Boon Hui wrote: > > > Hi all, > > > > when i execute cabal repl i get this: > > > > :1:8: error: > > > > Not in scope: ‘System.Directory.getCurrentDirectory’ > > > > No module named ‘System.Directory’ is imported. > > because my ghci.conf has > > :def pwd (\_-> System.Directory.getCurrentDirectory >>= print >> return > > "") > > > > Does anyone know how i can include System.Directory somehow even though > > its not used in my cabal project? > > > > -- > > Best Regards, > > Boon Hui > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: attachments/20161021/81bfe668/attachment-0001.html> > > ------------------------------ > > Message: 3 > Date: Sat, 22 Oct 2016 12:14:15 +1100 > From: Sumit Raja > To: beginners at haskell.org > Subject: Re: [Haskell-beginners] Monadic functions definitions for > free monadic DSL > Message-ID: > gmail.com> > Content-Type: text/plain; charset=UTF-8 > > > I feel like if you can figure out what you actually want Accept to do, it > > will become clearer. Here's my attempt. Accept takes a chan, takes a > > procedure to loop on, a procedure to accept on, and then returns the > server > > chan to continue the loop. I don't know if this is entirely right, but > it > > type checks and hopefully it will give you some ideas. > > As you've said Accept needs refinement. I tried writing a interpretUDP > which doesn't have an accept loop but does have a bind + listen. I > suspect that accept needs to move into the interpretTCP somehow and > the DSL needs to be Bind or similar. Bind makes more sense as well if > I wanted to write a pipes or a chan based interpreter. > > Thanks for the rewrite and the pointers. > > -Sumit > > > ------------------------------ > > Message: 4 > Date: Sat, 22 Oct 2016 08:27:32 +0700 > From: Kim-Ee Yeoh > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: Re: [Haskell-beginners] Monadic functions definitions for > free monadic DSL > Message-ID: > 7C7ogk-Xvpg at mail.gmail.com> > Content-Type: text/plain; charset="utf-8" > > Dear Sumit, > > You are right that there's something's fishy about the free monadic > modeling of accept. > > The following parallel construction will prove instructive: > > The native effect: > > send :: chan -> ByteString -> IO Bool > > is modeled in the free monad by the constructor for the base functor > > Send :: chan -> ByteString -> (Bool -> next) -> NetworkActivity chan > next > > which is the data wrapping used in the value level > > sendit :: chan -> ByteString -> Free (NetworkActivity chan) Bool > sendit chan buf = liftF (Send chan buf identity) > > Analogously, the native > > accept :: chan -> IO chan > > is modeled by > > Accept :: chan -> (chan -> next) -> NetworkActivity chan next > > used in > > acc :: chan -> Free (NetworkActivity chan) chan > acc chan = liftF (Accept chan identity) > > Except that you used a different constructor for the base functor. Not > > Accept :: chan -> (chan -> next) -> NetworkActivity chan next > > but > > Accept :: chan -> next -> (chan -> next) -> NetworkActivity chan next > > which is equivalent to > > Accept :: chan -> (Maybe chan -> next) -> NetworkActivity chan next > > The new free monadic term that substitutes for the native accept is the > same like before > > acc chan = liftF (Accept chan identity) > > only with a different type > > acc :: chan -> Free (NetworkActivity chan) (Maybe chan) > > modeling a native > > accept :: chan -> IO (Maybe chan) > > Given a native API, its free monad encoding is entirely boilerplate. I > wrote about the boilerplate process here (skip the sections that don't > concern you): > > http://www.atamo.com/articles/free-monads-wont-detox-your-colon/ > > Best, Kim-Ee > > -- Kim-Ee > > On Fri, Oct 14, 2016 at 6:44 AM, Sumit Raja wrote: > > > > I would really like to help you, but without your imports, packages, > etc, > > > it is really hard to interpret your program. Like where does > decodeUtf8 > > > come from, or receive, or TCPSocket? If they are functions you wrote, > I > > > don't need their code, the types would be sufficient. > > > > > Imports are: > > > > import Protolude > > import Control.Monad.Free > > import System.Socket > > import System.Socket.Family.Inet > > import System.Socket.Type.Stream > > import System.Socket.Protocol.TCP > > import Control.Exception ( bracket, catch ) > > import Data.ByteString as BS (uncons) > > > > decodeUtf8 :: ByteString -> Text > > encodeUtf8 :: Text -> ByteString > > > > I'm using the socket library for the actual networking > > (https://hackage.haskell.org/package/socket-0.6.0.1) > > > > type TCPSocket = Socket Inet Stream TCP > > receive :: Socket f t p -> Int -> MessageFlags -> IO ByteString > Source > > send :: Socket f t p -> ByteString -> MessageFlags -> IO Int > > accept :: (Family f, Storable (SocketAddress f)) => Socket f t p > > -> IO (Socket f t p, SocketAddress f) > > > > If it helps the full source is at > > https://bitbucket.org/sumitraja/network-free/src/ > > a4fcbc74c9e178e81d8b10b60d912b32c542b661/src/Lib.hs. > > > > Looking forward to your assistance. > > > > Thanks > > > > Sumit > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: attachments/20161022/742c58ac/attachment-0001.html> > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > ------------------------------ > > End of Beginners Digest, Vol 100, Issue 14 > ****************************************** > -- Best Regards, Boon Hui -------------- next part -------------- An HTML attachment was scrubbed... URL: From andreatj at mail.com Sun Oct 23 05:41:59 2016 From: andreatj at mail.com (Andrea ML) Date: Sun, 23 Oct 2016 07:41:59 +0200 Subject: [Haskell-beginners] Iterate in terms of scanlIterate in Message-ID: An HTML attachment was scrubbed... URL: From atrudyjane at protonmail.com Mon Oct 24 01:37:14 2016 From: atrudyjane at protonmail.com (Atrudyjane) Date: Sun, 23 Oct 2016 21:37:14 -0400 Subject: [Haskell-beginners] Iterate in terms of scanl Message-ID: Hello All, Sorry about my previous post, email was hosed. I was working on a homework problem where the task is to write the iterate function in terms of scanl. Came up with this: myIterate f x = scanl (const.f) x (repeat x) I went looking around for other solutions to check my work and found the following solution on the Haskell Wiki: iterate f x = scanl f x (repeat x) myIterate seems to work checked against the Prelude iterate but I don't know if it's a good solution or not, because the iterate solution on the Haskell Wiki throws a type error. Types of Prelude iterate and scanl are different, so I can see why, or am I missing something? Here is some REPL output: Prelude iterate: λ> take 10 (iterate (+1) 1) [1,2,3,4,5,6,7,8,9,10] myIterate: λ> take 10 (myIterate (+1) 1) [1,2,3,4,5,6,7,8,9,10] Haskell Wiki solution: λ> take 10 (iterate' (+1) 1) error: • Occurs check: cannot construct the infinite type: a ~ a -> a Expected type: a -> a -> a Actual type: (a -> a) -> a -> a • In the first argument of ‘iterate'’, namely ‘(+ 1)’ In the second argument of ‘take’, namely ‘(iterate' (+ 1) 1)’ In the expression: take 10 (iterate' (+ 1) 1) • Relevant bindings include it :: [a] (bound at :63:1) It will work if you pass it a function that takes two parameters though: λ> take 10 (iterate' (+) 1) [1,2,3,4,5,6,7,8,9,10] Any thoughts would be much appreciated! Andrea -------------- next part -------------- An HTML attachment was scrubbed... URL: From russ.abbott at gmail.com Tue Oct 25 18:58:20 2016 From: russ.abbott at gmail.com (Russ Abbott) Date: Tue, 25 Oct 2016 18:58:20 +0000 Subject: [Haskell-beginners] Open faculty position In-Reply-To: <36383e22-0b32-426d-7c9f-5e611bbca233@ucdavis.edu> References: <36383e22-0b32-426d-7c9f-5e611bbca233@ucdavis.edu> Message-ID: > > > Hi all, My Department is conducting a search for computer science faculty. I'm particularly interested in faculty who enjoy Haskell and functional programming. Here is the search announcement . If you are interested, please send application materials to the address on the announcement. Hope to hear from some of you. -- Russ Abbott -------------- next part -------------- An HTML attachment was scrubbed... URL: From matthewjwilliams101 at gmail.com Tue Oct 25 21:05:20 2016 From: matthewjwilliams101 at gmail.com (MJ Williams) Date: Tue, 25 Oct 2016 22:05:20 +0100 Subject: [Haskell-beginners] Open faculty position In-Reply-To: References: <36383e22-0b32-426d-7c9f-5e611bbca233@ucdavis.edu> Message-ID: Hello Russ. Have you heard of the English Russ Abbot? Sorry, but I really had to ask. On 25/10/2016, Russ Abbott wrote: >> >> >> Hi all, > > My Department is conducting a search for computer science faculty. I'm > particularly interested in faculty who enjoy Haskell and functional > programming. Here is the search announcement > . > If you are interested, please send application materials to the address on > the announcement. > > Hope to hear from some of you. > > -- Russ Abbott > From mdibaiee at aol.com Thu Oct 27 08:53:07 2016 From: mdibaiee at aol.com (Mahdi Dibaiee) Date: Thu, 27 Oct 2016 04:53:07 -0400 Subject: [Haskell-beginners] Writing huge result of `show` to file results in out of memory Message-ID: <1580557c3ae-16d1-390c@webstg-a06.mail.aol.com> Hi, So I have a data instance which contains a few big matrices, and I want to save my instance to a file so I can `read` it back later to avoid the long computation every time (I'm training a recurrent neural network). First attempt, the simplest method: writeFile "rnn" (show dataInstance) It starts to take all of the memory and then bails out with `out-of-memory` error. So I wrote a function to write the string chunk by chunk, without buffering, here is the code: https://github.com/mdibaiee/sibe/blob/728df02fbdd6f134af107c098f5477094c61ea76/examples/recurrent.hs#L52-L64 Copy/pasted from the link: saveRecurrent :: FilePath -> String -> Int -> IO () saveRecurrent path str chunkSize = do handle <- openFile path AppendMode hSetBuffering handle NoBuffering loop handle str hClose handle where loop _ [] = return () loop handle s = do hPutStr handle $ take chunkSize s hFlush handle loop handle $ drop chunkSize s But it doesn't work either, I still get `out-of-memory` errors. From what I understand, this should work, but it isn't. I asked on IRC and someone said "Show is not lazy enough", if that's the case, I would appreciate an explanation of that. Thanks, Mahdi -------------- next part -------------- An HTML attachment was scrubbed... URL: From timmelzer at gmail.com Fri Oct 28 07:33:41 2016 From: timmelzer at gmail.com (Norbert Melzer) Date: Fri, 28 Oct 2016 07:33:41 +0000 Subject: [Haskell-beginners] Writing huge result of `show` to file results in out of memory In-Reply-To: <1580557c3ae-16d1-390c@webstg-a06.mail.aol.com> References: <1580557c3ae-16d1-390c@webstg-a06.mail.aol.com> Message-ID: I do not think that `show` or `read` are good candidates for serialising data into a file. I'd suggest to use a dedicated serialisation library, to write out proper JSON/YAML/XML/binary. Mahdi Dibaiee schrieb am Do., 27. Okt. 2016 um 10:53 Uhr: Hi, So I have a data instance which contains a few big matrices, and I want to save my instance to a file so I can `read` it back later to avoid the long computation every time (I'm training a recurrent neural network). First attempt, the simplest method: writeFile "rnn" (show dataInstance) It starts to take all of the memory and then bails out with `out-of-memory` error. So I wrote a function to write the string chunk by chunk, without buffering, here is the code: https://github.com/mdibaiee/sibe/blob/728df02fbdd6f134af107c098f5477094c61ea76/examples/recurrent.hs#L52-L64 Copy/pasted from the link: saveRecurrent :: FilePath -> String -> Int -> IO () saveRecurrent path str chunkSize = do handle <- openFile path AppendMode hSetBuffering handle NoBuffering loop handle str hClose handle where loop _ [] = return () loop handle s = do hPutStr handle $ take chunkSize s hFlush handle loop handle $ drop chunkSize s But it doesn't work either, I still get `out-of-memory` errors. From what I understand, this should work, but it isn't. I asked on IRC and someone said "Show is not lazy *enough*", if that's the case, I would appreciate an explanation of that. Thanks, Mahdi _______________________________________________ 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 imantc at gmail.com Fri Oct 28 08:37:41 2016 From: imantc at gmail.com (Imants Cekusins) Date: Fri, 28 Oct 2016 10:37:41 +0200 Subject: [Haskell-beginners] Writing huge result of `show` to file results in out of memory In-Reply-To: References: <1580557c3ae-16d1-390c@webstg-a06.mail.aol.com> Message-ID: can https://hackage.haskell.org/package/cereal-streams help? I did not use this lib but it may be relevant. see also: https://hackage.haskell.org/package/io-streams-1.3.5.0/docs/System-IO-Streams-Tutorial.html -------------- next part -------------- An HTML attachment was scrubbed... URL: From mdibaiee at aol.com Fri Oct 28 16:57:22 2016 From: mdibaiee at aol.com (Mahdi Dibaiee) Date: Fri, 28 Oct 2016 12:57:22 -0400 Subject: [Haskell-beginners] Writing huge result of `show` to file results in out of memory Message-ID: <1580c39785d-a71-59aa@webstg-a04.mail.aol.com> I know that there are other ways of doing it, I just want to understand where lies the problem here. I just read the realworldhaskell book by O'Reilly, in one section, Lazy I/O [0], it's explained that there should be no problem writing big strings, without consuming the whole string to files, however big they are. I would appreciate it if someone could point out the problem there. Thanks [0]: http://book.realworldhaskell.org/read/io.html#io.lazy -------------- next part -------------- An HTML attachment was scrubbed... URL: From imantc at gmail.com Fri Oct 28 17:13:23 2016 From: imantc at gmail.com (Imants Cekusins) Date: Fri, 28 Oct 2016 19:13:23 +0200 Subject: [Haskell-beginners] Writing huge result of `show` to file results in out of memory In-Reply-To: <1580c39785d-a71-59aa@webstg-a04.mail.aol.com> References: <1580c39785d-a71-59aa@webstg-a04.mail.aol.com> Message-ID: > there should be no problem writing big strings, without consuming the whole string ​ lazy means evaluated when needed. However when a string is evaluated, it is evaluated fully, I guess. -------------- next part -------------- An HTML attachment was scrubbed... URL: From mdibaiee at aol.com Sat Oct 29 17:18:11 2016 From: mdibaiee at aol.com (Mahdi Dibaiee) Date: Sat, 29 Oct 2016 13:18:11 -0400 Subject: [Haskell-beginners] Beginners Digest, Vol 100, Issue 20 In-Reply-To: References: Message-ID: <1581172e335-25c9-3659@webstg-a07.mail.aol.com> That's not generally true (but I will give my guess about your assumption after this). See the example in the article I linked: ``` import Data.Char(toUpper) main = do inpStr <- readFile "input.txt" writeFile "output.txt" (map toUpper inpStr) ``` Here, `inputStr` has to be evaluated in order to run `map toUpper` on it, but it's done character by character. Character is read, `toUpper` is run on it, it's written to the file. (Though it might not be a character unless we use `NoBuffering`, it's going to be lines or blocks) Now, I guess your assumption _might_ be true in my case, because `show` might not be able to generate the string character by character (which is very likely), in this case, show will give us the whole string in one piece, leading to an out-of-memory error as the big string is getting loaded to memory in order to be used by `writeFile`. This is just a guess, I would appreciate it if someone could actually prove it. What do you think Imants? -----Original Message----- From: beginners-request To: beginners Sent: Sat, Oct 29, 2016 3:44 pm Subject: Beginners Digest, Vol 100, Issue 20 Send Beginners mailing list submissions to beginners at haskell.orgTo subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginnersor, via email, send a message with subject or body 'help' to beginners-request at haskell.orgYou can reach the person managing the list at beginners-owner at haskell.orgWhen replying, please edit your Subject line so it is more specificthan "Re: Contents of Beginners digest..."Today's Topics: 1. Writing huge result of `show` to file results in out of memory (Mahdi Dibaiee) 2. Re: Writing huge result of `show` to file results in out of memory (Imants Cekusins) Attached Message From Mahdi Dibaiee To beginners at haskell.org Subject [Haskell-beginners] Writing huge result of `show` to file resultsin out of memory Date Fri, 28 Oct 2016 12:57:22 -0400 I know that there are other ways of doing it, I just want to understand where lies the problem here. I just read the realworldhaskell book by O'Reilly, in one section, Lazy I/O [0], it's explained that there should be no problem writing big strings, without consuming the whole string to files, however big they are. I would appreciate it if someone could point out the problem there. Thanks [0]: http://book.realworldhaskell.org/read/io.html#io.lazy Attached Message From Imants Cekusins To The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject Re: [Haskell-beginners] Writing huge result of `show` to fileresults in out of memory Date Fri, 28 Oct 2016 19:13:23 +0200 > there should be no problem writing big strings, without consuming the whole string ​ lazy means evaluated when needed. However when a string is evaluated, it is evaluated fully, I guess. _______________________________________________Beginners mailing listBeginners at haskell.orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From imantc at gmail.com Sat Oct 29 17:46:00 2016 From: imantc at gmail.com (Imants Cekusins) Date: Sat, 29 Oct 2016 19:46:00 +0200 Subject: [Haskell-beginners] Beginners Digest, Vol 100, Issue 20 In-Reply-To: <1581172e335-25c9-3659@webstg-a07.mail.aol.com> References: <1581172e335-25c9-3659@webstg-a07.mail.aol.com> Message-ID: here is a very comprehensive explanation of lazy evaluation: https://github.com/takenobu-hs/lazy_evaluation ​ I understand < 5% of this so can't add to it. about your particular problem, I would not try to rely on lazy evaluation (regardless of how it actually works) but instead break data in chunks, serialize and write the chunks to disk using libraries. This should work. -------------- next part -------------- An HTML attachment was scrubbed... URL: From thisismyidashish at gmail.com Sun Oct 30 09:00:27 2016 From: thisismyidashish at gmail.com (Ashish Negi) Date: Sun, 30 Oct 2016 14:30:27 +0530 Subject: [Haskell-beginners] Beginners Digest, Vol 100, Issue 19 In-Reply-To: References: Message-ID: Can you write your own custom function for converting the data structure to string ? -------------- next part -------------- An HTML attachment was scrubbed... URL: From timmelzer at gmail.com Sun Oct 30 09:09:43 2016 From: timmelzer at gmail.com (Norbert Melzer) Date: Sun, 30 Oct 2016 09:09:43 +0000 Subject: [Haskell-beginners] Beginners Digest, Vol 100, Issue 19 In-Reply-To: References: Message-ID: When replying to the digest, could you provide more context? Ashish Negi schrieb am So., 30. Okt. 2016 10:00: > Can you write your own custom function for converting the data structure > to string ? > > _______________________________________________ > 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 matt.williams45.mw at gmail.com Sun Oct 30 09:18:59 2016 From: matt.williams45.mw at gmail.com (Matt Williams) Date: Sun, 30 Oct 2016 09:18:59 +0000 Subject: [Haskell-beginners] Beginners Digest, Vol 100, Issue 19 In-Reply-To: References: Message-ID: I suspect you want to provide your own definition of Show. Most tutorials show how to do this. M On Sun, 30 Oct 2016, 09:00 Ashish Negi, wrote: > Can you write your own custom function for converting the data structure > to string ? > > _______________________________________________ > 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 hallgren at chalmers.se Sun Oct 30 10:45:34 2016 From: hallgren at chalmers.se (Thomas Hallgren) Date: Sun, 30 Oct 2016 11:45:34 +0100 Subject: [Haskell-beginners] Writing huge result of `show` to file results in out of memory In-Reply-To: <1580557c3ae-16d1-390c@webstg-a06.mail.aol.com> References: <1580557c3ae-16d1-390c@webstg-a06.mail.aol.com> Message-ID: Hi, I just tried the following simple example: main = writeFile "numbers" (show [1..]) If I load it and run it in ghci (version 8.0.1), the memory use quickly grows to several gigabytes, but if I compile it with ghc -O --make, and run the executable, it runs in constant space (a few megabytes) for a long time. So two observations: 1) If you get a space leak or not can depend on how the code is compiled. Compilers have to take care to avoid space leaks when generating code. 2) I don't think there is any reason why show functions should leak in general, so if you see the space leak also when running properly compiled code, the leak is probably coming from the functions that build the data structure. For example, even though show [1..n] can run in constant space, show (reverse [1..n]) will use space proportional to the length of the list. With laziness, it could be that the data structure isn't built until you show it, so that is when you notice the space leak. Also, if you use the data structure for something else after writing the file, the entire data structure will of course be kept in memory. Thomas H On 2016-10-27 10:53, Mahdi Dibaiee wrote: > Hi, > > So I have a data instance which contains a few big matrices, and I want to save > my instance to a file so I can `read` it back later > to avoid the long computation every time (I'm training a recurrent neural network). > > First attempt, the simplest method: > > writeFile "rnn" (show dataInstance) > > It starts to take all of the memory and then bails out with `out-of-memory` error. > > So I wrote a function to write the string chunk by chunk, without buffering, > here is the code: > https://github.com/mdibaiee/sibe/blob/728df02fbdd6f134af107c098f5477094c61ea76/examples/recurrent.hs#L52-L64 > > Copy/pasted from the link: > > saveRecurrent :: FilePath -> String -> Int -> IO () saveRecurrent path str > chunkSize = do handle <- openFile path AppendMode hSetBuffering handle > NoBuffering loop handle str hClose handle where loop _ [] = return () loop > handle s = do hPutStr handle $ take chunkSize s hFlush handle loop handle $ drop > chunkSize s > > But it doesn't work either, I still get `out-of-memory` errors. From what I > understand, this should work, but it isn't. > I asked on IRC and someone said "Show is not lazy /enough/", if that's the case, > I would appreciate an explanation of that. > > Thanks, > Mahdi > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > From imantc at gmail.com Sun Oct 30 11:28:21 2016 From: imantc at gmail.com (Imants Cekusins) Date: Sun, 30 Oct 2016 12:28:21 +0100 Subject: [Haskell-beginners] Writing huge result of `show` to file results in out of memory In-Reply-To: References: <1580557c3ae-16d1-390c@webstg-a06.mail.aol.com> Message-ID: another thing which may make a difference: this large data structure, is it lazy by any chance? if it is, why not change it to strict? is there a reason why large data structures - especially if they are expected to be serialized at some point - should be used in their lazy form? ​ -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick.browne at dit.ie Sun Oct 30 20:56:12 2016 From: patrick.browne at dit.ie (PATRICK BROWNE) Date: Sun, 30 Oct 2016 20:56:12 +0000 Subject: [Haskell-beginners] Constructor classes and type classes. Message-ID: {- I am trying to understand constructor classes and their relationship with ordinary type classes. I wrote the code below to help me understand the distinction. The code is only for explanatory purposes, set operations use a tuple syntax. I use the naming convention of 'typeVar', 'typeCons', and 'dataCons' for type variables, type constructors, and data constructors respectively. Question1: Is my naming convention correct? I am particularly concerned about SetClass3 where the super class seems to use a type constructor but the subclass seems to use the same term as a data constructor. Q2: In this case constructor classes and type classes seem to provide similar functionality. In general what situation are each best suited? -} import Data.List data SetType typeVar = SetType [typeVar] deriving Show class SetClass1 typeCons where member1 :: Eq typeVar => (typeVar, typeCons typeVar) -> Bool intersect1 :: (Eq typeVar,Show typeVar) => (typeCons typeVar, typeCons typeVar) -> typeCons typeVar class SetClass2 dataCons typeVar where member2 :: (typeVar, (dataCons typeVar)) -> Bool intersect2 :: (dataCons typeVar, dataCons typeVar) -> dataCons typeVar class SetClass1 typeCons => SetClass3 typeCons dataVariable where union3 :: (Eq dataVariable,Show dataVariable) => (typeCons dataVariable, typeCons dataVariable) -> typeCons dataVariable instance SetClass1 SetType where member1 (x ,(SetType y)) = elem x y intersect1 ((SetType x),(SetType y)) = SetType (intersect x y) instance SetClass2 SetType Int where member2 (x ,SetType y) = elem x y intersect2 (SetType x,SetType y) = SetType (intersect x y) instance SetClass3 SetType Int where union3 (SetType x,SetType y) = SetType (union x y) test1a = member1 (1, (SetType [1,2])) test1b = intersect1 ((SetType [1,3,4]),(SetType [1,2])) test2a = member2 (1, (SetType [1::Int])) test2b = intersect2 ((SetType [1::Int]), (SetType [(1::Int)])) test3a = union3 ((SetType [1::Int,2::Int,3::Int]),(SetType [4::Int,5::Int])) -- This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. www.dit.ie Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. www.dit.ie Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman -------------- next part -------------- An HTML attachment was scrubbed... URL: From patrick.browne at dit.ie Mon Oct 31 14:41:17 2016 From: patrick.browne at dit.ie (PATRICK BROWNE) Date: Mon, 31 Oct 2016 14:41:17 +0000 Subject: [Haskell-beginners] Constructor classes and type classes. In-Reply-To: References: Message-ID: My intended use of the term 'subclass' is as the Haskell report: "class Num of numeric types is a subclass of Eq, since all numbers may be compared for equality" Of course what I have written may not be consistent with the report. Regards, Pat On 31 October 2016 at 09:26, Imants Cekusins wrote: > HelloPat, > > I am not sure if terms *superclass* and *subclass* are applicable to > Haskell classes. > > Haskell class is very different from Java class. It is more like an > interface. > > ;) > ​ > -- This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. www.dit.ie Is ó ITBÁC a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. www.dit.ie Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman -------------- next part -------------- An HTML attachment was scrubbed... URL: