[Haskell-cafe] Type error when trying to adapt http-proxy to new conduit
Hi, The http-proxy package isn't compatible any longer with the latest conduit. Since it is open source, I thought, I might as well try to adapt it and submit a patch. However I run into some difficulties. For example I get this type error when I'm trying to compile it : Network/HTTP/Proxy.hs:254:15: Couldn't match expected type `ResourceT IO (CIN.Pipe () () ByteString () (ResourceT IO) ())' with actual type `IO (CIN.Pipe () () ByteString () (ResourceT IO) ())' I can't figure it out. To me it reads like : Expected x, actual x. Anyone has a clue? thanks, Pieter -- Pieter Laeremans pie...@laeremans.org The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Mime / Mail library
Hi all, The MIME package that can be found on hackage, uses String as input. Would i be considered better if there would be a version based on Text, or ByteString ? kind regards, Pieter -- Pieter Laeremans pie...@laeremans.org The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] unix-compat and GHC 7.0.2
I've got the same problem. I don't have acces to the computer where I've go the problem (my home mac). But if I remember correctly cabal install unix-compat -V3 yielded more output, the problem was due to lbutil.h, that was not present. I've experienced the same problem on an EC2, with a redhat fedora, by amazon. On ubuntu I got it working by installing libbsd-dev or something like that. kind regards, Pieter PS : funny thing is, I've stumbled upon the problem when trying to install yesod ;-). On Tue, Mar 15, 2011 at 7:13 PM, Michael Snoyman mich...@snoyman.comwrote: Hi all, I'm having trouble compiling unix-compat with GHC 7.0.2. I'm fairly certain that other users are not running into this, but I'm not sure what would be wrong on my system. Here's the error I'm getting: Resolving dependencies... Configuring unix-compat-0.2.1.1... cabal: Missing dependency on a foreign library: * Missing header file: HsUnixCompat.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. cabal: Error: some packages failed to install: unix-compat-0.2.1.1 failed during the configure step. The exception was: ExitFailure 1 Any ideas? Thanks, Michael ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- Pieter Laeremans pie...@laeremans.org The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Low level problem with Data.Text.IO
Hello, I have a strange (low level) problem with the Data.Text library. Running the simple program below on a certain text file causes a low level error. runghc ./ReadFiles.hs testfile ghc(16402,0xb0103000) malloc: *** error for object 0x2501710: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug ghc(16402,0xb0103000) malloc: *** error for object 0x2501710: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug ReadFiles.hs: testfile: hGetContents: invalid argument (Illegal byte sequence) module Main where import qualified Data.Text.IO as TI import qualified Data.Text as T import System main = do args - getArgs let fileName:_ = args txt - TI.readFile fileName putStrLn $ show txt Unfortunately I can 't post the specific data file. But according to the file program it is a text file with : Non-ISO extended-ASCII text, with very long lines, with CRLF, LF line terminators encoding. How can I debug this problem ? What would you guys do ? Trying gdb ? thanks in advance, Pieter -- Pieter Laeremans pie...@laeremans.org The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: zip-archive performance/memmory usage
Thanks for your comments John. I appreciate your work. I think pandoc is fantastic! I'm interested to solve this problem, but time is also an issue. I'll try to toy around with it. Thanks, Pieter On Tue, Aug 10, 2010 at 7:06 PM, John MacFarlane j...@berkeley.edu wrote: Hi all, I'm the author of zip-archive. I wrote it for a fairly special purpose -- I wanted to create and read ODT files in pandoc -- and I know it could be improved. The main problem is that the parsing algorithm is kind of stupid; it just reads the whole archive in sequence, storing the files as it comes to them. So a file listing will take almost as much time as a full extract. There's a better way: The zip archive ends with an end of central directory record, which contains (among other things) the offset of the central directory from the beginning of the file. So, one could use something like the following strategy: 1. read the end of central directory record, which should be the last 22 bytes of the file. I think it should be possible to do this without allocating memory for the whole file. 2. parse that to determine the offset of the central directory. 3. seek to the offset of the central directory and parse it. This will give you a list of file headers. Each file header will tell you the name of a file in the archive, how it is compressed, and where to find it (its offset) in the file. At this point you'd have the list of files, and enough information to seek to any file and read it from the archive. The API could be changed to allow lazy reading of a single file without reading all of them. I don't think these changes would be too difficult, since you wouldn't have to change any of the functions that do the binary parsing -- it would just be a matter of changing the top-level functions. I don't have time to do this right now, but if one of you wants to tackle the problem, patches are more than welcome! There's some documentation on the ZIP format in comments in the source code. John +++ Neil Brown [Aug 10 10 12:35 ]: On 10/08/10 00:29, Pieter Laeremans wrote: Hello, I'm trying some haskell scripting. I'm writing a script to print some information from a zip archive. The zip-archive library does look nice but the performance of zip-archive/lazy bytestring doesn't seem to scale. Executing : eRelativePath $ head $ zEntries archive on an archive of around 12 MB with around 20 files yields Stack space overflow: current size 8388608 bytes. The script in question can be found at : http://github.com/plaeremans/HaskellSnipplets/blob/master/ZipList.hs I'm using the latest version of haskell platform. Are these libaries not production ready, or am I doing something terribly wrong ? I downloaded your program and compiled it (GHC 6.12.1, zip-archive 0.1.1.6, bytestring 0.9.1.5). I ran it on the JVM src.zip (20MB, ~8000 files) and it sat there for a minute (67s), taking 2.2% memory according to top, then completed successfully. Same behaviour with -O2. Which compares very badly in time to the instant return when I ran unzip -l on the same file, but I didn't see any memory problems. Presumably your archive is valid and works with unzip and other tools? Thanks, Neil. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- Pieter Laeremans pie...@laeremans.org The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] zip-archive performance/memmory usage
Hello, I'm trying some haskell scripting. I'm writing a script to print some information from a zip archive. The zip-archive library does look nice but the performance of zip-archive/lazy bytestring doesn't seem to scale. Executing : eRelativePath $ head $ zEntries archive on an archive of around 12 MB with around 20 files yields Stack space overflow: current size 8388608 bytes. The script in question can be found at : http://github.com/plaeremans/HaskellSnipplets/blob/master/ZipList.hs I'm using the latest version of haskell platform. Are these libaries not production ready, or am I doing something terribly wrong ? kind regards, Pieter -- Pieter Laeremans pie...@laeremans.org The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Installation problem with latest version of haskell platform
Hi, When I try to install haskell platform 64 bit on a x86_64 debian etch I get the following error after : make install: /usr/bin/install -c -m 755 -d /usr/local/lib/ghc-6.12.3/package.conf.d /usr/local/lib/ghc-6.12.3/ghc-pkg --force --global-conf /usr/local/lib/ghc-6.12.3/package.conf.d update libffi/package.conf.install /usr/local/lib/ghc-6.12.3/ghc-pkg: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory make[1]: *** [install_packages] Error 127 make: *** [install] Error 2 I have no good strategy to locate the needed library. A quick google search yielded ncurses, but I have already installed it, and it doesn't contain that binary. Furthermore configure doesn't yield any errors. And a dependency on ncurses does seem quite strange doesn't it ? thans in advance, Pieter -- Pieter Laeremans pie...@laeremans.org The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Network.UrlDisp
Hello, Has anyone some exampe usages of : Network.UrlDisp ? thanks in advance, Pieter -- Pieter Laeremans pie...@laeremans.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Network CGI : outputFPS ByteString problem
Hello, When I try to render a byteString template using outputFPS like this : outputBsTemplate :: StringTemplate ByteString - CGI CGIResult outputBsTemplate template = let bs = renderFPS template in outputFPS bs I get this error : Couldn't match expected type `Data.ByteString.Lazy.Internal.ByteString' against inferred type `ByteString' Is this a library problem? Is the CGI bound with a too specific ByteString ? Or am I doing something terrible wrong ? thanks in advance, Pieter -- Pieter Laeremans pie...@laeremans.org The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] ByteString/parsec
HI, I want to efficiently parse a large collections of files. The files are in the format : example title TITLE author name AUTHOR some lines with summary here SUMMARY the real text TEXT a list of links LINKS I want to use ByteString here, but which library should I use to parse ? attoparsec or bytestringparser, both export the same interface. When I use one of these I thaught it would be nice to write something like this : fileParser :: Parser Content fileParser = do title - manyTill getInput (string . pack \nTITLE\n) author - manyTill getInput (string. pack \nTITLE\n) return Content title author ... But this doesn't work. Even on a small example : parseTest (manyTill getInput (string $ pack SPLIT) (pack split the text at SPLIT part two) I get a stack overflow. Obviously I'm not understanding something here. Are there any good examples of open source projects which parse ByteString data ? thanks in advance, Pieter -- Pieter Laeremans [EMAIL PROTECTED] The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] GHC 6.10 / Mac OS X / Library problem
(first use in this function) Base.hsc:698: error: 'PCRE_INFO_STUDYSIZE' undeclared (first use in this function) Base.hsc:701: error: 'PCRE_INFO_DEFAULT_TABLES' undeclared (first use in this function) Base.hsc:690: error: 'PCRE_CONFIG_UTF8' undeclared (first use in this function) Base.hsc:693: error: 'PCRE_CONFIG_NEWLINE' undeclared (first use in this function) Base.hsc:696: error: 'PCRE_CONFIG_LINK_SIZE' undeclared (first use in this function) Base.hsc:699: error: 'PCRE_CONFIG_POSIX_MALLOC_THRESHOLD' undeclared (first use in this function) Base.hsc:702: error: 'PCRE_CONFIG_MATCH_LIMIT' undeclared (first use in this function) Base.hsc:705: error: 'PCRE_CONFIG_STACKRECURSE' undeclared (first use in this function) Base.hsc:708: error: 'PCRE_CONFIG_UNICODE_PROPERTIES' undeclared (first use in this function) Base.hsc:711: error: 'PCRE_CONFIG_MATCH_LIMIT_RECURSION' undeclared (first use in this function) Base.hsc:724: error: 'PCRE_EXTRA_STUDY_DATA' undeclared (first use in this function) Base.hsc:727: error: 'PCRE_EXTRA_MATCH_LIMIT' undeclared (first use in this function) Base.hsc:730: error: 'PCRE_EXTRA_CALLOUT_DATA' undeclared (first use in this function) Base.hsc:733: error: 'PCRE_EXTRA_TABLES' undeclared (first use in this function) Base.hsc:736: error: 'PCRE_EXTRA_MATCH_LIMIT_RECURSION' undeclared (first use in this function) compiling dist/build/Text/Regex/PCRE/Light/Base_hsc_make.c failed command was: /usr/bin/gcc -c -D__GLASGOW_HASKELL__=610 -I/Library/Frameworks/GHC.framework/Versions/610/usr/lib/ghc-6.10.1/bytestring-0.9.1.4/include -I/Library/Frameworks/GHC.framework/Versions/610/usr/lib/ghc-6.10.1/base-4.0.0.0/include -I/Library/Frameworks/GHC.framework/Versions/610/usr/lib/ghc-6.10.1/include -IPAPI_INCLUDE_DIR dist/build/Text/Regex/PCRE/Light/Base_hsc_make.c -o dist/build/Text/Regex/PCRE/Light/Base_hsc_make.o cabal: Error: some packages failed to install: pcre-light-0.3.1 failed during the building phase. The exception was: exit: ExitFailure 1 -- Pieter Laeremans [EMAIL PROTECTED] The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Haskell stacktrace
Hello, I've written a cgi script in haskell, it crashes sometimes with the error message Prelude . tail : empty list In Java we would use this approach to log the erro try { } catch (Exception e) { } -- Pieter Laeremans [EMAIL PROTECTED] The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Haskell stacktrace
Woops , I hit the send button to early. The java approach to locate the error would be try { ... }catch(Exception e ){ // log error throw new RuntimeException(e); } ... What 's the best equivalent haskell approach ? thanks in advance, Pieter On Tue, Sep 9, 2008 at 10:30 PM, Pieter Laeremans [EMAIL PROTECTED]wrote: Hello, I've written a cgi script in haskell, it crashes sometimes with the error message Prelude . tail : empty list In Java we would use this approach to log the erro try { } catch (Exception e) { } -- Pieter Laeremans [EMAIL PROTECTED] The future is here. It's just not evenly distributed yet. W. Gibson -- Pieter Laeremans [EMAIL PROTECTED] The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Haskell stacktrace
This : Prelude let f = (\x - return something went wrong) :: IOError - IO String Prelude let t = return $ show $ too short list !! 100 :: IO String Prelude catch t f *** Exception: Prelude.(!!): index too large doesn't work. kind regards, Pieter On Tue, Sep 9, 2008 at 10:35 PM, Pieter Laeremans [EMAIL PROTECTED]wrote: Woops , I hit the send button to early. The java approach to locate the error would be try { ... }catch(Exception e ){ // log error throw new RuntimeException(e); } ... What 's the best equivalent haskell approach ? thanks in advance, Pieter On Tue, Sep 9, 2008 at 10:30 PM, Pieter Laeremans [EMAIL PROTECTED]wrote: Hello, I've written a cgi script in haskell, it crashes sometimes with the error message Prelude . tail : empty list In Java we would use this approach to log the erro try { } catch (Exception e) { } -- Pieter Laeremans [EMAIL PROTECTED] The future is here. It's just not evenly distributed yet. W. Gibson -- Pieter Laeremans [EMAIL PROTECTED] The future is here. It's just not evenly distributed yet. W. Gibson -- Pieter Laeremans [EMAIL PROTECTED] The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Stripping text of xml tags and special symbols
Hi, I 've got a lot of files which I need to proces in order to make them indexable by sphinx. The files contain the data of a website with a custom perl based cms. Unfortunatly they sometimes contain xml/html tags like i And since most of the texts are in dutch and some are in French they also contain a lot of special characters like ë é, ... I'm trying to replace the custom based perl based cms by a haskell one. And I would like to add search capability. Since someone wrote sphinx bindings a few weeks ago I thought I try that. But transforming the files in something that sphinx seems a challenge. Most special character problems seem to go aways when I use encodeString (Codec.Binary.UTF8.String) on the indexable data. But the sphinx indexer complains that the xml isn't valid. When I look at the errors this seems due to some documents containing not well formed html. I would like to use a programmatic solution to this problem. And is there some haskell function which converts special tokens lik - amp; and é - egu; ? thanks in advance, Pieter -- Pieter Laeremans [EMAIL PROTECTED] The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] What's wrong with the classes/insances?
HI, What 's wrong with this: type Id = String class Catalog a where listItems :: a - IO [String] getItem :: a - Id - IO (Maybe String) class Item a where getCatalog :: Catalog catalog = a - catalog data Catalog c = Content c = Content {auteur :: String, inhoud:: String, catalog::c} instance Catalog c = Item (Content c) where getCatalog (Content _ _ c) = c I get this as error from ghci: Couldn't match expected type `catalog' against inferred type `c' `catalog' is a rigid type variable bound by the type signature for `getCatalog' at ../Sites/liberaleswebsite/www.liberales.be/cgi-bin/Test.hs:16:26 `c' is a rigid type variable bound by the instance declaration at ../Sites/liberaleswebsite/www.liberales.be/cgi-bin/Test.hs:20:17 In the expression: c In the definition of `getCatalog': getCatalog (Content _ _ c) = c In the definition for method `getCatalog' Failed, modules loaded: none. thanks in advance, P -- Pieter Laeremans [EMAIL PROTECTED] The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] What's wrong with the classes/insances?
Thanks all! I have to admit, I have an OO mndset :-). I think I have found a more functional design : import Text.StringTemplate.Classes import Text.StringTemplate data (ToSElem a) = Item a b = Item {cargo::a , catalog :: (Catalog a b)} instance (ToSElem a) = ToSElem (Item a b) where toSElem = (toSElem . cargo) type Id = String type ItemToSelem a b = Item a b - SElem b type ItemParser a b = String - Item a b type AllItemReader a b = IO [Item a b] type SingleItemReader a b = Id - IO (Item a b) data Catalog a b = Catalog {reader:: (AllItemReader a b), singleReader ::( SingleItemReader a b)} data Content = Content {authort :: String, text:: String} type ContentItem = Item Content String What I want to express is that there exists differnet kinds of catalogs which, depending on how they are configured can read from a file system or a database. And each catalog can contain a specific type of Item. For each Item I have to be able to produce the toSELem representation that subsequently can be used by HStringTemplate I thik that means I could declare On Sat, Jun 21, 2008 at 12:26 AM, Dan Doel [EMAIL PROTECTED] wrote: On Friday 20 June 2008, Pieter Laeremans wrote: type Id = String class Catalog a where listItems :: a - IO [String] getItem :: a - Id - IO (Maybe String) class Item a where getCatalog :: Catalog catalog = a - catalog data Catalog c = Content c = Content {auteur :: String, inhoud:: String, catalog::c} instance Catalog c = Item (Content c) where getCatalog (Content _ _ c) = c I get this as error from ghci: Couldn't match expected type `catalog' against inferred type `c' `catalog' is a rigid type variable bound by the type signature for `getCatalog' at ../Sites/liberaleswebsite/www.liberales.be/cgi-bin/Test.hs:16:26 `c' is a rigid type variable bound by the instance declaration at ../Sites/liberaleswebsite/www.liberales.be/cgi-bin/Test.hs:20:17 In the expression: c In the definition of `getCatalog': getCatalog (Content _ _ c) = c In the definition for method `getCatalog' Failed, modules loaded: none. The problem is in the type of getCatalog: (Item a, Catalog catalog) = a - catalog That type says that given the a, you can produce a value of any type 'catalog' so long as that type is an instance of Catalog. What you probably meant it to say is that you can produce *some particular* type that belongs to catalog. There are a couple ways you could express this. For instance, using functional dependencies: class Catalog cat = HasCatalog a cat | a - cat where getCatalog :: a - cat or the new type families: class (Catalog (Cat a)) = Item a where type Cat a :: * getCatalog :: a - Cat a Or you could wrap catalogues in an existential type: data SomeCatalog = forall c. Catalog c = Cat c class Item a where getCatalog :: a - SomeCatalog However, as just a word of warning, I'd say that when you run into something like this, it's probably an indication that you're structuring your program from an object oriented mindset, and that may not be the best fit for programming in Haskell (of course, it's possible an existential type or some such is the appropriate way to do things). Cheers, -- Dan -- Pieter Laeremans [EMAIL PROTECTED] The future is here. It's just not evenly distributed yet. W. Gibson ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Memory profiling
Hi, Which tools do you recommand for memory profiling haskell programs on a *nix system. I'm using haskell to develop a CGI program/script. The application has to be deployed on shared hosting infrastructure. Since I would like to be a good citizen , I would need to meassure the maximum amount of memory allocated to the program during execution. The best I can do now is look at top but that 's not satisfactory. thanks, Pieter -- Pieter Laeremans [EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Ghc / cgi static linking
Hello, I'm researching the use of Haskell to replace some perl scripts (in a web app). The app is deployed with a webhosting provider. CGI scripts can be executed = I can use Haskell. I've tried some hello world cgi scripts, compiled them on the same linux the hosting company uses and deployed them. It worked! But I would like to implement a search feature for the website. For Java/php/perl there 's lucene. For haskell there 's holumbus. Unfortunately, sqlite is a requirement for holumbus. It is not installed at the server of the hosting company. Is it possible to staticly link the sqlite3 library using ghc ? And are there other lucene like libs available like holumbus for haskell ? thanks in advance, Pieter -- Pieter Laeremans [EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] An alternative break
Hello, I need a break function that splits the list one element further than the ordinary break. This is the simplest solution I could imagine: breakI :: (a - Bool) - [a] - ([a], [a]) breakI p s = case break p s of ([], []) - ([], []) (x, []) - (x, []) (x, l) - (x ++ [head l], tail l ) Is there a better way to write this ? thanks in advance, Pieter -- Pieter Laeremans [EMAIL PROTECTED] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Application Level server state in Haskell Web server
Thanks for your response. There 's one thing I don't understand however: the countRef function returns the same MVar all the time. What 's the reason for that ?I would expect that every time that function is invoked, it returns a new MVar, but clearly it does not. From the documentation of unsafePerformIO Use {-# NOINLINE foo #-} as a pragma on any function foo that calls unsafePerformIO. If the call is inlined, the I/O may be performed more than once. I try to conclude that writing the Pragma NOINLINE somehow makes sure that the result of countRef is computed only once! And subsequent invocations of the function will return the same result. In a way this makes sense because a function that is invoked twice with the same arguments computes the same result. But, somehow it is a very nasty hack. Am I getting it, or am I completly wrong ? kind regards, Pieter On 18-feb-07, at 03:26, Donald Bruce Stewart wrote: pieter: Hello, I'm trying to write a simple module for the haskell web server (hws- cgi). And I would like to write a simple module that maintains some kind of state for a session. But I'm I write I cannot do this in pure Haskell ? Without adopting the sources of the Haskell web server ? I'll examplify to make it more concrete : The requestHandler function a module has to implement has this signature. requestHandler :: ServerState - ServerRequest - IO (Maybe Response) Let 's assume I have this implementation requestHandler st _ = return $ Just $ mkRequest where mkRequest = okResponse (serverConfig st) mkBody hs True mkBody = HereItIs This is a test hs = mkHeaders [contentTypeHeader text/html] And I would like the response to include, for example, a number indicating the number of calls that has been handled by the module. I would concider using an Mvar but I can't remember the mvar between requests. Am I right to assume that the interface of the requestHandler method has to be adapted ? Or that serverstate has to be adopted so that it can act as a datastore ? I don't think so. You could, for example store the count on disk, and read it back in. Or you could simulate a disk store by using a mutable variable, hidden in your module: module M (requestHandler) where import Control.Concurrent.MVar import System.IO.Unsafe -- -- A threadsafe mutable variable, internal to this module. Rather -- than use, say, a disk file as storage, we can keep the count here. -- countRef :: MVar Int countRef = unsafePerformIO $ newMVar 0 {-# NOINLINE countRef #-} -- -- -- And a quick example: type Response = Int requestHandler :: IO (Maybe Response) requestHandler = do n - modifyMVar countRef $ \c - return (c+1, c) print $ received ++ show n ++ requests. return $ case n of 0 - Nothing _ - Just n *Main requestHandler received 0 requests. Nothing *Main requestHandler received 1 requests. Just 1 *Main requestHandler received 2 requests. Just 2 *Main requestHandler received 3 requests. Just 3 This seems simpler than writing the count to disk. And as long as you stay in IO, perfectly safe. In the longer term, you might want to look at state-parameterised plugins for the HWS. We do this in lambdabot, associating a private state type with each plugin, which is managed by the main server. The plugins can then get or set internal state, without resorting to local mutable variables. -- Don ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Application Level server state in Haskell Web server
Hello, I'm trying to write a simple module for the haskell web server (hws- cgi). And I would like to write a simple module that maintains some kind of state for a session. But I'm I write I cannot do this in pure Haskell ? Without adopting the sources of the Haskell web server ? I'll examplify to make it more concrete : The requestHandler function a module has to implement has this signature. requestHandler :: ServerState - ServerRequest - IO (Maybe Response) Let 's assume I have this implementation requestHandler st _ = return $ Just $ mkRequest where mkRequest = okResponse (serverConfig st) mkBody hs True mkBody = HereItIs This is a test hs = mkHeaders [contentTypeHeader text/html] And I would like the response to include, for example, a number indicating the number of calls that has been handled by the module. I would concider using an Mvar but I can't remember the mvar between requests. Am I right to assume that the interface of the requestHandler method has to be adapted ? Or that serverstate has to be adopted so that it can act as a datastore ? kind regards, Pieter ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Newbie question
Hi, I'm reading the Haskell school of expression by Paul Hudok. Great book. However I would like some feedback about a solution to an exercise The problem is quite simple : define f1 and f2 (using higher order functions ) such that f1 (f2 (*) [1..4]) 5 = [5,10,15,20] I have come up with the following solution : f2 :: (a-b)-[a] - [b] f2 f xs = map f xs f1 fs a = map (applyOp a) fs applyOp b f = f b But I guess there must be some more elegant way. f2 isn't at all original. And the applyOp sounds silly. Any comments or suggestions are welcome. thanks, Pieter ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe