Send Beginners mailing list submissions to beginners@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-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: IO vars (Corentin Dupont) 2. Re: IO vars & ACID (Corentin Dupont) 3. Re: complex typeclass/constraint question (Brent Yorgey) 4. Re: IO vars (Brent Yorgey) 5. How to load multiple source files in GHCi? (Yang) 6. Re: How to load multiple source files in GHCi? (Alexander Bernauer) 7. Re: complex typeclass/constraint question (Dennis Raddle) ---------------------------------------------------------------------- Message: 1 Date: Wed, 29 Aug 2012 12:00:50 +0200 From: Corentin Dupont <corentin.dup...@gmail.com> Subject: Re: [Haskell-beginners] IO vars To: Ozgur Akgun <ozgurak...@gmail.com> Cc: beginners@haskell.org Message-ID: <caeyhvmobyrqdkf4gseycf3axtat2u_hqlhnke2dtmzur9e4...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" Thanks Eugene and Ozgur. I also looked on the side of IORef, but it doesn't looked to be a good solution since we have to use unsafePerformIO. I have a big program to modify, and I want to pass some new data to existing functions of type IO(). I'd like to avoid changing all the function's types down the chain... What is the best way to do that? On Wed, Aug 29, 2012 at 11:43 AM, Ozgur Akgun <ozgurak...@gmail.com> wrote: > On 29 August 2012 10:21, Corentin Dupont <corentin.dup...@gmail.com>wrote: > >> *f,g :: IO () >> f = withFile "toto" WriteMode (flip hPutStr "42") >> g = withFile "toto" ReadMode hGetLine >>= (\s -> putStrLn $ "Answer:" ++ >> s) >> main = f >> g* >> >> Is it possible to do the same without files (the types must remain IO())? >> > > One can use an IORef to get a similar effect. > > import Data.IORef > import System.IO.Unsafe > > {-# NOINLINE toto #-} > toto :: IORef String > toto = unsafePerformIO (newIORef "") > > f,g :: IO () > f = writeIORef toto "42" > g = readIORef toto >>= (\s -> putStrLn $ "Answer:" ++ s) > > main = f >> g > > HTH, > Ozgur > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120829/00ede05b/attachment-0001.htm> ------------------------------ Message: 2 Date: Wed, 29 Aug 2012 12:12:34 +0200 From: Corentin Dupont <corentin.dup...@gmail.com> Subject: Re: [Haskell-beginners] IO vars & ACID To: Ozgur Akgun <ozgurak...@gmail.com> Cc: beginners@haskell.org Message-ID: <CAEyhvmr8wHFxc3HZKa7P=G0V6R5UBSz=qcnr9rztfjsngvf...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" To give you more context, I have a soft using ACID state to store and retrieve values. For example I have a function like that: newPlayer :: PlayerName -> IO () newPlayer name = update $ AddPlayer name It seems that ACID uses a global state or a file to store the events... I'd like to suppress the ACID state for the moment, how should I do? Change all the* IO () *types to *StateT Game IO ()*? Indead the ACID state is started with: c <- localStartSystemState (Proxy :: Proxy Game) Best, C On Wed, Aug 29, 2012 at 12:00 PM, Corentin Dupont <corentin.dup...@gmail.com > wrote: > Thanks Eugene and Ozgur. > I also looked on the side of IORef, but it doesn't looked to be a good > solution since we have to use unsafePerformIO. > > I have a big program to modify, and I want to pass some new data to > existing functions of type IO(). I'd like to avoid changing all the > function's types down the chain... What is the best way to do that? > > > > > On Wed, Aug 29, 2012 at 11:43 AM, Ozgur Akgun <ozgurak...@gmail.com>wrote: > >> On 29 August 2012 10:21, Corentin Dupont <corentin.dup...@gmail.com>wrote: >> >>> *f,g :: IO () >>> f = withFile "toto" WriteMode (flip hPutStr "42") >>> g = withFile "toto" ReadMode hGetLine >>= (\s -> putStrLn $ "Answer:" ++ >>> s) >>> main = f >> g* >>> >>> Is it possible to do the same without files (the types must remain IO())? >>> >> >> One can use an IORef to get a similar effect. >> >> import Data.IORef >> import System.IO.Unsafe >> >> {-# NOINLINE toto #-} >> toto :: IORef String >> toto = unsafePerformIO (newIORef "") >> >> f,g :: IO () >> f = writeIORef toto "42" >> g = readIORef toto >>= (\s -> putStrLn $ "Answer:" ++ s) >> >> main = f >> g >> >> HTH, >> Ozgur >> > > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120829/4949d0fa/attachment-0001.htm> ------------------------------ Message: 3 Date: Wed, 29 Aug 2012 06:50:47 -0400 From: Brent Yorgey <byor...@seas.upenn.edu> Subject: Re: [Haskell-beginners] complex typeclass/constraint question To: beginners@haskell.org Message-ID: <20120829105047.ga12...@seas.upenn.edu> Content-Type: text/plain; charset=us-ascii On Wed, Aug 29, 2012 at 12:28:37AM -0700, Dennis Raddle wrote: > {- I'm writing code to do some combinatorial construction via > test-and-evaluate > algorithms. And I want to write a very generic algorithm. The idea is > that the item being constructed has a current state, which is > represented by a data type of typeclass EvalState. And the algorithm > will consider a set of new elements to add to the object being > constructed, represented by a data type of typeclass ElemSet. Here are > the class definitions: > > -} > class ElemSet a where > elemSetNumElements :: a -> Int > > class EvalState s where > isCompleteState :: s -> Bool > newElemSet :: ElemSet a => s -> a > integrateElem :: ElemSet a => a -> Int -> s -> s > -- given an elem set, and an index of the elem to choose , compute a score > scoreElem :: ElemSet a => a -> Int -> s -> EvalScore As it stands, the EvalState class is quite useless, and almost certainly not what you want. Consider the type of newElemSet: newElemSet :: ElemSet a => s -> a This means that, given some type s which is an instance of EvalState, a user calling newElemSet can ask for *any type they want* as the output as long as it is an instance of ElemSet, and newElemSet has to construct it for them. Which seems rather difficult when all newElemSet has to go on is that instances of ElemSet can be queried for their size --- not much help at all in *constructing* one. integrateElem and scoreElem have similar problems -- they have to be able to deal with arbitrary instances of ElemSet, but the only thing they have to go on is the number of elements I suspect what you really want is for each instance of EvalState to have some *particular* type associated with it to represent ElemSets. There are two ways to do this. One is using a multi-parameter type class with a functional dependency: class ElemSet a => EvalState s a | s -> a where ... newElemSet :: s -> a ... The other way is using an associated type family: class ElemSet (ESType s) => EvalState s where type ESType s :: * ... newElemSet :: s -> ESType s ... Personally I would use the latter in this case. It's also possible I've completely misunderstood what you're trying to do; but whatever it is, your current EvalState class is not going to work. -Brent ------------------------------ Message: 4 Date: Wed, 29 Aug 2012 06:55:19 -0400 From: Brent Yorgey <byor...@seas.upenn.edu> Subject: Re: [Haskell-beginners] IO vars To: beginners@haskell.org Message-ID: <20120829105519.gb12...@seas.upenn.edu> Content-Type: text/plain; charset=us-ascii On Wed, Aug 29, 2012 at 12:00:50PM +0200, Corentin Dupont wrote: > Thanks Eugene and Ozgur. > I also looked on the side of IORef, but it doesn't looked to be a good > solution since we have to use unsafePerformIO. > > I have a big program to modify, and I want to pass some new data to > existing functions of type IO(). I'd like to avoid changing all the > function's types down the chain... What is the best way to do that? There is no good way. IORef is probably the best, but as you correctly point out, it is quite ugly. The fact that you have to change your program's types is a feature, not a bug. One of Haskell's great strengths is that the types make plain any communication which can take place between different components. If you want to go behind the type system's back and add hidden communication channels, you might as well be writing in C, and should not be surprised that there is no good way to do it. -Brent ------------------------------ Message: 5 Date: Wed, 29 Aug 2012 15:07:09 +0200 From: Yang <register.battlenet.com...@gmail.com> Subject: [Haskell-beginners] How to load multiple source files in GHCi? To: beginners@haskell.org Message-ID: <503e13fd.6000...@gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Hi, I have a few source files in current directory to load as modules of my library in GHCi. How should I do with :load command? I tried ghci>:load a.hs where module a is defined and exported for testing purpose, but failed for a.hs:1:1: File name does not match module name: Saw: `Main' Expected: `a' Failed, modules loaded: none. However, I do not need Main to run for testing. I just want to test exported functions. -- Best Regards Yang Zhao ------------------------------ Message: 6 Date: Wed, 29 Aug 2012 17:08:04 +0200 From: Alexander Bernauer <alex-hask...@copton.net> Subject: Re: [Haskell-beginners] How to load multiple source files in GHCi? To: beginners@haskell.org Message-ID: <20120829150804.GD6875@apus> Content-Type: text/plain; charset="us-ascii" Hi On Wed, Aug 29, 2012 at 03:07:09PM +0200, Yang wrote: > I have a few source files in current directory to load as > modules of my library in GHCi. When starting ghci, specify the directory to your modules with the -i option. Then, withing GHCI say: ghci> :module +Name.Of.Module ghci> function_from_module HTH Alex -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: Digital signature URL: <http://www.haskell.org/pipermail/beginners/attachments/20120829/9b25848d/attachment-0001.pgp> ------------------------------ Message: 7 Date: Wed, 29 Aug 2012 23:10:11 -0700 From: Dennis Raddle <dennis.rad...@gmail.com> Subject: Re: [Haskell-beginners] complex typeclass/constraint question To: Brent Yorgey <byor...@seas.upenn.edu> Cc: Haskell Beginners <beginners@haskell.org> Message-ID: <CAKxLvop=65NiUM+QOnuPWp65fJn6grFndomkE6+uc2aTke-=c...@mail.gmail.com> Content-Type: text/plain; charset="iso-8859-1" Thanks Brent, yes, you got it right, as far as my intentions. The main thing you identified was that I haven't thought this through :) On Wed, Aug 29, 2012 at 3:50 AM, Brent Yorgey <byor...@seas.upenn.edu>wrote: > It's also possible I've completely misunderstood what you're trying to > do; but whatever it is, your current EvalState class is not going to > work. > > -Brent > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120829/e6e6ed8c/attachment.htm> ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 50, Issue 37 *****************************************