From ccherng at gmail.com Thu Jan 1 04:00:04 2015 From: ccherng at gmail.com (Cary Cherng) Date: Wed, 31 Dec 2014 20:00:04 -0800 Subject: [Haskell-beginners] Continuations Message-ID: I read (http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style) that in some circumstances, CPS can be used to improve performance by eliminating certain construction-pattern matching sequences (i.e. a function returns a complex structure which the caller will at some point deconstruct). And that attoparsec is an example of this. I don't see exactly how CPS gives rise to concrete examples of performance gains. Moreover how does this arise in parsing for example with attoparsec as mentioned. I also encountered various web frameworks such as mflow that are based on continuations. How a typical http restful system is made into something based around continuations is not something that is obvious to me. And what is gained from that? From michael at snoyman.com Thu Jan 1 06:42:28 2015 From: michael at snoyman.com (Michael Snoyman) Date: Thu, 01 Jan 2015 06:42:28 +0000 Subject: [Haskell-beginners] Continuations References: Message-ID: To give an idea of why CPS transform can sometimes be more efficient, consider the following two approaches to a "safe divide" function, which check if the denominator is 0: divMay :: Double -> Double -> Maybe Double divMay x y | y == 0 = Nothing | otherwise = Just (x / y) divCPS :: Double -> Double -> a -> (Double -> a) -> a divCPS x y onFail onSuccess | y == 0 = onFail | otherwise = onSuccess (x / y) Presumably, divMay will always force the allocation of a new object when y is not 0, through its usage of Just. divCPS, on the other hand, does not need to perform any allocation. I say presumably, because often times the compiler will be able to optimize this. To see evidence of that, I've put together a small benchmark[1], If I compile with optimizations turned of (-O0), I get results showing that CPS is faster. And looking at the core[2], we can see that divMay really does result in an extra allocation: $ ghc-core --no-cast -- -O0 foo.hs divMay'_r454 :: Double -> Double divMay'_r454 = \ (y_a4hf :: Double) -> Data.Maybe.fromMaybe @ Double (D# 0.0) (case == @ Double $fEqDouble y_a4hf (D# 0.0) of _ [Occ=Dead] { False -> Data.Maybe.Just @ Double (/ @ Double $fFractionalDouble (D# 10.0) y_a4hf); True -> Data.Maybe.Nothing @ Double }) divCPS'_r455 :: Double -> Double divCPS'_r455 = \ (y_a4hg :: Double) -> case == @ Double $fEqDouble y_a4hg (D# 0.0) of _ [Occ=Dead] { False -> id @ Double (/ @ Double $fFractionalDouble (D# 10.0) y_a4hg); True -> D# 0.0 } Yes, core has a lot of stuff going on due to the explicit type signatures, the "magic hash" unboxed types showing up, etc. But as you can see, there's a call to Data.Maybe.Just in divMay' that is not present in divCPS'. Now let's explain that "presumably" comment. GHC is really smart, and if you let it, it will often times optimize away these kinds of things, especially in small examples like this. In this case, the benchmark results show up as almost identical between the two versions, which is hardly surprising when you look at the core: $ ghc-core --no-cast -- -O2 foo.hs divMay'_r4ci :: Double -> Double divMay'_r4ci = \ (y_a4oE :: Double) -> case y_a4oE of _ [Occ=Dead] { D# x_a4Ml -> case x_a4Ml of wild1_X10 { __DEFAULT -> case /## 10.0 wild1_X10 of wild2_a4Rj { __DEFAULT -> D# wild2_a4Rj }; 0.0 -> main9 } } divCPS'_r4cj :: Double -> Double divCPS'_r4cj = \ (y_a4oF :: Double) -> case y_a4oF of _ [Occ=Dead] { D# x_a4Ml -> case x_a4Ml of wild1_X15 { __DEFAULT -> case /## 10.0 wild1_X15 of wild2_a4Rj { __DEFAULT -> D# wild2_a4Rj }; 0.0 -> main9 } } If you look past the different variable names, you'll see that the two functions are in fact identical. So to sum up this (longer than expected) email: * CPS allows you to avoid extra allocations, * but often times GHC can perform that optimization for you itself. You can look at the attoparsec codebase to see its usage of CPS, which is a more complicated usage of this same concept. Continuation passing web frameworks are a totally different story though, and have been covered pretty well elsewhere (probably best by Alberto[3], though others may have different links). [1] https://www.fpcomplete.com/user/snoyberg/random-code-snippets/cps-performance-benchmark [2] Core is a lower-level form that GHC compiles code to before generating machine code. It gives a very good idea of what optimizations have been applied. I used the ghc-core tool for this. Gabriel Gonzalez wrote a nice blog post about this a few years back: http://www.haskellforall.com/2012/10/hello-core.html [3] https://www.fpcomplete.com/user/agocorona On Thu Jan 01 2015 at 6:00:12 AM Cary Cherng wrote: > I read (http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style) > that in some circumstances, CPS can be used to improve performance by > eliminating certain construction-pattern matching sequences (i.e. a > function returns a complex structure which the caller will at some > point deconstruct). And that attoparsec is an example of this. > > I don't see exactly how CPS gives rise to concrete examples of > performance gains. Moreover how does this arise in parsing for example > with attoparsec as mentioned. > > > I also encountered various web frameworks such as mflow that are based > on continuations. How a typical http restful system is made into > something based around continuations is not something that is obvious > to me. And what is gained from that? > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jays at panix.com Thu Jan 1 07:15:37 2015 From: jays at panix.com (Jay Sulzberger) Date: Thu, 1 Jan 2015 02:15:37 -0500 (EST) Subject: [Haskell-beginners] Continuations In-Reply-To: References: Message-ID: On Wed, 31 Dec 2014, Cary Cherng wrote: > I read (http://en.wikibooks.org/wiki/Haskell/Continuation_passing_style) > that in some circumstances, CPS can be used to improve performance by > eliminating certain construction-pattern matching sequences (i.e. a > function returns a complex structure which the caller will at some > point deconstruct). And that attoparsec is an example of this. > > I don't see exactly how CPS gives rise to concrete examples of > performance gains. Moreover how does this arise in parsing for example > with attoparsec as mentioned. > > > I also encountered various web frameworks such as mflow that are based > on continuations. How a typical http restful system is made into > something based around continuations is not something that is obvious > to me. And what is gained from that? I just found this delightful article by John C. Reynolds: The Discoveries of Continuations http://www.math.bas.bg/bantchev/place/iswim/conti-disco.pdf oo--JS. From ky3 at atamo.com Thu Jan 1 10:08:40 2015 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Thu, 1 Jan 2015 17:08:40 +0700 Subject: [Haskell-beginners] Continuations In-Reply-To: References: Message-ID: On Thu, Jan 1, 2015 at 11:00 AM, Cary Cherng wrote: > I don't see exactly how CPS gives rise to concrete examples of > performance gains. > If you look at the "Reflection without Remorse" paper, you'll see performance gains painstakingly described for DLists / difference lists of type [a] -> [a]. The authors see such gains as an application of CPS, the moral being that CPS has a broad range of meanings. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From andrew.bernard at gmail.com Fri Jan 2 12:19:05 2015 From: andrew.bernard at gmail.com (Andrew Bernard) Date: Fri, 2 Jan 2015 23:19:05 +1100 Subject: [Haskell-beginners] Errors package Message-ID: When attempting to install the package ?errors? I get the following: $ cabal install errors Resolving dependencies... In order, the following would be installed: exceptions-0.6.1 (reinstall) changes: mtl-2.1.2 -> 2.2.1, transformers-0.3.0.0 -> 0.4.2.0 transformers-base-0.4.3 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 monad-control-1.0.0.1 (new version) transformers-compat-0.3.3.4 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 contravariant-1.2 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 distributive-0.4.4 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 comonad-4.2.2 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 semigroupoids-4.2 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 bifunctors-4.2 (reinstall) profunctors-4.3.2 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 free-4.10.0.1 (new version) either-4.3.2.1 (new version) errors-1.4.7 (new package) cabal: The following packages are likely to be broken by the reinstalls: temporary-1.2.0.3 ghc-mod-5.2.1.1 resourcet-1.1.2.3 yaml-0.8.9.3 stylish-haskell-0.5.11.0 persistent-sqlite-1.3.0.5 persistent-1.3.3 persistent-template-1.3.2.2 monad-logger-0.3.8 hoogle-4.2.36 conduit-extra-1.1.4.2 conduit-1.2.3 either-4.3.2 monad-journal-0.5.0.1 resource-pool-0.2.3.1 monad-control-0.3.3.0 lifted-base-0.2.3.0 io-choice-0.0.5 HTF-0.12.2.3 free-4.9 Use --force-reinstalls if you want to install anyway. I am using the ghc platform version 7.6.3. Is this package obsolete? Why are so many packages likely to be broken? With Haskell, how does one know what is a proper up to date package and what is not? Andrew -------------- next part -------------- An HTML attachment was scrubbed... URL: From andres.loeh at gmail.com Fri Jan 2 12:27:38 2015 From: andres.loeh at gmail.com (=?UTF-8?Q?Andres_L=C3=B6h?=) Date: Fri, 2 Jan 2015 13:27:38 +0100 Subject: [Haskell-beginners] Errors package In-Reply-To: References: Message-ID: Cabal-install doesn't always find the "best" install plan out of the possibly many, many options available. You can try if you get a better one that does not upgrade "transformers", by saying cabal install errors --constraint="transformers installed" Cheers, Andres On Fri, Jan 2, 2015 at 1:19 PM, Andrew Bernard wrote: > When attempting to install the package ?errors? I get the following: > > $ cabal install errors > Resolving dependencies... > In order, the following would be installed: > exceptions-0.6.1 (reinstall) changes: mtl-2.1.2 -> 2.2.1, > transformers-0.3.0.0 > -> 0.4.2.0 > transformers-base-0.4.3 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 > monad-control-1.0.0.1 (new version) > transformers-compat-0.3.3.4 (reinstall) changes: transformers-0.3.0.0 -> > 0.4.2.0 > contravariant-1.2 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 > distributive-0.4.4 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 > comonad-4.2.2 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 > semigroupoids-4.2 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 > bifunctors-4.2 (reinstall) > profunctors-4.3.2 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 > free-4.10.0.1 (new version) > either-4.3.2.1 (new version) > errors-1.4.7 (new package) > cabal: The following packages are likely to be broken by the reinstalls: > temporary-1.2.0.3 > ghc-mod-5.2.1.1 > resourcet-1.1.2.3 > yaml-0.8.9.3 > stylish-haskell-0.5.11.0 > persistent-sqlite-1.3.0.5 > persistent-1.3.3 > persistent-template-1.3.2.2 > monad-logger-0.3.8 > hoogle-4.2.36 > conduit-extra-1.1.4.2 > conduit-1.2.3 > either-4.3.2 > monad-journal-0.5.0.1 > resource-pool-0.2.3.1 > monad-control-0.3.3.0 > lifted-base-0.2.3.0 > io-choice-0.0.5 > HTF-0.12.2.3 > free-4.9 > Use --force-reinstalls if you want to install anyway. > > I am using the ghc platform version 7.6.3. > > Is this package obsolete? Why are so many packages likely to be broken? > > With Haskell, how does one know what is a proper up to date package and what > is not? > > Andrew > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From michael at snoyman.com Fri Jan 2 12:28:03 2015 From: michael at snoyman.com (Michael Snoyman) Date: Fri, 02 Jan 2015 12:28:03 +0000 Subject: [Haskell-beginners] Errors package References: Message-ID: I'm not sure why cabal has decided that it's best to reinstall a new version of transformers, that seems very strange to me. You can probably work around this by running: cabal install errors --constraint 'transformers == 0.3.0.0' I *would* recommend upgrading to GHC 7.8, as a number of packages have stopped supporting 7.6.3. (For the record, I don't believe any of the packages you have installed have dropped backwards compatibility.) And generally, I recommend using Stackage to avoid these kinds of versioning problems. There are instructions at: http://www.stackage.org Fair warning: I'm the primary Stackage maintainer. On Fri Jan 02 2015 at 2:19:20 PM Andrew Bernard wrote: > When attempting to install the package ?errors? I get the following: > > $ cabal install errors > Resolving dependencies... > In order, the following would be installed: > exceptions-0.6.1 (reinstall) changes: mtl-2.1.2 -> 2.2.1, > transformers-0.3.0.0 > -> 0.4.2.0 > transformers-base-0.4.3 (reinstall) changes: transformers-0.3.0.0 -> > 0.4.2.0 > monad-control-1.0.0.1 (new version) > transformers-compat-0.3.3.4 (reinstall) changes: transformers-0.3.0.0 -> > 0.4.2.0 > contravariant-1.2 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 > distributive-0.4.4 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 > comonad-4.2.2 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 > semigroupoids-4.2 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 > bifunctors-4.2 (reinstall) > profunctors-4.3.2 (reinstall) changes: transformers-0.3.0.0 -> 0.4.2.0 > free-4.10.0.1 (new version) > either-4.3.2.1 (new version) > errors-1.4.7 (new package) > cabal: The following packages are likely to be broken by the reinstalls: > temporary-1.2.0.3 > ghc-mod-5.2.1.1 > resourcet-1.1.2.3 > yaml-0.8.9.3 > stylish-haskell-0.5.11.0 > persistent-sqlite-1.3.0.5 > persistent-1.3.3 > persistent-template-1.3.2.2 > monad-logger-0.3.8 > hoogle-4.2.36 > conduit-extra-1.1.4.2 > conduit-1.2.3 > either-4.3.2 > monad-journal-0.5.0.1 > resource-pool-0.2.3.1 > monad-control-0.3.3.0 > lifted-base-0.2.3.0 > io-choice-0.0.5 > HTF-0.12.2.3 > free-4.9 > Use --force-reinstalls if you want to install anyway. > > I am using the ghc platform version 7.6.3. > > Is this package obsolete? Why are so many packages likely to be broken? > > With Haskell, how does one know what is a proper up to date package and > what is not? > > Andrew > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From andrew.bernard at gmail.com Fri Jan 2 13:21:01 2015 From: andrew.bernard at gmail.com (Andrew Bernard) Date: Sat, 3 Jan 2015 00:21:01 +1100 Subject: [Haskell-beginners] Errors package In-Reply-To: References: Message-ID: Thanks so much gentlemen! Upgrading to the Haskell Platform 7.8.3 solved all the issues completely. The Linux Mint 17.1 apt package is currently still at 7.6.3, but there is a generic linux binary available for 7.8.3 that works just fine. Andrew -------------- next part -------------- An HTML attachment was scrubbed... URL: From julian.birch at gmail.com Fri Jan 2 15:14:37 2015 From: julian.birch at gmail.com (Julian Birch) Date: Fri, 2 Jan 2015 15:14:37 +0000 Subject: [Haskell-beginners] Foldable of Foldable Message-ID: Apologies if this isn't clear, I suspect if I understood the terminology better I'd already have solved the problem. I've got a foldable of a foldable of a. (Specifically `[Set a]`) and I want to be able to express the concept that I can treat `(Foldable f1, Foldable f2) => f1 (f2 a)` as `f3 a` i.e. a foldable of a foldable of a can be newtyped to a foldable of a. At least, I think that's right. Sadly, my attempts at actually expressing this concept have met with incomprehension from GHC, could someone help me out, please? Thanks, Julian. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tonymorris at gmail.com Fri Jan 2 15:16:35 2015 From: tonymorris at gmail.com (Tony Morris) Date: Sat, 03 Jan 2015 01:16:35 +1000 Subject: [Haskell-beginners] Foldable of Foldable In-Reply-To: References: Message-ID: <54A6B653.6000900@gmail.com> A foldable of a foldable is itself foldable. That fact is expressed in Control.Compose: https://hackage.haskell.org/package/TypeCompose-0.9.10/docs/Control-Compose.html instance (Foldable g, Foldable f, Functor g) => Foldable (:. g f) On 03/01/15 01:14, Julian Birch wrote: > Apologies if this isn't clear, I suspect if I understood the > terminology better I'd already have solved the problem. I've got a > foldable of a foldable of a. (Specifically `[Set a]`) and I want to > be able to express the concept that I can treat `(Foldable f1, > Foldable f2) => f1 (f2 a)` as `f3 a` i.e. a foldable of a foldable of > a can be newtyped to a foldable of a. At least, I think that's right. > > Sadly, my attempts at actually expressing this concept have met with > incomprehension from GHC, could someone help me out, please? > > Thanks, > > Julian. > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From julian.birch at gmail.com Fri Jan 2 16:28:09 2015 From: julian.birch at gmail.com (Julian Birch) Date: Fri, 2 Jan 2015 16:28:09 +0000 Subject: [Haskell-beginners] Foldable of Foldable In-Reply-To: <54A6B653.6000900@gmail.com> References: <54A6B653.6000900@gmail.com> Message-ID: Thankyou Tony, that works perfectly. I think my biggest problem was that I didn't even know you could write ``` newtype (Compose g f) a = O (g (f a)) ``` It's logical, but I couldn't figure it out and none of the web pages I could find mentioned it (and the compiler just complains if you try "newtype Compose g f a = " Also, is the Functor declaration really necessary? Seems like you can write ``` instance (Foldable f1, Foldable f2) => Foldable (Compose f1 f2) where foldr f start (O list) = foldr g start list where g = flip . foldr f ``` I'm certainly going to spend some time examining Control.Compose. I think my Haskell brain bending just went up a level, (sadly, not my actual brain.) Thanks again, Julian. On 2 January 2015 at 15:16, Tony Morris wrote: > A foldable of a foldable is itself foldable. That fact is expressed in > Control.Compose: > > > https://hackage.haskell.org/package/TypeCompose-0.9.10/docs/Control-Compose.html > > instance (Foldable g, Foldable f, Functor g) => Foldable (:. g f) > > > On 03/01/15 01:14, Julian Birch wrote: > > Apologies if this isn't clear, I suspect if I understood the terminology > better I'd already have solved the problem. I've got a foldable of a > foldable of a. (Specifically `[Set a]`) and I want to be able to express > the concept that I can treat `(Foldable f1, Foldable f2) => f1 (f2 a)` as > `f3 a` i.e. a foldable of a foldable of a can be newtyped to a foldable of > a. At least, I think that's right. > > Sadly, my attempts at actually expressing this concept have met with > incomprehension from GHC, could someone help me out, please? > > Thanks, > > Julian. > > > _______________________________________________ > Beginners mailing listBeginners at haskell.orghttp://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Fri Jan 2 16:59:22 2015 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Fri, 2 Jan 2015 23:59:22 +0700 Subject: [Haskell-beginners] Foldable of Foldable In-Reply-To: References: <54A6B653.6000900@gmail.com> Message-ID: On Fri, Jan 2, 2015 at 11:28 PM, Julian Birch wrote: It's logical, but I couldn't figure it out and none of the web pages I > could find mentioned it (and the compiler just complains if you try > "newtype Compose g f a = " Whatever's on the right of = got eaten by them gremlins, but newtype (Compose g f) a = O (g (f a)) is equivalent to newtype Compose g f a = O (g (f a)) Try it. Application is left-associative at the type-level just like value-level. > Also, is the Functor declaration really necessary? Seems like you can > write > > ``` > instance (Foldable f1, Foldable f2) => Foldable (Compose f1 f2) where > foldr f start (O list) = foldr g start list > where g = flip . foldr f > Looks to me the case too. For the record, here's what's in Conal's TypeCompose 0.9.10: -- These next two instances are based on suggestions from Creighton Hogg: instance (Foldable g, Foldable f, Functor g) => Foldable (g :. f) where -- foldMap f = fold . fmap (foldMap f) . unO foldMap f = foldMap (foldMap f) . unO -- fold (O gfa) = fold (fold <$> gfa) -- fold = fold . fmap fold . unO fold = foldMap fold . unO -- I could let fold default -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From julian.birch at gmail.com Fri Jan 2 17:32:31 2015 From: julian.birch at gmail.com (Julian Birch) Date: Fri, 2 Jan 2015 17:32:31 +0000 Subject: [Haskell-beginners] Foldable of Foldable In-Reply-To: References: <54A6B653.6000900@gmail.com> Message-ID: Ah! That also works. I think I was a bit confused originally, not putting enough parameter on the left and not enough parens on the right. Being able to read Conal's code made all the difference. (Oh, and it should have been "flip $ foldr f".) J On 2 January 2015 at 16:59, Kim-Ee Yeoh wrote: > On Fri, Jan 2, 2015 at 11:28 PM, Julian Birch > wrote: > > It's logical, but I couldn't figure it out and none of the web pages I >> could find mentioned it (and the compiler just complains if you try >> "newtype Compose g f a = " > > > Whatever's on the right of = got eaten by them gremlins, but > > newtype (Compose g f) a = O (g (f a)) > > is equivalent to > > newtype Compose g f a = O (g (f a)) > > Try it. Application is left-associative at the type-level just like > value-level. > > > > >> Also, is the Functor declaration really necessary? Seems like you can >> write >> >> ``` >> instance (Foldable f1, Foldable f2) => Foldable (Compose f1 f2) where >> foldr f start (O list) = foldr g start list >> where g = flip . foldr f >> > > Looks to me the case too. > > For the record, here's what's in Conal's TypeCompose 0.9.10: > > -- These next two instances are based on suggestions from Creighton Hogg: instance (Foldable g, Foldable f, Functor g) => Foldable (g :. f) where -- foldMap f = fold . fmap (foldMap f) . unO foldMap f = foldMap (foldMap f) . unO -- fold (O gfa) = fold (fold <$> gfa) -- fold = fold . fmap fold . unO fold = foldMap fold . unO -- I could let fold default > > > -- Kim-Ee > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From thomas at koch.ro Sun Jan 4 11:14:07 2015 From: thomas at koch.ro (Thomas Koch) Date: Sun, 04 Jan 2015 12:14:07 +0100 Subject: [Haskell-beginners] Haskell way of defining and implementing OO interfaces Message-ID: <4373916.AyiZNG9BE1@x121e> Hi, I'm writing a password manager that implements a dbus-api using the dbus[1] package. I'd like to separate the code that implements from the dbus api from the code that stores and retrieves the secrets (passwords). In Java I'd use an interface, e.g.: interface PasswordStore { void store(Path path, String secret, Map metadata); (String secret, Map metadata) retrieve(Path path); (String secret, Map metadata) search(Map criteria); } And the dbus-api would export this interface: dbusClient.export(PasswordStore store) What would be a Haskell way to do the same? My only idea is to define a record: data PasswordStore { store :: Path -> Secret -> MetaData -> IO () , retrieve :: Path -> IO (Secret, MetaData) , search :: Criteria -> IO (Secret, MetaData) } Thank you for any suggestions! Thomas Koch [1] http://hackage.haskell.org/package/dbus-0.10.9 From thomas at koch.ro Sun Jan 4 11:26:54 2015 From: thomas at koch.ro (Thomas Koch) Date: Sun, 04 Jan 2015 12:26:54 +0100 Subject: [Haskell-beginners] Generate API implementations from XML Message-ID: <4360512.CQEcta1ySS@x121e> Hi, as written in my previous mail, I'm implementing a dbus interface in Haskell. The interface definition is given in XML[1] according to a given dtd[2]. For every dbus method one need to invoke[3] export :: Client -> ObjectPath -> [Method] -> IO () What would be a good way to autogenerate all this export calls and Method instances? All necessary informations are given in the dbus spec xml. One could write a script that writes Haskell code. Or one could use template haskell that reads the xml file at compile time? Or? Do you know other examples to learn from that autogenerate Haskell code from interface definitions? Thank you, Thomas Koch [1] http://code.metager.de/source/xref/freedesktop/xdg/specs/secret-service/org.freedesktop.Secrets.xml [2] http://standards.freedesktop.org/dbus/1.0/introspect.dtd [3] http://hackage.haskell.org/package/dbus-0.10.9/docs/DBus-Client.html#v:export From julian.birch at gmail.com Sun Jan 4 13:00:47 2015 From: julian.birch at gmail.com (Julian Birch) Date: Sun, 4 Jan 2015 13:00:47 +0000 Subject: [Haskell-beginners] Haskell way of defining and implementing OO interfaces In-Reply-To: <4373916.AyiZNG9BE1@x121e> References: <4373916.AyiZNG9BE1@x121e> Message-ID: Consider that interface PasswordStore { void store(Path path, String secret, Map metadata); } is identical to void store (PasswordStore store, Path path, String secret, Map metadata) or store :: PasswordStore -> Path -> secret -> MetaData -> IO () So, you can treat PasswordStore as a pure data structure (that has things like connection details) and just define functions that use it. I wouldn't worry about grouping the functions together.(*) I'm going to assume you don't really need an actual interface, but if you did, you could investigate typeclasses. Julian. (*) In general terms, the only reason to group functions together is to enforce laws that relate the behaviours together e.g. that you can retrieve something you stored. On 4 January 2015 at 11:14, Thomas Koch wrote: > Hi, > > I'm writing a password manager that implements a dbus-api using the dbus[1] > package. I'd like to separate the code that implements from the dbus api > from > the code that stores and retrieves the secrets (passwords). In Java I'd > use an > interface, e.g.: > > interface PasswordStore { > void store(Path path, String secret, Map metadata); > (String secret, Map metadata) retrieve(Path path); > (String secret, Map metadata) search(Map criteria); > } > > And the dbus-api would export this interface: > > dbusClient.export(PasswordStore store) > > What would be a Haskell way to do the same? My only idea is to define a > record: > > data PasswordStore { > store :: Path -> Secret -> MetaData -> IO () > , retrieve :: Path -> IO (Secret, MetaData) > , search :: Criteria -> IO (Secret, MetaData) > } > > Thank you for any suggestions! Thomas Koch > > [1] http://hackage.haskell.org/package/dbus-0.10.9 > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Sun Jan 4 15:16:59 2015 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Sun, 4 Jan 2015 22:16:59 +0700 Subject: [Haskell-beginners] Haskell way of defining and implementing OO interfaces In-Reply-To: References: <4373916.AyiZNG9BE1@x121e> Message-ID: On Sun, Jan 4, 2015 at 8:00 PM, Julian Birch wrote: Consider that > > interface PasswordStore { > void store(Path path, String secret, Map metadata); > } > > is identical to > > void store (PasswordStore store, Path path, String secret, Map metadata) > > or > > store :: PasswordStore -> Path -> secret -> MetaData -> IO () > > In fact, Thomas's original PasswordStore definition uses record syntax to simultaneously define 'store' as exactly that. That is, PasswordStore is a data triple and the store function picks out the first one. > So, you can treat PasswordStore as a pure data structure (that has things > like connection details) and just define functions that use it. I wouldn't > worry about grouping the functions together.(*) I'm going to assume you > don't really need an actual interface, but if you did, you could > investigate typeclasses. > If I understand correctly he wants the functions together because there's this mysterious piece of Java (?): dbusClient.export(PasswordStore store) I agree that type classes are probably not a good idea here. Thomas, good job on using a record of functions ! But this is the sort of discussion better suited on haskell-cafe, a strict superset of this list, where there are domain experts on dbus to contribute. Haskell-beginners is not better, merely more responsive on language rudiments and libraries circa haskell 98 and LYAH. > Julian. > > (*) In general terms, the only reason to group functions together is to > enforce laws that relate the behaviours together e.g. that you can retrieve > something you stored. > > On 4 January 2015 at 11:14, Thomas Koch wrote: > >> Hi, >> >> I'm writing a password manager that implements a dbus-api using the >> dbus[1] >> package. I'd like to separate the code that implements from the dbus api >> from >> the code that stores and retrieves the secrets (passwords). In Java I'd >> use an >> interface, e.g.: >> >> interface PasswordStore { >> void store(Path path, String secret, Map metadata); >> (String secret, Map metadata) retrieve(Path path); >> (String secret, Map metadata) search(Map criteria); >> } >> >> And the dbus-api would export this interface: >> >> dbusClient.export(PasswordStore store) >> >> What would be a Haskell way to do the same? My only idea is to define a >> record: >> >> data PasswordStore { >> store :: Path -> Secret -> MetaData -> IO () >> , retrieve :: Path -> IO (Secret, MetaData) >> , search :: Criteria -> IO (Secret, MetaData) >> } >> >> Thank you for any suggestions! Thomas Koch >> >> [1] http://hackage.haskell.org/package/dbus-0.10.9 >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From stuart.hungerford at gmail.com Sun Jan 4 20:51:57 2015 From: stuart.hungerford at gmail.com (Stuart Hungerford) Date: Mon, 5 Jan 2015 07:51:57 +1100 Subject: [Haskell-beginners] Relatively simple undirected graph library? Message-ID: Hi, I've recently started learning Haskell and I'm looking for a library to create and manipulate undirected graphs for a small learning project. Ideally I'd like to be able to: - represent undirected or directed graphs with vertex type a and edge type b - not need to manually maintain mappings between vertex values and vertices assumed to be integers - be able to add and remove vertices and edges by their values with appropriate checks for missing vertices etc - find the connected components of a graph Looking on Hackage I can see some very sophisticated graph libraries including Data.Graph and Data.Graph.Inductive and others, but they all seem to need vertices to be integers or not support changing the graph once constructed. This may well be my ignorance of both graph theory and Haskell but can someone point me to a library that meets my needs or should I extend the learning process and create one myself? Thanks, Stu From efasckenoth at gmail.com Mon Jan 5 04:26:59 2015 From: efasckenoth at gmail.com (Stefan =?iso-8859-1?Q?H=F6ck?=) Date: Mon, 5 Jan 2015 05:26:59 +0100 Subject: [Haskell-beginners] Relatively simple undirected graph library? In-Reply-To: References: Message-ID: <20150105042659.GA804@hunter.iway.ch> On Mon, Jan 05, 2015 at 07:51:57AM +1100, Stuart Hungerford wrote: > Hi, > > I've recently started learning Haskell and I'm looking for a library > to create and manipulate undirected graphs for a small learning > project. Ideally I'd like to be able to: > > - represent undirected or directed graphs with vertex type a and edge type b > > - not need to manually maintain mappings between vertex values and > vertices assumed to be integers > > - be able to add and remove vertices and edges by their values with > appropriate checks for missing vertices etc > > - find the connected components of a graph > > Looking on Hackage I can see some very sophisticated graph libraries > including Data.Graph and Data.Graph.Inductive and others, but they all > seem to need vertices to be integers or not support changing the graph > once constructed. > > This may well be my ignorance of both graph theory and Haskell but can > someone point me to a library that meets my needs or should I extend > the learning process and create one myself? > > Thanks, > > Stu Hi Stu I also was in need of a graph library comparable to what you describe, which I'd like to use later on to represent molecular graphs in cheminformatics projects. Since I did not find anything that fit my needs on Hackage, I started writing my own implementation. The code is not ready for anything and so far it's only unlabeled graphs. However, adding labelings later on is - from my experience - no big thing as most of the graph algorithms need only be implemented for unlabeled graphs. If you are interested, I could clean up the code and put it on github, then we could work on it together. Many of the things you need like creating edge- and vertex-induced subgraphs will require only very little work. The same goes for extracting connected subgraphs and filtering by edge or vertex type. Stefan From anomaro.en.ebi at gmail.com Mon Jan 5 08:44:39 2015 From: anomaro.en.ebi at gmail.com (ebi anomaro) Date: Mon, 5 Jan 2015 17:44:39 +0900 Subject: [Haskell-beginners] Beginners Digest, Vol 76, Issue 18 In-Reply-To: References: Message-ID: <7522255479251109642@unknownmsgid> iPhone???? 2014/10/20 21:00?"beginners-request at haskell.org" ??????: > Send Beginners mailing list submissions to > beginners at haskell.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://www.haskell.org/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: Is working with Haskell easier on Linux than on a Mac? > (Brandon Allbery) > 2. Re: Is working with Haskell easier on Linux than on a Mac? > (Brandon Allbery) > 3. Re: Is working with Haskell easier on Linux than on a Mac? > (Michael Martin) > 4. GhcDynamic and DYNAMIC_GHC_PROGRAMS (Jeremy) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Sun, 19 Oct 2014 20:36:56 -0400 > From: Brandon Allbery > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: Re: [Haskell-beginners] Is working with Haskell easier on > Linux than on a Mac? > Message-ID: > > Content-Type: text/plain; charset="utf-8" > > On Sun, Oct 19, 2014 at 8:34 PM, Michael Martin > wrote: > >> Signals.h provides an interface to OS process signals > > > That is , not "Signals.h". And while case may be fungible on OS > X, the trailing "s" is not. > > -- > brandon s allbery kf8nh sine nomine associates > allbery.b at gmail.com ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > > ------------------------------ > > Message: 2 > Date: Sun, 19 Oct 2014 20:40:01 -0400 > From: Brandon Allbery > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: Re: [Haskell-beginners] Is working with Haskell easier on > Linux than on a Mac? > Message-ID: > > Content-Type: text/plain; charset="utf-8" > > On Sun, Oct 19, 2014 at 8:36 PM, Brandon Allbery > wrote: > >> On Sun, Oct 19, 2014 at 8:34 PM, Michael Martin >> wrote: >> >>> Signals.h provides an interface to OS process signals >> >> >> That is , not "Signals.h". And while case may be fungible on OS >> X, the trailing "s" is not. > > To be quite clear: > > pyanfar:857 Z$ ls -l /usr/include/signal.h > -r--r--r-- 1 root wheel 5318 Oct 17 05:41 /usr/include/signal.h > pyanfar:858 Z$ pkgutil --file-info /usr/include/signal.h > volume: / > path: /usr/include/signal.h > > pkgid: com.apple.pkg.DevSDK_OSX109 > pkg-version: 6.1.0.0.1.1413057044 > install-time: 1413646296 > uid: 0 > gid: 0 > mode: 444 > > -- > brandon s allbery kf8nh sine nomine associates > allbery.b at gmail.com ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > > ------------------------------ > > Message: 3 > Date: Sun, 19 Oct 2014 19:54:01 -0500 > From: Michael Martin > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: Re: [Haskell-beginners] Is working with Haskell easier on > Linux than on a Mac? > Message-ID: <54445D29.7080605 at gmail.com> > Content-Type: text/plain; charset="windows-1252"; Format="flowed" > > >> On 10/19/2014 07:36 PM, Brandon Allbery wrote: >> On Sun, Oct 19, 2014 at 8:34 PM, Michael Martin > > wrote: >> >> Signals.h provides an interface to OS process signals >> >> >> That is , not "Signals.h". And while case may be fungible on >> OS X, the trailing "s" is not >> >> -- >> brandon s allbery kf8nh sine nomine associates >> allbery.b at gmail.com >> ballbery at sinenomine.net >> unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net > > You are quite right. My apologies for jumping the gun on that one. > >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > > ------------------------------ > > Message: 4 > Date: Mon, 20 Oct 2014 09:32:18 +0000 (UTC) > From: Jeremy > To: beginners at haskell.org > Subject: [Haskell-beginners] GhcDynamic and DYNAMIC_GHC_PROGRAMS > Message-ID: > Content-Type: text/plain; charset=us-ascii > > mk/config.mk contains GhcDynamic and DYNAMIC_GHC_PROGRAMS variables. What is > the difference between them? From the names and comments, it looks like > DYNAMIC_GHC_PROGRAMS controls whether all the GHC binaries will be static or > dynamically linked, but GhcDynamic only controls the GHC executable? > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > ------------------------------ > > End of Beginners Digest, Vol 76, Issue 18 > ***************************************** From nicholls.mark at vimn.com Mon Jan 5 10:54:32 2015 From: nicholls.mark at vimn.com (Nicholls, Mark) Date: Mon, 5 Jan 2015 10:54:32 +0000 Subject: [Haskell-beginners] typeclass woes...how to constain a typeclass to be "closed" under an operation.... Message-ID: > {-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleContexts, FlexibleInstances, GADTs #-} lets say I have a data type > data S a = S a deriving Show and a nice simple typeclass > class Foo a where > inc :: a -> S a make Integer an instance and.... > instance Foo Integer where > inc x = S x brilliant... now lets say I want my typeclass to enforce that any result of inc is also of a type that is an instance of the typeclass I'll follow my nose...which causes me a problem that makes me realise I maybe I don't understand whats going on. > class Bar a where > inc' :: (Bar (S a)) => a -> S a follow it to.... > instance Bar Integer where > inc' x = S x this WORKS!...even though "S Integer" is not an instance of Bar!...maybe I don't understand. > -- x = inc' (1 :: Integer) This would go BOOM ...."no instance of (Bar (S Integer))" ...so that makes sense.... How do I force Haskell to check the constraint before I evaluate an expression? follow my nose? > -- class (Baz (S a)) => Baz a where > -- inc'' :: a -> S a BOOM ...."Cycle in class declaration".... booo..but I suppose thats understandable.... so a) is my use of typeclasses correct?...or am I missing something? b) if so, how do I force the compiler to make sure my typeclass operations is nicely closed (under the typeclass)? CONFIDENTIALITY NOTICE This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT. -------------- next part -------------- An HTML attachment was scrubbed... URL: From qhfgva at gmail.com Mon Jan 5 20:28:48 2015 From: qhfgva at gmail.com (Dustin Lee) Date: Mon, 5 Jan 2015 13:28:48 -0700 Subject: [Haskell-beginners] Fwd: cis194 hw7 question - getting example to work In-Reply-To: References: Message-ID: (sorry if dup. I sent first before confirming my subscription) >From the homework description I've tried running: dlee at morty ~/repos/cis194/week7 $ runhaskell StringBufEditor.hs Editor.hs:11:8: Could not find module `Control.Monad.State' Perhaps you meant Control.Monad.ST (from base) Control.Monad.ST.Safe (from base) Control.Monad.Fix (from base) Use -v to see a list of the files searched for. Is my haskell install messed up? Have the libraries changed since the homework was created? Thanks, dustin -- Dustin Lee qhfgva=rot13(dustin) -------------- next part -------------- An HTML attachment was scrubbed... URL: From eyeinsky9 at gmail.com Mon Jan 5 21:25:04 2015 From: eyeinsky9 at gmail.com (Carl Eyeinsky) Date: Mon, 5 Jan 2015 23:25:04 +0200 Subject: [Haskell-beginners] Fwd: cis194 hw7 question - getting example to work In-Reply-To: References: Message-ID: Hi Dustin, perhaps you're missing mtl. (There are also C.M.Trans.State in transformers, though IIRC you have to use lift there to access the methods in your monad stack.) On Jan 5, 2015 9:28 PM, "Dustin Lee" wrote: > (sorry if dup. I sent first before confirming my subscription) > > From the homework description I've tried running: > > dlee at morty ~/repos/cis194/week7 $ runhaskell StringBufEditor.hs > > Editor.hs:11:8: > Could not find module `Control.Monad.State' > Perhaps you meant > Control.Monad.ST (from base) > Control.Monad.ST.Safe (from base) > Control.Monad.Fix (from base) > Use -v to see a list of the files searched for. > > Is my haskell install messed up? Have the libraries changed since the > homework was created? > > Thanks, > > dustin > > -- > Dustin Lee > qhfgva=rot13(dustin) > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From qhfgva at gmail.com Mon Jan 5 21:28:01 2015 From: qhfgva at gmail.com (Dustin Lee) Date: Mon, 5 Jan 2015 14:28:01 -0700 Subject: [Haskell-beginners] Fwd: cis194 hw7 question - getting example to work In-Reply-To: References: Message-ID: Thanks for the reply. What's the simplest path to getting this? I'm not very familiar with haskell tools, library management. I'm using ubuntu if that helps On Mon, Jan 5, 2015 at 2:25 PM, Carl Eyeinsky wrote: > Hi Dustin, > > perhaps you're missing mtl. (There are also C.M.Trans.State in > transformers, though IIRC you have to use lift there to access the methods > in your monad stack.) > On Jan 5, 2015 9:28 PM, "Dustin Lee" wrote: > >> (sorry if dup. I sent first before confirming my subscription) >> >> From the homework description I've tried running: >> >> dlee at morty ~/repos/cis194/week7 $ runhaskell StringBufEditor.hs >> >> Editor.hs:11:8: >> Could not find module `Control.Monad.State' >> Perhaps you meant >> Control.Monad.ST (from base) >> Control.Monad.ST.Safe (from base) >> Control.Monad.Fix (from base) >> Use -v to see a list of the files searched for. >> >> Is my haskell install messed up? Have the libraries changed since the >> homework was created? >> >> Thanks, >> >> dustin >> >> -- >> Dustin Lee >> qhfgva=rot13(dustin) >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Dustin Lee qhfgva=rot13(dustin) -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Mon Jan 5 21:28:19 2015 From: allbery.b at gmail.com (Brandon Allbery) Date: Mon, 5 Jan 2015 16:28:19 -0500 Subject: [Haskell-beginners] Fwd: cis194 hw7 question - getting example to work In-Reply-To: References: Message-ID: On Mon, Jan 5, 2015 at 3:28 PM, Dustin Lee wrote: > Is my haskell install messed up? Have the libraries changed since the > homework was created? > It sounds like you have a minimal ghc installation. Many things were shifted from ghc's minimal install to the Haskell Platform. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From cma at bitemyapp.com Mon Jan 5 21:30:59 2015 From: cma at bitemyapp.com (Christopher Allen) Date: Mon, 5 Jan 2015 15:30:59 -0600 Subject: [Haskell-beginners] Fwd: cis194 hw7 question - getting example to work In-Reply-To: References: Message-ID: You can see examples of how to use Cabal and sandboxes to install packages like mtl in an article I wrote for HowIStart: http://howistart.org/posts/haskell/1 People are getting steered toward minimal GHC installs on purpose. New people have to learn how to install packages anyway. You just have to show them and then they can handle it fine. --- Chris Allen On Mon, Jan 5, 2015 at 3:28 PM, Brandon Allbery wrote: > On Mon, Jan 5, 2015 at 3:28 PM, Dustin Lee wrote: > >> Is my haskell install messed up? Have the libraries changed since the >> homework was created? >> > > It sounds like you have a minimal ghc installation. Many things were > shifted from ghc's minimal install to the Haskell Platform. > > -- > brandon s allbery kf8nh sine nomine > associates > allbery.b at gmail.com > ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nicholls.mark at vimn.com Tue Jan 6 08:46:24 2015 From: nicholls.mark at vimn.com (Nicholls, Mark) Date: Tue, 6 Jan 2015 08:46:24 +0000 Subject: [Haskell-beginners] typeclass woes...how to constain a typeclass to be "closed" under an operation.... In-Reply-To: References: Message-ID: I dont seem to have any replies Booo Is my question too stupid? Or boring? Or unintelligable? Its quite common in maths to have operations in a theory that are (set) closed, i just want to translate that notion to a typeclass I have a suggestion that does work Class Foo m where op :: m a -> m (S a) That is closed, but now were working on types of kind ? -> ?, which is another leap of complexity Is this the idiom/pattern i should follow? Or can the closure contraint be expressed directly? Excuse the spelling, sent from a phone with itty bitty keys, it like trying to sow a button on a shirt with a sausage. On 5 Jan 2015, at 10:54, Nicholls, Mark > wrote: > {-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleContexts, FlexibleInstances, GADTs #-} lets say I have a data type > data S a = S a deriving Show and a nice simple typeclass > class Foo a where > inc :: a -> S a make Integer an instance and.... > instance Foo Integer where > inc x = S x brilliant... now lets say I want my typeclass to enforce that any result of inc is also of a type that is an instance of the typeclass I'll follow my nose...which causes me a problem that makes me realise I maybe I don't understand whats going on. > class Bar a where > inc' :: (Bar (S a)) => a -> S a follow it to.... > instance Bar Integer where > inc' x = S x this WORKS!...even though "S Integer" is not an instance of Bar!...maybe I don't understand. > -- x = inc' (1 :: Integer) This would go BOOM ....?no instance of (Bar (S Integer))" ...so that makes sense.... How do I force Haskell to check the constraint before I evaluate an expression? follow my nose? > -- class (Baz (S a)) => Baz a where > -- inc'' :: a -> S a BOOM ...."Cycle in class declaration".... booo..but I suppose thats understandable.... so a) is my use of typeclasses correct?...or am I missing something? b) if so, how do I force the compiler to make sure my typeclass operations is nicely closed (under the typeclass)? CONFIDENTIALITY NOTICE This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT. _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners CONFIDENTIALITY NOTICE This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT. -------------- next part -------------- An HTML attachment was scrubbed... URL: From TimHolzschuh at gmx.de Tue Jan 6 08:55:16 2015 From: TimHolzschuh at gmx.de (Tim Holzschuh) Date: Tue, 06 Jan 2015 09:55:16 +0100 Subject: [Haskell-beginners] *** GMX Spamverdacht *** Re: Relatively simple undirected graph library? In-Reply-To: <20150105042659.GA804@hunter.iway.ch> References: <20150105042659.GA804@hunter.iway.ch> Message-ID: <54ABA2F4.2030208@gmx.de> Hi Stefan! Although I don't need such a library for the moment, and although I'm still a beginner and I probably won't be able to improve your library I'd still love to see it on GitHub - just to check your code. So if you don't mind to have it online "for free" it would be really nice, if you'd put it on your GitHub! Thanks, Tim Am 05.01.2015 um 05:26 schrieb Stefan H?ck: > Hi Stu I also was in need of a graph library comparable to what you > describe, which I'd like to use later on to represent molecular graphs > in cheminformatics projects. Since I did not find anything that fit my > needs on Hackage, I started writing my own implementation. The code is > not ready for anything and so far it's only unlabeled graphs. However, > adding labelings later on is - from my experience - no big thing as > most of the graph algorithms need only be implemented for unlabeled > graphs. If you are interested, I could clean up the code and put it on > github, then we could work on it together. Many of the things you need > like creating edge- and vertex-induced subgraphs will require only > very little work. The same goes for extracting connected subgraphs and > filtering by edge or vertex type. Stefan > _______________________________________________ Beginners mailing list > Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners From qhfgva at gmail.com Tue Jan 6 16:17:28 2015 From: qhfgva at gmail.com (Dustin Lee) Date: Tue, 6 Jan 2015 09:17:28 -0700 Subject: [Haskell-beginners] Fwd: cis194 hw7 question - getting example to work In-Reply-To: References: Message-ID: Hmmm... wasn't able to use cabal: (note I use tsocks regularly to get around our firewall) sudo tsocks cabal update Downloading the latest package list from hackage.haskell.org Warning: http error: Network.Browser.request: Error raised ErrorClosed cabal: Network.Browser.request: Error raised ErrorClosed But I was able to install using: sudo tsocks apt-get install libghc-ghc-mtl-dev I guess I'll practice using cabal at home... On Mon, Jan 5, 2015 at 2:30 PM, Christopher Allen wrote: > You can see examples of how to use Cabal and sandboxes to install packages > like mtl in an article I wrote for HowIStart: > > http://howistart.org/posts/haskell/1 > > People are getting steered toward minimal GHC installs on purpose. New > people have to learn how to install packages anyway. You just have to show > them and then they can handle it fine. > > --- Chris Allen > > > On Mon, Jan 5, 2015 at 3:28 PM, Brandon Allbery > wrote: > >> On Mon, Jan 5, 2015 at 3:28 PM, Dustin Lee wrote: >> >>> Is my haskell install messed up? Have the libraries changed since the >>> homework was created? >>> >> >> It sounds like you have a minimal ghc installation. Many things were >> shifted from ghc's minimal install to the Haskell Platform. >> >> -- >> brandon s allbery kf8nh sine nomine >> associates >> allbery.b at gmail.com >> ballbery at sinenomine.net >> unix, openafs, kerberos, infrastructure, xmonad >> http://sinenomine.net >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Dustin Lee qhfgva=rot13(dustin) -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Tue Jan 6 16:41:09 2015 From: toad3k at gmail.com (David McBride) Date: Tue, 6 Jan 2015 11:41:09 -0500 Subject: [Haskell-beginners] typeclass woes...how to constain a typeclass to be "closed" under an operation.... In-Reply-To: References: Message-ID: I think you've slightly surpassed the skill level of this list. You might try on haskell-cafe, and I would bet you could get an in depth answer on stackoverflow. On Tue, Jan 6, 2015 at 3:46 AM, Nicholls, Mark wrote: > I dont seem to have any replies > Booo > Is my question too stupid? Or boring? Or unintelligable? > > Its quite common in maths to have operations in a theory that are (set) > closed, i just want to translate that notion to a typeclass > > I have a suggestion that does work > > Class Foo m where > op :: m a -> m (S a) > > That is closed, but now were working on types of kind ? -> ?, which is > another leap of complexity > > Is this the idiom/pattern i should follow? Or can the closure contraint > be expressed directly? > > > > Excuse the spelling, sent from a phone with itty bitty keys, it like > trying to sow a button on a shirt with a sausage. > > > On 5 Jan 2015, at 10:54, Nicholls, Mark wrote: > > > {-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleContexts, > FlexibleInstances, GADTs #-} > > > > lets say I have a data type > > > > > data S a = S a deriving Show > > > > and a nice simple typeclass > > > > > class Foo a where > > > inc :: a -> S a > > > > make Integer an instance and.... > > > > > instance Foo Integer where > > > inc x = S x > > > > brilliant... > > > > now lets say I want my typeclass to enforce that any result of inc is also > of a type that is an instance of the typeclass > > I'll follow my nose...which causes me a problem that makes me realise I > maybe I don't understand whats going on. > > > > > class Bar a where > > > inc' :: (Bar (S a)) => a -> S a > > > > follow it to.... > > > > > instance Bar Integer where > > > inc' x = S x > > > > this WORKS!...even though "S Integer" is not an instance of Bar!...maybe I > don't understand. > > > > > -- x = inc' (1 :: Integer) > > > > This would go BOOM ....?no instance of (Bar (S Integer))" > > ...so that makes sense.... > > > > How do I force Haskell to check the constraint before I evaluate an > expression? > > > > follow my nose? > > > > > -- class (Baz (S a)) => Baz a where > > > -- inc'' :: a -> S a > > > > BOOM ...."Cycle in class declaration".... > > booo..but I suppose thats understandable.... > > > > so > > > > a) is my use of typeclasses correct?...or am I missing something? > > b) if so, how do I force the compiler to make sure my typeclass operations > is nicely closed (under the typeclass)? > > > > > > > > CONFIDENTIALITY NOTICE > > This e-mail (and any attached files) is confidential and protected by > copyright (and other intellectual property rights). If you are not the > intended recipient please e-mail the sender and then delete the email and > any attached files immediately. Any further use or dissemination is > prohibited. > > While MTV Networks Europe has taken steps to ensure that this email and > any attachments are virus free, it is your responsibility to ensure that > this message and any attachments are virus free and do not affect your > systems / data. > > Communicating by email is not 100% secure and carries risks such as delay, > data corruption, non-delivery, wrongful interception and unauthorised > amendment. If you communicate with us by e-mail, you acknowledge and assume > these risks, and you agree to take appropriate measures to minimise these > risks when e-mailing us. > > MTV Networks International, MTV Networks UK & Ireland, Greenhouse, > Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions > International, Be Viacom, Viacom International Media Networks and VIMN and > Comedy Central are all trading names of MTV Networks Europe. MTV Networks > Europe is a partnership between MTV Networks Europe Inc. and Viacom > Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley > Crescent, London, NW1 8TT. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > CONFIDENTIALITY NOTICE > > This e-mail (and any attached files) is confidential and protected by > copyright (and other intellectual property rights). If you are not the > intended recipient please e-mail the sender and then delete the email and > any attached files immediately. Any further use or dissemination is > prohibited. > > While MTV Networks Europe has taken steps to ensure that this email and > any attachments are virus free, it is your responsibility to ensure that > this message and any attachments are virus free and do not affect your > systems / data. > > Communicating by email is not 100% secure and carries risks such as delay, > data corruption, non-delivery, wrongful interception and unauthorised > amendment. If you communicate with us by e-mail, you acknowledge and assume > these risks, and you agree to take appropriate measures to minimise these > risks when e-mailing us. > > MTV Networks International, MTV Networks UK & Ireland, Greenhouse, > Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions > International, Be Viacom, Viacom International Media Networks and VIMN and > Comedy Central are all trading names of MTV Networks Europe. MTV Networks > Europe is a partnership between MTV Networks Europe Inc. and Viacom > Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley > Crescent, London, NW1 8TT. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nicholls.mark at vimn.com Tue Jan 6 17:39:57 2015 From: nicholls.mark at vimn.com (Nicholls, Mark) Date: Tue, 6 Jan 2015 17:39:57 +0000 Subject: [Haskell-beginners] typeclass woes...how to constain a typeclass to be "closed" under an operation.... In-Reply-To: References: Message-ID: I can do the basic Haskell...I've done a course even, but as I don't use it... I quickly degenerate into a beginner. I think I've refound my feet....I will post it to the caf? and see what happens. From: Beginners [mailto:beginners-bounces at haskell.org] On Behalf Of David McBride Sent: 06 January 2015 4:41 PM To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] typeclass woes...how to constain a typeclass to be "closed" under an operation.... I think you've slightly surpassed the skill level of this list. You might try on haskell-cafe, and I would bet you could get an in depth answer on stackoverflow. On Tue, Jan 6, 2015 at 3:46 AM, Nicholls, Mark > wrote: I dont seem to have any replies Booo Is my question too stupid? Or boring? Or unintelligable? Its quite common in maths to have operations in a theory that are (set) closed, i just want to translate that notion to a typeclass I have a suggestion that does work Class Foo m where op :: m a -> m (S a) That is closed, but now were working on types of kind * -> *, which is another leap of complexity Is this the idiom/pattern i should follow? Or can the closure contraint be expressed directly? Excuse the spelling, sent from a phone with itty bitty keys, it like trying to sow a button on a shirt with a sausage. On 5 Jan 2015, at 10:54, Nicholls, Mark > wrote: > {-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleContexts, FlexibleInstances, GADTs #-} lets say I have a data type > data S a = S a deriving Show and a nice simple typeclass > class Foo a where > inc :: a -> S a make Integer an instance and.... > instance Foo Integer where > inc x = S x brilliant... now lets say I want my typeclass to enforce that any result of inc is also of a type that is an instance of the typeclass I'll follow my nose...which causes me a problem that makes me realise I maybe I don't understand whats going on. > class Bar a where > inc' :: (Bar (S a)) => a -> S a follow it to.... > instance Bar Integer where > inc' x = S x this WORKS!...even though "S Integer" is not an instance of Bar!...maybe I don't understand. > -- x = inc' (1 :: Integer) This would go BOOM ...."no instance of (Bar (S Integer))" ...so that makes sense.... How do I force Haskell to check the constraint before I evaluate an expression? follow my nose? > -- class (Baz (S a)) => Baz a where > -- inc'' :: a -> S a BOOM ...."Cycle in class declaration".... booo..but I suppose thats understandable.... so a) is my use of typeclasses correct?...or am I missing something? b) if so, how do I force the compiler to make sure my typeclass operations is nicely closed (under the typeclass)? CONFIDENTIALITY NOTICE This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT. _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners CONFIDENTIALITY NOTICE This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT. _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners CONFIDENTIALITY NOTICE This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT. -------------- next part -------------- An HTML attachment was scrubbed... URL: From efasckenoth at gmail.com Tue Jan 6 18:03:46 2015 From: efasckenoth at gmail.com (Stefan =?iso-8859-1?Q?H=F6ck?=) Date: Tue, 6 Jan 2015 19:03:46 +0100 Subject: [Haskell-beginners] Relatively simple undirected graph library? In-Reply-To: <54ABA2F4.2030208@gmx.de> References: <20150105042659.GA804@hunter.iway.ch> <54ABA2F4.2030208@gmx.de> Message-ID: <20150106180346.GA809@hunter.iway.ch> OK, I'll try and clean up the code and put it on GitHub. Mind that it's no big thing so far, but will hopefully evolve to something useful ... On Tue, Jan 06, 2015 at 09:55:16AM +0100, Tim Holzschuh wrote: > Hi Stefan! > > Although I don't need such a library for the moment, and although I'm still > a beginner and I probably won't be able to improve your library I'd still > love to see it on GitHub - just to check your code. > > So if you don't mind to have it online "for free" it would be really nice, > if you'd put it on your GitHub! > > Thanks, > Tim > > Am 05.01.2015 um 05:26 schrieb Stefan H?ck: > >Hi Stu I also was in need of a graph library comparable to what you > >describe, which I'd like to use later on to represent molecular graphs in > >cheminformatics projects. Since I did not find anything that fit my needs > >on Hackage, I started writing my own implementation. The code is not ready > >for anything and so far it's only unlabeled graphs. However, adding > >labelings later on is - from my experience - no big thing as most of the > >graph algorithms need only be implemented for unlabeled graphs. If you are > >interested, I could clean up the code and put it on github, then we could > >work on it together. Many of the things you need like creating edge- and > >vertex-induced subgraphs will require only very little work. The same goes > >for extracting connected subgraphs and filtering by edge or vertex type. > >Stefan _______________________________________________ Beginners mailing > >list Beginners at haskell.org > >http://www.haskell.org/mailman/listinfo/beginners > From akaberto at gmail.com Wed Jan 7 08:54:28 2015 From: akaberto at gmail.com (akash g) Date: Wed, 7 Jan 2015 14:24:28 +0530 Subject: [Haskell-beginners] typeclass woes...how to constain a typeclass to be "closed" under an operation.... In-Reply-To: References: Message-ID: (S Integer) is not an instance of Bar. I solved this by doing the following You can get around this using an empty instance declaration for (S Integer) like so instance Bar (S Integer) Now, the compiler will know that (S Integer) is an instance of this. It doesn't matter if there is no definition of inc' as you won't be using it. However, I don't understand why a class definition will contain itself as a constraint on one of its method. By doing so, That is, whenever you do something like this class Bar a where inc' :: Bar (S a) => a -> S a instance Bar Integer where inc' x = S x The resulting expression will also have to be an instance of Bar (.ie.,instance Bar (S Integer) ) I just don't think this makes any sense. Also, tried searching for any such type signatures in Hoogle, but didn't come across anything like this. On Tue, Jan 6, 2015 at 11:09 PM, Nicholls, Mark wrote: > I can do the basic Haskell?I?ve done a course even, but as I don?t use > it? I quickly degenerate into a beginner. > > > > I think I?ve refound my feet?.I will post it to the caf? and see what > happens. > > > > *From:* Beginners [mailto:beginners-bounces at haskell.org] *On Behalf Of *David > McBride > *Sent:* 06 January 2015 4:41 PM > *To:* The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > *Subject:* Re: [Haskell-beginners] typeclass woes...how to constain a > typeclass to be "closed" under an operation.... > > > > I think you've slightly surpassed the skill level of this list. You might > try on haskell-cafe, and I would bet you could get an in depth answer on > stackoverflow. > > > > On Tue, Jan 6, 2015 at 3:46 AM, Nicholls, Mark > wrote: > > I dont seem to have any replies > > Booo > > Is my question too stupid? Or boring? Or unintelligable? > > > > Its quite common in maths to have operations in a theory that are (set) > closed, i just want to translate that notion to a typeclass > > > > I have a suggestion that does work > > > > Class Foo m where > > op :: m a -> m (S a) > > > > That is closed, but now were working on types of kind ? -> ?, which is > another leap of complexity > > > > Is this the idiom/pattern i should follow? Or can the closure contraint be > expressed directly? > > > > > > Excuse the spelling, sent from a phone with itty bitty keys, it like > trying to sow a button on a shirt with a sausage. > > > > > On 5 Jan 2015, at 10:54, Nicholls, Mark wrote: > > > {-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleContexts, > FlexibleInstances, GADTs #-} > > > > lets say I have a data type > > > > > data S a = S a deriving Show > > > > and a nice simple typeclass > > > > > class Foo a where > > > inc :: a -> S a > > > > make Integer an instance and.... > > > > > instance Foo Integer where > > > inc x = S x > > > > brilliant... > > > > now lets say I want my typeclass to enforce that any result of inc is also > of a type that is an instance of the typeclass > > I'll follow my nose...which causes me a problem that makes me realise I > maybe I don't understand whats going on. > > > > > class Bar a where > > > inc' :: (Bar (S a)) => a -> S a > > > > follow it to.... > > > > > instance Bar Integer where > > > inc' x = S x > > > > this WORKS!...even though "S Integer" is not an instance of Bar!...maybe I > don't understand. > > > > > -- x = inc' (1 :: Integer) > > > > This would go BOOM ....?no instance of (Bar (S Integer))" > > ...so that makes sense.... > > > > How do I force Haskell to check the constraint before I evaluate an > expression? > > > > follow my nose? > > > > > -- class (Baz (S a)) => Baz a where > > > -- inc'' :: a -> S a > > > > BOOM ...."Cycle in class declaration".... > > booo..but I suppose thats understandable.... > > > > so > > > > a) is my use of typeclasses correct?...or am I missing something? > > b) if so, how do I force the compiler to make sure my typeclass operations > is nicely closed (under the typeclass)? > > > > > > > > CONFIDENTIALITY NOTICE > > This e-mail (and any attached files) is confidential and protected by > copyright (and other intellectual property rights). If you are not the > intended recipient please e-mail the sender and then delete the email and > any attached files immediately. Any further use or dissemination is > prohibited. > > While MTV Networks Europe has taken steps to ensure that this email and > any attachments are virus free, it is your responsibility to ensure that > this message and any attachments are virus free and do not affect your > systems / data. > > Communicating by email is not 100% secure and carries risks such as delay, > data corruption, non-delivery, wrongful interception and unauthorised > amendment. If you communicate with us by e-mail, you acknowledge and assume > these risks, and you agree to take appropriate measures to minimise these > risks when e-mailing us. > > MTV Networks International, MTV Networks UK & Ireland, Greenhouse, > Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions > International, Be Viacom, Viacom International Media Networks and VIMN and > Comedy Central are all trading names of MTV Networks Europe. MTV Networks > Europe is a partnership between MTV Networks Europe Inc. and Viacom > Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley > Crescent, London, NW1 8TT. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > CONFIDENTIALITY NOTICE > > This e-mail (and any attached files) is confidential and protected by > copyright (and other intellectual property rights). If you are not the > intended recipient please e-mail the sender and then delete the email and > any attached files immediately. Any further use or dissemination is > prohibited. > > While MTV Networks Europe has taken steps to ensure that this email and > any attachments are virus free, it is your responsibility to ensure that > this message and any attachments are virus free and do not affect your > systems / data. > > Communicating by email is not 100% secure and carries risks such as delay, > data corruption, non-delivery, wrongful interception and unauthorised > amendment. If you communicate with us by e-mail, you acknowledge and assume > these risks, and you agree to take appropriate measures to minimise these > risks when e-mailing us. > > MTV Networks International, MTV Networks UK & Ireland, Greenhouse, > Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions > International, Be Viacom, Viacom International Media Networks and VIMN and > Comedy Central are all trading names of MTV Networks Europe. MTV Networks > Europe is a partnership between MTV Networks Europe Inc. and Viacom > Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley > Crescent, London, NW1 8TT. > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > > > CONFIDENTIALITY NOTICE > > This e-mail (and any attached files) is confidential and protected by > copyright (and other intellectual property rights). If you are not the > intended recipient please e-mail the sender and then delete the email and > any attached files immediately. Any further use or dissemination is > prohibited. > > While MTV Networks Europe has taken steps to ensure that this email and > any attachments are virus free, it is your responsibility to ensure that > this message and any attachments are virus free and do not affect your > systems / data. > > Communicating by email is not 100% secure and carries risks such as delay, > data corruption, non-delivery, wrongful interception and unauthorised > amendment. If you communicate with us by e-mail, you acknowledge and assume > these risks, and you agree to take appropriate measures to minimise these > risks when e-mailing us. > > MTV Networks International, MTV Networks UK & Ireland, Greenhouse, > Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions > International, Be Viacom, Viacom International Media Networks and VIMN and > Comedy Central are all trading names of MTV Networks Europe. MTV Networks > Europe is a partnership between MTV Networks Europe Inc. and Viacom > Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley > Crescent, London, NW1 8TT. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From nicholls.mark at vimn.com Wed Jan 7 13:20:17 2015 From: nicholls.mark at vimn.com (Nicholls, Mark) Date: Wed, 7 Jan 2015 13:20:17 +0000 Subject: [Haskell-beginners] typeclass woes...how to constain a typeclass to be "closed" under an operation.... In-Reply-To: References: Message-ID: Ouch I?m now posting about the same problem in both Haskell mailing lists?. The problem was that if you comment out --instance Bar (S Integer) The code still compiles?the ?Bar (S a)? isnt evaluated until someone invokes inc??.I want my operation to be closed by definition?.in advance. A solution has been posted in the caf?, which is basically the same as the one I posted here, but fixes the cyclic typeclass error.. > class Foo_ a -- just to prevent a cycle in superclass constraints > instance Foo a => Foo_ a > class Foo_ (S a) => Foo a where > type S a > op :: a -> (S a) > -- and an example where you get a compile error if "op x" has an instance, > -- but "op (op x)" does not have an instance. > instance Foo Int where > type S Int = Char > op = toEnum > instance Foo Char where > type S Char = (Char,Char) > op x = (x,x) > instance Foo (Char,Char) where > type S (Char,Char) = Int > op (x,y) = fromEnum x I?ve now tried to write some code using both ?idioms??.i.e. the above?and the The above idiom forces you to define an associated type that inhabits the typeclass The *->* (e.g. monad) idiom forces you to define a parameter to infer the return type, that also inhabits the typeclass?(technically I suppose ?m b? doesn?t inhabit the typeclass, ?m? does?.but So? (>>=) :: Monad m => m a -> (a -> m b) -> m b Is closed. (S Integer) is not an instance of Bar. I solved this by doing the following You can get around this using an empty instance declaration for (S Integer) like so instance Bar (S Integer) Now, the compiler will know that (S Integer) is an instance of this. It doesn't matter if there is no definition of inc' as you won't be using it. However, I don't understand why a class definition will contain itself as a constraint on one of its method. By doing so, That is, whenever you do something like this class Bar a where inc' :: Bar (S a) => a -> S a instance Bar Integer where inc' x = S x The resulting expression will also have to be an instance of Bar (.ie.,instance Bar (S Integer) ) I just don't think this makes any sense. Also, tried searching for any such type signatures in Hoogle, but didn't come across anything like this. On Tue, Jan 6, 2015 at 11:09 PM, Nicholls, Mark > wrote: I can do the basic Haskell?I?ve done a course even, but as I don?t use it? I quickly degenerate into a beginner. I think I?ve refound my feet?.I will post it to the caf? and see what happens. From: Beginners [mailto:beginners-bounces at haskell.org] On Behalf Of David McBride Sent: 06 January 2015 4:41 PM To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell Subject: Re: [Haskell-beginners] typeclass woes...how to constain a typeclass to be "closed" under an operation.... I think you've slightly surpassed the skill level of this list. You might try on haskell-cafe, and I would bet you could get an in depth answer on stackoverflow. On Tue, Jan 6, 2015 at 3:46 AM, Nicholls, Mark > wrote: I dont seem to have any replies Booo Is my question too stupid? Or boring? Or unintelligable? Its quite common in maths to have operations in a theory that are (set) closed, i just want to translate that notion to a typeclass I have a suggestion that does work Class Foo m where op :: m a -> m (S a) That is closed, but now were working on types of kind ? -> ?, which is another leap of complexity Is this the idiom/pattern i should follow? Or can the closure contraint be expressed directly? Excuse the spelling, sent from a phone with itty bitty keys, it like trying to sow a button on a shirt with a sausage. On 5 Jan 2015, at 10:54, Nicholls, Mark > wrote: > {-# LANGUAGE TypeFamilies, MultiParamTypeClasses, FlexibleContexts, FlexibleInstances, GADTs #-} lets say I have a data type > data S a = S a deriving Show and a nice simple typeclass > class Foo a where > inc :: a -> S a make Integer an instance and.... > instance Foo Integer where > inc x = S x brilliant... now lets say I want my typeclass to enforce that any result of inc is also of a type that is an instance of the typeclass I'll follow my nose...which causes me a problem that makes me realise I maybe I don't understand whats going on. > class Bar a where > inc' :: (Bar (S a)) => a -> S a follow it to.... > instance Bar Integer where > inc' x = S x this WORKS!...even though "S Integer" is not an instance of Bar!...maybe I don't understand. > -- x = inc' (1 :: Integer) This would go BOOM ....?no instance of (Bar (S Integer))" ...so that makes sense.... How do I force Haskell to check the constraint before I evaluate an expression? follow my nose? > -- class (Baz (S a)) => Baz a where > -- inc'' :: a -> S a BOOM ...."Cycle in class declaration".... booo..but I suppose thats understandable.... so a) is my use of typeclasses correct?...or am I missing something? b) if so, how do I force the compiler to make sure my typeclass operations is nicely closed (under the typeclass)? CONFIDENTIALITY NOTICE This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT. _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners CONFIDENTIALITY NOTICE This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT. _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners CONFIDENTIALITY NOTICE This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT. _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners CONFIDENTIALITY NOTICE This e-mail (and any attached files) is confidential and protected by copyright (and other intellectual property rights). If you are not the intended recipient please e-mail the sender and then delete the email and any attached files immediately. Any further use or dissemination is prohibited. While MTV Networks Europe has taken steps to ensure that this email and any attachments are virus free, it is your responsibility to ensure that this message and any attachments are virus free and do not affect your systems / data. Communicating by email is not 100% secure and carries risks such as delay, data corruption, non-delivery, wrongful interception and unauthorised amendment. If you communicate with us by e-mail, you acknowledge and assume these risks, and you agree to take appropriate measures to minimise these risks when e-mailing us. MTV Networks International, MTV Networks UK & Ireland, Greenhouse, Nickelodeon Viacom Consumer Products, VBSi, Viacom Brand Solutions International, Be Viacom, Viacom International Media Networks and VIMN and Comedy Central are all trading names of MTV Networks Europe. MTV Networks Europe is a partnership between MTV Networks Europe Inc. and Viacom Networks Europe Inc. Address for service in Great Britain is 17-29 Hawley Crescent, London, NW1 8TT. -------------- next part -------------- An HTML attachment was scrubbed... URL: From qhfgva at gmail.com Thu Jan 8 19:01:49 2015 From: qhfgva at gmail.com (Dustin Lee) Date: Thu, 8 Jan 2015 12:01:49 -0700 Subject: [Haskell-beginners] help understanding error from attempt at CIS 194 : Homework 7 - indexJ Message-ID: Here's what I have so far for JoinList.hs: ( http://www.seas.upenn.edu/~cis194/spring13/hw/07-folds-monoids.pdf) ==== module JoinList where import Data.Monoid import Sized data JoinList m a = Empty | Single m a | Append m (JoinList m a) (JoinList m a) deriving (Eq, Show) tag :: Monoid m => JoinList m a -> m tag Empty = mempty tag (Single m a) = m tag (Append m _ _) = m (+++) :: Monoid m => JoinList m a -> JoinList m a -> JoinList m a (+++) x y = Append (tag x <> tag y) x y indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> Maybe a indexJ _ Empty = Nothing indexJ i (Single m a) | i == 0 = Just a | otherwise = Nothing indexJ i (Append m x y) | (getSize (tag x)) >= i = indexJ i x | otherwise = indexJ (i - (getSize (tag x))) y ===== Here is the error I'm getting. Haven't been able to make sense of it yet. *Sized> :load "JoinList.hs" [1 of 2] Compiling Sized ( Sized.hs, interpreted ) [2 of 2] Compiling JoinList ( JoinList.hs, interpreted ) JoinList.hs:50:21: Could not deduce (b ~ Size) from the context (Sized b, Monoid b) bound by the type signature for indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> Maybe a at JoinList.hs:44:11-63 `b' is a rigid type variable bound by the type signature for indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> Maybe a at JoinList.hs:44:11 Expected type: JoinList Size a Actual type: JoinList b a In the first argument of `tag', namely `x' In the first argument of `getSize', namely `(tag x)' In the first argument of `(>=)', namely `(getSize (tag x))' Failed, modules loaded: Sized. thanks! -- Dustin Lee qhfgva=rot13(dustin) -------------- next part -------------- An HTML attachment was scrubbed... URL: From idivyanshu.ranjan at gmail.com Thu Jan 8 19:26:44 2015 From: idivyanshu.ranjan at gmail.com (divyanshu ranjan) Date: Fri, 9 Jan 2015 00:56:44 +0530 Subject: [Haskell-beginners] help understanding error from attempt at CIS 194 : Homework 7 - indexJ In-Reply-To: References: Message-ID: Hi Dustin, (tag x) return something of type b which implements Sized and Monoid. Where as getSize takes value of type Size. Hence the error. b might not be equal to Size. Thus compiler is complaining. Given b is (Sized b), how one can convert it into Size ? Thanks Divyanshu Ranjan On Fri, Jan 9, 2015 at 12:31 AM, Dustin Lee wrote: > Here's what I have so far for JoinList.hs: ( > http://www.seas.upenn.edu/~cis194/spring13/hw/07-folds-monoids.pdf) > > ==== > module JoinList > > where > > import Data.Monoid > import Sized > > data JoinList m a = Empty > | Single m a > | Append m (JoinList m a) (JoinList m a) > deriving (Eq, Show) > > > tag :: Monoid m => JoinList m a -> m > tag Empty = mempty > tag (Single m a) = m > tag (Append m _ _) = m > > (+++) :: Monoid m => JoinList m a -> JoinList m a -> JoinList m a > (+++) x y = Append (tag x <> tag y) x y > > > indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> Maybe a > indexJ _ Empty = Nothing > indexJ i (Single m a) > | i == 0 = Just a > | otherwise = Nothing > indexJ i (Append m x y) > | (getSize (tag x)) >= i = indexJ i x > | otherwise = indexJ (i - (getSize (tag x))) y > > ===== > > Here is the error I'm getting. Haven't been able to make sense of it yet. > > *Sized> :load "JoinList.hs" > [1 of 2] Compiling Sized ( Sized.hs, interpreted ) > [2 of 2] Compiling JoinList ( JoinList.hs, interpreted ) > > JoinList.hs:50:21: > Could not deduce (b ~ Size) > from the context (Sized b, Monoid b) > bound by the type signature for > indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> > Maybe a > at JoinList.hs:44:11-63 > `b' is a rigid type variable bound by > the type signature for > indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> Maybe a > at JoinList.hs:44:11 > Expected type: JoinList Size a > Actual type: JoinList b a > In the first argument of `tag', namely `x' > In the first argument of `getSize', namely `(tag x)' > In the first argument of `(>=)', namely `(getSize (tag x))' > Failed, modules loaded: Sized. > > > > thanks! > > -- > Dustin Lee > qhfgva=rot13(dustin) > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From qhfgva at gmail.com Thu Jan 8 19:32:46 2015 From: qhfgva at gmail.com (Dustin Lee) Date: Thu, 8 Jan 2015 12:32:46 -0700 Subject: [Haskell-beginners] help understanding error from attempt at CIS 194 : Homework 7 - indexJ In-Reply-To: References: Message-ID: Ahhh.... I'm using the size function now and at least I don't get any compile errors anymore..... Now to check the code actually works. Thanks! On Thu, Jan 8, 2015 at 12:26 PM, divyanshu ranjan < idivyanshu.ranjan at gmail.com> wrote: > Hi Dustin, > > (tag x) return something of type b which implements Sized and Monoid. > Where as getSize takes value of type Size. > Hence the error. b might not be equal to Size. Thus compiler is > complaining. > > Given b is (Sized b), how one can convert it into Size ? > > > > Thanks > Divyanshu Ranjan > > > On Fri, Jan 9, 2015 at 12:31 AM, Dustin Lee wrote: > >> Here's what I have so far for JoinList.hs: ( >> http://www.seas.upenn.edu/~cis194/spring13/hw/07-folds-monoids.pdf) >> >> ==== >> module JoinList >> >> where >> >> import Data.Monoid >> import Sized >> >> data JoinList m a = Empty >> | Single m a >> | Append m (JoinList m a) (JoinList m a) >> deriving (Eq, Show) >> >> >> tag :: Monoid m => JoinList m a -> m >> tag Empty = mempty >> tag (Single m a) = m >> tag (Append m _ _) = m >> >> (+++) :: Monoid m => JoinList m a -> JoinList m a -> JoinList m a >> (+++) x y = Append (tag x <> tag y) x y >> >> >> indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> Maybe a >> indexJ _ Empty = Nothing >> indexJ i (Single m a) >> | i == 0 = Just a >> | otherwise = Nothing >> indexJ i (Append m x y) >> | (getSize (tag x)) >= i = indexJ i x >> | otherwise = indexJ (i - (getSize (tag x))) y >> >> ===== >> >> Here is the error I'm getting. Haven't been able to make sense of it >> yet. >> >> *Sized> :load "JoinList.hs" >> [1 of 2] Compiling Sized ( Sized.hs, interpreted ) >> [2 of 2] Compiling JoinList ( JoinList.hs, interpreted ) >> >> JoinList.hs:50:21: >> Could not deduce (b ~ Size) >> from the context (Sized b, Monoid b) >> bound by the type signature for >> indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> >> Maybe a >> at JoinList.hs:44:11-63 >> `b' is a rigid type variable bound by >> the type signature for >> indexJ :: (Sized b, Monoid b) => Int -> JoinList b a -> Maybe >> a >> at JoinList.hs:44:11 >> Expected type: JoinList Size a >> Actual type: JoinList b a >> In the first argument of `tag', namely `x' >> In the first argument of `getSize', namely `(tag x)' >> In the first argument of `(>=)', namely `(getSize (tag x))' >> Failed, modules loaded: Sized. >> >> >> >> thanks! >> >> -- >> Dustin Lee >> qhfgva=rot13(dustin) >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Dustin Lee qhfgva=rot13(dustin) -------------- next part -------------- An HTML attachment was scrubbed... URL: From letiziagalli.wise at gmail.com Mon Jan 12 18:24:54 2015 From: letiziagalli.wise at gmail.com (Letizia Galli) Date: Mon, 12 Jan 2015 19:24:54 +0100 Subject: [Haskell-beginners] bitmap image with transparent background Message-ID: Hi :-) I have just joined and I don't know if this is a beginner question, anyway I have been given an Haskell code which uses images, but when I convert a tiff with transparent background into a bitmap, their background becomes white and I need it to be transparent. Is there a way in Haskel to convert a bitmap with solid background into a bitmap with transparent background? Is there another format that can be used instead of bitmap in Haskell? Best, Letizia -- Letizia Galli 0039 328 6917393 www.letiziagalli.it -------------- next part -------------- An HTML attachment was scrubbed... URL: From timmelzer at gmail.com Mon Jan 12 18:57:41 2015 From: timmelzer at gmail.com (Norbert Melzer) Date: Mon, 12 Jan 2015 19:57:41 +0100 Subject: [Haskell-beginners] bitmap image with transparent background In-Reply-To: References: Message-ID: Bitmap means exactly which Format? If you refer to BMP, IT does not support an alpha channel, so no transparency in the picture itself. Am 12.01.2015 19:25 schrieb "Letizia Galli" : > Hi :-) > I have just joined and I don't know if this is a beginner question, > anyway I have been given an Haskell code which uses images, > but when I convert a tiff with transparent background into a bitmap, their > background becomes > white and I need it to be transparent. > Is there a way in Haskel to convert a bitmap with solid background into a > bitmap with transparent > background? > Is there another format that can be used instead of bitmap in Haskell? > Best, > Letizia > > -- > Letizia Galli > 0039 328 6917393 > www.letiziagalli.it > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ahammel87 at gmail.com Mon Jan 12 19:25:54 2015 From: ahammel87 at gmail.com (Alex Hammel) Date: Mon, 12 Jan 2015 11:25:54 -0800 Subject: [Haskell-beginners] Associated data type confusion Message-ID: Hello list, I've got a collection of types which come from generated code (it's protocol buffer stuff). I'd like to write a polymorphic function which maps the protocol buffer generated data types to native data types. I've tried something like this: class FromProto a where type InputDataType fromProto :: InputDataType -> a instance ToProto SomeNativeType where type OutputDataType = SomeGeneratedType toProto = {- etc -} instance FromProto SomeOtherNativeType where type InputDataType = SomeOtherGeneratedType fromProto = {- etc -} Which works fine for mapping one native data type to *one* generated data type, but breaks when I want to define different *InputDataType*s for different instances of *FromProto*. I feel like I'm making this more complicated than I need to. Is there an easy way to get the data type to data type mapping that I'm looking for? Cheers, Alex -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael at orlitzky.com Mon Jan 12 20:39:36 2015 From: michael at orlitzky.com (Michael Orlitzky) Date: Mon, 12 Jan 2015 15:39:36 -0500 Subject: [Haskell-beginners] Associated data type confusion In-Reply-To: References: Message-ID: <54B43108.5060200@orlitzky.com> On 01/12/2015 02:25 PM, Alex Hammel wrote: > Hello list, > > I've got a collection of types which come from generated code (it's > protocol buffer stuff). I'd like to write a polymorphic function which > maps the protocol buffer generated data types to native data types. I've > tried something like this: > This should get you started. My ProtoBool and ProtoInt types are just dumb wrappers -- your conversions will be more complicated. But this will compile and run in ghci: ghci> toProto (3::Int) ProtoInt {mkProtoInt = 3} ghci> toProto True ProtoBool {mkProtoBool = True} If the toProto and fromProto definitions look goofy in the instances it's because they're in point-free style. You could just as well have e.g., toProto b = ProtoBool b or, fromProto i = mkProtoInt i ---- {-# LANGUAGE TypeFamilies #-} module Proto where -- | Just a wrapper around a 'Bool'. newtype ProtoBool = ProtoBool { mkProtoBool :: Bool } deriving (Show) -- | Just a wrapper around an 'Int'. newtype ProtoInt = ProtoInt { mkProtoInt :: Int } deriving (Show) class ToFromProto a where -- | The type 'a' has another type associated with it, the -- "protocol buffer type" that we can convert to/from. 'Proto' -- below is a type function which when applied to 'a' should -- return this associated type. type Proto a :: * toProto :: a -> (Proto a) fromProto :: (Proto a) -> a instance ToFromProto Bool where -- | The type associated with 'Bool' is 'ProtoBool' type Proto Bool = ProtoBool -- | How do we make a 'ProtoBool' from a 'Bool'? Just wrap it. toProto = ProtoBool -- | How do we get a 'Bool' From a 'ProtoBool'? Unwrap it. fromProto = mkProtoBool -- | The same for 'Int' and 'ProtoInt'. instance ToFromProto Int where type Proto Int = ProtoInt toProto = ProtoInt fromProto = mkProtoInt From ahammel87 at gmail.com Mon Jan 12 21:35:11 2015 From: ahammel87 at gmail.com (Alex Hammel) Date: Mon, 12 Jan 2015 13:35:11 -0800 Subject: [Haskell-beginners] Associated data type confusion In-Reply-To: <54B43108.5060200@orlitzky.com> References: <54B43108.5060200@orlitzky.com> Message-ID: Awesome, that did the trick! I think I need a bit more practice with 'type functions' before I'm really comfortable with them. Thanks a lot! Cheers, Alex On Mon, Jan 12, 2015 at 12:39 PM, Michael Orlitzky wrote: > On 01/12/2015 02:25 PM, Alex Hammel wrote: > > Hello list, > > > > I've got a collection of types which come from generated code (it's > > protocol buffer stuff). I'd like to write a polymorphic function which > > maps the protocol buffer generated data types to native data types. I've > > tried something like this: > > > > This should get you started. My ProtoBool and ProtoInt types are just > dumb wrappers -- your conversions will be more complicated. But this > will compile and run in ghci: > > ghci> toProto (3::Int) > ProtoInt {mkProtoInt = 3} > > ghci> toProto True > ProtoBool {mkProtoBool = True} > > > If the toProto and fromProto definitions look goofy in the instances > it's because they're in point-free style. You could just as well have e.g., > > toProto b = ProtoBool b > > or, > > fromProto i = mkProtoInt i > > > ---- > > {-# LANGUAGE TypeFamilies #-} > > module Proto > where > > -- | Just a wrapper around a 'Bool'. > newtype ProtoBool = ProtoBool { mkProtoBool :: Bool } deriving (Show) > > -- | Just a wrapper around an 'Int'. > newtype ProtoInt = ProtoInt { mkProtoInt :: Int } deriving (Show) > > > class ToFromProto a where > -- | The type 'a' has another type associated with it, the > -- "protocol buffer type" that we can convert to/from. 'Proto' > -- below is a type function which when applied to 'a' should > -- return this associated type. > type Proto a :: * > > toProto :: a -> (Proto a) > fromProto :: (Proto a) -> a > > instance ToFromProto Bool where > -- | The type associated with 'Bool' is 'ProtoBool' > type Proto Bool = ProtoBool > > -- | How do we make a 'ProtoBool' from a 'Bool'? Just wrap it. > toProto = ProtoBool > > -- | How do we get a 'Bool' From a 'ProtoBool'? Unwrap it. > fromProto = mkProtoBool > > > -- | The same for 'Int' and 'ProtoInt'. > instance ToFromProto Int where > type Proto Int = ProtoInt > toProto = ProtoInt > fromProto = mkProtoInt > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From marcin.jan.mrotek at gmail.com Mon Jan 12 22:37:12 2015 From: marcin.jan.mrotek at gmail.com (Marcin Mrotek) Date: Mon, 12 Jan 2015 23:37:12 +0100 Subject: [Haskell-beginners] bitmap image with transparent background In-Reply-To: References: Message-ID: Not sure if it helps you, but the Juicy Pixels library can load many different formats, including TIFF: https://hackage.haskell.org/package/JuicyPixels Can you show your code that causes trouble? Regards, Marcin From letiziagalli.wise at gmail.com Tue Jan 13 07:53:05 2015 From: letiziagalli.wise at gmail.com (Letizia Galli) Date: Tue, 13 Jan 2015 08:53:05 +0100 Subject: [Haskell-beginners] bitmap image with transparent background In-Reply-To: References: Message-ID: Hi thank you for your reply, yes I mean BMP and I know it doesn't have a alpha channel I was just asking if there is a way to use images in a format with a transparent background. Best Letizia 2015-01-12 19:57 GMT+01:00 Norbert Melzer : > Bitmap means exactly which Format? If you refer to BMP, IT does not > support an alpha channel, so no transparency in the picture itself. > Am 12.01.2015 19:25 schrieb "Letizia Galli" : > >> Hi :-) >> I have just joined and I don't know if this is a beginner question, >> anyway I have been given an Haskell code which uses images, >> but when I convert a tiff with transparent background into a bitmap, >> their background becomes >> white and I need it to be transparent. >> Is there a way in Haskel to convert a bitmap with solid background into a >> bitmap with transparent >> background? >> Is there another format that can be used instead of bitmap in Haskell? >> Best, >> Letizia >> >> -- >> Letizia Galli >> 0039 328 6917393 >> www.letiziagalli.it >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Letizia Galli 0039 328 6917393 www.letiziagalli.it -------------- next part -------------- An HTML attachment was scrubbed... URL: From letiziagalli.wise at gmail.com Tue Jan 13 08:16:22 2015 From: letiziagalli.wise at gmail.com (Letizia Galli) Date: Tue, 13 Jan 2015 09:16:22 +0100 Subject: [Haskell-beginners] bitmap image with transparent background In-Reply-To: References: Message-ID: Hi Marcin, Thank you for your reply I'll look into it. I would like to show you the code but I have to ask as we like to keep it confidential at the moment. Best, Letizia 2015-01-12 23:37 GMT+01:00 Marcin Mrotek : > Not sure if it helps you, but the Juicy Pixels library can load many > different formats, including TIFF: > https://hackage.haskell.org/package/JuicyPixels Can you show your code > that causes trouble? > > Regards, > Marcin > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Letizia Galli 0039 328 6917393 www.letiziagalli.it -------------- next part -------------- An HTML attachment was scrubbed... URL: From haskell at elisehuard.be Wed Jan 14 10:24:24 2015 From: haskell at elisehuard.be (Elise Huard) Date: Wed, 14 Jan 2015 11:24:24 +0100 Subject: [Haskell-beginners] garbage collected object Message-ID: Hi, Maybe a stupid question: is there a way to check whether a particular data structure (or set of data structures) has been garbage collected? Or indirectly: is there a way to check what's still alive in the heap, so that you can potentially diff from one moment to another? Thanks, Elise Huard From timmelzer at gmail.com Wed Jan 14 12:12:11 2015 From: timmelzer at gmail.com (Norbert Melzer) Date: Wed, 14 Jan 2015 13:12:11 +0100 Subject: [Haskell-beginners] garbage collected object In-Reply-To: References: Message-ID: If you know enough about an object to find it on the heap, then you hold a reference, if you hold a reference, then it's probably not gced... Am 14.01.2015 11:24 schrieb "Elise Huard" : > Hi, > > Maybe a stupid question: is there a way to check whether a particular > data structure (or set of data structures) has been garbage collected? > Or indirectly: is there a way to check what's still alive in the heap, > so that you can potentially diff from one moment to another? > Thanks, > > Elise Huard > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ryan.trinkle at gmail.com Wed Jan 14 12:17:33 2015 From: ryan.trinkle at gmail.com (Ryan Trinkle) Date: Wed, 14 Jan 2015 07:17:33 -0500 Subject: [Haskell-beginners] garbage collected object In-Reply-To: References: Message-ID: You can use the 'vacuum' package to explore the heap, and you can use System.Mem.Weak to create a reference to something that won't keep it from being GCed and will let you check on it later. You can also use the built-in heap profiling capabilities to track things like the memory usage of the entire heap from moment to moment. On Wed, Jan 14, 2015 at 5:24 AM, Elise Huard wrote: > Hi, > > Maybe a stupid question: is there a way to check whether a particular > data structure (or set of data structures) has been garbage collected? > Or indirectly: is there a way to check what's still alive in the heap, > so that you can potentially diff from one moment to another? > Thanks, > > Elise Huard > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kalawatia_abhinav at yahoo.com Wed Jan 14 12:54:34 2015 From: kalawatia_abhinav at yahoo.com (Abhinav Kalawatia) Date: Wed, 14 Jan 2015 12:54:34 +0000 (UTC) Subject: [Haskell-beginners] Error while trying to install ByteString Package Message-ID: <1496218722.1011416.1421240074267.JavaMail.yahoo@jws10604.mail.bf1.yahoo.com> Hi,?I get the following error while I tried installing the ByteString package. Could you please help me resolve this?>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> C:\Users> cabal install bytestring Resolving dependencies... Configuring bytestring-0.10.4.1... Failed to install bytestring-0.10.4.1 Last 10 lines of the build log ( C:\Users\AppData\Roaming\caba l\logs\bytestring-0.10.4.1.log ): setup-Cabal-1.18.1.3-i386-windows-ghc-7.8.3.exe: Missing dependency on a foreign library: * Missing (or bad) header file: fpstring.h This problem can usually be solved by installing the system package that provides this library (you may need the "-dev" version). If the library is already installed but in a non-standard location then you can use the flags --extra-include-dirs= and --extra-lib-dirs= to specify where it is. If the header file does exist, it may contain errors that are caught by the C compiler at the preprocessing stage. In this case you can re-run configure with the verbosity flag -v3 to see the error messages. cabal: Error: some packages failed to install: bytestring-0.10.4.1 failed during the configure step. The exception was: ExitFailure 1>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>?Regards,Abhinav? -------------- next part -------------- An HTML attachment was scrubbed... URL: From miroslav.karpis at gmail.com Thu Jan 15 11:51:37 2015 From: miroslav.karpis at gmail.com (Miro Karpis) Date: Thu, 15 Jan 2015 12:51:37 +0100 Subject: [Haskell-beginners] help with IO guards Message-ID: Hi, please is there a way to have guards with 'where' that communicates with IO? Or is there some other more elegant way? I can do this with classic if/else,...but I just find it nicer with guards. I have something like this (just an example): f :: Int -> IO String f x | null dbOutput = return "no db record" | otherwise = return "we got some db records" where dbOutput = getDBRecord x getDBRecord :: Int -> IO [Int] getDBRecord recordId = do putStrLn $ "checking dbRecord" ++ show recordId --getting data from DB return [1,2] problem is that db dbOutput is IO and the guard check does not like it: Couldn't match expected type ?[a0]? with actual type ?IO [Int]? In the first argument of ?null?, namely ?dbOutput? In the expression: null dbOutput Cheers, Miro -------------- next part -------------- An HTML attachment was scrubbed... URL: From julian.birch at gmail.com Thu Jan 15 12:51:53 2015 From: julian.birch at gmail.com (Julian Birch) Date: Thu, 15 Jan 2015 12:51:53 +0000 Subject: [Haskell-beginners] help with IO guards In-Reply-To: References: Message-ID: Here's how I would do it: Write two functions. One (f) takes the int and gets the IO record. One (g) takes a (non-IO) record and returns the string. Now you can use a bind (=<<) to combine the two. Once you've got that working, you can move the bits into the where clause of your original function. Hope this helps. J On Thursday, January 15, 2015, Miro Karpis wrote: > Hi, > > please is there a way to have guards with 'where' that communicates with > IO? Or is there some other more elegant way? I can do this with classic > if/else,...but I just find it nicer with guards. > > > I have something like this (just an example): > > > f :: Int -> IO String > f x > | null dbOutput = return "no db record" > | otherwise = return "we got some db records" > where dbOutput = getDBRecord x > > > getDBRecord :: Int -> IO [Int] > getDBRecord recordId = do > putStrLn $ "checking dbRecord" ++ show recordId > --getting data from DB > return [1,2] > > > problem is that db dbOutput is IO and the guard check does not like it: > > Couldn't match expected type ?[a0]? with actual type ?IO [Int]? > In the first argument of ?null?, namely ?dbOutput? > In the expression: null dbOutput > > > > Cheers, > Miro > -- Sent from an iPhone, please excuse brevity and typos. -------------- next part -------------- An HTML attachment was scrubbed... URL: From defigueiredo at ucdavis.edu Thu Jan 15 15:24:30 2015 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Thu, 15 Jan 2015 13:24:30 -0200 Subject: [Haskell-beginners] help with IO guards In-Reply-To: References: Message-ID: <54B7DBAE.6010709@ucdavis.edu> I would not say that the problem is with the guard check. The problem is with 'null'. It's type is Prelude> :t null null :: [a] -> Bool So, it expects a list of something, rather than an IO of something, whence the complaint. Dimitri On 15/01/15 09:51, Miro Karpis wrote: > Hi, > > please is there a way to have guards with 'where' that communicates > with IO? Or is there some other more elegant way? I can do this with > classic if/else,...but I just find it nicer with guards. > > > I have something like this (just an example): > > > f :: Int -> IO String > f x > | null dbOutput = return "no db record" > | otherwise = return "we got some db records" > where dbOutput = getDBRecord x > > > getDBRecord :: Int -> IO [Int] > getDBRecord recordId = do > putStrLn $ "checking dbRecord" ++ show recordId > --getting data from DB > return [1,2] > > > problem is that db dbOutput is IO and the guard check does not like it: > > Couldn't match expected type ?[a0]? with actual type ?IO [Int]? > In the first argument of ?null?, namely ?dbOutput? > In the expression: null dbOutput > > > > Cheers, > Miro > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From mwm at mired.org Thu Jan 15 16:42:27 2015 From: mwm at mired.org (Mike Meyer) Date: Thu, 15 Jan 2015 10:42:27 -0600 Subject: [Haskell-beginners] help with IO guards In-Reply-To: <54B7DBAE.6010709@ucdavis.edu> References: <54B7DBAE.6010709@ucdavis.edu> Message-ID: On Thu, Jan 15, 2015 at 9:24 AM, Dimitri DeFigueiredo < defigueiredo at ucdavis.edu> wrote: > I would not say that the problem is with the guard check. The problem is > with 'null'. It's type is > > Prelude> :t null > null :: [a] -> Bool > > So, it expects a list of something, rather than an IO of something, whence > the complaint. > While that's the source of the error, the problem is the combination of the guard check and where to bind a value in the IO monad. Guard checks must have a value of Bool. getDBRecord returns something of type IO [Int]. Where just binds a name, so you either need a way to extract the [Int] from the return value before binding it in the where, or a function of type IO [Int] -> Bool for the guard. Note that this isn't an IO issue but a monad issue. There isn't a monad method that returns a value not in the monad, so you can't write either of the two options above using monad methods. The best solution is the one already proposed - write a function from [Int] -> IO String, and use bind (>>=) on that function to handle things. You could also use the do sugaring of <- to get a less functional version. The last option is to use the IO-specific function unsafePerformIO to write something like nullIO = null . unsafePerformIO. But it's called UNSAFE and tucked away in a module of similar operations for a reason. Using bind is much preferred. > On 15/01/15 09:51, Miro Karpis wrote: > > Hi, > > please is there a way to have guards with 'where' that communicates with > IO? Or is there some other more elegant way? I can do this with classic > if/else,...but I just find it nicer with guards. > > > I have something like this (just an example): > > > f :: Int -> IO String > f x > | null dbOutput = return "no db record" > | otherwise = return "we got some db records" > where dbOutput = getDBRecord x > > > getDBRecord :: Int -> IO [Int] > getDBRecord recordId = do > putStrLn $ "checking dbRecord" ++ show recordId > --getting data from DB > return [1,2] > > > problem is that db dbOutput is IO and the guard check does not like it: > > Couldn't match expected type ?[a0]? with actual type ?IO [Int]? > In the first argument of ?null?, namely ?dbOutput? > In the expression: null dbOutput > > > > Cheers, > Miro > > > _______________________________________________ > Beginners mailing listBeginners at haskell.orghttp://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From miroslav.karpis at gmail.com Thu Jan 15 20:44:29 2015 From: miroslav.karpis at gmail.com (Miro Karpis) Date: Thu, 15 Jan 2015 21:44:29 +0100 Subject: [Haskell-beginners] help with IO guards In-Reply-To: References: <54B7DBAE.6010709@ucdavis.edu> Message-ID: Many thanks everyone,.. but I'm not so confident with monads. If I understand I could translate with them for example IO String to String? If this is true I'm having troubles to achieve this. Here is my simple test (only to test the logic), which ends with error: Couldn't match type ?[]? with ?IO? Expected type: IO Char Actual type: String In the expression: f a In the second argument of ?(>>=)?, namely ?(\ a -> f a)? g = readLn >>= (\a -> f a) f :: String -> String f s_ = s_ ++ "!" Cheers, Miro On Thu, Jan 15, 2015 at 5:42 PM, Mike Meyer wrote: > On Thu, Jan 15, 2015 at 9:24 AM, Dimitri DeFigueiredo < > defigueiredo at ucdavis.edu> wrote: > >> I would not say that the problem is with the guard check. The problem is >> with 'null'. It's type is >> >> Prelude> :t null >> null :: [a] -> Bool >> >> So, it expects a list of something, rather than an IO of something, >> whence the complaint. >> > > > While that's the source of the error, the problem is the combination of > the guard check and where to bind a value in the IO monad. > > Guard checks must have a value of Bool. getDBRecord returns something of > type IO [Int]. Where just binds a name, so you either need a way to extract > the [Int] from the return value before binding it in the where, or a > function of type IO [Int] -> Bool for the guard. > > Note that this isn't an IO issue but a monad issue. There isn't a monad > method that returns a value not in the monad, so you can't write either of > the two options above using monad methods. The best solution is the one > already proposed - write a function from [Int] -> IO String, and use bind > (>>=) on that function to handle things. You could also use the do sugaring > of <- to get a less functional version. > > The last option is to use the IO-specific function unsafePerformIO to > write something like nullIO = null . unsafePerformIO. But it's called > UNSAFE and tucked away in a module of similar operations for a reason. > Using bind is much preferred. > > >> On 15/01/15 09:51, Miro Karpis wrote: >> >> Hi, >> >> please is there a way to have guards with 'where' that communicates with >> IO? Or is there some other more elegant way? I can do this with classic >> if/else,...but I just find it nicer with guards. >> >> >> I have something like this (just an example): >> >> >> f :: Int -> IO String >> f x >> | null dbOutput = return "no db record" >> | otherwise = return "we got some db records" >> where dbOutput = getDBRecord x >> >> >> getDBRecord :: Int -> IO [Int] >> getDBRecord recordId = do >> putStrLn $ "checking dbRecord" ++ show recordId >> --getting data from DB >> return [1,2] >> >> >> problem is that db dbOutput is IO and the guard check does not like it: >> >> Couldn't match expected type ?[a0]? with actual type ?IO [Int]? >> In the first argument of ?null?, namely ?dbOutput? >> In the expression: null dbOutput >> >> >> >> Cheers, >> Miro >> >> >> _______________________________________________ >> Beginners mailing listBeginners at haskell.orghttp://www.haskell.org/mailman/listinfo/beginners >> >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From mwm at mired.org Thu Jan 15 21:01:19 2015 From: mwm at mired.org (Mike Meyer) Date: Thu, 15 Jan 2015 15:01:19 -0600 Subject: [Haskell-beginners] help with IO guards In-Reply-To: References: <54B7DBAE.6010709@ucdavis.edu> Message-ID: f has the wrong type. It needs to return a value in the IO monad. For instance: f: String -> IO String f s = return (s ++ "!") g :: IO String g = readLn >>= f You could also let f return a value of a type other than String, say: f :: String -> IO () f = print The type of >>= tells you this: Monad m => m a -> (a -> m b) -> m b. The second argument has type a -> m b. String -> IO () or String -> IO String both match that. As a warning, readLn expects a Haskell value, and f requires that it be a string. So you have to type in a string the way you would in a program, as "Hello" or ['H', 'e', 'l', 'l', 'o'] or you'll get a parse failure. On Thu, Jan 15, 2015 at 2:44 PM, Miro Karpis wrote: > Many thanks everyone,.. but I'm not so confident with monads. If I > understand I could translate with them for example IO String to String? If > this is true I'm having troubles to achieve this. > > Here is my simple test (only to test the logic), which ends with error: > > Couldn't match type ?[]? with ?IO? > Expected type: IO Char > Actual type: String > In the expression: f a > In the second argument of ?(>>=)?, namely ?(\ a -> f a)? > > > > g = readLn > >>= (\a -> f a) > > f :: String -> String > f s_ = s_ ++ "!" > > > > Cheers, Miro > > On Thu, Jan 15, 2015 at 5:42 PM, Mike Meyer wrote: > >> On Thu, Jan 15, 2015 at 9:24 AM, Dimitri DeFigueiredo < >> defigueiredo at ucdavis.edu> wrote: >> >>> I would not say that the problem is with the guard check. The problem >>> is with 'null'. It's type is >>> >>> Prelude> :t null >>> null :: [a] -> Bool >>> >>> So, it expects a list of something, rather than an IO of something, >>> whence the complaint. >>> >> >> >> While that's the source of the error, the problem is the combination of >> the guard check and where to bind a value in the IO monad. >> >> Guard checks must have a value of Bool. getDBRecord returns something of >> type IO [Int]. Where just binds a name, so you either need a way to extract >> the [Int] from the return value before binding it in the where, or a >> function of type IO [Int] -> Bool for the guard. >> >> Note that this isn't an IO issue but a monad issue. There isn't a monad >> method that returns a value not in the monad, so you can't write either of >> the two options above using monad methods. The best solution is the one >> already proposed - write a function from [Int] -> IO String, and use bind >> (>>=) on that function to handle things. You could also use the do sugaring >> of <- to get a less functional version. >> >> The last option is to use the IO-specific function unsafePerformIO to >> write something like nullIO = null . unsafePerformIO. But it's called >> UNSAFE and tucked away in a module of similar operations for a reason. >> Using bind is much preferred. >> >> >>> On 15/01/15 09:51, Miro Karpis wrote: >>> >>> Hi, >>> >>> please is there a way to have guards with 'where' that communicates >>> with IO? Or is there some other more elegant way? I can do this with >>> classic if/else,...but I just find it nicer with guards. >>> >>> >>> I have something like this (just an example): >>> >>> >>> f :: Int -> IO String >>> f x >>> | null dbOutput = return "no db record" >>> | otherwise = return "we got some db records" >>> where dbOutput = getDBRecord x >>> >>> >>> getDBRecord :: Int -> IO [Int] >>> getDBRecord recordId = do >>> putStrLn $ "checking dbRecord" ++ show recordId >>> --getting data from DB >>> return [1,2] >>> >>> >>> problem is that db dbOutput is IO and the guard check does not like it: >>> >>> Couldn't match expected type ?[a0]? with actual type ?IO [Int]? >>> In the first argument of ?null?, namely ?dbOutput? >>> In the expression: null dbOutput >>> >>> >>> >>> Cheers, >>> Miro >>> >>> >>> _______________________________________________ >>> Beginners mailing listBeginners at haskell.orghttp://www.haskell.org/mailman/listinfo/beginners >>> >>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From marcin.jan.mrotek at gmail.com Thu Jan 15 21:03:35 2015 From: marcin.jan.mrotek at gmail.com (Marcin Mrotek) Date: Thu, 15 Jan 2015 22:03:35 +0100 Subject: [Haskell-beginners] help with IO guards In-Reply-To: References: <54B7DBAE.6010709@ucdavis.edu> Message-ID: Hello, A list ([]) is also a monad, and a String is defined as a list of characters ([Char]). So in your example, it's as if you were trying to use (>>=) operator on two different monads ([] and IO), which is impossible. To make a pure value a monadic value, you need to use return: g = readLn >>= (\a -> return (f a)) which is equivalent to composing f with return: g = readLn >>= return.f Regards, Marcin From julian.birch at gmail.com Thu Jan 15 21:12:11 2015 From: julian.birch at gmail.com (Julian Birch) Date: Thu, 15 Jan 2015 21:12:11 +0000 Subject: [Haskell-beginners] help with IO guards In-Reply-To: References: <54B7DBAE.6010709@ucdavis.edu> Message-ID: Going back to an earlier question: a monad is a bit like a roach motel. You can check in but you can't leave. (This isn't true of every Monad, but the point is there's no guarantees.) In particular, you can't go from IO String to String _at all_. But you can, through Functor, pass it to a function that takes a plain String. And through Monad, you can turn IO (IO String) back to IO String. Hope this helps. On Thursday, January 15, 2015, Marcin Mrotek wrote: > Hello, > > A list ([]) is also a monad, and a String is defined as a list of > characters ([Char]). So in your example, it's as if you were trying to > use (>>=) operator on two different monads ([] and IO), which is > impossible. To make a pure value a monadic value, you need to use > return: > > g = readLn >>= (\a -> return (f a)) > > which is equivalent to composing f with return: > > g = readLn >>= return.f > > Regards, > Marcin > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Sent from an iPhone, please excuse brevity and typos. -------------- next part -------------- An HTML attachment was scrubbed... URL: From miroslav.karpis at gmail.com Thu Jan 15 21:26:05 2015 From: miroslav.karpis at gmail.com (Miro Karpis) Date: Thu, 15 Jan 2015 22:26:05 +0100 Subject: [Haskell-beginners] help with IO guards In-Reply-To: References: <54B7DBAE.6010709@ucdavis.edu> Message-ID: many thanks,....but then I unfortunately don't understand how can I fix my initial problem: to use IO check in guards - is that possible? Regards, Miro On Thu, Jan 15, 2015 at 10:12 PM, Julian Birch wrote: > Going back to an earlier question: a monad is a bit like a roach motel. > You can check in but you can't leave. (This isn't true of every Monad, but > the point is there's no guarantees.) In particular, you can't go from IO > String to String _at all_. But you can, through Functor, pass it to a > function that takes a plain String. And through Monad, you can turn IO > (IO String) back to IO String. > > Hope this helps. > > > On Thursday, January 15, 2015, Marcin Mrotek > wrote: > >> Hello, >> >> A list ([]) is also a monad, and a String is defined as a list of >> characters ([Char]). So in your example, it's as if you were trying to >> use (>>=) operator on two different monads ([] and IO), which is >> impossible. To make a pure value a monadic value, you need to use >> return: >> >> g = readLn >>= (\a -> return (f a)) >> >> which is equivalent to composing f with return: >> >> g = readLn >>= return.f >> >> Regards, >> Marcin >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > > -- > Sent from an iPhone, please excuse brevity and typos. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From julian.birch at gmail.com Thu Jan 15 21:27:19 2015 From: julian.birch at gmail.com (Julian Birch) Date: Thu, 15 Jan 2015 21:27:19 +0000 Subject: [Haskell-beginners] help with IO guards In-Reply-To: References: <54B7DBAE.6010709@ucdavis.edu> Message-ID: Not really, for the same reason: a guard needs a Bool, and you can't get a Bool from IO Bool. On Thursday, January 15, 2015, Miro Karpis wrote: > many thanks,....but then I unfortunately don't understand how can I fix my > initial problem: > > to use IO check in guards - is that possible? > > Regards, > Miro > > On Thu, Jan 15, 2015 at 10:12 PM, Julian Birch > wrote: > >> Going back to an earlier question: a monad is a bit like a roach motel. >> You can check in but you can't leave. (This isn't true of every Monad, but >> the point is there's no guarantees.) In particular, you can't go from IO >> String to String _at all_. But you can, through Functor, pass it to a >> function that takes a plain String. And through Monad, you can turn IO >> (IO String) back to IO String. >> >> Hope this helps. >> >> >> On Thursday, January 15, 2015, Marcin Mrotek > > wrote: >> >>> Hello, >>> >>> A list ([]) is also a monad, and a String is defined as a list of >>> characters ([Char]). So in your example, it's as if you were trying to >>> use (>>=) operator on two different monads ([] and IO), which is >>> impossible. To make a pure value a monadic value, you need to use >>> return: >>> >>> g = readLn >>= (\a -> return (f a)) >>> >>> which is equivalent to composing f with return: >>> >>> g = readLn >>= return.f >>> >>> Regards, >>> Marcin >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >> >> >> -- >> Sent from an iPhone, please excuse brevity and typos. >> > > -- Sent from an iPhone, please excuse brevity and typos. -------------- next part -------------- An HTML attachment was scrubbed... URL: From cma at bitemyapp.com Thu Jan 15 21:28:55 2015 From: cma at bitemyapp.com (Christopher Allen) Date: Thu, 15 Jan 2015 15:28:55 -0600 Subject: [Haskell-beginners] help with IO guards In-Reply-To: References: <54B7DBAE.6010709@ucdavis.edu> Message-ID: I haven't seen anybody explain the real reasons you can't yank `a` out of something of type `IO a`, so I thought I'd attempt to clear a few things up. It's true that you cannot *in general* extract a value of type `a` from something of type `Monad m => m a` but those reasons are *not* why you can't in the case of IO. You can't with Monad in general because the typeclass doesn't provide a method with the type `m a -> a`, in fact, it only provides the opposite: `a -> m a`. The real reason you can't pattern match on or otherwise extract values from IO is that it's an abstract datatype - on purpose. The constructor is not exported, the internals are hidden. You are using a non-strict programming language. The IO datatype is how the implementation knows not to replace the thunk with its value when evaluated. Want to see what I mean? Play with some code that uses https://hackage.haskell.org/package/time-1.3/docs/Data-Time-Clock.html#v:getCurrentTime to get the current time. Note how each time you force evaluation of the `IO UTCTime` you're getting a new UTCTime each time. Then wrap it in `unsafePerformIO` - it'll only get executed once. This is *definitely* not the semantics you want, you're too new to know when you'd want the unsafe* functions for now. If you aren't comfortable with monads, no amount of thrashing is going to let you avoid using Functor/Applicative/Monad if you want to interact with values produced in IO. I wrote a guide for learning Haskell, it covers Functor/Applicative/Monad which are *everywhere* - not just for use with IO. This is the guide: https://github.com/bitemyapp/learnhaskell There are people used to teaching and assisting with the courses recommended in my guide (cis194 & NICTA course) on Freenode IRC at #haskell-beginners. There are tons of people equipped to help you in #haskell as well. --- Chris Allen On Thu, Jan 15, 2015 at 3:12 PM, Julian Birch wrote: > Going back to an earlier question: a monad is a bit like a roach motel. > You can check in but you can't leave. (This isn't true of every Monad, but > the point is there's no guarantees.) In particular, you can't go from IO > String to String _at all_. But you can, through Functor, pass it to a > function that takes a plain String. And through Monad, you can turn IO > (IO String) back to IO String. > > Hope this helps. > > > On Thursday, January 15, 2015, Marcin Mrotek > wrote: > >> Hello, >> >> A list ([]) is also a monad, and a String is defined as a list of >> characters ([Char]). So in your example, it's as if you were trying to >> use (>>=) operator on two different monads ([] and IO), which is >> impossible. To make a pure value a monadic value, you need to use >> return: >> >> g = readLn >>= (\a -> return (f a)) >> >> which is equivalent to composing f with return: >> >> g = readLn >>= return.f >> >> Regards, >> Marcin >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > > -- > Sent from an iPhone, please excuse brevity and typos. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From miroslav.karpis at gmail.com Thu Jan 15 21:34:49 2015 From: miroslav.karpis at gmail.com (Miro Karpis) Date: Thu, 15 Jan 2015 22:34:49 +0100 Subject: [Haskell-beginners] help with IO guards In-Reply-To: References: <54B7DBAE.6010709@ucdavis.edu> Message-ID: many thanks! I will take a look at it. cheers and thanks to all :-) On Thu, Jan 15, 2015 at 10:28 PM, Christopher Allen wrote: > I haven't seen anybody explain the real reasons you can't yank `a` out of > something of type `IO a`, so I thought I'd attempt to clear a few things up. > > It's true that you cannot *in general* extract a value of type `a` from > something of type `Monad m => m a` but those reasons are *not* why you > can't in the case of IO. You can't with Monad in general because the > typeclass doesn't provide a method with the type `m a -> a`, in fact, it > only provides the opposite: `a -> m a`. > > The real reason you can't pattern match on or otherwise extract values > from IO is that it's an abstract datatype - on purpose. The constructor is > not exported, the internals are hidden. > > You are using a non-strict programming language. The IO datatype is how > the implementation knows not to replace the thunk with its value when > evaluated. > > Want to see what I mean? > > Play with some code that uses > https://hackage.haskell.org/package/time-1.3/docs/Data-Time-Clock.html#v:getCurrentTime > to get the current time. Note how each time you force evaluation of the `IO > UTCTime` you're getting a new UTCTime each time. Then wrap it in > `unsafePerformIO` - it'll only get executed once. This is *definitely* not > the semantics you want, you're too new to know when you'd want the unsafe* > functions for now. > > If you aren't comfortable with monads, no amount of thrashing is going to > let you avoid using Functor/Applicative/Monad if you want to interact with > values produced in IO. > > I wrote a guide for learning Haskell, it covers Functor/Applicative/Monad > which are *everywhere* - not just for use with IO. This is the guide: > https://github.com/bitemyapp/learnhaskell > > There are people used to teaching and assisting with the courses > recommended in my guide (cis194 & NICTA course) on Freenode IRC at > #haskell-beginners. There are tons of people equipped to help you in > #haskell as well. > > --- Chris Allen > > > On Thu, Jan 15, 2015 at 3:12 PM, Julian Birch > wrote: > >> Going back to an earlier question: a monad is a bit like a roach motel. >> You can check in but you can't leave. (This isn't true of every Monad, but >> the point is there's no guarantees.) In particular, you can't go from IO >> String to String _at all_. But you can, through Functor, pass it to a >> function that takes a plain String. And through Monad, you can turn IO >> (IO String) back to IO String. >> >> Hope this helps. >> >> >> On Thursday, January 15, 2015, Marcin Mrotek >> wrote: >> >>> Hello, >>> >>> A list ([]) is also a monad, and a String is defined as a list of >>> characters ([Char]). So in your example, it's as if you were trying to >>> use (>>=) operator on two different monads ([] and IO), which is >>> impossible. To make a pure value a monadic value, you need to use >>> return: >>> >>> g = readLn >>= (\a -> return (f a)) >>> >>> which is equivalent to composing f with return: >>> >>> g = readLn >>= return.f >>> >>> Regards, >>> Marcin >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >> >> >> -- >> Sent from an iPhone, please excuse brevity and typos. >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From haskell at elisehuard.be Fri Jan 16 10:52:39 2015 From: haskell at elisehuard.be (Elise Huard) Date: Fri, 16 Jan 2015 11:52:39 +0100 Subject: [Haskell-beginners] garbage collected object In-Reply-To: References: Message-ID: Thanks! I knew about profiling but System.Mem.Weak is interesting, for more specific cases! I tried a toy example, triggering the GC manually, but I may be doing it wrong: import System.Mem.Weak import System.Mem (performGC) import Control.Concurrent (threadDelay) main :: IO () main = do let x = 5 y = "done" z = 3 a <- mkWeak z x (Just (putStrLn "garbage collected")) performGC threadDelay 20000000 print y Any tips? Thanks, Elise On 14 January 2015 at 13:17, Ryan Trinkle wrote: > You can use the 'vacuum' package to explore the heap, and you can use > System.Mem.Weak to create a reference to something that won't keep it from > being GCed and will let you check on it later. You can also use the > built-in heap profiling capabilities to track things like the memory usage > of the entire heap from moment to moment. > > On Wed, Jan 14, 2015 at 5:24 AM, Elise Huard wrote: >> >> Hi, >> >> Maybe a stupid question: is there a way to check whether a particular >> data structure (or set of data structures) has been garbage collected? >> Or indirectly: is there a way to check what's still alive in the heap, >> so that you can potentially diff from one moment to another? >> Thanks, >> >> Elise Huard >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From haskell at elisehuard.be Fri Jan 16 11:23:11 2015 From: haskell at elisehuard.be (Elise Huard) Date: Fri, 16 Jan 2015 12:23:11 +0100 Subject: [Haskell-beginners] garbage collected object In-Reply-To: References: Message-ID: Sorry, just realized I wasn't very clear: I mean I was expecting this bit of code to output "garbage collected" at some point :) On 16 January 2015 at 11:52, Elise Huard wrote: > Thanks! I knew about profiling but System.Mem.Weak is interesting, for > more specific cases! > > I tried a toy example, triggering the GC manually, but I may be doing it wrong: > > import System.Mem.Weak > import System.Mem (performGC) > import Control.Concurrent (threadDelay) > > > main :: IO () > main = do let x = 5 > y = "done" > z = 3 > a <- mkWeak z x (Just (putStrLn "garbage collected")) > performGC > threadDelay 20000000 > print y > > Any tips? > Thanks, > > Elise > > On 14 January 2015 at 13:17, Ryan Trinkle wrote: >> You can use the 'vacuum' package to explore the heap, and you can use >> System.Mem.Weak to create a reference to something that won't keep it from >> being GCed and will let you check on it later. You can also use the >> built-in heap profiling capabilities to track things like the memory usage >> of the entire heap from moment to moment. >> >> On Wed, Jan 14, 2015 at 5:24 AM, Elise Huard wrote: >>> >>> Hi, >>> >>> Maybe a stupid question: is there a way to check whether a particular >>> data structure (or set of data structures) has been garbage collected? >>> Or indirectly: is there a way to check what's still alive in the heap, >>> so that you can potentially diff from one moment to another? >>> Thanks, >>> >>> Elise Huard >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >> >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> From hjgtuyl at chello.nl Sat Jan 17 08:07:22 2015 From: hjgtuyl at chello.nl (Henk-Jan van Tuyl) Date: Sat, 17 Jan 2015 09:07:22 +0100 Subject: [Haskell-beginners] Error while trying to install ByteString Package In-Reply-To: <1496218722.1011416.1421240074267.JavaMail.yahoo@jws10604.mail.bf1.yahoo.com> References: <1496218722.1011416.1421240074267.JavaMail.yahoo@jws10604.mail.bf1.yahoo.com> Message-ID: On Wed, 14 Jan 2015 13:54:34 +0100, Abhinav Kalawatia wrote: > Hi, I get the following error while I tried installing the ByteString > package. Could you please help me resolve this > >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> : > setup-Cabal-1.18.1.3-i386-windows-ghc-7.8.3.exe: Missing dependency on a > foreign library: > * Missing (or bad) header file: fpstring.h : The file fpstring.h is part of the bytestring package, so cabal-install should be able to find it. The package installed fine on my computer. You probably have more chance to get a helpful answer on the Haskell Caf? mailing list. Regards, Henk-Jan van Tuyl -- Folding at home What if you could share your unused computer power to help find a cure? In just 5 minutes you can join the world's biggest networked computer and get us closer sooner. Watch the video. http://folding.stanford.edu/ http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming -- From sgf.dma at gmail.com Mon Jan 19 13:01:44 2015 From: sgf.dma at gmail.com (Dmitriy Matrosov) Date: Mon, 19 Jan 2015 17:01:44 +0400 Subject: [Haskell-beginners] Defining ExtensionClass (Maybe a) instance in xmonad. Message-ID: Hi. I've tried to define (Maybe a) instance for ExtensionClass from XMonad/Core.hs in such way, that extensionType value would use the same data constructor as was used for the type a itself. But the code below typechecks only, if i add (Show a) and (Read a) constraints to (Maybe a) instance definition, what makes such definition useless for types, which do not have these instances and do not want to use PersistentExtension . How can i define (Maybe a) instance without (Show a) and (Read a) constraints? > {-# LANGUAGE ExistentialQuantification #-} > import Data.Typeable > -- This one does not typecheck > --instance ExtensionClass a => ExtensionClass (Maybe a) where > instance (Show a, Read a, ExtensionClass a) => ExtensionClass (Maybe a) where > initialValue = Nothing > extensionType x = let Just i = (Just initialValue) `asTypeOf` x > in case extensionType i of > PersistentExtension _ -> PersistentExtension x > StateExtension _ -> StateExtension x Here is class definition from XMonad/Core.hs: > class Typeable a => ExtensionClass a where > initialValue :: a > extensionType :: a -> StateExtension > extensionType = StateExtension > data StateExtension = > forall a. ExtensionClass a => StateExtension a > | forall a. (Read a, Show a, ExtensionClass a) => PersistentExtension a -- Dmitriy Matrosov -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at webconect.ch Mon Jan 19 14:29:11 2015 From: lists at webconect.ch (Elias Diem) Date: Mon, 19 Jan 2015 15:29:11 +0100 Subject: [Haskell-beginners] Let the compiler do the work Message-ID: <20150119142911.GA5141@webconect.local> Hi there I'm referring to a post from Stefan H?ck and will quote from there: http://article.gmane.org/gmane.comp.lang.haskell.beginners/14198 Now, load this into GHCi or compile with GHC. If it compiles, you're on the right track. Now, you want to implement it using a fold (try both, foldl and foldr): last5 :: [a] -> Maybe a last5 xs = foldr _ _ xs The underscores are 'type holes'. This tells the compiler to give you some information about what is supposed to be placed at the two positions. For the moment, we are only interested in the types of the things that go there. The compiler will tell you, that the hole at the first position is of type a -> Maybe a -> Maybe a How does the compiler tell me this? I didn't find any flags for GHC to turn this on. What do I miss? -- Greetings Elias From toad3k at gmail.com Mon Jan 19 14:41:07 2015 From: toad3k at gmail.com (David McBride) Date: Mon, 19 Jan 2015 09:41:07 -0500 Subject: [Haskell-beginners] Let the compiler do the work In-Reply-To: <20150119142911.GA5141@webconect.local> References: <20150119142911.GA5141@webconect.local> Message-ID: I believe the feature "typed holes" first came about around ghc 7.8. I'm not sure if there ever was a pragma for it because it got everyone so excited it was just made to default on from its inception. On Mon, Jan 19, 2015 at 9:29 AM, Elias Diem wrote: > Hi there > > I'm referring to a post from Stefan H?ck and will quote from > there: > > http://article.gmane.org/gmane.comp.lang.haskell.beginners/14198 > > > Now, load this into GHCi or compile with GHC. If it compiles, you're > on the right track. Now, you want to implement it using a fold > (try both, foldl and foldr): > > last5 :: [a] -> Maybe a > last5 xs = foldr _ _ xs > > The underscores are 'type holes'. This tells the compiler to give you > some information about what is supposed to be placed at the two > positions. For the moment, we are only interested in the types of the > things that go there. The compiler will tell you, that > the hole at the first position is of type > > a -> Maybe a -> Maybe a > > > How does the compiler tell me this? I didn't find any flags > for GHC to turn this on. What do I miss? > > -- > Greetings > Elias > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Mon Jan 19 14:55:40 2015 From: allbery.b at gmail.com (Brandon Allbery) Date: Mon, 19 Jan 2015 09:55:40 -0500 Subject: [Haskell-beginners] Defining ExtensionClass (Maybe a) instance in xmonad. In-Reply-To: References: Message-ID: On Mon, Jan 19, 2015 at 8:01 AM, Dmitriy Matrosov wrote: > I've tried to define (Maybe a) instance for ExtensionClass from > XMonad/Core.hs > in such way, that extensionType value would use the same data constructor > as > was used for the type a itself. But the code below typechecks only, if i > add > (Show a) and (Read a) constraints to (Maybe a) instance definition, what > makes > such definition useless for types, which do not have these instances and do > not want to use PersistentExtension . > ExtensionClass data is stored in the layout and therefore requires those constraints. No, there is no magic to cause non-persistent ExtensionClass data to be stored in some other place different from where the rest of it is stored. I'm also wondering how much trouble you can get into by conflicting with some other ExtensionClass that already uses Maybe. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From lists at webconect.ch Mon Jan 19 15:56:19 2015 From: lists at webconect.ch (Elias Diem) Date: Mon, 19 Jan 2015 16:56:19 +0100 Subject: [Haskell-beginners] Let the compiler do the work In-Reply-To: References: <20150119142911.GA5141@webconect.local> Message-ID: <20150119155618.GA6237@webconect.local> Hi David On 2015-01-19, David McBride wrote: > I believe the feature "typed holes" first came about around ghc 7.8. Well that might explain why I can't find anything in GHC 7.4 ;-) Thanks. -- Greetings Elias From sgf.dma at gmail.com Tue Jan 20 19:51:34 2015 From: sgf.dma at gmail.com (Dmitriy Matrosov) Date: Tue, 20 Jan 2015 22:51:34 +0300 Subject: [Haskell-beginners] Defining ExtensionClass (Maybe a) instance in xmonad. In-Reply-To: References: Message-ID: <54BEB1C6.9060604@gmail.com> On 2015?01?19? 17:55, Brandon Allbery wrote: > ExtensionClass data is stored in the layout and therefore > requires those constraints. No, there is no magic to > cause non-persistent ExtensionClass data to be stored in > some other place different from where the rest of it is > stored. Thanks for answer, but i don't understand why it should be stored differently (i don't know how Typeable and ExtensionClass works, so may be this is the reason): if i define instance (ExtensionClass a) => ExtensionClass (Maybe a) where initialValue = Nothing extensionType = StateExtension then there is no (Show a, Read a) constraint. It will come up only, when i mention PersistentExtension in one of case branches, but, on the other hand, may be i can avoid pattern-matching on StateExtension constructor and somehow use data constructor from (extensionType :: a -> StateExtension) directly? I'm already have (ExtensibleState a) constraint, so does type a has Show and Read instances or not, i think, this is correct (at least theoretically) to define extensionType for (Maybe a) to use the same data constructor. > I'm also wondering how much trouble you can get into by > conflicting with some other ExtensionClass that already > uses Maybe. Well, i've just tried to restart xmobar properly, when i reload xmonad. I've noticed, that xmobar restarts with xmonad only, when it uses StdinReader (in template), otherwise new (another) xmobar instance spawned. I want to define generic way for restarting something, spawned by xmonad. > {-# LANGUAGE MultiParamTypeClasses > , FunctionalDependencies > , FlexibleInstances > , FlexibleContexts > , DeriveDataTypeable #-} > > import XMonad > import qualified XMonad.Util.ExtensibleState as XS > import System.Posix.Process > import System.IO > import System.Posix.IO > import System.Posix.Types I may define a data type containing required start/stop functions and depending on some identifier (ProcessID actually): > data Restartable a = Restartable > { killP :: a -> X () > , runP :: X a > } or i can define interface, which all identifiers should support: > class RestartClass a where > killP' :: a -> X () > runP' :: X a Then i may store (Maybe a) in extensible state and write generic restart functions: > restartP :: (ExtensionClass (Maybe a)) => Restartable a -> X () > restartP r = do > mp <- XS.get > whenJust mp (killP r) > p' <- runP r > XS.put (Just p') > > restartP' :: (ExtensionClass (Maybe a), RestartClass a) => X a > restartP' = do > mp <- XS.get > whenJust mp killP' > p' <- runP' > XS.put (Just p' `asTypeOf` mp) > return p' and, finally, i may define Restartable value and RestartClass instance for xmobar, and define restart function for xmobar: > newtype XmobarPID = XmobarPID ProcessID > deriving (Show, Read, Typeable) > > newtype XmobarHandle = XmobarHandle (Maybe Handle) > deriving (Typeable) > > instance ExtensionClass XmobarHandle where > initialValue = XmobarHandle Nothing > > > instance (Show a, Read a, Typeable a) => ExtensionClass (Maybe a) where > initialValue = Nothing > extensionType = PersistentExtension > > > -- For data type approach.. > xmobarP :: Restartable XmobarPID > xmobarP = Restartable killXmobar runXmobar > where > killXmobar :: XmobarPID -> X () > killXmobar (XmobarPID p) = io $ spawn ("kill " ++ show p) > runXmobar :: X XmobarPID > runXmobar = do > (h, p) <- spawnPipe' ["/usr/bin/xmobar", "/home/sgf/.xmobarrc"] > XS.put (XmobarHandle (Just h)) > return (XmobarPID p) > > restartXmobar :: X () > restartXmobar = restartP xmobarP > > > -- For type-class approach.. > instance RestartClass XmobarPID where > killP' (XmobarPID p) = io $ spawn ("kill " ++ show p) > runP' = do > (h, p) <- spawnPipe' ["/usr/bin/xmobar", "/home/sgf/.xmobarrc"] > XS.put (XmobarHandle (Just h)) > return (XmobarPID p) > > restartXmobar' :: X () > restartXmobar' = do > p <- restartP' > let _ = p `asTypeOf` XmobarPID undefined > return () > > -- Rewritten version from XMonad.Util.Run: do not run shell and return > -- ProcessID . > spawnPipe' :: [String] -> X (Handle, ProcessID) > spawnPipe' (x : xs) = io $ do > (rd, wr) <- createPipe > setFdOption wr CloseOnExec True > h <- fdToHandle wr > hSetBuffering h LineBuffering > p <- xfork $ do > _ <- dupTo rd stdInput > executeFile x False xs Nothing > closeFd rd > return (h, p) but here i can't reuse ExtensionClass (Maybe a) instance for XmobarHandle, because Handle does not have Read instance and can't have extensionType = PersistentExtension . So i've added Maybe in XmobarHandle newtype. Also, i may go the other way: add Maybe to XmobarPID value, and then define ExtensionClass instance for XmobarPID. Then i won't need ExtensionClass (Maybe a) instance. > newtype XmobarPID2 = XmobarPID2 (Maybe ProcessID) > deriving (Typeable, Show, Read) > > instance ExtensionClass XmobarPID2 where > initialValue = XmobarPID2 Nothing > extensionType = PersistentExtension > In this case i need a way to convert value of some type into Maybe: > class Lens a b | a -> b where > view :: a -> b > set :: b -> a -> a > > instance Lens XmobarPID2 (Maybe XmobarPID) where > view (XmobarPID2 x) = fmap XmobarPID x > set (Just (XmobarPID x)) _ = XmobarPID2 (Just x) > set Nothing z = z then restartP and restartP' should be adjusted to use view/set from Lens class > -- Why ghc can't infer type a from b here? I.e. i need to return X a, not > -- X () as before. Is it because of functional dependency a -> b in Lens > -- definition ? > restartP2 :: (ExtensionClass a, Lens a (Maybe b)) => Restartable b -> X a > restartP2 r = do > mp <- XS.get > whenJust (view mp) (killP r) > p' <- runP r > let mp' = set (Just p') mp > XS.put mp' > return mp' > > restartP2' :: (ExtensionClass a, Lens a (Maybe b), RestartClass b) => X a > restartP2' = do > mp <- XS.get > whenJust (view mp) killP' > p' <- runP' > let mp' = set (Just p') mp > XS.put mp' > return mp' but now restartXmobar with Restartable value will not be that simple as before: > restartXmobar2 :: X () > restartXmobar2 = do > p <- restartP2 xmobarP > let _ = p `asTypeOf` XmobarPID2 undefined > return () > > restartXmobar2' :: X () > restartXmobar2' = do > p <- restartP2' > let _ = p `asTypeOf` XmobarPID2 undefined > return () So, i end up with two start/stop interface implementations: - Restartable data type - or RestartClass. and with two extensible state implementations: - store identifier in Maybe - or add Maybe to identifier itself. I don't like `asTypeOf` in restartXmobar variants . And i don't like, that i can't reuse ExtensionClass (Maybe a) instance for XmobarHandle .. What implementation will be more idiomatic? Or, may be, something completely different? From allbery.b at gmail.com Tue Jan 20 20:14:24 2015 From: allbery.b at gmail.com (Brandon Allbery) Date: Tue, 20 Jan 2015 15:14:24 -0500 Subject: [Haskell-beginners] Defining ExtensionClass (Maybe a) instance in xmonad. In-Reply-To: <54BEB1C6.9060604@gmail.com> References: <54BEB1C6.9060604@gmail.com> Message-ID: On Tue, Jan 20, 2015 at 2:51 PM, Dmitriy Matrosov wrote: > then there is no (Show a, Read a) constraint. It will come > up only, when i mention PersistentExtension in one of case > branches, but, on the other hand, may be i can avoid > That would be expected, I believe; if you mentioned it, it must apply to the whole instance, not just one case branch. But I'm not quite clear on what you are saying here. > Well, i've just tried to restart xmobar properly, when i > reload xmonad. I've noticed, that xmobar restarts with > xmonad only, when it uses StdinReader (in template), > otherwise new (another) xmobar instance spawned. > That is expected. You are expecting some kind of magic happening where we track every pipe and forcibly terminate all of them, when all we are doing is using normal POSIX semantics where pipes get closed automatically but the process on the other side will only find out if it is reading from the pipe (i.e. xmobar's StdinReader). Doing the magic that you and others seem to expect would be in one way or another very expensive --- either we make xmonad only work on one particular OS family, or we accept that we can only spawn so many pipes because of child process limits, or we have to make spawnPipe spawn a backchannel to report the ultimate child *and* we must restrict what kinds of things you can run on the other end so we can keep track and kill it. (You probably want to consider the above with respect to your proposed extension; the POSIX subprocess model has many dark corners. In particular, remember that the child process "closest" to xmonad in a spawnPipe is a shell, *not* the program you ran. And that shell has the same problem, so killing it will not kill the xmobar it starts!) -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From thbach at students.uni-mainz.de Tue Jan 20 22:17:49 2015 From: thbach at students.uni-mainz.de (Thomas Bach) Date: Tue, 20 Jan 2015 23:17:49 +0100 Subject: [Haskell-beginners] Defining ExtensionClass (Maybe a) instance in xmonad. In-Reply-To: <54BEB1C6.9060604@gmail.com> (Dmitriy Matrosov's message of "Tue, 20 Jan 2015 22:51:34 +0300") References: <54BEB1C6.9060604@gmail.com> Message-ID: <87zj9dhzaq.fsf@ilxwinb01.fritz.box> Hi, sadly, I don't have an answer to all your technical question. Furthermore, I consider myself to be a Haskell starter so I cannot answer your questions concerning idiomatic code properly. But here is an answer anyway. :) Dmitriy Matrosov writes: > On 2015?01?19? 17:55, Brandon Allbery wrote: > [?] > > I want to define generic way for restarting something, > spawned by xmonad. > > [?] > > I may define a data type containing required start/stop > functions and depending on some identifier (ProcessID > actually): > I had kind of the same problem and went for the this option, i.e. defining a data type. But I implemented this via a pid file which gets saved on xmonad start up. This way these program can even survive a restart of the XServer properly. https://github.com/fuzzy-id/my-xmonad/blob/master/My/PidProg.hs (see Config.hs in the same repo for an example of how I use them.) However, I don't see the point in defining the data type to contain start/stop functions. These will be the same for most of the programs, won't they? > [?] > > or i can define interface, which all identifiers should > support: > This makes sense if you want to prepare to have a whole group of different instances. We cannot know whether or not this is your case. If you want to be prepared for this ? i.e. over-engineer the whole thing ? I'd probably go this path. Define a class which asks its instances to have a start/stop function and so on. Then you can define an instance which xmobar and probably a whole lot of other programs will nicely fit. Hope this helps Thomas. From jeffbrown.the at gmail.com Thu Jan 22 00:23:09 2015 From: jeffbrown.the at gmail.com (Jeffrey Brown) Date: Wed, 21 Jan 2015 16:23:09 -0800 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes Message-ID: Dear Haskellers, The following compiles. (Rev stands for Reversible, and Dirn for Direction.) class Rev a where rev :: a -> a data Dirn = Succ | Pred deriving (Eq, Show, Ord) -- implement Ord (<=) Succ Pred = False (<=) _ _ = True -- implement Rev instance Rev Dirn where rev Succ = Pred rev Pred = Succ But if I try to define the Rev instance the same way the Ord instance is being defined, it does not compile: class Rev a where rev :: a -> a data Dirn = Succ | Pred deriving (Eq, Show, Ord, Rev) -- implement Ord, because Dirn is used as a key in a Map (<=) Succ Pred = False (<=) _ _ = True -- implement Rev rev Succ = Pred rev Pred = Succ What's going on? Many thanks, Jeff -------------- next part -------------- An HTML attachment was scrubbed... URL: From ryan.trinkle at gmail.com Thu Jan 22 00:31:51 2015 From: ryan.trinkle at gmail.com (Ryan Trinkle) Date: Wed, 21 Jan 2015 19:31:51 -0500 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: Message-ID: In both cases, it's not actually creating a typeclass instance. However, because (<=) from Ord is declared in GHC.Classes, you're able to create a new (but completely unrelated) function named (<=). The fully qualified names for these would be GHC.Classes.<= and YourModule.<=, so they don't clash (but if you tried to use <= without qualifying it, you'd get an ambiguous reference error). In the case of Rev, you get an error, though, because both the class method and the standalone function are declared in YourModule, which is illegal (multiple declarations of the same name). So, long story short, go with the "instance" syntax. On Wed, Jan 21, 2015 at 7:23 PM, Jeffrey Brown wrote: > Dear Haskellers, > > The following compiles. (Rev stands for Reversible, and Dirn for > Direction.) > > class Rev a where > rev :: a -> a > > data Dirn = Succ | Pred > deriving (Eq, Show, Ord) > > -- implement Ord > (<=) Succ Pred = False > (<=) _ _ = True > > -- implement Rev > instance Rev Dirn where > rev Succ = Pred > rev Pred = Succ > > But if I try to define the Rev instance the same way the Ord instance is > being defined, it does not compile: > > class Rev a where > rev :: a -> a > > data Dirn = Succ | Pred > deriving (Eq, Show, Ord, Rev) > > -- implement Ord, because Dirn is used as a key in a Map > (<=) Succ Pred = False > (<=) _ _ = True > > -- implement Rev > rev Succ = Pred > rev Pred = Succ > > What's going on? > > Many thanks, > Jeff > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Thu Jan 22 00:32:38 2015 From: allbery.b at gmail.com (Brandon Allbery) Date: Wed, 21 Jan 2015 19:32:38 -0500 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: Message-ID: On Wed, Jan 21, 2015 at 7:23 PM, Jeffrey Brown wrote: > The following compiles. (Rev stands for Reversible, and Dirn for > Direction.) > > class Rev a where > rev :: a -> a > > data Dirn = Succ | Pred > deriving (Eq, Show, Ord) > > -- implement Ord > (<=) Succ Pred = False > (<=) _ _ = True > You derived Ord; why are you doing this? You are locally overriding the Ord instance you derived with a (<=) that is *not* the one that is part of Ord.. and will therefore not contribute to the other functions from Ord. If you want to implement an instance manually, use the instance keyword. instance Ord Dim where (<=) Succ Pred = False (<=) _ _ = True The same applies to Rev later, but in that case it's redefining something defined in the same module and errors out. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael at orlitzky.com Thu Jan 22 00:48:56 2015 From: michael at orlitzky.com (Michael Orlitzky) Date: Wed, 21 Jan 2015 19:48:56 -0500 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: Message-ID: <54C048F8.9020302@orlitzky.com> On 01/21/2015 07:32 PM, Brandon Allbery wrote: > > instance Ord Dim where Insufficient keming on that font? =) From allbery.b at gmail.com Thu Jan 22 00:50:43 2015 From: allbery.b at gmail.com (Brandon Allbery) Date: Wed, 21 Jan 2015 19:50:43 -0500 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: <54C048F8.9020302@orlitzky.com> References: <54C048F8.9020302@orlitzky.com> Message-ID: On Wed, Jan 21, 2015 at 7:48 PM, Michael Orlitzky wrote: > Insufficient keming on that font? =) Probably... also half blind due to sinuses and the drugs trying to keep them at bay :/ -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From michael at orlitzky.com Thu Jan 22 00:51:39 2015 From: michael at orlitzky.com (Michael Orlitzky) Date: Wed, 21 Jan 2015 19:51:39 -0500 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: Message-ID: <54C0499B.5010604@orlitzky.com> On 01/21/2015 07:23 PM, Jeffrey Brown wrote: > > data Dirn = Succ | Pred > deriving (Eq, Show, Ord) > When you "derive" a typeclass instance, you get it magically for free. So you don't need to do this: > -- implement Ord > (<=) Succ Pred = False > (<=) _ _ = True > > But if I try to define the Rev instance the same way the Ord instance is > being defined, it does not compile: > > ... > deriving (Eq, Show, Ord, Rev) But you can't derive classes that you've defined yourself, it only works for predefined ones. (That's how the compiler can do it magically.) So "deriving (Rev)" won't work, because you made the "Rev" typeclass yourself. In the second case, you have to do: instance Rev Dirn where rev Succ = Pred rev Pred = Succ because you can't derive Rev, because it isn't a predefined typeclass. One more thing: when "deriving (Ord)", the compiler uses the order that you've typed the constructors in. So actually, the derived instance would have Succ <= Pred, probably not what you want. I'd switch it to "Pred | Succ" if I were you. The details of why all this failed in a confusing way have been explained in the other responses. From michael at orlitzky.com Thu Jan 22 00:52:36 2015 From: michael at orlitzky.com (Michael Orlitzky) Date: Wed, 21 Jan 2015 19:52:36 -0500 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: <54C048F8.9020302@orlitzky.com> Message-ID: <54C049D4.7090507@orlitzky.com> On 01/21/2015 07:50 PM, Brandon Allbery wrote: > On Wed, Jan 21, 2015 at 7:48 PM, Michael Orlitzky > wrote: > > Insufficient keming on that font? =) > > > Probably... also half blind due to sinuses and the drugs trying to keep > them at bay :/ > I only noticed because I copy/pasted from your response and knew something was wrong but couldn't figure it out for a good 5 minutes. From ky3 at atamo.com Thu Jan 22 16:57:59 2015 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Thu, 22 Jan 2015 23:57:59 +0700 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: Message-ID: Jeffrey, You didn't explain what you're trying to accomplish, and therefore folks can only address the symptoms. Here's what I'm seeing: Suppose you have: data A = A | B | C | D | E You'd like a function that given A returns E, given B, returns D given C, returns er, C given D, returns B, given E, returns A. So you're hoping to write a Rev type class with singleton member rev :: Rev a => a -> a that would do the trick. Thing is, you can already write a very generic rev :: (Enum a, Bounded a) => a -> a that would do the job. Why is that sweet? Because Enum + Bounded are auto-derivable for huge swathes of data types. So you write the rev function (not type class instance!) once, and it'll work for lots of your structures. The best type class is often no type class. Especially when the compiler isn't smart enough to read your mind. But I'm sure it's flattered you thought so highly of it ;) -- Kim-Ee On Thu, Jan 22, 2015 at 7:23 AM, Jeffrey Brown wrote: > Dear Haskellers, > > The following compiles. (Rev stands for Reversible, and Dirn for > Direction.) > > class Rev a where > rev :: a -> a > > data Dirn = Succ | Pred > deriving (Eq, Show, Ord) > > -- implement Ord > (<=) Succ Pred = False > (<=) _ _ = True > > -- implement Rev > instance Rev Dirn where > rev Succ = Pred > rev Pred = Succ > > But if I try to define the Rev instance the same way the Ord instance is > being defined, it does not compile: > > class Rev a where > rev :: a -> a > > data Dirn = Succ | Pred > deriving (Eq, Show, Ord, Rev) > > -- implement Ord, because Dirn is used as a key in a Map > (<=) Succ Pred = False > (<=) _ _ = True > > -- implement Rev > rev Succ = Pred > rev Pred = Succ > > What's going on? > > Many thanks, > Jeff > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeffbrown.the at gmail.com Fri Jan 23 03:34:15 2015 From: jeffbrown.the at gmail.com (Jeffrey Brown) Date: Thu, 22 Jan 2015 19:34:15 -0800 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: Message-ID: Thank you, everybody. I had thought "deriving" and "instance" were just different words for the same thing. Your answers were deeper than I am currently capable of understanding. I found instance declarations specified in 7.6.3 of The Glorious Glasgow Haskell Compilation System User's Guide, Version 7.0.2: https://downloads.haskell.org/~ghc/7.0.2/docs/html/users_guide/type-class-extensions.html I have not found the "deriving" keyword's specification anywhere. On Thu, Jan 22, 2015 at 8:57 AM, Kim-Ee Yeoh wrote: > Jeffrey, > > You didn't explain what you're trying to accomplish, and therefore folks > can only address the symptoms. > > Here's what I'm seeing: > > Suppose you have: > > data A = A | B | C | D | E > > You'd like a function that > given A returns E, > given B, returns D > given C, returns er, C > given D, returns B, > given E, returns A. > > So you're hoping to write a Rev type class with singleton member > rev :: Rev a => a -> a > that would do the trick. > > Thing is, you can already write a very generic > > rev :: (Enum a, Bounded a) => a -> a > > that would do the job. > > Why is that sweet? > > Because Enum + Bounded are auto-derivable for huge swathes of data types. > So you write the rev function (not type class instance!) once, and it'll > work for lots of your structures. > > The best type class is often no type class. Especially when the compiler > isn't smart enough to read your mind. But I'm sure it's flattered you > thought so highly of it ;) > > > -- Kim-Ee > > On Thu, Jan 22, 2015 at 7:23 AM, Jeffrey Brown > wrote: > >> Dear Haskellers, >> >> The following compiles. (Rev stands for Reversible, and Dirn for >> Direction.) >> >> class Rev a where >> rev :: a -> a >> >> data Dirn = Succ | Pred >> deriving (Eq, Show, Ord) >> >> -- implement Ord >> (<=) Succ Pred = False >> (<=) _ _ = True >> >> -- implement Rev >> instance Rev Dirn where >> rev Succ = Pred >> rev Pred = Succ >> >> But if I try to define the Rev instance the same way the Ord instance is >> being defined, it does not compile: >> >> class Rev a where >> rev :: a -> a >> >> data Dirn = Succ | Pred >> deriving (Eq, Show, Ord, Rev) >> >> -- implement Ord, because Dirn is used as a key in a Map >> (<=) Succ Pred = False >> (<=) _ _ = True >> >> -- implement Rev >> rev Succ = Pred >> rev Pred = Succ >> >> What's going on? >> >> Many thanks, >> Jeff >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Fri Jan 23 03:40:46 2015 From: allbery.b at gmail.com (Brandon Allbery) Date: Thu, 22 Jan 2015 22:40:46 -0500 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: Message-ID: On Thu, Jan 22, 2015 at 10:34 PM, Jeffrey Brown wrote: > I have not found the "deriving" keyword's specification anywhere. The correct place to look for this and other things, including instances, is the Haskell Report. https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-780004.3.3 4.3.3. Derived Instances https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-770004.3.2 4.3.2. Instance Declarations -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeffbrown.the at gmail.com Fri Jan 23 04:52:31 2015 From: jeffbrown.the at gmail.com (Jeffrey Brown) Date: Thu, 22 Jan 2015 20:52:31 -0800 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: Message-ID: Thanks (again) Brandon! On Thu, Jan 22, 2015 at 7:40 PM, Brandon Allbery wrote: > On Thu, Jan 22, 2015 at 10:34 PM, Jeffrey Brown > wrote: > >> I have not found the "deriving" keyword's specification anywhere. > > > The correct place to look for this and other things, including instances, > is the Haskell Report. > > > https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-780004.3.3 > 4.3.3. Derived Instances > > > https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-770004.3.2 > 4.3.2. Instance Declarations > > -- > brandon s allbery kf8nh sine nomine > associates > allbery.b at gmail.com > ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hsyl20 at gmail.com Fri Jan 23 18:50:40 2015 From: hsyl20 at gmail.com (Sylvain Henry) Date: Fri, 23 Jan 2015 19:50:40 +0100 Subject: [Haskell-beginners] help with IO guards In-Reply-To: References: Message-ID: Hi, Using LambdaCase extension, you can write something quite elegant: {-# LANGUAGE LambdaCase #-} f :: Int -> IO String f x = getDBRecord x >>= \case dbOutput | null dbOutput -> return "no db record" | otherwise -> return "we got some db records" Or better: f :: Int -> IO String f x = getDBRecord x >>= \case [] -> return "no db record" _ -> return "we got some db records" But you can also use another function for the pure part: recordMsg :: [a] -> String recordMsg [] = "no db record" recordMsg _ = "we got some db records" f :: Int -> IO String f = fmap recordMsg . getDBRecord Regards, Sylvain 2015-01-15 12:51 GMT+01:00 Miro Karpis : > Hi, > > please is there a way to have guards with 'where' that communicates with > IO? Or is there some other more elegant way? I can do this with classic > if/else,...but I just find it nicer with guards. > > > I have something like this (just an example): > > > f :: Int -> IO String > f x > | null dbOutput = return "no db record" > | otherwise = return "we got some db records" > where dbOutput = getDBRecord x > > > getDBRecord :: Int -> IO [Int] > getDBRecord recordId = do > putStrLn $ "checking dbRecord" ++ show recordId > --getting data from DB > return [1,2] > > > problem is that db dbOutput is IO and the guard check does not like it: > > Couldn't match expected type ?[a0]? with actual type ?IO [Int]? > In the first argument of ?null?, namely ?dbOutput? > In the expression: null dbOutput > > > > Cheers, > Miro > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From jeffbrown.the at gmail.com Fri Jan 23 19:39:25 2015 From: jeffbrown.the at gmail.com (Jeffrey Brown) Date: Fri, 23 Jan 2015 11:39:25 -0800 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: Message-ID: Should I always look first in the 98 Report, and only if I don't find something there then look in the User's Guide? Should I always look through both, lest something in the 98 Report be obsolete? Is nothing in it obsolete? On Thu, Jan 22, 2015 at 7:40 PM, Brandon Allbery wrote: > On Thu, Jan 22, 2015 at 10:34 PM, Jeffrey Brown > wrote: > >> I have not found the "deriving" keyword's specification anywhere. > > > The correct place to look for this and other things, including instances, > is the Haskell Report. > > > https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-780004.3.3 > 4.3.3. Derived Instances > > > https://www.haskell.org/onlinereport/haskell2010/haskellch4.html#x10-770004.3.2 > 4.3.2. Instance Declarations > > -- > brandon s allbery kf8nh sine nomine > associates > allbery.b at gmail.com > ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Fri Jan 23 20:01:53 2015 From: allbery.b at gmail.com (Brandon Allbery) Date: Fri, 23 Jan 2015 15:01:53 -0500 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: Message-ID: On Fri, Jan 23, 2015 at 2:39 PM, Jeffrey Brown wrote: > Should I always look first in the 98 Report, and only if I don't find > something there then look in the User's Guide? Should I always look through > both, lest something in the 98 Report be obsolete? Is nothing in it > obsolete? You really want the 2010 report, not the 98 one. https://www.haskell.org/onlinereport/haskell2010/ You may first check the extensions part of the GHC manual: https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/ghc-language-features.html Usually extensions will be called out with a pragma, though, so if there are not {-# LANGUAGE #-} sections at the top then you probably don't need to check the GHC manual. If there are, look over the sections of the GHC manual relating to those LANGUAGE pragmas. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From animeshsaxena at icloud.com Sat Jan 24 01:21:20 2015 From: animeshsaxena at icloud.com (Animesh Saxena) Date: Sat, 24 Jan 2015 01:21:20 +0000 (GMT) Subject: [Haskell-beginners] Show and showList Message-ID: I am trying to understand the concept of overlapping instances. In the book "Real World Haskell" there is an example of showList which avoids overlapping instances.? As per my understanding showList is taking the same problem that method "show" would have for overlapping instances. Here's the snippet which explains how it works... "The Show class defines both a show method, which renders one value, and a showList method, which renders a list of values. The default implementation of showList renders a list using square brackets and commas. The instance of Show for [a] is implemented using showList. The instance of Show for Char provides a special implementation of showList that uses double quotes and escapes non-ASCII-printable characters. As a result, if someone applies show to a [Char] value, the implementation of showList will be chosen, and it will correctly render the string using quotes. At least sometimes, then, we can avoid the need for the OverlappingInstances extension with a little bit of lateral thinking."? GHC Code class Show a where ? showsPrec :: Int -> a -> ShowS ? show :: a -> String ? showList :: [a] -> ShowS *Main> show [1,2,3] "[1,2,3]" *Main> show ['a','b','c'] "\"abc\"" *Main> It's the same problem now transfered to showList which will be declared differently for a list of int's or a list of chars, but still it outputs them differently as can be seen from the above code.?How it is able to differentiate without throwing up the overlap error? -A -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Sat Jan 24 01:28:32 2015 From: allbery.b at gmail.com (Brandon Allbery) Date: Fri, 23 Jan 2015 20:28:32 -0500 Subject: [Haskell-beginners] Show and showList In-Reply-To: References: Message-ID: On Fri, Jan 23, 2015 at 8:21 PM, Animesh Saxena wrote: > It's the same problem now transfered to showList which will be declared > differently for a list of int's or a list of chars, but still it outputs > them differently as can be seen from the above code. How it is able to > differentiate without throwing up the overlap error? The key is that showList is defined on e.g. Int or Char, not [Int] or [Char]. If we tried to do the "obvious" thing for a list, we'd define instance Show a => Show [a] where ... but then we have an overlap any time we try to define a different handler for a specific list of things (in this case, instance Show [Char] would be the overlapping instance we'd want, to produce the String representation). We avoid this by building the representation for lists into the representation for single items (thus, a showList method); we can provide a default implementation of that method in the Show class to do the usual brackets-and-commas representation, using the mechanism intended to allow optimized versions of typeclass methods for particular types, and then provide a specific implementation for instance Show Char without invoking overlap. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From karl at karlv.net Sat Jan 24 04:15:32 2015 From: karl at karlv.net (Karl Voelker) Date: Fri, 23 Jan 2015 20:15:32 -0800 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: Message-ID: <1422072932.1088322.218161969.1C09F9AC@webmail.messagingengine.com> On Fri, Jan 23, 2015, at 12:01 PM, Brandon Allbery wrote: > Usually extensions will be called out with a pragma Unless you are looking at the source of a cabal package, in which case they might be centralized in the .cabal file. -Karl -------------- next part -------------- An HTML attachment was scrubbed... URL: From ky3 at atamo.com Sat Jan 24 04:37:29 2015 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Sat, 24 Jan 2015 11:37:29 +0700 Subject: [Haskell-beginners] Defining an instance: Syntax that works exactly sometimes In-Reply-To: References: Message-ID: > Is anything in the 98 report obsolete? The base language is remarkably stable. Haskell 2010 removed (n+k) pattern matching. And that's the only thing obsoleted over 12 years ! So it's all add, no removal. A better question to ask is, what's the 20% of the report that's used 80% of the time? > Should I always look first in the 98 Report, and only if I don't find something there then look in the User's Guide? Sounds like a good plan. Over time, you'll understand what's haskell and what are ghc extensions. Humble advice: it's not enough to read the language def. You'll understand better by reading the conference proceedings that made the language what it is today. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From robert.dodier at gmail.com Sat Jan 24 05:02:25 2015 From: robert.dodier at gmail.com (Robert Dodier) Date: Sat, 24 Jan 2015 05:02:25 +0000 (UTC) Subject: [Haskell-beginners] ghc failed to compile trifecta while installing idris Message-ID: Hi, I'm trying to install Idris. I am working on Ubuntu 14.04. ghc --version reports 7.6.3. After installing Haskell via apt-get, I then executed cabal update, which succeeded. I then tried cabal install idris and it eventually failed while trying to install trifecta. I then tried cabal install -v3 idris and got the following info about the trifecta problem, which I've appended to this message as a PS. Maybe I can patch Highlight.hs? How would I go about that? Unpack the tar.gz, patch the file, and repack it? For the record, I tried to follow the instructions at: https://github.com/idris-lang/Idris-dev/wiki/Idris-on-Ubuntu but ran into various errors, e.g., cabal install cabal-install failed with ExitFailure 139. Thanks for any advice. best, Robert Dodier PS. The tail end of the output of cabal install -v3 idris: [ 8 of 13] Compiling Text.Trifecta.Highlight ( src/Text/Trifecta/Highlight.hs, dist/build/Text/Trifecta/Highlight.o ) *** Parser: *** Renamer/typechecker: src/Text/Trifecta/Highlight.hs:46:15: Ambiguous occurrence `Comment' It could refer to either `Text.Blaze.Internal.Comment', imported from `Text.Blaze.Internal' at src/Text/Trifecta/Highlight.hs:35:1-26 or `Text.Parser.Token.Highlight.Comment', imported from `Text.Parser.Token.Highlight' at src/Text/Trifecta/Highlight.hs:36:1-34 Upsweep partially successful. *** Deleting temp files: Deleting: /tmp/ghc13030_0/ghc13030_0.s Warning: deleting non-existent /tmp/ghc13030_0/ghc13030_0.s link(batch): upsweep (partially) failed OR Main.main not exported; not linking. *** Deleting temp files: Deleting: /tmp/ghc13030_0/ghc13030_0.hscpp *** Deleting temp dirs: Deleting: /tmp/ghc13030_0 /usr/bin/ghc returned ExitFailure 1 Failed to install trifecta-1.5 World file is already up to date. cabal: Error: some packages failed to install: idris-0.9.16 depends on trifecta-1.5 which failed to install. trifecta-1.5 failed during the building phase. The exception was: ExitFailure 1 From sumit.sahrawat.apm13 at iitbhu.ac.in Sat Jan 24 08:26:30 2015 From: sumit.sahrawat.apm13 at iitbhu.ac.in (Sumit Sahrawat, Maths & Computing, IIT (BHU)) Date: Sat, 24 Jan 2015 13:56:30 +0530 Subject: [Haskell-beginners] ghc failed to compile trifecta while installing idris In-Reply-To: References: Message-ID: Take a look here: https://github.com/ekmett/trifecta/issues/41 On 24 January 2015 at 10:32, Robert Dodier wrote: > Hi, > > I'm trying to install Idris. > > I am working on Ubuntu 14.04. ghc --version reports 7.6.3. > > After installing Haskell via apt-get, I then executed cabal update, > which succeeded. > > I then tried cabal install idris and it eventually failed while > trying to install trifecta. > > I then tried cabal install -v3 idris and got the following info > about the trifecta problem, which I've appended to this message > as a PS. > > Maybe I can patch Highlight.hs? How would I go about that? > Unpack the tar.gz, patch the file, and repack it? > > For the record, I tried to follow the instructions at: > https://github.com/idris-lang/Idris-dev/wiki/Idris-on-Ubuntu > but ran into various errors, e.g., cabal install cabal-install > failed with ExitFailure 139. > > Thanks for any advice. > > best, > > Robert Dodier > > PS. The tail end of the output of cabal install -v3 idris: > > [ 8 of 13] Compiling Text.Trifecta.Highlight ( > src/Text/Trifecta/Highlight.hs, dist/build/Text/Trifecta/Highlight.o ) > *** Parser: > *** Renamer/typechecker: > > src/Text/Trifecta/Highlight.hs:46:15: > Ambiguous occurrence `Comment' > It could refer to either `Text.Blaze.Internal.Comment', > imported from `Text.Blaze.Internal' at > src/Text/Trifecta/Highlight.hs:35:1-26 > or `Text.Parser.Token.Highlight.Comment', > imported from `Text.Parser.Token.Highlight' at > src/Text/Trifecta/Highlight.hs:36:1-34 > Upsweep partially successful. > *** Deleting temp files: > Deleting: /tmp/ghc13030_0/ghc13030_0.s > Warning: deleting non-existent /tmp/ghc13030_0/ghc13030_0.s > link(batch): upsweep (partially) failed OR > Main.main not exported; not linking. > *** Deleting temp files: > Deleting: /tmp/ghc13030_0/ghc13030_0.hscpp > *** Deleting temp dirs: > Deleting: /tmp/ghc13030_0 > /usr/bin/ghc returned ExitFailure 1 > Failed to install trifecta-1.5 > World file is already up to date. > cabal: Error: some packages failed to install: > idris-0.9.16 depends on trifecta-1.5 which failed to install. > trifecta-1.5 failed during the building phase. The exception was: > ExitFailure 1 > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Regards Sumit Sahrawat -------------- next part -------------- An HTML attachment was scrubbed... URL: From alexispraga at mailoo.org Sat Jan 24 17:29:13 2015 From: alexispraga at mailoo.org (Alexis Praga) Date: Sat, 24 Jan 2015 18:29:13 +0100 Subject: [Haskell-beginners] Help in reading XML into a data type with HXT Message-ID: <20150124172913.GA24462@localhost.localdomain> Hi, I am trying to read a very simple xml node containing a list of other xml nodes into a data structure. Running the code yields : fatal error: document unpickling failed xpCheckEmptyContents: unprocessed XML content detected context: element "animelist" contents: Bleach Naruto Can you help me debug this ? The code and xml are below : 8<---Code--------------------------------------------------------------------- module Main where import System.Environment import Text.XML.HXT.Core type Anime = String type AnimeList = [Anime] xpAnime :: PU Anime xpAnime = xpElem "title" $ xpText xpAnimeList :: PU AnimeList xpAnimeList = xpElem "animelist" $ xpList $ xpAnime main :: IO () main = do [src] <- getArgs a <- runX ( xunpickleDocument xpAnimeList [] src) mapM_ putStrLn a return () 8<---------------------------------------------------------------------------- 8<---XML--------------------------------------------------------------------- Bleach Naruto 8<---------------------------------------------------------------------------- Thanks, -- Alexis Praga, PhD Student (CERFACS) GPG key : AD4A AF6D BB5C 042F 9422 1223 06E1 C1BF E287 65D0 -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 473 bytes Desc: Digital signature URL: From michael at orlitzky.com Sat Jan 24 18:19:02 2015 From: michael at orlitzky.com (Michael Orlitzky) Date: Sat, 24 Jan 2015 13:19:02 -0500 Subject: [Haskell-beginners] Help in reading XML into a data type with HXT In-Reply-To: <20150124172913.GA24462@localhost.localdomain> References: <20150124172913.GA24462@localhost.localdomain> Message-ID: <54C3E216.70703@orlitzky.com> On 01/24/2015 12:29 PM, Alexis Praga wrote: > > Bleach > Naruto > HXT is picky about whitespace, you have to tell it to ignore those leading spaces. Try, runX ( xunpickleDocument xpAnimeList [ withRemoveWS yes ] src) instead. From alexispraga at mailoo.org Sat Jan 24 18:56:43 2015 From: alexispraga at mailoo.org (Alexis Praga) Date: Sat, 24 Jan 2015 19:56:43 +0100 Subject: [Haskell-beginners] Help in reading XML into a data type with HXT In-Reply-To: <54C3E216.70703@orlitzky.com> References: <20150124172913.GA24462@localhost.localdomain> <54C3E216.70703@orlitzky.com> Message-ID: <20150124185642.GC24462@localhost.localdomain> On Sat, Jan 24, 2015 at 01:19:02PM -0500, Michael Orlitzky wrote: > HXT is picky about whitespace, you have to tell it to ignore those > leading spaces. > > Try, > > runX ( xunpickleDocument xpAnimeList [ withRemoveWS yes ] src) > > instead. Thanks, I was going in circles there ! -- Alexis Praga, PhD Student (CERFACS) GPG key : AD4A AF6D BB5C 042F 9422 1223 06E1 C1BF E287 65D0 -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 473 bytes Desc: Digital signature URL: From animeshsaxena at icloud.com Sun Jan 25 04:01:03 2015 From: animeshsaxena at icloud.com (Animesh Saxena) Date: Sun, 25 Jan 2015 04:01:03 +0000 (GMT) Subject: [Haskell-beginners] Show and showList In-Reply-To: Message-ID: <8de19ae4-7004-4e38-8d7e-dac1b8527d38@me.com> Thanks Brandon....this is a very nice subtle distinction! -Animesh On Jan 23, 2015, at 05:28 PM, Brandon Allbery wrote: On Fri, Jan 23, 2015 at 8:21 PM, Animesh Saxena wrote: It's the same problem now transfered to showList which will be declared differently for a list of int's or a list of chars, but still it outputs them differently as can be seen from the above code.? How it is able to differentiate without throwing up the overlap error? The key is that showList is defined on e.g. Int or Char, not [Int] or [Char]. If we tried to do the "obvious" thing for a list, we'd define ? ? instance Show a => Show [a] where ... but then we have an overlap any time we try to define a different handler for a specific list of things (in this case, instance Show [Char] would be the overlapping instance we'd want, to produce the String representation). We avoid this by building the representation for lists into the representation for single items (thus, a showList method); we can provide a default implementation of that method in the Show class to do the usual brackets-and-commas representation, using the mechanism intended to allow optimized versions of typeclass methods for particular types, and then provide a specific implementation for instance Show Char without invoking overlap. -- brandon s allbery kf8nh ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? sine nomine associates allbery.b at gmail.com ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad ? ? ? ?http://sinenomine.net _______________________________________________ Beginners mailing list Beginners at haskell.org http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From robert.dodier at gmail.com Sun Jan 25 05:33:17 2015 From: robert.dodier at gmail.com (Robert Dodier) Date: Sun, 25 Jan 2015 05:33:17 +0000 (UTC) Subject: [Haskell-beginners] ghc failed to compile trifecta while installing idris References: Message-ID: Sumit Sahrawat, Maths & Computing, IIT (BHU iitbhu.ac.in> writes: > Take a look here:?https://github.com/ekmett/trifecta/issues/41 Thanks for the hint. The problem is fixed in Git. I guess an updated trifecta package will be put in Hackage. In the meantime, cabal install idris --constraint='blaze-markup <0.6.3' is a workaround (thanks to David Christiansen for that). best, Robert Dodier From sgf.dma at gmail.com Sun Jan 25 18:09:38 2015 From: sgf.dma at gmail.com (Dmitriy Matrosov) Date: Sun, 25 Jan 2015 21:09:38 +0300 Subject: [Haskell-beginners] Defining ExtensionClass (Maybe a) instance in xmonad. In-Reply-To: <87zj9dhzaq.fsf@ilxwinb01.fritz.box> References: <54BEB1C6.9060604@gmail.com> <87zj9dhzaq.fsf@ilxwinb01.fritz.box> Message-ID: <54C53162.9060701@gmail.com> Hi. Sorry for big delay, rewriting my previous solution takes a lot of time. On 2015?01?20? 23:14, Brandon Allbery wrote: > > then there is no (Show a, Read a) constraint. It will come > > up only, when i mention PersistentExtension in one of case > > branches, but, on the other hand, may be i can avoid > > > > That would be expected, I believe; if you mentioned it, it must apply to > the whole instance, not just one case branch. But I'm not quite clear on > what you are saying here. I mean, that i already have contraint (ExtensionClass a) in e.g. instance (ExtensionClass a) => ExtensionClass (Maybe a) where initialValue = Nothing then if type a had Show and Read instances, then (Maybe a) will had them too and i may define 'extensionType = PersistentExtension' in (Maybe a) instance. If type a had no Show and Read instances, then i may only define (Maybe a) to be a StateExtension . Thus, theoretically, (ExtensionClass a) contraint is enough, and i want to express above condition on (Maybe a) extensionType value with only that contraint. I can't express it by pattern-matching on type a's StateExtension data contructor, because that will bring up (Show a, Read a) constraint on (Maybe a) instance. I.e. i, probably, can't do it with condition at data level. Then, may be, i can make a condition at type level somehow? > In particular, remember that the child process "closest" to xmonad in a > spawnPipe is a shell, *not* the program you ran. And that shell has the > same problem, so killing it will not kill the xmobar it starts!) Yes, i've noticed that, and that's why i modify spawnPipe from XMonad.Util.Run to run specified process directly, not through the shell. But, perhaps, i also may add 'exec ' before command executed by shell too. On 2015?01?21? 01:17, Thomas Bach wrote: > I had kind of the same problem and went for the this option, i.e. defining > a data type. But I implemented this via a pid file which gets saved on > xmonad start up. This way these program can even survive a restart of the > XServer properly. Thanks for answer, i've tried to integrate your solution with my (below). But i don't understand, why may i need to know pid of processes after restarting X server? They all will die anyway, won't they? > However, I don't see the point in defining the data type to contain > start/stop functions. These will be the same for most of the programs, Not exactly. Let's consider three of them: xmobar, trayer and feh . For xmobar i need to create a pipe and save Handle in extensible state, so i can access it later from dynamicLogWithPP . For trayer i just need to start a program with arguments. And for feh i need to check existence of '~/.fehbg' file and evaluate its content through shell. If file does not exist, i need to use some fallback, like `xsetroot -grey`. Though, feh finishes right after setting background and is not very good exameple, still start/stop functions may not be the same: they may open pipes, check existence of different files, etc. - all that you usually do in shell scripts :) Your PidProg-s implement only "trayer" case. xmobar in your Config.hs started using `xmobar` function from XMonad.Hooks.DynamicLog , and, i guess, it restarts correctly with xmonad only, because you have StdinReader in template in your xmobarrc (in other words, your Config.hs has the same problem with xmobar, as i try to solve here, you just don't see it). However, it turns out, that your PidProg and my per-program newtype-s has one more difference: you have names for ProcessID-s (e.g. command record may be thought as such), but i have not. So, let's start from the beginning. I may just store all ProcessID-s in list - [ProcessID]. But then i don't even know which ProcessID belongs to which process. Then, i may add names for ProcessID-s, so i can distinguish them later - e.g. [(String, ProcessID)]. This is essentially your solution: pid file (and PidProg value) binds process name and pid together. But now all ProcessID-s have the same start/stop functions. Finally, i may store start/stop functions in data type as well, but such type can't have Show and Read instance, so it can't be stored in extensible state persistently with 'extensionType = PersistentExtension'. Then i may try to store in extensible state only ProcessID and name and find start/stop function for them using type-class . Moreover, i even does not need name - i can use Eq instance for this and compare any fields (not only e.g. command names) there. So, here is my rewritten implementation of that ideas: > {-# LANGUAGE FlexibleContexts > , DeriveDataTypeable > , GeneralizedNewtypeDeriving #-} > > import Data.Monoid > import XMonad > import qualified XMonad.Util.ExtensibleState as XS > import System.Posix.Process > import System.IO > import System.Posix.IO > import System.Posix.Types > import System.Posix.Signals > import System.Directory > import System.FilePath > import Control.Exception > import Control.Monad > > spawnPipe' :: [String] -> X (Handle, ProcessID) > spawnPipe' (x : xs) = io $ do > (rd, wr) <- createPipe > setFdOption wr CloseOnExec True > h <- fdToHandle wr > hSetBuffering h LineBuffering > p <- xfork $ do > _ <- dupTo rd stdInput > --executeFile "/bin/sh" False ["-c", encodeString x] Nothing > executeFile x True xs Nothing > closeFd rd > return (h, p) > > spawnPID' :: MonadIO m => [String] -> m ProcessID > spawnPID' (x : xs) = xfork $ executeFile x True xs Nothing I've defined XmobarPID3 newtype allowing to launching several xmobar's > data XmobarPID3 = XmobarPID3 > { xmobarPID :: First ProcessID > , xmobarConf :: FilePath > } > deriving (Show, Read, Typeable) > instance Eq XmobarPID3 where > XmobarPID3 {xmobarConf = xcf} == XmobarPID3 {xmobarConf = ycf} > | xcf == ycf = True > | otherwise = False > instance Monoid XmobarPID3 where > mempty = XmobarPID3 > { xmobarPID = First Nothing > , xmobarConf = "" } > x `mappend` y = XmobarPID3 > { xmobarPID = xmobarPID x `mappend` xmobarPID y > , xmobarConf = xmobarConf x } > > newtype XmobarHandle = XmobarHandle (Maybe Handle) > deriving (Typeable) > instance ExtensionClass XmobarHandle where > initialValue = XmobarHandle Nothing and TrayerPID3 and FehPID3 designed for one program instance only (because all values of these types are equal): > newtype TrayerPID3 = TrayerPID3 {trayerPID :: First ProcessID} > deriving (Show, Read, Typeable, Monoid) > instance Eq TrayerPID3 where > _ == _ = True > > newtype FehPID3 = FehPID3 {fehPID :: First ProcessID} > deriving (Show, Read, Typeable, Monoid) > instance Eq FehPID3 where > _ == _ = True Then i define typeclass for start/stop interface (why there is Monoid constraint see runWith code below): > class (Eq a, Monoid a) => RestartClass3 a where > getPidP3 :: a -> Maybe ProcessID > setPidP3 :: Maybe ProcessID -> a -> a > runP3 :: a -> X a > -- restartP3' relies on PID 'Nothing' after killP3, because it then calls > -- startP3' and it won't do anything, if PID will still exist. So, here i > -- should either set it to Nothing, or wait until it really terminates. > killP3 :: a -> X a > killP3 x = io $ do > whenJust (getPidP3 x) $ signalProcess sigTERM > return (setPidP3 Nothing x) > > defaultRunP3 :: RestartClass3 a => [String] -> a -> X a > defaultRunP3 xs z = do > p <- spawnPID' xs > return (setPidP3 (Just p) z) then i define instances for XmobarPID3 and FehPID3 with custom runP3 functions: > instance RestartClass3 XmobarPID3 where > getPidP3 = getFirst . xmobarPID > setPidP3 mp' x = x{xmobarPID = First mp'} > runP3 x = do > (h, p) <- spawnPipe' ["/usr/bin/xmobar", xmobarConf x] > XS.put (XmobarHandle (Just h)) > return (x{xmobarPID = First (Just p)}) > > instance RestartClass3 FehPID3 where > getPidP3 = getFirst . fehPID > setPidP3 mp' x = x{fehPID = First mp'} > runP3 x = do > h <- io $ getHomeDirectory > let f = h ".fehbg" > b <- io $ doesFileExist f > p <- if b then do > cmd <- io $ readFile f > -- ~/.fehbg content written assuming evaluation by shell, > -- but i still need real process's PID, so add 'exec' . > spawnPID ("exec " ++ cmd) > else spawnPID' ["xsetroot", "-grey"] > return (x{fehPID = First (Just p)}) trayer will use default run/kill implementation: > instance RestartClass3 TrayerPID3 where > getPidP3 = getFirst . trayerPID > setPidP3 mp' x = x{trayerPID = First mp'} > runP3 = defaultRunP3 > [ "trayer" > , "--edge", "top", "--align", "right" > , "--SetDockType", "true", "--SetPartialStrut", "true" > , "--expand", "true", "--width", "10" > , "--transparent", "true" , "--tint", "0x191970" > , "--height", "12" > ] The other deficiency of my previous implementation was that i may store only one program's info for each newtype (e.g. i may run only one xmobar, because i've used (Maybe XmobarPID) to store its pid). Now i'll switch to list instead of Maybe in extensible state. Also, i want to make 'respawn' record of PidProg implicit: if i call 'restart' i ever want to kill program and run again; if i just want to be sure, that program is running, i should call the 'start' instead. > instance (Show a, Read a, Typeable a) => ExtensionClass [a] where > initialValue = [] > extensionType = PersistentExtension > > -- Similar to insertWith from Data.Map, but for lists. > insertWith :: Eq a => (a -> a -> a) -> a -> [a] -> [a] > insertWith f y [] = [y] > insertWith f y (x : xs) > | y == x = f y x : xs > | otherwise = x : insertWith f y xs > > -- Run function on matched PIDs with specific type. > -- Argument's Eq instance is used to find value in extensible state > -- to act upon. Also, argument is `mappend`-ed to found value, > -- so i should pass mempty, if i want to just "match", and > -- something different, if i want to "match and replace". > runWith :: (Eq a, Monoid a, ExtensionClass [a]) => (a -> X a) -> a -> X () > runWith f y = do > xs <- XS.gets (insertWith mappend y) > xs'' <- mapM (\x -> if y == x then f x else return x) xs > XS.put xs'' > > -- Based on doesPidProgRun . > refreshPid :: (MonadIO m, RestartClass3 a) => a -> m a > refreshPid x = case (getPidP3 x) of > Nothing -> return x > Just p -> liftIO $ do > either (const (setPidP3 Nothing x)) (const x) > `fmap` (try $ getProcessPriority p :: IO (Either IOException Int)) > > -- Run, if program is not running or already dead, otherwise do nothing. > -- Note, that this function work on argument, not on extensible state. > startP3' :: RestartClass3 a => a -> X a > startP3' x = do > x' <- refreshPid x > case (getPidP3 x') of > Nothing -> runP3 x' > Just _ -> return x' > > -- Kill program and run again. Note, that it will run again only, > -- if killP3 kills it properly: either sets pid to Nothing > -- or waits until it dies. > -- Note, that this function work on argument, not on extensible state. > restartP3' :: RestartClass3 a => a -> X a > restartP3' = startP3' <=< killP3 <=< refreshPid > > -- Here are versions of start/restart working on extensible state. > -- Usually, these should be used. > startP3 :: (ExtensionClass [a], RestartClass3 a) => a -> X () > startP3 = runWith startP3' > > restartP3 :: (ExtensionClass [a], RestartClass3 a) => a -> X () > restartP3 = runWith restartP3' Finally, i may define some concerete examples: > xmobarTop :: XmobarPID3 > xmobarTop = XmobarPID3 > { xmobarPID = First Nothing > , xmobarConf = "/home/sgf" ".xmobarrc" > } > xmobarBot :: XmobarPID3 > xmobarBot = XmobarPID3 > { xmobarPID = First Nothing > , xmobarConf = "/home/sgf" ".xmobarrc2" > } > trayer :: TrayerPID3 > trayer = TrayerPID3 {trayerPID = First Nothing} > > feh :: FehPID3 > feh = FehPID3 {fehPID = First Nothing} > > restartXmobarTop :: X () > restartXmobarTop = restartP3 xmobarTop > startXmobarTop :: X () > startXmobarTop = startP3 xmobarTop > > restartXmobarBoth :: X () > restartXmobarBoth = mapM_ restartP3 [xmobarTop, xmobarBot] > startXmobarBoth :: X () > startXmobarBoth = mapM_ startP3 [xmobarTop, xmobarBot] > > restartAll :: X () > restartAll = do > startP3 feh > restartP3 trayer > mapM_ restartP3 [xmobarTop, xmobarBot] > startAll :: X () > startAll = do > startP3 feh > startP3 trayer > mapM_ startP3 [xmobarTop, xmobarBot] Usually, i should just use `restartAll` in startupHook . From sgf.dma at gmail.com Sun Jan 25 21:07:31 2015 From: sgf.dma at gmail.com (Dmitriy Matrosov) Date: Mon, 26 Jan 2015 00:07:31 +0300 Subject: [Haskell-beginners] Defining ExtensionClass (Maybe a) instance in xmonad. In-Reply-To: <54C53162.9060701@gmail.com> References: <54BEB1C6.9060604@gmail.com> <87zj9dhzaq.fsf@ilxwinb01.fritz.box> <54C53162.9060701@gmail.com> Message-ID: <54C55B13.7070704@gmail.com> On 2015?01?25? 21:09, Dmitriy Matrosov wrote: > Hi. > > Sorry for big delay, rewriting my previous solution takes a lot of time. > > On 2015?01?20? 23:14, Brandon Allbery wrote: > > > then there is no (Show a, Read a) constraint. It will come > > > up only, when i mention PersistentExtension in one of case > > > branches, but, on the other hand, may be i can avoid > > > > > > > That would be expected, I believe; if you mentioned it, it must > apply to > > the whole instance, not just one case branch. But I'm not quite > clear on > > what you are saying here. > > I mean, that i already have contraint (ExtensionClass a) in e.g. > > instance (ExtensionClass a) => ExtensionClass (Maybe a) where > initialValue = Nothing > > then if type a had Show and Read instances, then (Maybe a) will had them > too > and i may define 'extensionType = PersistentExtension' in (Maybe a) > instance. > If type a had no Show and Read instances, then i may only define (Maybe > a) to be > a StateExtension . Thus, theoretically, (ExtensionClass a) contraint is > enough, and i want to express above condition on (Maybe a) extensionType > value > with only that contraint. I can't express it by pattern-matching on type > a's > StateExtension data contructor, because that will bring up (Show a, Read a) > constraint on (Maybe a) instance. I.e. i, probably, can't do it with > condition > at data level. Then, may be, i can make a condition at type level somehow? Hm, i think, i miss something here. There should be: if type a had Show and Read instances * and have 'extensionType = PersistentExtension' * , then (Maybe a) * may * had it too. If type a had no Show and Read instances, then i may only define (Maybe a) to be a StateExtension . From georg+haskell at schaathun.net Mon Jan 26 12:19:26 2015 From: georg+haskell at schaathun.net (Hans Georg Schaathun) Date: Mon, 26 Jan 2015 13:19:26 +0100 Subject: [Haskell-beginners] Avoiding stack space overflow Message-ID: <20150126121926.GA1981@golay.schaathun.net> Hi, can someone give some hints on how to get around a stack space overflow? My problem is with the training function for a neural network: trainNetwork :: Double -> Samples -> Int -> Network -> Network trainNetwork _ _ 0 n = n trainNetwork eta samples c n = trainNetwork eta samples (c-1) $! epoch eta n samples epoch :: Double -> Network -> Samples -> Network So trainNetwork runs epoch c times, each time taking a Network in and modifying the Network as output. Clearly, space complexity can be made constant in c, but I get stack overflow if and only if c is too large. As you can see, I have tried to make the epoch evaluation strict ($!). I have also tried bang patterns on the input parameter n, and I have tried rewriting with foldr/foldl/foldl', and I have tried switchin the inner and outer calls (epoch vs. trainNetwork), all to no avail. I reckon this loop like pattern should be fairly common ... does it have a common solution too? TIA -- :-- Hans Georg From toad3k at gmail.com Mon Jan 26 14:55:03 2015 From: toad3k at gmail.com (David McBride) Date: Mon, 26 Jan 2015 09:55:03 -0500 Subject: [Haskell-beginners] Avoiding stack space overflow In-Reply-To: <20150126121926.GA1981@golay.schaathun.net> References: <20150126121926.GA1981@golay.schaathun.net> Message-ID: I don't think the problem is with trainNetwork, but rather epoch. You might try adjusting your network datatype and sub datatypes in the manner of data Network = Network !Int !Int until you narrow down which piece is causing the problem. On Mon, Jan 26, 2015 at 7:19 AM, Hans Georg Schaathun < georg+haskell at schaathun.net> wrote: > Hi, > > can someone give some hints on how to get around a stack space > overflow? > > My problem is with the training function for a neural network: > > trainNetwork :: Double -> Samples -> Int -> Network -> Network > trainNetwork _ _ 0 n = n > trainNetwork eta samples c n = trainNetwork eta samples (c-1) $! > epoch eta n samples > epoch :: Double -> Network -> Samples -> Network > > So trainNetwork runs epoch c times, each time taking a Network > in and modifying the Network as output. Clearly, space complexity > can be made constant in c, but I get stack overflow if and only > if c is too large. > > As you can see, I have tried to make the epoch evaluation strict > ($!). I have also tried bang patterns on the input parameter n, > and I have tried rewriting with foldr/foldl/foldl', and I have > tried switchin the inner and outer calls (epoch vs. trainNetwork), > all to no avail. > > I reckon this loop like pattern should be fairly common ... > does it have a common solution too? > > TIA > -- > :-- Hans Georg > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From georg+haskell at schaathun.net Mon Jan 26 16:45:17 2015 From: georg+haskell at schaathun.net (Hans Georg Schaathun) Date: Mon, 26 Jan 2015 17:45:17 +0100 Subject: [Haskell-beginners] Avoiding stack space overflow In-Reply-To: References: <20150126121926.GA1981@golay.schaathun.net> Message-ID: <20150126164517.GA27771@golay.schaathun.net> On Mon, Jan 26, 2015 at 09:55:03AM -0500, David McBride wrote: > I don't think the problem is with trainNetwork, but rather epoch. You > might try adjusting your network datatype and sub datatypes in the manner > of data Network = Network !Int !Int until you narrow down which piece is > causing the problem. It turns out that you are absolutely right. A number of randomly inserted bang patterns got rid of the overflow. Thanks a lot for the idea.. I feel even more clueless than I used too, but I have at least a way to proceed. -- :-- Hans Georg From bob at redivi.com Mon Jan 26 16:58:37 2015 From: bob at redivi.com (Bob Ippolito) Date: Mon, 26 Jan 2015 08:58:37 -0800 Subject: [Haskell-beginners] Avoiding stack space overflow In-Reply-To: <20150126164517.GA27771@golay.schaathun.net> References: <20150126121926.GA1981@golay.schaathun.net> <20150126164517.GA27771@golay.schaathun.net> Message-ID: Here are my go-to resources for Haskell's evaluation: http://chimera.labs.oreilly.com/books/1230000000929/ch02.html#sec_par-eval-whnf https://hackhands.com/lazy-evaluation-works-haskell/ On Mon, Jan 26, 2015 at 8:45 AM, Hans Georg Schaathun < georg+haskell at schaathun.net> wrote: > On Mon, Jan 26, 2015 at 09:55:03AM -0500, David McBride wrote: > > I don't think the problem is with trainNetwork, but rather epoch. You > > might try adjusting your network datatype and sub datatypes in the manner > > of data Network = Network !Int !Int until you narrow down which piece is > > causing the problem. > > It turns out that you are absolutely right. > A number of randomly inserted bang patterns got rid of the overflow. > Thanks a lot for the idea.. > > I feel even more clueless than I used too, but I have at least a way > to proceed. > > -- > :-- Hans Georg > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Mon Jan 26 17:20:56 2015 From: toad3k at gmail.com (David McBride) Date: Mon, 26 Jan 2015 12:20:56 -0500 Subject: [Haskell-beginners] Avoiding stack space overflow In-Reply-To: <20150126164517.GA27771@golay.schaathun.net> References: <20150126121926.GA1981@golay.schaathun.net> <20150126164517.GA27771@golay.schaathun.net> Message-ID: We're all guilty of throwing around the bang patterns to fix performance problems, and that is a reasonable solution. But if you wish to understand the problem, see if you can narrow down much of your leak to a specific field within one of your types, then you can look at epoch and sub functions and identify why that particular field is not being fully evaluated by the time you have returned from epoch. Hopefully it will be obvious in hindsight and you will not run into this next time you have similar code. GHCI's debugger may also give you a nice way to determine where your problem lies. On Mon, Jan 26, 2015 at 11:45 AM, Hans Georg Schaathun < georg+haskell at schaathun.net> wrote: > On Mon, Jan 26, 2015 at 09:55:03AM -0500, David McBride wrote: > > I don't think the problem is with trainNetwork, but rather epoch. You > > might try adjusting your network datatype and sub datatypes in the manner > > of data Network = Network !Int !Int until you narrow down which piece is > > causing the problem. > > It turns out that you are absolutely right. > A number of randomly inserted bang patterns got rid of the overflow. > Thanks a lot for the idea.. > > I feel even more clueless than I used too, but I have at least a way > to proceed. > > -- > :-- Hans Georg > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From animeshsaxena at icloud.com Tue Jan 27 04:43:00 2015 From: animeshsaxena at icloud.com (Animesh Saxena) Date: Tue, 27 Jan 2015 04:43:00 +0000 (GMT) Subject: [Haskell-beginners] Fixed Point function Message-ID: <552791ea-ceda-4053-8fd6-077fad07c535@me.com> I stumbled across this example as a shiny way of explaining why Lazy eval matters.... ? ? fact = fix $ \f n -> if n == 0 then 1 else n * f (n-1) fix f = f (fix f) With lazy eval, I get? fact 4 = fix $ \f 4 (......) since haskell goes outside to inside I have, fac 4 =?$ \f 4 (......)?(fix?$ \f 4 (......)) Now the first chunk can get evaluated, but what about the rest of the expression. The outer "f" from "fix f" function has the parameter (fix f). So when writing "f (fix f)" apparently f got evaluated without using this parameter. I understand that the anonymous lambda does have 4 as the param, but somehow "f (fix f)" doesn't feel complete.? -Animesh -------------- next part -------------- An HTML attachment was scrubbed... URL: From karl at karlv.net Tue Jan 27 06:24:26 2015 From: karl at karlv.net (Karl Voelker) Date: Mon, 26 Jan 2015 22:24:26 -0800 Subject: [Haskell-beginners] Fixed Point function In-Reply-To: <552791ea-ceda-4053-8fd6-077fad07c535@me.com> References: <552791ea-ceda-4053-8fd6-077fad07c535@me.com> Message-ID: <1422339866.2943779.219346645.012AB54D@webmail.messagingengine.com> On Mon, Jan 26, 2015, at 08:43 PM, Animesh Saxena wrote: > I stumbled across this example as a shiny way of explaining why Lazy > eval matters.... > > fact = fix $ \f n -> if n == 0 then 1 else n * f (n-1) fix f = > f (fix f) > > With lazy eval, I get > > fact 4 = fix $ \f 4 (......) Not quite. Let's go one step at a time: fact 4 = (fix $ \f n -> if n == 0 then 1 else n * f (n - 1)) 4 Now we can bring in the definition of fix, substituting the argument to fix for all of the occurrences of f in the definition of fix. Notice that we can't substitute the 4 for n yet. fact 4 = ((\f n -> if n == 0 then 1 else n * f (n - 1)) (fix $ \f n -> if n == 0 then 1 else n * f (n - 1))) 4 I think if you are very patient and methodical about performing the substitutions (and writing them out fully - no dot-dot-dot), then you'll figure out how it all works. -Karl -------------- next part -------------- An HTML attachment was scrubbed... URL: From sumit.sahrawat.apm13 at iitbhu.ac.in Tue Jan 27 07:22:09 2015 From: sumit.sahrawat.apm13 at iitbhu.ac.in (Sumit Sahrawat, Maths & Computing, IIT (BHU)) Date: Tue, 27 Jan 2015 12:52:09 +0530 Subject: [Haskell-beginners] Fixed Point function In-Reply-To: <1422339866.2943779.219346645.012AB54D@webmail.messagingengine.com> References: <552791ea-ceda-4053-8fd6-077fad07c535@me.com> <1422339866.2943779.219346645.012AB54D@webmail.messagingengine.com> Message-ID: Take a look here . The above webpage uses stepval , it can evaluate any expression step by step using equational reasoning. On 27 January 2015 at 11:54, Karl Voelker wrote: > On Mon, Jan 26, 2015, at 08:43 PM, Animesh Saxena wrote: > > I stumbled across this example as a shiny way of explaining why Lazy eval > matters.... > > fact = fix $ \f n -> if n == 0 then 1 else n * f (n-1) > fix f = f (fix f) > > With lazy eval, I get > > fact 4 = fix $ \f 4 (......) > > > Not quite. Let's go one step at a time: > > fact 4 = (fix $ \f n -> if n == 0 then 1 else n * f (n - 1)) 4 > > Now we can bring in the definition of fix, substituting the argument to > fix for all of the occurrences of f in the definition of fix. Notice that > we can't substitute the 4 for n yet. > > fact 4 = ((\f n -> if n == 0 then 1 else n * f (n - 1)) (fix $ \f n -> if > n == 0 then 1 else n * f (n - 1))) 4 > > I think if you are very patient and methodical about performing the > substitutions (and writing them out fully - no dot-dot-dot), then you'll > figure out how it all works. > > -Karl > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Regards Sumit Sahrawat -------------- next part -------------- An HTML attachment was scrubbed... URL: From michal at bazzle.me Tue Jan 27 14:45:44 2015 From: michal at bazzle.me (Michal Kawalec) Date: Tue, 27 Jan 2015 15:45:44 +0100 Subject: [Haskell-beginners] GPIO/I2C/PWM and Haskell Message-ID: <54C7A498.1010202@bazzle.me> Hey list, I got myself a Beaglebone and will use it to stabilize and fly a drone with Haskell. The ghc support seems good enough (text doesn't compile, everything else does), but I have a question about inputs/outputs. They are all set through sysfs, and I think a wrapper over it would be easiest and most straightforward to have. The operation of most pins goes along the lines of 'activate a pin -> do some stuff -> turn it off'. Is there some particular mechanism/library you would suggest me to look into to handle that? Any thoughts or own experience with doing similar things with Haskell? Any tips will be appreciated:) Michal -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 819 bytes Desc: OpenPGP digital signature URL: From mike at proclivis.com Tue Jan 27 16:02:07 2015 From: mike at proclivis.com (Michael Jones) Date: Tue, 27 Jan 2015 09:02:07 -0700 Subject: [Haskell-beginners] GPIO/I2C/PWM and Haskell In-Reply-To: <54C7A498.1010202@bazzle.me> References: <54C7A498.1010202@bazzle.me> Message-ID: <69262E1D-72B2-4474-A945-68921314E332@proclivis.com> Michal, I am doing similar things (not arial) on a MinnowBoardMax. Did not want to deal with ARM and Haskell. But if you make that work, I think it would be worth publishing how you did it. I struggled to build a cross compiler for ARM and gave up. As for MinnowBoardMax, I am running Ubuntu with a 3.18.1 kernel and a solid state drive. A little heavy on the weight when you consider the batteries required to run a 5W system. I have libraries for I2C, UART, and GPIO, but not published. Here is an example of how I deal with ALERTB module SMBusAlert ( alert ) where import System.IO alert :: IO Bool alert = do writeFile "/sys/class/gpio/export" "340" writeFile "/sys/class/gpio/gpio340/direction" "in" s <- readFile "/sys/class/gpio/gpio340/value" s `seq` writeFile "/sys/class/gpio/unexport" "340" if s!!0 == '0' then return True else return False On Jan 27, 2015, at 7:45 AM, Michal Kawalec wrote: > Hey list, > > I got myself a Beaglebone and will use it to stabilize and fly a drone > with Haskell. The ghc support seems good enough (text doesn't compile, > everything else does), but I have a question about inputs/outputs. > > They are all set through sysfs, and I think a wrapper over it would be > easiest and most straightforward to have. The operation of most pins > goes along the lines of 'activate a pin -> do some stuff -> turn it > off'. Is there some particular mechanism/library you would suggest me to > look into to handle that? Any thoughts or own experience with doing > similar things with Haskell? > > > Any tips will be appreciated:) > Michal > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners From hawu.bnu at gmail.com Tue Jan 27 23:29:22 2015 From: hawu.bnu at gmail.com (Jean Lopes) Date: Tue, 27 Jan 2015 21:29:22 -0200 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= Message-ID: Hello everyone! I'm learning Haskell by solving simple exercises on www.hackerrank.com... The problem to be solved: https://www.hackerrank.com/contests/projecteuler/challenges/euler001 as stated in this section: https://www.hackerrank.com/environment constraints version: haskell-plataform 2013.2.0.0 time limit: 5 seconds memory: 512mb I keep getting "timed out" on some test cases (#2 and #3). Trying to process 10^5 numbers between 1 to 10^9 *seems* impossible to me (Please, prove me wrong!) here is my code: import Data.Maybe import qualified Data.ByteString.Char8 as B nearestMultipleOf :: Integral a => a -> a -> a nearestMultipleOf n k = if mod n k == 0 then n else nearestMultipleOf (n-1) k sumMultiplesOf :: Integral a => a -> a -> a sumMultiplesOf n k = foldl (+) 0 [k,k*2..nearest] where nearest = nearestMultipleOf n k solution :: Integral a => a -> a solution n = s03 + s05 - s15 where s03 = sumMultiplesOf (n-1) 3 s05 = sumMultiplesOf (n-1) 5 s15 = sumMultiplesOf (n-1) 15 main = do c <- B.getContents let ns = tail $ B.lines c putStr $ unlines $ map show $ map (solution . fst . fromJust . B.readInt) ns as always, any input is really welcome! -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Tue Jan 27 23:38:31 2015 From: allbery.b at gmail.com (Brandon Allbery) Date: Tue, 27 Jan 2015 18:38:31 -0500 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: References: Message-ID: On Tue, Jan 27, 2015 at 6:29 PM, Jean Lopes wrote: > The problem to be solved: > https://www.hackerrank.com/contests/projecteuler/challenges/euler001 It's worth remembering that the Euler problems are all about math understanding; often they are designed such that brute force solutions will time out or otherwise fail. -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From hawu.bnu at gmail.com Tue Jan 27 23:57:23 2015 From: hawu.bnu at gmail.com (Jean Lopes) Date: Tue, 27 Jan 2015 21:57:23 -0200 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: References: Message-ID: I'm not really good at math, maybe I am missing something obvious ? Maybe some pointers as of where to start studying math in order to avoid this brute force attempts, at least to help in this particular problem 2015-01-27 21:38 GMT-02:00 Brandon Allbery : > On Tue, Jan 27, 2015 at 6:29 PM, Jean Lopes wrote: > >> The problem to be solved: >> https://www.hackerrank.com/contests/projecteuler/challenges/euler001 > > > It's worth remembering that the Euler problems are all about math > understanding; often they are designed such that brute force solutions will > time out or otherwise fail. > > -- > brandon s allbery kf8nh sine nomine > associates > allbery.b at gmail.com > ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From maydwell at gmail.com Wed Jan 28 00:11:53 2015 From: maydwell at gmail.com (Lyndon Maydwell) Date: Wed, 28 Jan 2015 11:11:53 +1100 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: References: Message-ID: I remember that when I had a look at Euler 1 I found that there's a fun solution that should run in "constant" time. You can find the sum of the multiples of 3, add the multiples of 5, and then subtract the multiples of 3*5. Is that the kind of thing you're looking for? - Lyndon On Wed, Jan 28, 2015 at 10:57 AM, Jean Lopes wrote: > I'm not really good at math, maybe I am missing something obvious ? > Maybe some pointers as of where to start studying math in order to avoid > this brute force attempts, at least to help in this particular problem > > 2015-01-27 21:38 GMT-02:00 Brandon Allbery : > >> On Tue, Jan 27, 2015 at 6:29 PM, Jean Lopes wrote: >> >>> The problem to be solved: >>> https://www.hackerrank.com/contests/projecteuler/challenges/euler001 >> >> >> It's worth remembering that the Euler problems are all about math >> understanding; often they are designed such that brute force solutions will >> time out or otherwise fail. >> >> -- >> brandon s allbery kf8nh sine nomine >> associates >> allbery.b at gmail.com >> ballbery at sinenomine.net >> unix, openafs, kerberos, infrastructure, xmonad >> http://sinenomine.net >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hawu.bnu at gmail.com Wed Jan 28 00:15:03 2015 From: hawu.bnu at gmail.com (Jean Lopes) Date: Tue, 27 Jan 2015 22:15:03 -0200 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: References: Message-ID: Thats actually what I did... 2015-01-27 22:11 GMT-02:00 Lyndon Maydwell : > I remember that when I had a look at Euler 1 I found that there's a fun > solution that should run in "constant" time. > > You can find the sum of the multiples of 3, add the multiples of 5, and > then subtract the multiples of 3*5. > > Is that the kind of thing you're looking for? > > - Lyndon > > > On Wed, Jan 28, 2015 at 10:57 AM, Jean Lopes wrote: > >> I'm not really good at math, maybe I am missing something obvious ? >> Maybe some pointers as of where to start studying math in order to avoid >> this brute force attempts, at least to help in this particular problem >> >> 2015-01-27 21:38 GMT-02:00 Brandon Allbery : >> >>> On Tue, Jan 27, 2015 at 6:29 PM, Jean Lopes wrote: >>> >>>> The problem to be solved: >>>> https://www.hackerrank.com/contests/projecteuler/challenges/euler001 >>> >>> >>> It's worth remembering that the Euler problems are all about math >>> understanding; often they are designed such that brute force solutions will >>> time out or otherwise fail. >>> >>> -- >>> brandon s allbery kf8nh sine nomine >>> associates >>> allbery.b at gmail.com >>> ballbery at sinenomine.net >>> unix, openafs, kerberos, infrastructure, xmonad >>> http://sinenomine.net >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From maydwell at gmail.com Wed Jan 28 00:25:57 2015 From: maydwell at gmail.com (Lyndon Maydwell) Date: Wed, 28 Jan 2015 11:25:57 +1100 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: References: Message-ID: Ah sorry, I didn't notice that you were doing that. The effectiveness of the trick really only comes into play though if you use an analytic solution for finding the sum of the multiples of 3, etc. I haven't tested this code in a while, but here's what I wrote some time ago: sum2 :: Integer -> Integer -> Integer -> Integer sum2 a b ceiling = aX + bX - abX where aX = sum1 a ceiling bX = sum1 b ceiling abX = sum1 (a * b) ceiling sum1 :: Integer -> Integer -> Integer sum1 x ceiling = sum1' (even times) times x where times = ceiling `div` x sum1' :: Bool -> Integer -> Integer -> Integer sum1' True times x = area where area = (times + 1) * (times * x) `div` 2 sum1' False times x = max + area' where max = times * x area' = sum1' True (times - 1) x Please excuse the poor Haskell style as it is quite possibly the first Haskell program I ever wrote. On Wed, Jan 28, 2015 at 11:15 AM, Jean Lopes wrote: > Thats actually what I did... > > 2015-01-27 22:11 GMT-02:00 Lyndon Maydwell : > > I remember that when I had a look at Euler 1 I found that there's a fun >> solution that should run in "constant" time. >> >> You can find the sum of the multiples of 3, add the multiples of 5, and >> then subtract the multiples of 3*5. >> >> Is that the kind of thing you're looking for? >> >> - Lyndon >> >> >> On Wed, Jan 28, 2015 at 10:57 AM, Jean Lopes wrote: >> >>> I'm not really good at math, maybe I am missing something obvious ? >>> Maybe some pointers as of where to start studying math in order to avoid >>> this brute force attempts, at least to help in this particular problem >>> >>> 2015-01-27 21:38 GMT-02:00 Brandon Allbery : >>> >>>> On Tue, Jan 27, 2015 at 6:29 PM, Jean Lopes wrote: >>>> >>>>> The problem to be solved: >>>>> https://www.hackerrank.com/contests/projecteuler/challenges/euler001 >>>> >>>> >>>> It's worth remembering that the Euler problems are all about math >>>> understanding; often they are designed such that brute force solutions will >>>> time out or otherwise fail. >>>> >>>> -- >>>> brandon s allbery kf8nh sine nomine >>>> associates >>>> allbery.b at gmail.com >>>> ballbery at sinenomine.net >>>> unix, openafs, kerberos, infrastructure, xmonad >>>> http://sinenomine.net >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hawu.bnu at gmail.com Wed Jan 28 00:43:22 2015 From: hawu.bnu at gmail.com (Jean Lopes) Date: Tue, 27 Jan 2015 22:43:22 -0200 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: References: Message-ID: Your solution runs really quick! I'll study it. Thank you 2015-01-27 22:25 GMT-02:00 Lyndon Maydwell : > Ah sorry, I didn't notice that you were doing that. The effectiveness of > the trick really only comes into play though if you use an analytic > solution for finding the sum of the multiples of 3, etc. > > I haven't tested this code in a while, but here's what I wrote some time > ago: > > > sum2 :: Integer -> Integer -> Integer -> Integer > sum2 a b ceiling = aX + bX - abX > where > aX = sum1 a ceiling > bX = sum1 b ceiling > abX = sum1 (a * b) ceiling > > sum1 :: Integer -> Integer -> Integer > sum1 x ceiling = sum1' (even times) times x > where > times = ceiling `div` x > > sum1' :: Bool -> Integer -> Integer -> Integer > sum1' True times x = area > where > area = (times + 1) * (times * x) `div` 2 > > sum1' False times x = max + area' > where > max = times * x > area' = sum1' True (times - 1) x > > > Please excuse the poor Haskell style as it is quite possibly the first > Haskell program I ever wrote. > > > > On Wed, Jan 28, 2015 at 11:15 AM, Jean Lopes wrote: > >> Thats actually what I did... >> >> 2015-01-27 22:11 GMT-02:00 Lyndon Maydwell : >> >> I remember that when I had a look at Euler 1 I found that there's a fun >>> solution that should run in "constant" time. >>> >>> You can find the sum of the multiples of 3, add the multiples of 5, and >>> then subtract the multiples of 3*5. >>> >>> Is that the kind of thing you're looking for? >>> >>> - Lyndon >>> >>> >>> On Wed, Jan 28, 2015 at 10:57 AM, Jean Lopes wrote: >>> >>>> I'm not really good at math, maybe I am missing something obvious ? >>>> Maybe some pointers as of where to start studying math in order to >>>> avoid this brute force attempts, at least to help in this particular problem >>>> >>>> 2015-01-27 21:38 GMT-02:00 Brandon Allbery : >>>> >>>>> On Tue, Jan 27, 2015 at 6:29 PM, Jean Lopes >>>>> wrote: >>>>> >>>>>> The problem to be solved: >>>>>> https://www.hackerrank.com/contests/projecteuler/challenges/euler001 >>>>> >>>>> >>>>> It's worth remembering that the Euler problems are all about math >>>>> understanding; often they are designed such that brute force solutions will >>>>> time out or otherwise fail. >>>>> >>>>> -- >>>>> brandon s allbery kf8nh sine nomine >>>>> associates >>>>> allbery.b at gmail.com >>>>> ballbery at sinenomine.net >>>>> unix, openafs, kerberos, infrastructure, xmonad >>>>> http://sinenomine.net >>>>> >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners at haskell.org >>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From animeshsaxena at icloud.com Wed Jan 28 01:09:32 2015 From: animeshsaxena at icloud.com (Animesh Saxena) Date: Wed, 28 Jan 2015 01:09:32 +0000 (GMT) Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: Message-ID: <167aa675-4559-4fd6-975f-af5357267628@me.com> why not just use infinite series. mathematically... series 1 = Mutiples of 3 ? series 2 = Multiples of 5 Apply filter and sum to get the answer -Animesh On Jan 27, 2015, at 04:43 PM, Jean Lopes wrote: > Your solution runs really quick! I'll study it. Thank you > > 2015-01-27 22:25 GMT-02:00 Lyndon Maydwell : > > Ah sorry, I didn't notice that you were doing that. The effectiveness of the trick really only comes into play though if you use an analytic solution for finding the sum of the multiples of 3, etc. > > I haven't tested this code in a while, but here's what I wrote some time ago: > > > sum2 :: Integer -> Integer -> Integer -> Integer > sum2 a b ceiling = aX + bX - abX > where > aX = sum1 a ceiling > bX = sum1 b ceiling > abX = sum1 (a * b) ceiling > > sum1 :: Integer -> Integer -> Integer > sum1 x ceiling = sum1' (even times) times x > where > times = ceiling `div` x > > sum1' :: Bool -> Integer -> Integer -> Integer > sum1' True times x = area > where > area = (times + 1) * (times * x) `div` 2 > > sum1' False times x = max + area' > where > max = times * x > area' = sum1' True (times - 1) x > > > Please excuse the poor Haskell style as it is quite possibly the first Haskell program I ever wrote. > > > > On Wed, Jan 28, 2015 at 11:15 AM, Jean Lopes wrote: > > Thats actually what I did... > > 2015-01-27 22:11 GMT-02:00 Lyndon Maydwell : > > I remember that when I had a look at Euler 1 I found that there's a fun solution that should run in "constant" time. > > You can find the sum of the multiples of 3, add the multiples of 5, and then subtract the multiples of 3*5. > > Is that the kind of thing you're looking for? > > - Lyndon > > > On Wed, Jan 28, 2015 at 10:57 AM, Jean Lopes wrote: > > I'm not really good at math, maybe I am missing something obvious ? > Maybe some pointers as of where to start studying math in order to avoid this brute force attempts, at least to help in this particular problem > > 2015-01-27 21:38 GMT-02:00 Brandon Allbery : > > On Tue, Jan 27, 2015 at 6:29 PM, Jean Lopes wrote: > > The problem to be solved: https://www.hackerrank.com/contests/projecteuler/challenges/euler001 > > > It's worth remembering that the Euler problems are all about math understanding; often they are designed such that brute force solutions will time out or otherwise fail. > > -- > brandon s allbery kf8nh sine nomine associates > allbery.b at gmail.com ballbery at sinenomine.net > > unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From nrujac at gmail.com Wed Jan 28 01:31:01 2015 From: nrujac at gmail.com (Arjun Comar) Date: Tue, 27 Jan 2015 20:31:01 -0500 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: <167aa675-4559-4fd6-975f-af5357267628@me.com> References: <167aa675-4559-4fd6-975f-af5357267628@me.com> Message-ID: The key to solving any Euler problem is to realize that you want an analytic solution whenever possible. In this case, if you try to construct and sum lists, you're going to hit the time boundaries every time. On the other hand, you can do some basic algebra and come up with a fully analytic solution that will run in constant (well, nearly, it depends on the number of bits in the upper bound) time. So for the problem at hand, you've already realized that you can compute 3 separate sums and combine them into your answer. When you want to sum numbers from 1 to n, there's an analytic formula for the summation: n*(n+1) / 2. So rewrite your sums as summations from 1 to some number, insert the analytic formula and algebraically reduce the resulting equation to a couple of simple terms that are easy to compute. If you've never done this, let me know, and I'll walk you through the steps more slowly. Thanks, Arjun On Tue, Jan 27, 2015 at 8:09 PM, Animesh Saxena wrote: > why not just use infinite series. mathematically... > > series 1 = Mutiples of 3 > series 2 = Multiples of 5 > Apply filter and sum to get the answer > > -Animesh > > On Jan 27, 2015, at 04:43 PM, Jean Lopes wrote: > > Your solution runs really quick! I'll study it. Thank you > > 2015-01-27 22:25 GMT-02:00 Lyndon Maydwell : > >> Ah sorry, I didn't notice that you were doing that. The effectiveness of >> the trick really only comes into play though if you use an analytic >> solution for finding the sum of the multiples of 3, etc. >> >> I haven't tested this code in a while, but here's what I wrote some time >> ago: >> >> >> sum2 :: Integer -> Integer -> Integer -> Integer >> sum2 a b ceiling = aX + bX - abX >> where >> aX = sum1 a ceiling >> bX = sum1 b ceiling >> abX = sum1 (a * b) ceiling >> >> sum1 :: Integer -> Integer -> Integer >> sum1 x ceiling = sum1' (even times) times x >> where >> times = ceiling `div` x >> >> sum1' :: Bool -> Integer -> Integer -> Integer >> sum1' True times x = area >> where >> area = (times + 1) * (times * x) `div` 2 >> >> sum1' False times x = max + area' >> where >> max = times * x >> area' = sum1' True (times - 1) x >> >> >> Please excuse the poor Haskell style as it is quite possibly the first >> Haskell program I ever wrote. >> >> >> >> On Wed, Jan 28, 2015 at 11:15 AM, Jean Lopes wrote: >> >>> Thats actually what I did... >>> >>> 2015-01-27 22:11 GMT-02:00 Lyndon Maydwell : >>> >>> I remember that when I had a look at Euler 1 I found that there's a fun >>>> solution that should run in "constant" time. >>>> >>>> You can find the sum of the multiples of 3, add the multiples of 5, and >>>> then subtract the multiples of 3*5. >>>> >>>> Is that the kind of thing you're looking for? >>>> >>>> - Lyndon >>>> >>>> >>>> On Wed, Jan 28, 2015 at 10:57 AM, Jean Lopes >>>> wrote: >>>> >>>>> I'm not really good at math, maybe I am missing something obvious ? >>>>> Maybe some pointers as of where to start studying math in order to >>>>> avoid this brute force attempts, at least to help in this particular problem >>>>> >>>>> 2015-01-27 21:38 GMT-02:00 Brandon Allbery : >>>>> >>>>>> On Tue, Jan 27, 2015 at 6:29 PM, Jean Lopes >>>>>> wrote: >>>>>> >>>>>>> The problem to be solved: >>>>>>> https://www.hackerrank.com/contests/projecteuler/challenges/euler001 >>>>>> >>>>>> >>>>>> It's worth remembering that the Euler problems are all about math >>>>>> understanding; often they are designed such that brute force solutions will >>>>>> time out or otherwise fail. >>>>>> >>>>>> -- >>>>>> brandon s allbery kf8nh sine nomine >>>>>> associates >>>>>> allbery.b at gmail.com >>>>>> ballbery at sinenomine.net >>>>>> >>>>>> unix, openafs, kerberos, infrastructure, xmonad >>>>>> http://sinenomine.net >>>>>> >>>>>> >>>>>> _______________________________________________ >>>>>> Beginners mailing list >>>>>> Beginners at haskell.org >>>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>>> >>>>>> >>>>> >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners at haskell.org >>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hawu.bnu at gmail.com Wed Jan 28 01:33:16 2015 From: hawu.bnu at gmail.com (Jean Lopes) Date: Tue, 27 Jan 2015 23:33:16 -0200 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: <167aa675-4559-4fd6-975f-af5357267628@me.com> References: <167aa675-4559-4fd6-975f-af5357267628@me.com> Message-ID: Yes, that would work, but this approach will exceeding the time limit of 5 seconds. Keep in mind that this program should process 100.000 numbers which will range from 1 to 1.000.000.000 in under five seconds My current implementation it won't succeed regarding the time factor, but I did get some insights, I know where to look now (I guess)! Just to illustrate what I am saying: for instance this function > sum [3,6..9999999] Takes ~1 second to return, and these are just the multiples of 3, to get the real answer I would have to first sum the multiples of 5 and 15... 2015-01-27 23:09 GMT-02:00 Animesh Saxena : > why not just use infinite series. mathematically... > > series 1 = Mutiples of 3 > series 2 = Multiples of 5 > Apply filter and sum to get the answer > > -Animesh > > On Jan 27, 2015, at 04:43 PM, Jean Lopes wrote: > > Your solution runs really quick! I'll study it. Thank you > > 2015-01-27 22:25 GMT-02:00 Lyndon Maydwell : > >> Ah sorry, I didn't notice that you were doing that. The effectiveness of >> the trick really only comes into play though if you use an analytic >> solution for finding the sum of the multiples of 3, etc. >> >> I haven't tested this code in a while, but here's what I wrote some time >> ago: >> >> >> sum2 :: Integer -> Integer -> Integer -> Integer >> sum2 a b ceiling = aX + bX - abX >> where >> aX = sum1 a ceiling >> bX = sum1 b ceiling >> abX = sum1 (a * b) ceiling >> >> sum1 :: Integer -> Integer -> Integer >> sum1 x ceiling = sum1' (even times) times x >> where >> times = ceiling `div` x >> >> sum1' :: Bool -> Integer -> Integer -> Integer >> sum1' True times x = area >> where >> area = (times + 1) * (times * x) `div` 2 >> >> sum1' False times x = max + area' >> where >> max = times * x >> area' = sum1' True (times - 1) x >> >> >> Please excuse the poor Haskell style as it is quite possibly the first >> Haskell program I ever wrote. >> >> >> >> On Wed, Jan 28, 2015 at 11:15 AM, Jean Lopes wrote: >> >>> Thats actually what I did... >>> >>> 2015-01-27 22:11 GMT-02:00 Lyndon Maydwell : >>> >>> I remember that when I had a look at Euler 1 I found that there's a fun >>>> solution that should run in "constant" time. >>>> >>>> You can find the sum of the multiples of 3, add the multiples of 5, and >>>> then subtract the multiples of 3*5. >>>> >>>> Is that the kind of thing you're looking for? >>>> >>>> - Lyndon >>>> >>>> >>>> On Wed, Jan 28, 2015 at 10:57 AM, Jean Lopes >>>> wrote: >>>> >>>>> I'm not really good at math, maybe I am missing something obvious ? >>>>> Maybe some pointers as of where to start studying math in order to >>>>> avoid this brute force attempts, at least to help in this particular problem >>>>> >>>>> 2015-01-27 21:38 GMT-02:00 Brandon Allbery : >>>>> >>>>>> On Tue, Jan 27, 2015 at 6:29 PM, Jean Lopes >>>>>> wrote: >>>>>> >>>>>>> The problem to be solved: >>>>>>> https://www.hackerrank.com/contests/projecteuler/challenges/euler001 >>>>>> >>>>>> >>>>>> It's worth remembering that the Euler problems are all about math >>>>>> understanding; often they are designed such that brute force solutions will >>>>>> time out or otherwise fail. >>>>>> >>>>>> -- >>>>>> brandon s allbery kf8nh sine nomine >>>>>> associates >>>>>> allbery.b at gmail.com >>>>>> ballbery at sinenomine.net >>>>>> >>>>>> unix, openafs, kerberos, infrastructure, xmonad >>>>>> http://sinenomine.net >>>>>> >>>>>> >>>>>> _______________________________________________ >>>>>> Beginners mailing list >>>>>> Beginners at haskell.org >>>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>>> >>>>>> >>>>> >>>>> _______________________________________________ >>>>> Beginners mailing list >>>>> Beginners at haskell.org >>>>> http://www.haskell.org/mailman/listinfo/beginners >>>>> >>>>> >>>> >>>> _______________________________________________ >>>> Beginners mailing list >>>> Beginners at haskell.org >>>> http://www.haskell.org/mailman/listinfo/beginners >>>> >>>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From allbery.b at gmail.com Wed Jan 28 01:37:58 2015 From: allbery.b at gmail.com (Brandon Allbery) Date: Tue, 27 Jan 2015 20:37:58 -0500 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: References: <167aa675-4559-4fd6-975f-af5357267628@me.com> Message-ID: On Tue, Jan 27, 2015 at 8:33 PM, Jean Lopes wrote: > Just to illustrate what I am saying: for instance this function > > sum [3,6..9999999] > Takes ~1 second to return, and these are just the multiples of 3, to get > the real answer I would have to first sum the multiples of 5 and 15... > Yes, and what people are telling you is how to do this without generating and iterating through the list, just using the description of it. This is the key to Euler problems; you're using the brute force solution, not the math that lets you skip the slow part and get the answer immediately. (I've been keeping quiet because I'm not very good at this kind of math myself; I just recognize that Euler problems are always looking for it and that almost any time you find yourself iterating through a generated list of numbers, you're doing it wrong. Even in the cases where you *do* need to do so, there's usually some math that will cut the search space down considerably; you'll run into this if you do one of the Euler problems involving prime numbers, most likely.) -- brandon s allbery kf8nh sine nomine associates allbery.b at gmail.com ballbery at sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net -------------- next part -------------- An HTML attachment was scrubbed... URL: From hawu.bnu at gmail.com Wed Jan 28 10:23:10 2015 From: hawu.bnu at gmail.com (Jean Lopes) Date: Wed, 28 Jan 2015 07:23:10 -0300 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: References: <167aa675-4559-4fd6-975f-af5357267628@me.com> Message-ID: @Arjun Comar: I would like this walkthrough! 2015-01-27 22:37 GMT-03:00 Brandon Allbery : > On Tue, Jan 27, 2015 at 8:33 PM, Jean Lopes wrote: > >> Just to illustrate what I am saying: for instance this function >> > sum [3,6..9999999] >> Takes ~1 second to return, and these are just the multiples of 3, to get >> the real answer I would have to first sum the multiples of 5 and 15... >> > > Yes, and what people are telling you is how to do this without generating > and iterating through the list, just using the description of it. This is > the key to Euler problems; you're using the brute force solution, not the > math that lets you skip the slow part and get the answer immediately. > > (I've been keeping quiet because I'm not very good at this kind of math > myself; I just recognize that Euler problems are always looking for it and > that almost any time you find yourself iterating through a generated list > of numbers, you're doing it wrong. Even in the cases where you *do* need to > do so, there's usually some math that will cut the search space down > considerably; you'll run into this if you do one of the Euler problems > involving prime numbers, most likely.) > > -- > brandon s allbery kf8nh sine nomine > associates > allbery.b at gmail.com > ballbery at sinenomine.net > unix, openafs, kerberos, infrastructure, xmonad > http://sinenomine.net > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From raabe at froglogic.com Wed Jan 28 11:12:50 2015 From: raabe at froglogic.com (Frerich Raabe) Date: Wed, 28 Jan 2015 12:12:50 +0100 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: References: Message-ID: <378cc426d4e35ca191bc03bc1bb6fcfe@roundcube.froglogic.com> On 2015-01-28 00:57, Jean Lopes wrote: > I'm not really good at math, maybe I am missing something obvious ? > Maybe some pointers as of where to start studying math in order to avoid > this brute force attempts, at least to help in this particular > problem I'm not too familiar with 'Project Euler', but summing all multiples of 3 and 5 below some given 'n' made me think: e.g. 16 it would be... 3 + 5 + 6 + 9 + 10 + 12 + 15 = 3 + 6 + 9 + 12 + 15 + 5 + 10 | reordered summands = 3 + 6 + 9 + 12 + 15 + 5 + 10 + 15 - 15 | +15-15 appended to prepare factoring out = 3 + 6 + 9 + 12 + 15 + 5 + 10 + 15 - 15 | whitespace for readability = 3 * (1+2+3+4+5) + 5 * (1+2+3) - 15 * (1) | factoring out Now I remembered a trick to get the sum of the first N numbers (I only remember it's called "der kleine Gau?" in german): 1 + 2 + ... + n = (n^2 + n) / 2 Maybe that'll help. - Frerich From nrujac at gmail.com Wed Jan 28 14:56:14 2015 From: nrujac at gmail.com (Arjun Comar) Date: Wed, 28 Jan 2015 09:56:14 -0500 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: <378cc426d4e35ca191bc03bc1bb6fcfe@roundcube.froglogic.com> References: <378cc426d4e35ca191bc03bc1bb6fcfe@roundcube.froglogic.com> Message-ID: Jean, This is going to be a bit tough over email, but here goes. The main trick here is to realize that you can write the multiples of k as k*i and sum from 1 to floor(n/k) to get the sum of the multiples of k from 1 to n. For example, the sum of the multiples of 3 can be written as: sum(3*i, 1, n / 3) Because of distributivity, we can pull the 3 out of the sum and get 3 * sum(i, 1, n / 3) which is pretty cool because now we have a sum from 1 to n/3. We can now apply the formula that gives us the sum of numbers from 1 to m: m * (m + 1) / 2 and substitute m = n / 3. This gives us: 3 * n / 3 * [(n / 3) + 1] / 2 More generally, we can write the sum of the multiples of k as: k * n / k * [(n/k) + 1] / 2 and simplify to: n/2 * [(n/k) + 1] Now you can write out the 3 sums in this form and simplify to an analytic answer for a closed form answer to the problem: n/2 * [(n / 3) + 1] + n/2 * [(n/5) + 1] - n/2 * [(n/15) + 1] Factor out the n/2: n/2 * [(n/3) + (n/5) - (n/15) + (1 + 1 - 1)] We can now give a common base to the n/k fractions and simplify: n/2 * [ 5n/15 + 3n/15 - n/15 + 1] = n/2 * [7n/15 + 1] Which is the closed form solution you were hoping for. All that said, don't trust my math and work it out by hand for yourself. I likely made some mistakes through this procedure :). Thanks, Arjun On Wed, Jan 28, 2015 at 6:12 AM, Frerich Raabe wrote: > On 2015-01-28 00:57, Jean Lopes wrote: > >> I'm not really good at math, maybe I am missing something obvious ? >> Maybe some pointers as of where to start studying math in order to avoid >> this brute force attempts, at least to help in this particular >> problem >> > > I'm not too familiar with 'Project Euler', but summing all multiples of 3 > and 5 below some given 'n' made me think: e.g. 16 it > would be... > > 3 + 5 + 6 + 9 + 10 + 12 + 15 > = 3 + 6 + 9 + 12 + 15 + 5 + 10 | reordered summands > = 3 + 6 + 9 + 12 + 15 + 5 + 10 + 15 - 15 | +15-15 appended to > prepare factoring out > = 3 + 6 + 9 + 12 + 15 + 5 + 10 + 15 - 15 | whitespace for > readability > = 3 * (1+2+3+4+5) + 5 * (1+2+3) - 15 * (1) | factoring out > > Now I remembered a trick to get the sum of the first N numbers (I only > remember it's called "der kleine Gau?" in german): > > 1 + 2 + ... + n = (n^2 + n) / 2 > > Maybe that'll help. > > - Frerich > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hawu.bnu at gmail.com Wed Jan 28 16:14:15 2015 From: hawu.bnu at gmail.com (Jean Lopes) Date: Wed, 28 Jan 2015 13:14:15 -0300 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: References: <378cc426d4e35ca191bc03bc1bb6fcfe@roundcube.froglogic.com> Message-ID: I got it now, thanks everyone. -------------- next part -------------- An HTML attachment was scrubbed... URL: From chaddai.fouche at gmail.com Wed Jan 28 20:39:23 2015 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Wed, 28 Jan 2015 21:39:23 +0100 Subject: [Haskell-beginners] GPIO/I2C/PWM and Haskell In-Reply-To: <69262E1D-72B2-4474-A945-68921314E332@proclivis.com> References: <54C7A498.1010202@bazzle.me> <69262E1D-72B2-4474-A945-68921314E332@proclivis.com> Message-ID: On Tue, Jan 27, 2015 at 5:02 PM, Michael Jones wrote: > Michal, > > I am doing similar things (not arial) on a MinnowBoardMax. Did not want to > deal with ARM and Haskell. But if you make that work, I think it would be > worth publishing how you did it. I struggled to build a cross compiler for > ARM and gave up. > > As for MinnowBoardMax, I am running Ubuntu with a 3.18.1 kernel and a > solid state drive. A little heavy on the weight when you consider the > batteries required to run a 5W system. > > I have libraries for I2C, UART, and GPIO, but not published. > > Here is an example of how I deal with ALERTB > > module SMBusAlert ( > alert > ) where > > import System.IO > > alert :: IO Bool > alert = do > writeFile "/sys/class/gpio/export" "340" > writeFile "/sys/class/gpio/gpio340/direction" "in" > s <- readFile "/sys/class/gpio/gpio340/value" > s `seq` writeFile "/sys/class/gpio/unexport" "340" > if s!!0 == '0' then return True else return False > > While I do not understand much about what you're doing, I would suggest instead : alert :: IO Bool alert = do writeGpio "export" "340" writeGpio "gpio340/direction" "in" c <- withFile "/sys/class/gpio/gpio340/value" ReadMode getChar writeGpio "unexport" "340" return (c == '0') where writeGpio p = writeFile ("/sys/class/gpio/" ++ p) and maybe writeGpio (or a general gpio transformer) should be part of an utility library since those IO to related files/devices seems to form a large part of your code. (Also the "if blabla then return True else return False" == "return blabla" is a common mistake) -- Jeda? -------------- next part -------------- An HTML attachment was scrubbed... URL: From chaddai.fouche at gmail.com Wed Jan 28 21:04:54 2015 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Wed, 28 Jan 2015 22:04:54 +0100 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: References: <378cc426d4e35ca191bc03bc1bb6fcfe@roundcube.froglogic.com> Message-ID: Well I did it pretty dirty, not trying to simplify my solution (I'm skeptic of "k * n/k = n" given that this "n / k" is really "floor (n / k)" ... does this really work for you ?), this gave me : import Control.Monad (replicateM_) main = do t <- readLn replicateM_ t (readLn >>= print . pe1 . subtract 1) pe1 n = ( (3 + m3 * 3) * m3 + (5 + m5 * 5) * m5 - (15 + m15 * 15) * m15 ) `quot` 2 where m3 = n `quot` 3 m5 = n `quot` 5 m15 = n `quot` 15 Note that the sum of multiples of 3/5/15 can be seen as a sum of terms of an arithmetic sequence which is always "number of terms * (first term + last term) / 2", easily proven by the expedient of writing the sequence twice : one in the right order and the other in reverse under it, you then see that the sum of two term in column is always the same so ... -- Jeda? -------------- next part -------------- An HTML attachment was scrubbed... URL: From nrujac at gmail.com Thu Jan 29 01:27:22 2015 From: nrujac at gmail.com (Arjun Comar) Date: Wed, 28 Jan 2015 20:27:22 -0500 Subject: [Haskell-beginners] =?utf-8?q?Project_Euler_=2301_on_HackerRank?= =?utf-8?q?=2C_Performance_issue=E2=80=8F?= In-Reply-To: References: <378cc426d4e35ca191bc03bc1bb6fcfe@roundcube.froglogic.com> Message-ID: That's why I warned about errors :). Thanks for the catch. On Wed, Jan 28, 2015 at 4:04 PM, Chadda? Fouch? wrote: > Well I did it pretty dirty, not trying to simplify my solution (I'm > skeptic of "k * n/k = n" given that this "n / k" is really "floor (n / k)" > ... does this really work for you ?), this gave me : > > import Control.Monad (replicateM_) > > main = do > t <- readLn > replicateM_ t (readLn >>= print . pe1 . subtract 1) > > pe1 n = ( (3 + m3 * 3) * m3 + (5 + m5 * 5) * m5 - (15 + m15 * 15) * m15 ) > `quot` 2 > where > m3 = n `quot` 3 > m5 = n `quot` 5 > m15 = n `quot` 15 > > > Note that the sum of multiples of 3/5/15 can be seen as a sum of terms of > an arithmetic sequence which is always "number of terms * (first term + > last term) / 2", easily proven by the expedient of writing the sequence > twice : one in the right order and the other in reverse under it, you then > see that the sum of two term in column is always the same so ... > > -- > Jeda? > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From guthrie at mum.edu Fri Jan 30 13:49:00 2015 From: guthrie at mum.edu (Gregory Guthrie) Date: Fri, 30 Jan 2015 07:49:00 -0600 Subject: [Haskell-beginners] install trouble: diagrams Message-ID: <08EF9DA445C4B5439C4733E1F35705BA0492BF9150FF@MAIL.cs.mum.edu> Trying to install Diagrams, but am stuck in a typical Cabal maze of install errors; any hints or help? >cabal install Diagrams Resolving dependencies... Configuring arithmoi-0.4.1.1... Building arithmoi-0.4.1.1... Preprocessing library arithmoi-0.4.1.1... ... : Warning: Couldn't figure out LLVM version! Make sure you have installed LLVM [ 1 of 34] Compiling Math.NumberTheory.Primes.Sieve.Indexing ( Math\NumberTheory \Primes\Sieve\Indexing.hs, dist\dist-sandbox-b31b5bcf\build\Math\NumberTheory\Primes\Sieve\Indexing.o ) : Warning: Couldn't figure out LLVM version! Make sure you have installed LLVM Failed to install arithmoi-0.4.1.1 cabal: Error: some packages failed to install: arithmoi-0.4.1.1 failed during the building phase. The exception was: ExitFailure 1 diagrams-1.2 depends on arithmoi-0.4.1.1 which failed to install. diagrams-contrib-1.1.2.5 depends on arithmoi-0.4.1.1 which failed to install. > cabal install llvm ... Failed to install type-level-0.2.4 cabal: Error: some packages failed to install: llvm-3.2.0.2 depends on type-level-0.2.4 which failed to install. llvm-base-3.2.0.2 failed during the configure step. The exception was: ExitFailure 1 type-level-0.2.4 failed during the building phase. The exception was: ExitFailure 1 >Cabal install type-level ... src\Data\TypeLevel\Num\Ops.hs:336:10: Illegal instance declaration for `Div10 x q' The liberal coverage condition fails in class `Div10' for functional dependency: `q -> x' Reason: lhs type `q' does not determine rhs type `x' In the instance declaration for `Div10 x q' Failed to install type-level-0.2.4 cabal: Error: some packages failed to install: type-level-0.2.4 failed during the building phase. The exception was: ExitFailure 1 ?? ------------------------------------------- From guthrie at mum.edu Fri Jan 30 13:54:36 2015 From: guthrie at mum.edu (Gregory Guthrie) Date: Fri, 30 Jan 2015 07:54:36 -0600 Subject: [Haskell-beginners] Cabal problems - as usual.. Message-ID: <08EF9DA445C4B5439C4733E1F35705BA0492BF915100@MAIL.cs.mum.edu> I have a very basic usage pattern and installation on a few machines, and yet continually run into the often discussed cabal-maze of problems on all of them. I have read all of the many explanations of why it is a hard problem, but so far the only solution I have heard for repair is to erase the installed library files, and start over. This can be hard if one has any external libraries installed which were themselves a lot of work to get functioning, an example are graphics libraries (like WxWin). Has anyone created any scripts which would note everything which is installed, clear out all libraries, and then try to re-install everything? (This is on Windows....) Or would this typically just run into the same problems? Doing a cabal update, it notes a new version of cabal -install, so trying that fails, and now it looks like cabal itself is broken (no installs work). Any short-term \remedies, and/or long term better approaches? >cabal update Downloading the latest package list from hackage.haskell.org Skipping download: Local and remote files match. Note: there is a new version of cabal-install available. To upgrade, run: cabal install cabal-install >cabal install cabal-install Resolving dependencies... cabal: Could not resolve dependencies: trying: Cabal-1.20.0.2/installed-a16... (user goal) trying: ghc-syb-utils-0.2.3/installed-f83... (user goal) next goal: ghc (dependency of ghc-syb-utils-0.2.3/installed-f83...) rejecting: ghc-7.8.3/installed-a4e... (conflict: Cabal==1.20.0.2/installed-a16..., ghc => Cabal==1.18.1.3/installed-421...) rejecting: ghc-7.4.2/installed-4d6... (conflict: ghc-syb-utils => ghc==7.8.3/installed-a4e...) Backjump limit reached (change with --max-backjumps). Note: when using a sandbox, all packages are required to have consistent dependencies. Try reinstalling/unregistering the offending packages or recreating the sandbox. -------------- next part -------------- An HTML attachment was scrubbed... URL: From hjgtuyl at chello.nl Fri Jan 30 22:33:42 2015 From: hjgtuyl at chello.nl (Henk-Jan van Tuyl) Date: Fri, 30 Jan 2015 23:33:42 +0100 Subject: [Haskell-beginners] Cabal problems - as usual.. In-Reply-To: <08EF9DA445C4B5439C4733E1F35705BA0492BF915100@MAIL.cs.mum.edu> References: <08EF9DA445C4B5439C4733E1F35705BA0492BF915100@MAIL.cs.mum.edu> Message-ID: On Fri, 30 Jan 2015 14:54:36 +0100, Gregory Guthrie wrote: > Doing a cabal update, it notes a new version of cabal -install, so > trying that fails, and now it looks like cabal itself is broken (no > installs work). > Any short-term \remedies, and/or long term better approaches? A method that usually works is as follows: md CabalInstall cd CabalInstall cabal sandbox init cabal install cabal-install Then copy CabalInstall\.cabal-sandbox\bin\cabal.exe to the location of the old cabal.exe (probably something like "C:\Program Files\Haskell Platform\2014.2.0.0\lib\extralibs\bin\" (Maybe rename the old cabal.exe to cabal-1.20.0.4.exe (or something similar) first) Check with cabal --version if the correct version of cabal.exe is run. If you are using a sandbox, all locally installed packages are ignored, so there is less chance of dependency problems. Regards, Henk-Jan van Tuyl -- Folding at home What if you could share your unused computer power to help find a cure? In just 5 minutes you can join the world's biggest networked computer and get us closer sooner. Watch the video. http://folding.stanford.edu/ http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html Haskell programming -- From dani at dpwright.com Fri Jan 30 23:54:19 2015 From: dani at dpwright.com (Daniel P. Wright) Date: Sat, 31 Jan 2015 08:54:19 +0900 Subject: [Haskell-beginners] install trouble: diagrams In-Reply-To: <08EF9DA445C4B5439C4733E1F35705BA0492BF9150FF@MAIL.cs.mum.edu> References: <08EF9DA445C4B5439C4733E1F35705BA0492BF9150FF@MAIL.cs.mum.edu> Message-ID: I've run into problems like this before; there seems to be some issues with certain versions of llvm. I got around it by compiling arithmoi without llvm support: $ cabal install Diagrams --constrain "arithmoi -llvm" 30 Jan 2015 22:49?Gregory Guthrie ??????: > Trying to install Diagrams, but am stuck in a typical Cabal maze of install errors; any hints or help? > >> cabal install Diagrams > Resolving dependencies... > Configuring arithmoi-0.4.1.1... > Building arithmoi-0.4.1.1... > Preprocessing library arithmoi-0.4.1.1... > ... > : > Warning: Couldn't figure out LLVM version! > Make sure you have installed LLVM > [ 1 of 34] Compiling Math.NumberTheory.Primes.Sieve.Indexing ( Math\NumberTheory > \Primes\Sieve\Indexing.hs, dist\dist-sandbox-b31b5bcf\build\Math\NumberTheory\Primes\Sieve\Indexing.o ) > > : > Warning: Couldn't figure out LLVM version! > Make sure you have installed LLVM > Failed to install arithmoi-0.4.1.1 > cabal: Error: some packages failed to install: > arithmoi-0.4.1.1 failed during the building phase. The exception was: > ExitFailure 1 > diagrams-1.2 depends on arithmoi-0.4.1.1 which failed to install. > diagrams-contrib-1.1.2.5 depends on arithmoi-0.4.1.1 which failed to install. > > >> cabal install llvm > ... > Failed to install type-level-0.2.4 > cabal: Error: some packages failed to install: > llvm-3.2.0.2 depends on type-level-0.2.4 which failed to install. > llvm-base-3.2.0.2 failed during the configure step. The exception was: > ExitFailure 1 > type-level-0.2.4 failed during the building phase. The exception was: > ExitFailure 1 > > >> Cabal install type-level > ... > src\Data\TypeLevel\Num\Ops.hs:336:10: > Illegal instance declaration for `Div10 x q' > The liberal coverage condition fails in class `Div10' > for functional dependency: `q -> x' > Reason: lhs type `q' does not determine rhs type `x' > In the instance declaration for `Div10 x q' > Failed to install type-level-0.2.4 > cabal: Error: some packages failed to install: > type-level-0.2.4 failed during the building phase. The exception was: > ExitFailure 1 > > > ?? > > ------------------------------------------- > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From kc1956 at gmail.com Sat Jan 31 01:34:22 2015 From: kc1956 at gmail.com (KC) Date: Fri, 30 Jan 2015 17:34:22 -0800 Subject: [Haskell-beginners] Cabal problems - as usual.. In-Reply-To: References: <08EF9DA445C4B5439C4733E1F35705BA0492BF915100@MAIL.cs.mum.edu> Message-ID: If you're on Windows install development environments outside the Programs Folder which has special protections on it. -- -- Sent from an expensive device which will be obsolete in a few months! :D Casey On Jan 30, 2015 2:33 PM, "Henk-Jan van Tuyl" wrote: > On Fri, 30 Jan 2015 14:54:36 +0100, Gregory Guthrie > wrote: > > Doing a cabal update, it notes a new version of cabal -install, so trying >> that fails, and now it looks like cabal itself is broken (no installs work). >> Any short-term \remedies, and/or long term better approaches? >> > > A method that usually works is as follows: > md CabalInstall > cd CabalInstall > cabal sandbox init > cabal install cabal-install > Then copy CabalInstall\.cabal-sandbox\bin\cabal.exe to the location of > the old cabal.exe (probably something like "C:\Program Files\Haskell > Platform\2014.2.0.0\lib\extralibs\bin\" > (Maybe rename the old cabal.exe to cabal-1.20.0.4.exe (or something > similar) first) > Check with > cabal --version > if the correct version of cabal.exe is run. > > If you are using a sandbox, all locally installed packages are ignored, so > there is less chance of dependency problems. > > Regards, > Henk-Jan van Tuyl > > > -- > Folding at home > What if you could share your unused computer power to help find a cure? In > just 5 minutes you can join the world's biggest networked computer and get > us closer sooner. Watch the video. > http://folding.stanford.edu/ > > > http://Van.Tuyl.eu/ > http://members.chello.nl/hjgtuyl/tourdemonad.html > Haskell programming > -- > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dontdieych at gmail.com Sat Jan 31 15:39:41 2015 From: dontdieych at gmail.com (YCH) Date: Sun, 01 Feb 2015 00:39:41 +0900 Subject: [Haskell-beginners] Cannot find cause of space leak Message-ID: <2191211.qPyKtf21Jc@ych> Hello Haskellers, http://lpaste.net/119693 ~~~ {-# LANGUAGE TemplateHaskell #-} import Test.Tasty import Test.Tasty.TH import Test.Tasty.QuickCheck main :: IO () main = defaultMain tests tests = $(testGroupGenerator) -- Problem 15 -- -- (**) Replicate the elements of a list a given number of times. -- -- Example: -- -- * (repli '(a b c) 3) -- (A A A B B B C C C) -- -- Example in Haskell: -- -- >>> repli "abc" 3 -- "aaabbbccc" prop_repli xs n = a && b where types = ((xs::[Int]), (n::Int)) ys = repli xs n ys' = groupByN ys n where groupByN [] _ = [] groupByN zs m = take m zs : groupByN (drop m zs) m a = and $ fmap ((==n).length) ys' b = and $ fmap f ys' where f (x:xs) = all (==x) xs repli :: [a] -> Int -> [a] repli [] _ = [] repli (x:xs) n | n <= 1 = (x:xs) | otherwise = repli' x n ++ repli xs n where repli' x' 0 = [] repli' x' n' = x':repli' x' (n'-1) ~~~ > prop_repli xs n = a && b If I set 'n' as ' n>=1', it's ok. But with just n, does not terminate. Any advice would be appreciated. Thanks. -- YCH From ky3 at atamo.com Sat Jan 31 17:03:05 2015 From: ky3 at atamo.com (Kim-Ee Yeoh) Date: Sun, 1 Feb 2015 00:03:05 +0700 Subject: [Haskell-beginners] Cannot find cause of space leak In-Reply-To: <2191211.qPyKtf21Jc@ych> References: <2191211.qPyKtf21Jc@ych> Message-ID: On Sat, Jan 31, 2015 at 10:39 PM, YCH wrote: > ys' = groupByN ys n > where > groupByN [] _ = [] > groupByN zs m = take m zs : groupByN (drop m zs) m > This groupByN doesn't look like it'll terminate for negative n. So ys' becomes an infinite list. Which causes divergence downstream. -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: From dontdieych at gmail.com Sat Jan 31 17:10:00 2015 From: dontdieych at gmail.com (YCH) Date: Sun, 1 Feb 2015 02:10:00 +0900 Subject: [Haskell-beginners] Cannot find cause of space leak In-Reply-To: References: <2191211.qPyKtf21Jc@ych> Message-ID: 2015. 2. 1. ?? 2:03? "Kim-Ee Yeoh" ?? ??: > > > On Sat, Jan 31, 2015 at 10:39 PM, YCH wrote: >> >> ys' = groupByN ys n >> where >> groupByN [] _ = [] >> groupByN zs m = take m zs : groupByN (drop m zs) m > > > This groupByN doesn't look like it'll terminate for negative n. So ys' becomes an infinite list. Which causes divergence downstream. > > -- Kim-Ee Thanks, Kim-Ee. Now I can see. -------------- next part -------------- An HTML attachment was scrubbed... URL: