Re: [Haskell-cafe] Increasing memory use in stream computation
Hi Arie, On 10/10/13 14:02, Arie Peterson wrote: (Sorry for the long email.) Summary: why does the attached program have non-constant memory use? Looking at the heap profile graph (generated with +RTS -h, no need to compile with profiling) I see the increasing memory use is split about evenly between STACK and BLACKHOLE. I don't know what that means or why it occurs, but replacing `small` solved that problem for me: small = V.fromList $ S.stream (replicateM 7 [-1,0,0,1]) I get the same output 3999744 from your version and my changed version. Claude Introduction I've written a program to do a big computation. Unfortunately, the computation takes a very long time (expectedly), and the memory use increases slowly (unexpectedly), until it fills up the entire memory and swap space of the computer (many gigabytes). The rough structure of the program is: • create a long (up to 20 million) list of objects; • compute a number for each of those objects; • compute the sum of the resulting list. I switched the intermediate data structure from a list to a Stream (from the stream-fusion package), hoping to fix the memory issue. It decreased both the memory use and the rate of its increase, but after a long time, the program still uses up all available memory. A simple program After many hours of cutting down my program, I now have a small program (attached) that shows the same behaviour. It uses only said stream-fusion package, and vector. (I haven't yet tried to cut out the use of vector. I hope it is irrelevant, because all vectors are of fixed small size.) I compile the program with ghc-7.6.1 using ghc --make -threaded -rtsopts -with-rtsopts=-M1G -K128M -O2 -main-is Test.main Test The rts options may not be strictly necessary: I added them at some point to allow the use of multiple cores, and to prevent the program from crashing the machine by using all available memory. When running the program, the resident memory quickly grows to about 3.5 MB (which I am fine with); this stays constant for a long time, but after about 7 minutes, it starts to grow further. The growth is slow, but I really would hope this program to run in constant memory. The code Note that I added an instance for Monad Stream, using concatMap. This is implicitly used in the definition of the big stream. The source of Data.Stream contains many alternative implementations of concat and concatMap, and alludes to the difficulty of making it fuse properly. Could it be that the fusion did not succeed in this case? Thanks for any help! Regards, Arie -- http://mathr.co.uk ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] more gtk help
Hi Brian, On 12/08/13 03:52, bri...@aracnet.com wrote: ... Couldn't match expected type ... Gtk.on Gtk.exposeEvent glCanvas $ \ _ - putStrLn foo ... I looked up the type of Gtk.on and exposeEvent : ... on :: object - Signal object callback - callback - IO (ConnectId object) ... I think you have the arguments flipped, try: Gtk.on glCanvas Gtk.exposeEvent $ \_ - ... As for explaining the types - as I understand it, you have an object and a callback, and the Signal associates the object with the callback for a specific event type. The type variables occur more than once in the whole type for safety: the right type of callback must be provided for the event type, and the object must support the event type too. Claude -- http://mathr.co.uk ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Diagrams and GTK
Hi Michael, On 09/08/13 08:21, Michael Oswald wrote: Hello, I am currently writing an application which draws the structure of some packets with help of the diagrams library directly to a GTK GUI. Now the packets can have several hundreds of parameters which have to be drawn so it takes some seconds to calculate the diagram itself. Of course this now blocks the GUI thread, so the basic idea was to put the calculation in a separate thread. Unfortunately this doesn't work as lazyness kicks in and the final diagram is calculated when it is rendered and not evaluated before. Using seq didn't help (because of WHNF) and there seems to be no deepseq instance for the diagrams. Does somebody has an idea on how to speed this up / get the diagram evaluated strictly as especially scrolling the DrawingArea is now really a pain? Cairo is thread safe* so you could render the whole thing (if it isn't super huge dimensions) to an image surface in the background thread, then displaying could be a matter of copying the correct part (for scrolling) of the surface to the DrawingArea. Something like this perhaps (untested, incomplete): 8 import qualified Diagrams.(...) as D import qualified Graphics.Rendering.Cairo as C renderDiaToSurface width height diagram = do let w' = fromIntegral width h' = fromIntegral height opts = D.CairoOptions (D.Dims w' h') D.RenderOnly False (_, render) = D.renderDia D.Cairo opts diagram surface - C.createImageSurface C.FormatARGB32 width height C.renderWith surface render return surface ... 8 Hope this is useful, Claude * I had some very rare crashes in heavily threaded code, still not found the root cause... -- http://mathr.co.uk ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] haskell-gtk entry question
Hi Brian, On 25/07/13 04:14, bri...@aracnet.com wrote: This should be simple, and I thought it had it working, but I've broken it and can't figure out why. What I want is to invoke the callback whenever the user activates and entry in a dialogbox, so I did both this : Not sure what you mean by dialogbox here. A complete (but small) example would help. Gtk.on entry Gtk.entryActivate (boxHandler entry) Perhaps it's a terminology confusion: in GTK, activate for an entry means pressing return key. This program works fine for me, pressing return prints the text I entered in the box: 8 import Graphics.UI.Gtk main :: IO () main = do initGUI window - windowNew entry - entryNew set window [ containerBorderWidth := 10, containerChild := entry ] entry `on` entryActivate $ putStrLn = entryGetText entry onDestroy window mainQuit widgetShowAll window mainGUI 8 (I believe this supercedes the previous method which was onEntryActivate) and this Gtk.on entry Gtk.entryPreeditChanged (boxHandler entry) however neither method will invoke the callback. The program compiles and works just fine, it's just that the callback never runs. Maybe you instead mean to do something when the widget is focussed for input? 8 import Control.Monad.Trans (liftIO) ... entry `on` focusInEvent $ tryEvent $ liftIO $ putStrLn focusInEvent 8 Claude -- http://mathr.co.uk ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How to correctly benchmark code with Criterion?
Hi Janek, On 18/10/12 10:23, Janek S. wrote: during past few days I spent a lot of time trying to figure out how to write Criterion benchmarks, so that results don't get skewed by lazy evaluation. I want to benchmark different versions of an algorithm doing numerical computations on a vector. For that I need to create an input vector containing a few thousand elements. I decided to create random data, but that really doesn't matter - I could have as well use infinite lists instead of random ones. [snip] The question is how to generate data so that its evaluation won't be included in the benchmark. Something like this might work, not sure what the canonical way is. ---8--- main = do ... let input = L.dataBuild gen evaluate (rnf input) defaultMain ... bench Lists $ nf L.benchThisFunction input ... ---8--- I did use something like this in practice here: https://gitorious.org/bitwise/bitwise/blobs/master/extra/benchmark.hs#line155 Thanks, Claude -- http://mathr.co.uk ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Ordering of BigFloats in numbers-3000.0.0.0
On 08/10/12 01:31, Michael Orlitzky wrote: http://hackage.haskell.org/package/variable-precision Mine may be unacceptably slow due to the dependent libraries. I gave it a try -- the speed isn't an issue for me, but I need the trig functions (which look like they pass through Double). These are basically homework problems and related experiments for which I'm trying to avoid MATLAB at all costs, so sin and cos show up a lot. Ah thanks for the reminder that its implementation is incomplete so far - I do plan to implement the remaining algorithms properly but lack time right now, sorry. Claude -- http://mathr.co.uk ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Ordering of BigFloats in numbers-3000.0.0.0
On 07/10/12 19:00, Michael Orlitzky wrote: I'm trying to use, http://hackage.haskell.org/package/numbers-3000.0.0.0 You might also try my: http://hackage.haskell.org/package/variable-precision It's Ord should work, I've used it in anger, otherwise let me know and I'll fix it when I have time! to get better precision for free out of some numerical code. Mine may be unacceptably slow due to the dependent libraries. I sent some make it turbo patches to the maintainer of 'type-level-natural-number' but got no response, would it be ok for me to do a non-maintainer-upload that contains purely performance enhancements and no functionality changes? Claude -- http://mathr.co.uk ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] a parallel mapM?
On 28/09/12 19:58, Patrick Mylund Nielsen wrote: Check out the parallel combinators in parallel-io: http://hackage.haskell.org/packages/archive/parallel-io/0.3.2/doc/html/Control-Concurrent-ParallelIO-Global.html also http://hackage.haskell.org/packages/archive/spawn/latest/doc/html/Control-Concurrent-Spawn.html#v:parMapIO combined with http://hackage.haskell.org/packages/archive/spawn/latest/doc/html/Control-Concurrent-Spawn.html#v:pool might be a solution Claude -- http://mathr.co.uk ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] [Haskell] Well-Typed and Skills Matter offer Haskell courses in London in October
Hi Andres, list, On 19/09/12 09:41, Andres Löh wrote: Oops, I hit send too prematurely, sorry for the seeming bluntness (but it is still a blunt message, can't apologize for that I suppose): No need to apologize. There's a need for informal meetings as much (or even more) as there is for courses and conferences. Thank you. Regarding London, I know there's the Haskell Hoodlums meetup (http://www.meetup.com/hoodlums/) Thanks, I wasn't aware of this one. I have signed up. Claude ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] [Haskell] Well-Typed and Skills Matter offer Haskell courses in London in October
On 19/09/12 08:52, Andres Löh wrote: Registration for all these events is open. I hope to see many of you there. Is there an informal hangout without the £225 price-tag Cheers, Andres ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] [Haskell] Well-Typed and Skills Matter offer Haskell courses in London in October
Hi list, Oops, I hit send too prematurely, sorry for the seeming bluntness (but it is still a blunt message, can't apologize for that I suppose): On 19/09/12 09:14, Claude Heiland-Allen wrote: On 19/09/12 08:52, Andres Löh wrote: Registration for all these events is open. I hope to see many of you there. (Referring to the October 10th event: http://skillsmatter.com/event/haskell/haskell-exchange-2012 ) Is there an informal hangout without the £225 price-tag ? Perhaps a monthly informal gathering similarly (self-)organised to Dorkbot but focussed on Haskell (and other functional languages) is what I'm really after, but that still requires some organisation time and a venue, neither of which necessarily come cheap :( So, I understand the need to charge, but it seems quite a high fee to me (especially as there don't seem to be concessions for unwaged / students / etc). Cheers, Andres Thanks, sorry if this caused offence. Three cheers if it revives a democratic informal londonhug thing. Claude ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How to implement nested loops with tail recursion?
Hi! On 19/09/12 19:00, sdiy...@sjtu.edu.cn wrote: So how do I force IO actions whose results are discarded (including IO ()) to be strict? () - foo :: IO () -- should work as it pattern matches, can wrap it in a prettier combinator !_ - foo :: IO a -- could work with -XBangPatterns I've not tested either (been away from Haskell for a while..), but see also: http://markmail.org/message/i7eufihlhgq4jqt6 (regarding modifyIORef and leaky issues) Claude -- http://mathr.co.uk ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Bad interface problem.
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi, On 11/07/12 05:51, Magicloud Magiclouds wrote: I cleaned out everything, no luck On Fri, Jul 6, 2012 at 2:14 AM, Albert Y. C. Lai tre...@vex.net wrote: On 12-07-03 04:19 AM, Magicloud Magiclouds wrote: template-haskell-2.6.0.0:Language.Haskell.TH differs from name found in the interface file template-haskell:Language.Haskell.TH You installed a bad template-haskell version. You can only use a version corresponding to your ghc version. I had a similar problem recently. My solution process was as follows: 1. check which template-haskell version came with my ghc: $ ghc -V The Glorious Glasgow Haskell Compilation System, version 7.4.2 $ ghc-pkg list template-haskell /home/claude/opt/lib/ghc-7.4.2/package.conf.d template-haskell-2.7.0.0 /home/claude/.ghc/x86_64-linux-7.4.2/package.conf.d $ 2. make sure to forbid every other version of template-haskell (because it will break horribly, as you found): $ cabal install --constraint='template-haskell==2.7.0.0' foo 3. if foo fails to install because it thinks it needs a different version of template-haskell, try adjusting dependencies in foo.cabal 4. if foo installs and works with the adjusted dependencies, let the maintainer know I think things are so messed up that it is time to clean out everything. See my http://www.vex.net/~trebla/haskell/sicp.xhtml#remove In fact, time to read the whole article and avoid unsafe re-installs and upgrades. It's a good read for sure! Perhaps it could be updated to add a problem I ran into recently: cabal install --solver=modular --avoid-reinstalls sounds perfect, if sicp.xhtml scared you properly. But excessively avoiding reinstalls is bad, as cabal-install seems to install a different allowable version instead. The result for me was horrible diamond dependency problems - half my packages were built with one version of mtl/transformers, and the other half with a different version of mtl/transformers. When I then tried to ghci using some modules from both halves of my installed packages, I got very very confusing type errors complaining about almost-identical-looking types not being identical. Claude -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.12 (GNU/Linux) iQEcBAEBAgAGBQJP/RBbAAoJEHZDo4jueIiW164IALlHcaauJX2AjBZTDExU0mKC wlH+dIbaKkl8H1IMIXQnWSX0GxFGMsbPTdBXf/BC2CMXTcSJr8YMiyKewMAs734g DijNU/x/nQlcVruOk1c8EAijIKs938vT3dF0j863+afMAA+cRWlyLWfV50Y7AIG6 4hF0Fr5Q73GwonFzTXuX+iWLxBL1i2jXgPjKJvNTJZr+iGn5txCj+6ZpJyfIXaaw PZtQrnX/37vQ/ctbKsnDqRQI27/ENJyW3zm76Gax47EIpMvL8fHzEg8IpyR9/eR8 8ZfGKYNA1EsARHT3KS6pBPsVQdhn/qYInVZ5NYQ1r/kd9D6nqoy5pETdz3z/23Y= =Rzob -END PGP SIGNATURE- ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] ANN: prof2pretty-0.1.0.0
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Hi all, I wanted to try visual-prof[1] but found it was broken by ghc RTS changes[2]. I ended up writing prof2pretty[3]: http://hackage.haskell.org/package/prof2pretty - --8-- sccpragmabomb adds SCC pragmas encoding source location. prof2pretty extracts those SCCs from the profiling output to annotate the sources using HTML + CSS + JavaScript. - --8-- [1] http://hackage.haskell.org/package/visual-prof [2] http://hackage.haskell.org/trac/ghc/ticket/4094 [3] prof2pretty started as a fork of visual-prof but soon there was none of the original code left, though the essential concept remains the same Thanks, Claude -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.12 (GNU/Linux) iQEcBAEBAgAGBQJP7wPyAAoJEHZDo4jueIiW7WsH/3Lmk2k7o+2i5u+Vxn+EZXRX 5XRDXZ8mLm1NBb/OSfRFr6RIyZxwou8JeavZJuhqOBL38KAlppf89OnaJyxUB+ap Cp03ui/1LbihqIbMxupNH03pQwkNR7b2MV2S07c5TOvOJHsksE53zEin/EHQQ6jO 6QxrQdNibjSYlXDNtZCzEj0HWVpSBD4gXdPFv9nFS+RKPjAkgC4gmY4yItYDep60 qboOrxKVhYJdqTmhl+OXhe7LAAJ9Nc3xiheGLbeyTfH2owAnT8X4zOdtssOsPPdm AQBgxKa8p5KckZCKS7Elj8pap9uzUHGc8KuINpjx40b7envHP44t65xngEq1TaU= =znB1 -END PGP SIGNATURE- ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Confused by ghci output
Hi Clark, ghci is defaulting to Integer modexp2 forces Int Int overflows with 3^40 On 31/05/12 17:35, Clark Gaebel wrote: *X 3^40 `mod` 3 == modexp2 3 40 3 False *X fromInteger (3^40 `mod` 3) == modexp2 3 40 3 True *X modexp2 3 40 3 0 *X 3^40 `mod` 3 0 *X 3^40 `mod` 3 ::Int 2 I'm confused. Last I checked, 0 == 0. Int overflow is ugly! *X 3^40 12157665459056928801 *X maxBound :: Int 9223372036854775807 Claude Using GHC 7.4.1, and the file x.hs (which has been loaded in ghci) can be found here: http://hpaste.org/69342 I noticed this after prop_sanemodexp was failing. Any help would be appreciated, - Clark ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] webcam library on github
Hi there, On 25/05/12 08:07, . wrote: On Fri, 2012-05-25 at 08:24 +0800, Conrad Parker wrote: I've downloaded and built this. I had to also download Claude Heiland-Allen's v4l2 source from gitorious, as that package does not seem to be on hackage (though his other related packages are). I guess your package won't build on hackage until v4l2 is uploaded ... I've uploaded the 'v4l2' package to hackage now. When I initially wrote it last year I had hoped to use it as a base for something similar to 'repa-v4l2' and perhaps 'opengl-v4l2' (*). Alas, I don't have a pressing need to access v4l2 devices at the moment, so I don't expect there to be rapid development or even prompt bug fixes (unless people provide patches) - if someone more involved than I am at present in video devices + Haskell on Linux wants to take over the maintainership of these v4l2-related packages, let me know: https://gitorious.org/hsv4l2 Thanks, Claude (*) I had written some code along those lines, though all I can find at the moment is a screenshot (live webcam video, processed to generate an overlay and displayed using opengl): http://claudiusmaximus.goto10.org/g/haskell/v4l2hist.png ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] ANN: variable-precision floating point
Hi all, I'm pleased to announce variable-precision-0.2: http://hackage.haskell.org/package/variable-precision There was no announcement for previous versions, as I quickly found their flaws to be too irritating in practice. --8-- excerpt from the hackage page Software floating point with type-tagged variable mantissa precision, implemented using a strict pair of Integer and Int scaled alike to decodeFloat. Instances of the usual numeric type classes are provided, along with additional operators (with carefully chosen fixities) to coerce, adjust and reify precisions. --8-- end excerpt Known shortcomings of this release: * no implementation of (inverse) (hyperbolic) trigonometric functions * no support for negative zero * no support for rounding modes * accuracy has not been extensively verified * termination of algorithms has not been proven The intention of this package is to be simple yet useable in the meantime until there is a version of hmpfr[1] or fixed-precision[2] that work without requiring a custom-compiled GHC. The design of variable-precision was inspired by the latter, and variable-precision is probably inferior, but the lifetime of the variable-precision package is hopefully going to be short. [1] http://hackage.haskell.org/package/hmpfr [2] http://hackage.haskell.org/package/fixed-precision I'm currently mainly using variable-precision for exploring the Mandelbrot Set, particularly in algorithms using Newton's method (for example, tracing external rays, where more precision is needed the closer you get to the boundary of the set). Thanks, Claude PS: --8-- excerpt from ghci session Prelude Numeric.VariablePrecision (pi :: F24) .@$ 200 $ show . (pi -) 1.5815072737072126433832795028841971693993751058209749096292229e-6 Prelude Numeric.VariablePrecision (pi :: F53) .@$ 200 $ show . (pi -) 4.1192675685652988632476805983544532308209749096292228904414057e-15 Prelude Numeric.VariablePrecision (2 :: F53) .@$ 200 $ show . log 0.69314718055994530941723212145817656807550013436025525413214402 Prelude Numeric.VariablePrecision log 2 0.6931471805599453 Prelude Numeric.VariablePrecision (2 :: F53) .@$ 200 $ show . exp 7.3890560989306502272304274605750078131803155705518473240869941 Prelude Numeric.VariablePrecision exp 2 7.38905609893065 --8-- end excerpt ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] ANN: bitwise - fast multi-dimensional unboxed bit packed Bool arrays
Hi all, I'm pleased to announce bitwise-0.1: http://hackage.haskell.org/package/bitwise --8-- excerpt from the hackage page Unboxed multidimensional bit packed Bool arrays with fast aggregate operations based on lifting Bool operations to bitwise operations. There are many other bit packed structures out there, but none met all of these requirements: 1. unboxed bit packed Bool array, 2. multi-dimensional indexing, 3. fast (de)serialization, or interoperable with foreign code, 4. fast aggregate operations (fold, map, zip). --8-- end excerpt bitwise has been tested in hugs -98, ghc-7.0.4, ghc-7.4.1. Bool data structures comparison (features numbered as excerpt above): package features additional notes 1 2 3 4 arrayY Y n n bitarray Y n n ? based on array bitstreamn n ? ? based on vector bitstringY n ? ? based on bytestring bit-vector n n Y ? based on vector bitwise Y Y Y Y repa n Y Y ? based on vector uvector Y n ? ? deprecated vector n n Y ? uses a Word8 for each Bool Known shortcomings of this first release of bitwise: * missing common instances (Eq, Ord, Read, Show, ...); * missing additional functions (counting, searching, ...); * missing documentation on ByteString format: the bits are taken from the array and packed into Word8s, with the first bit (in array order) becoming the least significant bit of the first Word8; the last Word8 is padded with 0 at the most-significant-bit end; * PBM reading is not compliant to the official specification; * misleading benchmark (bitwise zipWith is fast, but there may be a faster way to zipWith UArray ix Bool than the one I used). What I'm using bitwise for at the moment: A building block for fractal imaging: an image is made up of cells, each cell can be in one of 4 states (represented with 2 bits): * cell is 100% interior to a set of points in the plane * cell is 100% exterior * partially interior and partially exterior (ie, on the boundary) * unknown with recursive subdivision of cells into images taking place only when the cell is on the boundary of the set being imaged. Thanks, Claude ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] hint and type synonyms
Hi all, What's the recommended way to get hint[0] to play nice with type synonyms[1]? A problem occurs with in scope type synonyms involving types not in scope. I came up with this after looking at the source[2], but it makes me feel ill: --8-- -- hint and type synonyms don't play nice module Main where import Language.Haskell.Interpreter import Data.Typeable as T import Data.Typeable.Internal import GHC.Fingerprint.Type main = failure success test t = (print =) . runInterpreter $ do setImports [ Prelude ] interpret 1/5 t failure = test (as :: Rational) -- Left (WontCompile [GhcError {errMsg = Not in scope: type constructor or class `Ratio'}]) success = test (as :: Q) -- Right (1 % 5) newtype Q = Q Rational instance Show Q where show (Q a) = show a showsPrec n (Q a) = showsPrec n a instance Typeable Q where typeOf _ = TypeRep (Fingerprint 0 0) (T.mkTyCon Rational) [] --8-- Thanks, Claude [0] http://hackage.haskell.org/package/hint [1] http://www.haskell.org/onlinereport/decls.html#type-synonym-decls [2] http://hackage.haskell.org/packages/archive/hint/0.3.3.4/doc/html/src/Hint-Eval.html#interpret ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] hint and type synonyms
Hi Daniel, cafe, On 31/03/12 17:47, Daniel Gorín wrote: Could you provide a short example of the code you'd like to write but gives you problems? I'm not able to infer it from your workaround alone... This problem originally came up on #haskell, where Rc43 had a problem making a library with a common module that re-exports several other modules: http://hpaste.org/66281 My personal interest is somewhat secondary, having not yet used hint in a real project, but code I would like to write at some point in the future is much like the 'failure' below, unrolled it looks like: main = (print =) . runInterpreter $ do setImports [Prelude] interpret 1/5 (as :: Rational) -- fails Having to remember that Rational is defined as type Rational = Ratio Integer and that Ratio is defined in Data.Ratio and then to add that to the setImports list is a little inconvenient: main = (print =) . runInterpreter $ do setImports [Prelude, Data.Ratio ] interpret 1/5 (as :: Rational) -- works But for my own purposes this is probably much saner in the long run than my newtype wrapping approach below. However, this is not always possible: supposing Ratio itself was defined as a type synonym of Ratio2, and Ratio2 was not exported. Perhaps this is what Rc43 was experiencing, but I shouldn't speculate, as this is all getting a bit theoretical - I should try out hint in the real world to see if this problem makes things impractical for me - sorry for the noise! Thanks, Claude Thanks, Daniel On Mar 31, 2012, at 6:19 PM, Claude Heiland-Allen wrote: Hi all, What's the recommended way to get hint[0] to play nice with type synonyms[1]? A problem occurs with in scope type synonyms involving types not in scope. I came up with this after looking at the source[2], but it makes me feel ill: --8-- -- hint and type synonyms don't play nice module Main where import Language.Haskell.Interpreter import Data.Typeable as T import Data.Typeable.Internal import GHC.Fingerprint.Type main = failure success test t = (print =) . runInterpreter $ do setImports [ Prelude ] interpret 1/5 t failure = test (as :: Rational) -- Left (WontCompile [GhcError {errMsg = Not in scope: type constructor or class `Ratio'}]) success = test (as :: Q) -- Right (1 % 5) newtype Q = Q Rational instance Show Q where show (Q a) = show a showsPrec n (Q a) = showsPrec n a instance Typeable Q where typeOf _ = TypeRep (Fingerprint 0 0) (T.mkTyCon Rational) [] --8-- Thanks, Claude [0] http://hackage.haskell.org/package/hint [1] http://www.haskell.org/onlinereport/decls.html#type-synonym-decls [2] http://hackage.haskell.org/packages/archive/hint/0.3.3.4/doc/html/src/Hint-Eval.html#interpret ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Unable to call function from DLL using FFI
On 14/03/12 14:01, rajendra prasad wrote: My c++ code(HelloWorld.cpp) looks like this: Try adding extern C { ... } to use the C ABI instead of a C++ ABI (which usually features symbol name mangling to add type information, among other things). (This may not solve the entire problem, but is an important puzzle-piece). __declspec(dllexport) int doubleMe(int a) { return 2*a; } Claude ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] space leak when repeatedly calling Control.Monad.State.Strict.modify
Hi, On 30/01/12 01:07, Joey Hess wrote: The attached test case quickly chews up hundreds of MB of memory. If modified to call work' instead, it runs in constant space. Somehow the value repeatedly read in from the file and stored in the state is leaking. Can anyone help me understand why? Control.Monad.State.Strict is strict in the actions, but the state itself is still lazy, so you end up building a huge thunk in the state containing all the updates that ever took place to the initial state. Using this should fix it: modify' :: MonadState s m = (s - s) - m () modify' f = do x - get put $! f x -- force the new state when storing it With the attached code, the first case (using modify) prints out a trace like: test work:1 modify work:2 modify work:3 modify work:4 modify work:5 modify work:6 modify work:7 modify work:8 modify work:9 modify work:10 modify update:vnbz update:dzgd update:hzla update:nudd update:bzfl update:muht update:hims update:jakj update:lvrt update:qdxo initial MyState {val = vnbz} Notice how the state updates are only evaluated right at the end, when the value is forced - note also that this means that all the data needs to hang around until then. The second case (using modify') forces the state as it goes along: test' work:1 modify' update:zwre initial work:2 modify' update:fefg work:3 modify' update:eoqa work:4 modify' update:xtak work:5 modify' update:tekd work:6 modify' update:qrsz work:7 modify' update:fdgj work:8 modify' update:alwj work:9 modify' update:kqsp work:10 modify' update:lazz MyState {val = lazz} Claude {-# LANGUAGE GeneralizedNewtypeDeriving #-} module Main where import Debug.Trace import System.Random import Control.Monad.State.Strict modify' :: MonadState s m = (s - s) - m () modify' f = do x - get put $! f x data MyState = MyState { val :: String } deriving Show newtype Foo a = Foo { run :: StateT MyState IO a } deriving ( Monad, MonadState MyState, MonadIO ) main :: IO () main = do print = execStateT (run test) (trace initial $ MyState ) print = execStateT (run test') (trace initial $ MyState ) test :: Foo () test = trace test $ mapM_ work [1..10]-- massive memory leak test' :: Foo () test' = trace test' $ mapM_ work' [1..10] work :: Integer - Foo () work n = trace (work:++show n) $ do v - readSomeFile trace modify modify $ trace (update:++v) (\s - s { val = v }) work' :: Integer - Foo () work' n = trace (work:++show n) $ do v - readSomeFile trace modify' modify' $ trace (update:++v) (\s - s { val = v }) readSomeFile :: Foo String readSomeFile = liftIO $ replicateM 4 (randomRIO ('a', 'z')) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] DB vs read/show for persisting large data
On 14/12/11 13:59, Marc Weber wrote: Excerpts from Michael Snoyman's message of Wed Dec 14 14:34:30 +0100 2011: On Wed, Dec 14, 2011 at 3:31 PM, C K Kashyapckkash...@gmail.com wrote: Definite *don't* use read/show: if you make any updates to your data structures, all old files will be lost. Well you can work around it: data MyDataV1 = { name :: String } deriving (Read,Show) then you make an update: data MyDataV2 = { name :: String, age : Int } deriving (Read,Show) then you can do let (v1 :: MyDataV1) = tryReadDataToMaybe data let (v2 :: MyDataV2) = tryReadDataToMaybe data let real_data = upgrade v1 `or` v2 But you already see that you start writing boilerplate code. It can be done for easy data structures .. But it soon will be a night mare if you have complex data. If you use a version control system you don't loose your data - it will just be hard to update. [snip] I ran into this very nightmare in one project, and was recommend safecopy [0] by someone on the #haskell IRC channel. I've not (yet) used it but it looks very nice! [0] http://hackage.haskell.org/package/safecopy Claude ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Ridiculously slow FFI, or cairo binding?
On 02/11/11 09:17, Eugene Kirpichov wrote: Hello, I've got two very simple programs that draw a very simple picture using cairo, doing a couple hundred thousand of cairo calls. One program is in C++. The other is in Haskell and uses the cairo library bindings. The C++ program completes in a fraction of a second, the Haskell program takes about 7-8 seconds to run. They produce exactly the same output. What could be at fault here? Why are the cairo bindings working so slow? (I suppose there isn't too much cairo-specific stuff here, perhaps it's a general FFI question?) I filed a bug report about this some months ago, having noticed similar slowness: gtk2hs ticket #1228 cairo performance is very bad http://hackage.haskell.org/trac/gtk2hs/ticket/1228 My conclusion was that it isn't FFI being slow, but some other reason, possibly too much redirection / high level fanciness in the implementation of cairo bindings that the compiler can't see through to optimize aggressively, or possibly some Double / CDouble / realToFrac rubbishness. Claude ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Cabal rebuilding all of the C++ code for wxHaskell
On 30/09/11 02:45, DukeDave wrote: 1. Is there some reason (other than 'safety') that cabal install cleans everything up? As far as I've experienced and understand it, it doesn't - it's more that GHC can detect when Haskell modules don't need recompiling while the same is not true for C or C++ sources. For example, I change one module and see GHC report only that module and its dependents being recompiled, while the other compiled modules are reused from previous 'cabal install' runs. The C-sources: are recompiled every time even if unchanged, which I too find it somewhat annoying even with my small projects. 2. Why does setting cleanHook to return () not have any effect? I think becausae the clean hook is probably not called by 'cabal install', but by 'cabal clean'. Claude ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] video for linux two (v4l2) bindings
Hi, On 05/07/11 10:19, Christopher Done wrote: [snip] /usr/bin/ld: /home/chris/.cabal/lib/bindings-posix-1.2.2/ghc-6.12.3/libHSbindings-posix-1.2.2.a(Signal.o):(.text+0x5dfb): error: undefined reference to 'pthread_kill' [snip] I guess on Claude's system it's linked to by default. So for guys trying the examples with that error I'd recommend the following commandline: cabal install v4l2-examples --ghc-option=-lpthread I think this could be a bug in bindings-posix (maintainer CC'd), I guess it could do with this in its .cabal file: Extra-libraries: pthread Can't really play about with the webcam right now but I'd had a small but cool project I've had in mind for a while that v4l is perfectly suited for, so I'll get back to you with my experiences. Thanks for releasing it, Claude. :-) Cool, look forward to seeing it! Hopefully I'll soon find time to upload to Hackage a preliminary (feature-incomplete) release of the higher-level interface that I'm working on; until then: https://gitorious.org/hsv4l2/v4l2 Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] video for linux two (v4l2) bindings
Greetings all, I uploaded 4 new packages that may be of interest: bindings-linux-videodev2 0.1 - bindings to Video For Linux Two (v4l2) kernel interfaces http://hackage.haskell.org/package/bindings-linux-videodev2-0.1 bindings-mmap 0.1 - bindings to mmap for POSIX http://hackage.haskell.org/package/bindings-mmap-0.1 bindings-libv4l2 0.1 - bindings to libv4l2 for Linux http://hackage.haskell.org/package/bindings-libv4l2-0.1 v4l2-examples 0.1 - video for linux two examples http://hackage.haskell.org/package/v4l2-examples-0.1 The first wraps the low-level structures and ioctls of the V4L2 API. The second wraps the raw mmap/munmap functions and constants (which I couldn't seem to find on hackage, only higher level wrappers which I couldn't use for various reasons). It would make more sense for this to be folded into bindings-posix at some point. The third wraps libv4l2, which provides an interface very similar to the use of V4L2 ioctls, but the library adds extra capabilities behind the scenes (I'm led to believe colour space conversion is one of them) which is hopefully useful. And the last is a crude example dumping frames from a video device (like a webcam) to stdout in YUV4MPEG2 format. The code is *very* low-level and imperative (almost a direct translation from the C example provided on the linuxtv API reference), there is plenty of room for a much nicer layer to (for example) get video data into RePa arrays or OpenGL textures. As these are raw bindings, documentation is best found upstream. Thanks to Maurício C. Antunes's package for making this possible, I recommend it: http://hackage.haskell.org/package/bindings-DSL Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Data import from octave / text file
Hi, On 15/06/11 12:13, kaffeepause73 wrote: let m = read text :: [[Double]] signalImport: Prelude.read: no parse read :: String - [[Double]] -- expects Haskell syntax try something like: parse :: String - [[Double]] -- expects plainer syntax parse = map (map read . words) . lines Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] cap 3: stopping thread 3 (stackoverflow)
Hi, On 07/06/11 14:22, Johannes Waldmann wrote: Would this work better with Data.Sequence instead of List? (Is there a really cheap way (O(1)) to split some Data.Sequence roughly in half?) I came up with this using immutable unboxed arrays, which gives a nice parallel speedup (and somehow avoids the stack overflows, I didn't work out where they were coming from unfortunately): SPARKS: 1000268 (102821 converted, 0 pruned) INIT time0.02s ( 0.02s elapsed) MUT time0.90s ( 0.46s elapsed) GCtime0.03s ( 0.03s elapsed) EXIT time0.01s ( 0.04s elapsed) Total time0.97s ( 0.53s elapsed) %GC time 3.1% (5.8% elapsed) Alloc rate586,961,335 bytes per MUT second Productivity 94.4% of total user, 173.5% of total elapsed on my dual-core laptop until around 1e6 elements when I compile with: ghc -O2 -Wall --make -threaded -rtsopts -fforce-recomp Subseqsum.hs and run with: ./Subseqsum 1e6 +RTS -N -s -M1G -A512M but after that (eg: 1e7) the GC time dominates and it slows right down. Note that I haven't tested it for correctness! So there may be bugs: 8 import Data.List (unfoldr) import Control.Parallel (par, pseq) import Data.Monoid (Monoid, mempty, mappend) import Data.Array.Unboxed (UArray, listArray, (!)) import System.Environment (getArgs) main :: IO () main = do [ nn ] - getArgs let n = read nn xs = stuff a = listArray (0, n - 1) xs print . t $ sss 0 n a stuff :: [Int] stuff = unfoldr ( \ x - seq x $ Just ( x, mod (113 * x + 558) 335 - 167 ) ) 0 data O = O { s :: ! Int, l :: !Int, r :: !Int , t :: !Int } instance Monoid O where mempty = O { s = 0, r = 0, l = 0, t = 0 } o1 `mappend` o2 = let s' = s o1 + s o2 r' = max (r o2) ( s o2 + r o1 ) l' = max (l o1) ( s o1 + l o2 ) t' = max (r o1 + l o2) $ max ( t o1 ) ( t o2 ) in O { s = s', r = r', l = l', t = t' } msingle :: Int - O msingle x = O { s = x, r = max x 0, l = max x 0, t = max x 0} sss :: Int - Int - UArray Int Int - O sss lo hi a | lo == hi = mempty | lo + 1 == hi = msingle (a ! lo) | otherwise = let mid = (lo + hi) `div` 2 x = sss lo mid a y = sss mid hi a in x `par` y `pseq` (x `mappend` y) 8 PS: I keep telling my students that structural parallel programming I don't know that term, so I might be missing the point. Sorry if so. Thanks, Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Sub-optimal [code]
On 15/02/11 20:35, Daniel Fischer wrote: Which makes me wonder: unwanted sharing of lists [1 .. n] or similar is a frequent cause of space leaks, so would it be possible to teach GHC to not share such lists (unless they're bound to a name to indicate sharing is wanted)? In particular for enumerations [a .. b] of type [Int], [Integer] or similar, I'm pretty sure that the cost of recomputation is far outweighed by the memory consumption of sharing in almost all cases. Compare with the heap profile graph output from this short program which uses a horrible data-dependency hack to force recomputation: main = do print $ length [(x,y) | x - [(1 :: Int) .. 1], y - [(1 :: Int) .. 1]] print $ length [(x,y) | x - [(1 :: Int) .. 1], y - [x+1-x .. 1]] The heap profile graph looks a little like this: ___ (Tested with ghc 6.12.3 -O2 on linux x86_64) Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] HMatrix Vector/Matrix interpolation ala Matlab interp/interp2 ??
Hi Phil, On 22/01/11 23:13, gutti wrote: - are t a b c d points or curve parameters ? a b c d are points, t is the interpolation coefficient (between 0 and 1) - how does lifting to matrix create a 1d spline to a 2d spline ? -- I don't see how it works essentially, it creates a matrix of 1d splines, but now I see that this isn't what you wanted... for interpolated 2d matrix lookup, something like this, perhaps: -- using the cubic interpolator from earlier in the thread m @@+ (x, y) = let (i, j) = (floor x, floor y) (s, t) = (x - fromIntegral i, y - fromIntegral j) cx j' = cubic s (m@@(i-1,j')) (m@@(i,j')) (m@@(i+1,j')) (m@@(i+2,j')) in cubic t (cx (j-1)) (cx j) (cx (j+1)) (cx (j+2)) test = let m = (1616) [0 ..] n = 36 r = 5 (x0, y0) = (8, 8) in [ m @@+ (x, y) | a - [0 .. n - 1] , let a' = 2 * pi * fromIntegral a / fromIntegral n , let x = x0 + r * cos a' , let y = y0 + r * sin a' ] Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] HMatrix Vector/Matrix interpolation ala Matlab interp/interp2 ??
Hi Phil, On 22/01/11 14:07, gutti wrote: Dear Haskellers, I'm looking for Vector and especially Matric interpolation ala: z = interp2 (xMatrix, yMatrix, zMatrix, x, y) - x and y can be single values or also vectors or matrices - indeally with the options nearest, linear, quadratic, qubic.. Any hope that there is something similar especially using the HMatrix matrices (Matrix Double). - If I have to code it any suggestions ? Cheers Phil I'm not sure if this is what you mean, as I'm not familiar with matlab, so apologies if this is totally a waste of time.. But I had a simple cubic interpolation implemented already, it wasn't too hard to lift it into matrices (there are many elevation permutations, I imagine), I used (fromLists ...stuff... toLists), but maybe there's a better way: {-# LANGUAGE NoMonomorphismRestriction #-} module Interpolate where import Numeric.LinearAlgebra import Data.List (zipWith5) -- cubic interpolation cubic t a b c d = let a1 = 0.5 * (c - a) a2 = a - 2.5 * b + 2.0 * c - 0.5 * d a3 = 0.5 * (d - a) + 1.5 * (b - c) in ((a3 * t + a2) * t + a1) * t + b -- boring manual lifting liftMatrix5 f a b c d e = let la = toLists a lb = toLists b lc = toLists c ld = toLists d le = toLists e in fromLists (zipWith5 (zipWith5 f) la lb lc ld le) -- test mt = (33) [0, 0.1 ..] ma = (33) [0, 1 ..] mb = (33) [0, 2 ..] mc = (33) [0, 3 ..] md = (33) [0, 4 ..] test = liftMatrix5 cubic mt ma mb mc md {- test output (33) [0.0, 2.1, 4.4 ,6.9, 9.6, 12.5 , 15.601, 18.9, 22.4 ] -} -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] implementing RealFloat decodeFloat
Hi everyone, I've been working on [0] Haskell bindings for [1] libqd for [2] double-double and quad-double arithmetic, and have been struggling to implement [3] RealFloat, in particular [4] decodeFloat, mostly because of its postcondition but also some issues relating to variable precision: ---8--- If decodeFloat x yields (m,n), then x is equal in value to m*b^^n, where b is the floating-point radix, and furthermore, either m and n are both zero or else b^(d-1) = m b^d, where d is the value of floatDigits x. In particular, decodeFloat 0 = (0,0). ---8--- (BTW: that should probably really be ... = abs m ...; perhaps this code could be added to the report and/or documentation, if it is correct: validDecode :: RealFloat f = f - Bool validDecode f = case decodeFloat f of (0,0) - True (m, e) - b^(d-1) = abs m abs m b^d where b = floatRadix f d = floatDigits f ) The double-double and quad-double types do not have a fixed precision, though they do have a fixed minimum precision: this means that decodeFloat will (in general) lose some precision. This is because for example in: data DoubleDouble = DoubleDouble !CDouble !CDouble dd = DoubleDouble a b the semantics are that dd = a + b, with |a||b|; coupled with the IEEE implicit leading 1 bit, this means that there may be large gaps between exponents: for example: 1 + 0.5**100 :: DoubleDouble. So far I've got a mostly working implementation thus: decodeFloat !(DoubleDouble a b) = case (decodeFloat a, decodeFloat b) of ((0, 0), (0, 0)) - (0, 0) ((0, 0), (m, e)) - (m `shiftL` f, e - f) ((m, e), (0, 0)) - (m `shiftL` f, e - f) ((m1, e1), (m2, e2)) - let fixup m e = if m mMin then fixup (m `shiftL` 1) (e - 1) else if m = mMax then fixup (m `shiftR` 1) (e + 1) else (m, e) mMin = 1 `shiftL` (ff - 1) mMax = 1 `shiftL` ff ff = floatDigits (0 :: DoubleDouble) g = e1 - e2 - f in fixup ((m1 `shiftL` f) + (m2 `shiftR` g)) (e1 - f) where f = floatDigits (0 :: CDouble) This does meet the postcondition as specified (which leads to breakage in other RealFloat methods), but has a recursion with no termination proof (so far), and is lossy in general: let check f = uncurry encodeFloat (decodeFloat f) == f check (1 + 0.5 ** 120 :: DoubleDouble) False It does however seem to meet a weaker condition: let check2 f = (decodeFloat . (`asTypeOf` f) . uncurry encodeFloat . decodeFloat $ f) == decodeFloat f check2 (1 + 0.5 ** 120 :: DoubleDouble) True Questions: 1. Is this weaker condition likely to be good enough in practice? 2. Can my implementation of decodeFloat be simplified? Thanks for any insights, Claude [0] http://hackage.haskell.org/package/qd [1] http://crd.lbl.gov/~dhbailey/mpdist/ [2] http://crd.lbl.gov/~dhbailey/dhbpapers/arith15.pdf [3] http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#t:RealFloat [4] http://hackage.haskell.org/packages/archive/base/latest/doc/html/Prelude.html#v:decodeFloat -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] concurrency vs. I/O in GHC
On 23/10/10 23:17, Donn Cave wrote: Quoth Claude Heiland-Allenclaudiusmaxi...@goto10.org, ... The conclusion I drew was that unsafe foreign functions block the current capability (OS thread) and any threads (Haskell forkIO etc) currently scheduled on that capability, but other capabilities and threads continue executing as normal. ... until GC time when all capabilities must be ready. (?) If a trivial test program would help, here I call the sleep() function, which I believe on a POSIX platform suspends the thread until receipt of a SIGALRM. I wrote a program which shows some interesting behaviour: 8 {-# LANGUAGE ForeignFunctionInterface #-} module Main (main) where import GHC.Conc (forkOnIO, numCapabilities) import Control.Concurrent (threadDelay) import Foreign.C (CInt) import System.Environment (getArgs) foreign import ccall unsafe sleep sleep :: CInt - IO CInt delayer :: Int - IO () delayer n = do print (delayer, n) threadDelay 10 -- 10Hz delayer n sleeper :: Int - IO () sleeper n = do print (sleeper, n) _ - sleep 1 -- 1Hz sleeper n main :: IO () main = do m - (read . head) `fmap` getArgs mapM_ (\n - forkOnIO n $ delayer n) [1 .. numCapabilities] mapM_ (\n - forkOnIO n $ sleeper n) [1 .. numCapabilities - m] threadDelay 1 -- 100s 8 $ ghc --version The Glorious Glasgow Haskell Compilation System, version 6.12.3 $ uname -a Linux zebimus 2.6.32-25-generic #44-Ubuntu SMP Fri Sep 17 20:05:27 UTC 2010 x86_64 GNU/Linux $ ghc -O2 -Wall -threaded --make DelayedSleep.hs $ ./DelayedSleep +RTS -N4 -S -RTS 3 [snip] 8 By interesting I mean there is lots of output from the delayer threads on capabilities without sleeper threads (as you would expect), with the delayer threads on capabilities also having sleeper threads being much less frequent (as you might also expect). But then there are some long pauses where there is no output from any thread: my hypothesis is that the whole runtime is blocked waiting for all threads to be ready for GC (because +RTS -S shows some GC stats after the end of those pauses). Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Error Calling Lua Function
Hi Aditya, The problem is not that the file was not loaded, but that in Lua, loading a file only loads it and does not execute it; Lua is a dynamic language, by which I mean that definitions are created through execution. Attached is a simple example, note that there is no proper error checking - you probably want to check the results rather than pattern matching against constants. Thanks, Claude On 23/10/10 03:21, aditya siram wrote: Hi all, I'm having some issues calling Lua functions from Haskell. I have the following in Haskell2Lua.lua: function hello () return hello world end And my Haskell file Haskell2Lua.hs looks like this: import qualified Scripting.Lua as Lua main = do l- Lua.newstate Lua.openlibs l succ- Lua.loadfile l /Haskell2Lua.lua print succ Lua.callproc l hello Lua.close l When I compile and run this file I get: 0 Haskell2Lua: user error (attempt to call a nil value) I have tried some variations on calling the function including: Lua.callproc l hello Lua.callfunc l hello Lua.callfunc l hello I know the Lua bridge is working because the following works: Lua.callproc l print Hello from Lua Any help is appreciated! -deech -- http://claudiusmaximus.goto10.org function hello () return hello world end import qualified Scripting.Lua as L main :: IO () main = do -- create a new Lua interpreter with libraries l - L.newstate L.openlibs l -- load a Lua source code file 0 - L.loadfile l test.lua -- execute the loaded file 0 - L.pcall l 0 0 0 -- call a function defined when the loaded file was executed r - L.callfunc l hello -- print the result of the function call putStrLn r -- destroy the Lua interpreter L.close l ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] concurrency vs. I/O in GHC
On 23/10/10 17:42, Gregory Crosswhite wrote: On 10/23/10 7:54 AM, John Lato wrote: On Fri, Oct 22, 2010 at 6:16 PM, Bulat Ziganshin bulat.zigans...@gmail.com mailto:bulat.zigans...@gmail.com wrote: Hello John, Monday, October 18, 2010, 8:15:42 PM, you wrote: If anyone is listening, I would very much like for there to be a mechanism by which external functions can be called unsafe-ly, but without blocking all other Haskell threads. I have code that does this: +RTS -N2 This doesn't work, which was why the OP asked in the first place. When a thread calls an unsafe foreign function, it blocks everything until that function returns. Is that true? The last time we discussed this in Haskell Cafe the conclusion I drew from the discussion was that unsafe foreign functions block the current thread but not any other thread. The conclusion I drew was that unsafe foreign functions block the current capability (OS thread) and any threads (Haskell forkIO etc) currently scheduled on that capability, but other capabilities and threads continue executing as normal. Thanks, Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] back doors into the IO monad
On 23/10/10 23:28, Manlio Perillo wrote: Hi. What are the available methods to execute IO actions from pure code? I know only unsafePerformIO and foreign import (to call a non pure foreign function). Assuming I want to execute external untrusted code using plugins (via the `plugins` package), is it possible to completely forbid execution of IO actions from user code? Even pure code can cause denial of service through huge memory consumption or infinite loop. Perhaps it would be wise to look at how 'lambdabot'/'mueval' work, using OS-level security limits to restrict amount of memory and time that can be used by untrusted code (which already has had its IO priviledges revoked, see their source codes for details). Thanks Manlio Claude ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Maybe to Either -- is there a better way?
On 02/08/10 15:14, Tom Davies wrote: I find it convenient sometimes to convert a Maybe value to an Either thus (excuse the syntax, it's CAL, not Haskell): maybeToEither :: a - Maybe b - Either a b; maybeToEither errorValue = maybe (Left errorValue) (\x - Right x); but that seemingly obvious function isn't in Hoogle, AFAICT, so perhaps there's some other approach? I just uploaded djinn-th [1], a fork of Lennart Augustsson's djinn [2] which uses TemplateHaskell to do things like: {-# LANGUAGE TemplateHaskell, ScopedTypeVariables #-} import Language.Haskell.Djinn (djinnD) $(djinnD maybeToEither [t|forall a b . a - Maybe b - Either a b|]) main = print . map (maybeToEither foo) $ [Nothing, Just bar] and get some results, if not always the one you intended. [1] http://hackage.haskell.org/package/djinn-th [2] http://hackage.haskell.org/package/djinn Thanks, Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] minimizing jitter while maximizing throughput
Greeting, Jitter vs Throughput Scenario I have the following scenario: CPU with [C] cores concurrent program the 1 main thread uses OpenGL for animated visual output [W] worker threads uses FFI to lengthy numerical computations with the following desires : (J) minimize jitter: the 1 main thread needs to be responsive (T) maximize throughput: idle CPU time is a waste of time The problem is, I want both (J) and (T)! Benchmarks -- Some rough benchmarks from my 'production' implementation[1] where: jitter = stddev of actual period (target period = 40ms) idle = C - (real time) / (user+sys time)(cores used elsewhere) (but for example Xorg will use some time for the OpenGL output, etc) C W jitter idle 2 0 1.5ms 168% threaded RTS 2 1 1.9ms 65% 2 2 9.4ms 46% 2 3 13.3ms 37% For comparison, a (very) minimal GLUT program gives: 0.3ms threaded RTS 0.5ms non-threaded RTS Picking (J) over (T), then setting W=1 when C=2 gives best jitter Picking (T) over (J), then setting W=2 when C=2 gives best throughput Question What is the best way to re-structure the program to have both low jitter and high throughput? Options I am considering: 1. worker threads estimate the time needed to complete each job, and don't start a job if it is likely to break the deadline (bonus points if just those worker Haskell threads running on the same OS thread as the main Haskell thread need to pause) 2. change the foreign code to break jobs into smaller pieces, for example perhaps something like: worker :: IO (IO Bool) - IO () worker getJob = forever $ do job - getJob whileM_ job yield -- [2] instead of worker :: IO (IO ()) - IO () worker = forever . join 3. re-implement the foreign code in Haskell instead of C and hope that GHC makes the Haskell go as fast as GCC makes the C go 5. wait (for a long time) for a new RTS with: full pre-emption (including interrupting/resuming foreign code) user settable thread priorities (1) is a fun challenge (there may be PhDs awarded for less, I imagine) (2) isn't quite trivial, some scope for tuning subjob chunk size (3) is boring translation but could lead to interesting benchmarks even (especially?) if it fails to be as fast as C Which would you pick? Links - [1] http://hackage.haskell.org/package/mandulia [2] http://hackage.haskell.org/packages/archive/monad-loops/latest/doc/html/Control-Monad-Loops.html#v:whileM_ Thanks for any insight and advice, Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] dear traversable
On 31/07/10 12:13, wren ng thornton wrote: Stephen Tetley wrote: wren ng thornton wrote: Ben wrote: unzipMap :: M.Map a (b, c) - (M.Map a b, M.Map a c) unzipMap m = (M.map fst m, M.map snd m) I don't think you can give a more efficient implementation using the public interface of Data.Map. You need to have a sort of mapping function that allows you to thread them together, either via continuations or via a primitive: Unless I'm missing something. This one has one traversal... unzipMap :: Ord a = M.Map a (b, c) - (M.Map a b, M.Map a c) unzipMap = M.foldrWithKey fn (M.empty,M.empty) where fn k a (m1,m2) = (M.insert k (fst a) m1, M.insert k (snd a) m2) Well, that's one traversal of the original map, but you have to traverse the new maps repeatedly with all those M.insert calls. And since Data.Map is a balanced tree, that could lead to a whole lot of work rebalancing things. However, because we are not altering the set of keys, we are guaranteed that the structure of both new maps will be identical to the structure of the old map. Therefore, with the right primitives, we can keep one finger in each of the three maps and traverse them all in parallel without re-traversing any part of the spine. (The Either and Or variants will have some retraversal as the smart constructors prune out the spine leading to deleted keys. But this is, arguably, necessary.) Why not something like this (with the correctness proof as an exercise): \begin{code} import Data.Map (Map) import qualified Data.Map as M unzipMap :: Map a (b, c) - (Map a b, Map a c) unzipMap m = let (ab, ac) = unzip . map fiddle . M.toAscList $ m in (M.fromDistinctAscList ab, M.fromDistinctAscList ac) where fiddle :: (x, (y, z)) - ((x, y), (x, z)) fiddle (x, (y, z)) = ((x, y), (x, z)) \end{code} (and I now see after writing this that Henning Thielemann wrote much the same some hours ago, however there are some slight differences so I'm sending this anyway) Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] dear traversable
On 31/07/10 13:49, Stephen Tetley wrote: Although I haven't calculated the Big-O scores suspect that original post will actually be the best, the solutions that metamorph into a list and out again look like they're doing needless extra work. They're both O(size m) time, and yes the original is best (not least for its simplicity and elegance); I now think that (on my part) it was a case of following optimisation strategies without thinking hard enough whether they apply: ie, traversing only once can be beneficial for space reasons under certain circumstances [1] But as Data.Map is spine-strict, there is no space saving here by traversing only once, as the spine is already there taking up O(size m) space before we even start traversing (whereas with lazy lists the spine might not be taking up any space yet). [1] to give a classic example: mean :: Fractional a = [a] - a mean xs = sum xs / genericLength xs which often consumes O(length xs) space, reducible to O(1) if only one traversal is performed. Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] A question about State Monad and Monad in general
Hi, On 16/07/10 07:35, C K Kashyap wrote: Haskell without using any standard library stuff? For example, if I wanted an image representation such as this [[(Int,Int.Int)]] - basically a list of lists of 3 tuples (rgb) and wanted to do in place replacement to set the pixel values, how could I go about it. Break the problem down into parts: 1. replace a single pixel 2. modify an element in a list at a given index using a given modification function 3. modify an element in a list of lists at a pair of given indices using a given replacement function I had a stab at it. Without any standard library stuff I couldn't figure out how to print any output, though - so who knows if the code I wrote does what I intended. The point is, it's libraries all the way down - so use them, study them where necessary for understanding, and write them and share them when you find something missing. Claude -- http://claudiusmaximus.goto10.org {-# LANGUAGE NoImplicitPrelude #-} module CKKashyap where -- numbers are in the standard library data Nat = Zero | Succ Nat (+) :: Nat - Nat - Nat Zero + y = y (Succ x) + y = x + Succ y zero = Zero one = Succ zero two = one + one four = two + two eight = four + four sixteen = eight + eight thirtytwo = sixteen + sixteen sixtyfour = thirtytwo + thirtytwo onehundredandtwentyeight = sixtyfour + sixtyfour twohundredandfiftysix = onehundredandtwentyeight + onehundredandtwentyeight Succ twohundredandfiftyfive = twohundredandfiftysix -- booleans are in the standard library data Bool = False | True gt :: Nat - Nat - Bool Zero `gt` x = False Succ x `gt` Zero = True Succ x `gt` Succ y = x `gt` y eq :: Nat - Nat - Bool Zero `eq` Zero = True Succ x `eq` Succ y = x `eq` y x `eq` y = False -- lists are in the standard library data List a = Nil | Cons a (List a) map :: (a - b) - List a - List b map f Nil = Nil map f (Cons x xs) = Cons (f x) (map f xs) repeat :: a - List a repeat x = Cons x (repeat x) take :: Nat - List a - List a take Zero ys = Nil take (Succ x) Nil = Nil take (Succ x) (Cons y ys) = Cons y (take x ys) foldr :: (a - b - b) - b - List a - b foldr op e Nil = e foldr op e (Cons x xs) = op x (foldr op e xs) nats :: List Nat nats = Cons Zero (map Succ nats) -- tuples are in the standard library data Tuple2 a b = Tuple2 a b data Tuple3 a b c = Tuple3 a b c zip :: List a - List b - List (Tuple2 a b) zip Nil _ = Nil zip _ Nil = Nil zip (Cons a as) (Cons b bs) = Cons (Tuple2 a b) (zip as bs) -- modify list elements modifyAtIndex :: (a - a) - Nat - List a - List a modifyAtIndex f i as = let ias = zip nats as g (Tuple2 j a) = case i `eq` j of False - a True - f a in map g ias modifyAtIndex2 :: (a - a) - Nat - Nat - List (List a) - List (List a) modifyAtIndex2 f i j = modifyAtIndex (modifyAtIndex f i) j -- images as lists of lists of tuple3 of nat type Pixel = Tuple3 Nat Nat Nat black :: Pixel black = Tuple3 Zero Zero Zero white :: Pixel white = Tuple3 twohundredandfiftyfive twohundredandfiftyfive twohundredandfiftyfive type Scanline = List Pixel type Image = List Scanline blank :: Image blank = take thirtytwo (repeat (take thirtytwo (repeat black))) plotPixel :: Nat - Nat - Image - Image plotPixel x y img = modifyAtIndex2 (\_ - white) x y img diagonal :: Image diagonal = let is = take thirtytwo nats ijs = map (\i - Tuple2 i i) is in foldr (\(Tuple2 i j) img - plotPixel i j img) blank ijs ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] How to get a list of constructors for a type?
Gregory Propf wrote: Say I have something like data DT a = Foo a | Bar a | Boo a I want something like a list of the constructors of DT, perhaps as [TypeRep]. I'm using Data.Typeable but can't seem to find what I need in there. Everything there operates over constructors, not types. The approach below won't work in general because the constructors may have different (argument) types, but for the above: [Foo, Bar, Boo] :: [a - DT a] And you could manually build a list of constructor types, as a try: [typeOf Nothing, typeOf Just] :: [TypeRep] which doesn't work, due to polymorphic ambiguity, so you'd have to: [typeOf (Nothing :: Maybe Int), typeOf (Just :: Int - Maybe Int)] In general, maybe Data.Dynamic is what you need, but I don't think that can handle polymorphic values yet either. What do you want to do with the list of constructors once you have them? Where are they defined? Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] river crossing puzzle
pat browne wrote: Hi, Does anyone know where there are any Haskell implementations of the the River Crossing puzzle (AKA Farmer/Fox/Goose/Grain). I wrote some code to generate a map of some version of the game: https://code.goto10.org/svn/maximus/2009/boatman/BoatMan.hs ghc -O2 --make BoatMan.hs ./BoatMan | neato -Tpng | display Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Parallel graphics
Andrew Coppin wrote: (OK, well the *best* way is to use the GPU. But AFAIK that's still a theoretical research project, so we'll leave that for now.) Works for me :-) http://claudiusmaximus.goto10.org/cm/2009-09-24_fl4m6e_in_haskell.html There doesn't need to be a sound theoretical foundation for everything, sometimes sufficient ugly hackery will make pretty pretty pictures... Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] graphical user interface library for editing graphs?
Greetings, I have an idea for a project. The eventual aim is rather eccentric, but the specifications I have sketched out are roughly as follows (best viewed with fixed-width font due to diagrams): 0. the graphical user interface is entirely mouse driven, mouse position/clicks/releases are shown by [ ] 1. the world consists of empty space, nodes, and links between nodes 2. nodes may either be filled (*) or hollow (o); call hollow nodes targets, targets may be highlighted (O) 3a. left-click on empty creates a filled node linked to a target: [ ]* = | o 3b. left-click on a target fills it and links it to a new target: : : [o]* = | o 4a. right-click on empty creates a filled node linked to two targets: [ ]* = / \ o o 4b. right-click on a target fills it and links it to two new targets: : : [o]* = / \ o o 5a. left-click-drag on a filled node with 1 descending link highlights all targets linked under it: : : [*] [*] : =: * * / \ / \ o o O O 5b. left-click-drag as in 5a, when released on a highlighted target, creates a weak link (not considered as descending for purposes of 5a) from the original node to that target. that target is filled, all other targets are unhighlighted: : : * * : =:\ * * \ / \ / \| O [O]o [*] 6a. right-click-drag from a filled node at the top of a connected component highlights all targets not in that connected component: [*] * [*] * : | = : | o O 6b. right-click-drag as in 6a, when released on a highlighted target, joins the graphs together, such that the original node fills that target, and all other targets are unhighlighted: * * * : | = | [O] [*] : 7. when a connected component has no targets, double-clicking anywhere on it will launchMissiles My question: can you suggest a library that would make implementing this specification relatively painless? OpenGL-based would be preferable, as I would like to scale the graph under construction automatically to fit the display, and launchMissiles will require something with fast rendering. Thanks, Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] When folding is there a way to pick out the last point being processed?
Casey Hawthorne wrote: When folding is there a way to pick out the last point being processed? I came up with these: safeHead = foldr (const . Just) Nothing safeLast = foldr (flip mplus . Just) Nothing Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] createProcess problem
Hi Vasili, Vasili I. Galchin wrote: [snip] import System.Process main = do handle - runProcess blastall -- executable [-p blastn] -- CLI args Try: [-p, blastn] This passes multiple command line arguments instead of just one that contains a space. Most shells use spaces to separate arguments. [snip] Hope this helps, Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Type question in instance of a class
Maurício wrote: Hi, Why is this wrong? class MyClass r where function :: r - s data MyData u = MyData u instance MyClass (MyData v) where function (MyData a) = a GHC says that the type of the result of 'function' is both determined by the rigid type from MyClass and the rigid type from MyData. But why can't both be the same? particular instances can't add extra restrictions (eg: both types are the same) to the interface declared by the class (eg: both types are arbitrary). Compare this version: 8 {-# LANGUAGE MultiParamTypeClasses, FlexibleInstances #-} module Test where class MyClass r s where function :: r - s data MyData u = MyData u instance MyClass (MyData v) v where function (MyData a) = a 8 And ghci session: 8 *Test function (MyData hello) interactive:1:0: No instance for (MyClass (MyData [Char]) s) arising from a use of `function' at interactive:1:0-24 Possible fix: add an instance declaration for (MyClass (MyData [Char]) s) In the expression: function (MyData hello) In the definition of `it': it = function (MyData hello) *Test :t function (MyData hello) function (MyData hello) :: (MyClass (MyData [Char]) s) = s *Test function (MyData hello) :: String hello 8 I don't know how evil those language extensions are, though - I just fiddled until it worked... What am I doing wrong? Claude -- http://claudiusmaximus.goto10.org/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Dealing with CStringLen
Maurício wrote: Hi, How is CStringLen supposed to be used? The only usual C string I'm familiar with is the null terminated sequence of chars. I remember some Pascal version where the first 2 bytes were used as an integer counting the following characters. What is the convention used in Foreign.C.String? the docs (via Hoogle) tell me: withCStringLen :: String - (CStringLen - IO a) - IO a type CStringLen = (Ptr CChar, Int) I have a C function signature like this: void function (int,const void*,int,const void*) where each (int,const void*) pair is a string. Can (should) I use CStringLen here? How? seems an approprate use case for withCStringLen, yes, as long as you take care with character encodings: The translation between Unicode and the encoding of the current locale may be lossy. So for a quick hack withCStringLen is probably the quickest solution, otherwise something that takes into account Unicode and locales etc would be preferable (I think there are some UTF string packages around, maybe on hackage?). for withCStringLen something like this might work (completely untested!): foreign import ccall function.h function function_c :: CInt - Ptr CChar - CInt - Ptr CChar - IO () function :: String - String - IO () function s t = withCStringLen s (\(sp,sl) - withCStringLen t (\(tp,tl) - function_c (toEnum sl) sp (toEnum tl) tp ) ) -- where toEnum :: Int - CInt -- perhaps castPtr is useful depending on import declaration too... Hopefully this is in the right direction, Thanks, Maurício Claude ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Looking for a more functional way to do this
Jefferson Heard wrote: Adrian, my understanding is that it's not that simple, because I need to preserve the state between calls to GLUT's callbacks, which all return IO (). Then maybe see: http://www.haskell.org/pipermail/haskell-cafe/2007-July/028501.html 2008/8/6 Adrian Neumann [EMAIL PROTECTED]: There is the State Monad which is build just for that kind of purpose, I believe: http://www.haskell.org/all_about_monads/html/statemonad.html That would safe you from passing around the State Jefferson Heard schrieb: Working with HOpenGL and GLUT, [snip] Claude ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] parsec linking problem
naruto canada wrote: I run into linking error with parsec: ghc -o /tmp/a.out accu.hs Try ghc --make, or pass appropriate package flags on the command line so that it gets linked with Parsec. Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] example of FFI FunPtr
Galchin, Vasili wrote: Hello, I want to model a Haskell function that is a callback from C. I have only found one example in the unix package's Semaphore.hsc, which apparently is not used. I want to be able to marshall a Haskell function that is a first class citizen residing in a Haskell data type and pass to a C function via FFI. Are there examples of this? Attached is a simple example. The main thing to note is 'foreign import ccall wrapper' which gives you a factory for turning Haskell functions into foreign function pointers. More information: http://www.cse.unsw.edu.au/~chak/haskell/ffi/ Claude -- http://claudiusmaximus.goto10.org module Main(main) where import Foreign.C.Types(CDouble) import Foreign.Ptr(FunPtr, freeHaskellFunPtr) foreign import ccall wrapper wrap :: (CDouble - CDouble) - IO (FunPtr (CDouble - CDouble)) foreign import ccall callerback.h twice twice :: FunPtr (CDouble - CDouble) - CDouble - IO CDouble square :: CDouble - CDouble square x = x * x main :: IO () main = do squareW - wrap square let x = 4 y - twice squareW x z - twice squareW y print y print z freeHaskellFunPtr squareW #include callerback.h double twice(d2d f, double x) { return f(f(x)); } #ifndef CALLERBACK_H #define CALLERBACK_H typedef double (d2d)(double); double twice(d2d f, double x); #endif CallBacker: CallBacker.hs callerback.c callerback.h ghc -O2 -Wall -fffi -o CallBacker CallBacker.hs callerback.c ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Random numbers / monads - beginner question
Madoc wrote: Given a list of numbers, I want to modify each of those numbers by adding a random offset. However, each such modified number shall stay within certain bounds, given by the integers minValue and maxValue. After that, I want to continue computation with the resulting list of type [Int]. Personally, I'd do something like this, isolate the IO code outside the algorithm to keep the algorithm pure: modify' :: Int - Int - Int modify' offset a = normalize (a + offset) generateInfiniteListOfRandomNumbers :: IO [Int] -- implementation left as an exercise main = do randomNumbers - generateInfiniteListOfRandomNumbers print $ zipWith modify' randomNumbers [0, 200, 400, 600, 800, 1000] hope this helps, Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] sound synthesis
Thomas Girod wrote: Hi there. Following this advice (http://reddit.com/info/6hknz/comments/c03vdc7), I'm posting here. Recently, I read a few articles about Haskell (and FP in general) and music/sound. I remember an article ranting about how lazy evaluation would be great to do signal processing, but it was lacking real world example. I tried to do a little something about it, even though I'm still an haskell apprentice. So, here I come with a small bit of code, waiting for your insights to improve it. The task is to generate a sine wave and pipe it to /dev/dsp on my linux box. There is probably a nicer way to make some noise, like using SDL audio API bindings, but I didn't take the time to poke around this yet. So here it is : module Main where import qualified Data.ByteString as B import Data.Word import IO (stdout) rate = 44100 sinusFloat :: [Float] sinusFloat = map (\t - (1 + sin (t*880*2*pi/rate)) / 2) [0..44099] sinusWord :: [Word8] sinusWord = map (\s - floor (s * max)) sinusFloat where max = 255 byte = B.pack sinusWord main = B.hPut stdout byte It is supposed to generate a 880hz sine wav playing for one second, by typing ./bin /dev/dsp, assuming your soundcard has a 44100hz samplingrate. /dev/dsp is supposed to receive its audio flux as an unsigned byte stream, that's why I'm converting my sine from [-1;1] to [0;1] and then to [0;255] Word8. However, I must miss something because the sound does not have the right frequency and is played too long. I guess the default sound format is 44100hz 16bits stereo, which would explain why it doesn't behave as expected. Nope: The default is 8-bit unsigned samples, using one channel (mono), and an 8 kHz sampling rate. http://www.oreilly.de/catalog/multilinux/excerpt/ch14-05.htm Changing to rate = 8000 and sinusFloat = ... [0..rate-1] gives the expected output. I'm wondering how I could convert a [Word16] to ByteString, and how I could use lazy evaluation to generate an infinite sine that stops with the interupt. Thomas Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] elementary Maybe Monad problem .. sigh
Galchin, Vasili wrote: data Bozo = Bozo { id :: Int } bonzo :: Maybe Bozo - IO () bonzo maybe_bozo = do if maybe_bozo == (Just (Bozo x)) then return () else return () ~ I want x to be a pattern that matches id ?? Try: bonzo (Just (Bozo x)) = return () bonzo Nothing = return () Kind regards, Vasili Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Beginners arrow question
Paul Johnson wrote: I'm using arrows for the first time, with HXT. I think I'm getting the hang of it, but one problem has me stumped. I have a function lookupFormatter which takes a string and returns an arrow, and I want to use that arrow. Something like this: myProblem :: (ArrowXml a) - a XmlTree String myProblem = proc xml do name - getAttrValue name - xml fmt - arr lookupFormatter - name fmt - xml But I can't figure out how to do it. I get fmt: not in scope. ArrowApply is one way, if there is an instance for it: module Main where import Control.Arrow adder :: (Arrow a) = Int - a Int Int adder i = arr (i+) adderUser :: (ArrowApply a, Arrow a) = a Int Int adderUser = proc x - do a - arr adder - x y - app - (a,x) returnA - y main = mapM_ (print . (adderUser :: (-) Int Int)) [1..10] Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Doing without IORef
Jinwoo Lee wrote: Hi, Recently I wrote a code that uses readline library (System.Console.Readline). I had to maintain a state (file path) and do IO throughout the code, so I decided to use StateT monad. The problem was that in order to retrieve the current state (file path) inside the handler that had been registered by using bindKey function of readline, I had to resort back to using IORef rather than using the state stored in the StateT monad. It's because the handler for bindKey should have the type of Int - Char - IO Int. [snip] Is there any way in which I can do without IORef in tabHandler and commandLoop (written in red and bold, if you can see)? Probably not, but this is worth a read if you want to hide the plumbing behind the scenes a bit: Monadic tunnelling: the art of threading one monad through another http://www.haskell.org/pipermail/haskell-cafe/2007-July/028501.html Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] compilation succeeds -- execution fails
Jason Dusek wrote: Stefan O'Rear [EMAIL PROTECTED] wrote: The only type that you are allowed to assume corresponds to a C int is CInt, in the Foreign.C.Types module. This probably isn't the problem, but it could make problems of its own on a 64-bit or otherwise weird system. So say I turn everything back to pointers to CInt, what is the accepted way to convert from CInt to Int and CInt to Char? Type class methods: -- for numbers like Int, CInt fromIntegral :: (Num b, Integral a) = a - b -- for Char fromEnum :: (Enum a) = a - Int toEnum :: (Enum a) = Int - a Is relying on the fact that CInt always wraps a Haskell integer an okay way to go? I don't know what you mean by this. I might was well learn these things now, before I get into bad habits. Hope this helps, Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Recursion problem in infinite list model
Hans Aberg wrote: When experimenting with list index sets (i.e. lists more general than provided by Haskell), I arrived at the problem that in the example code below for example h(list 6) does not (in Hugs) write out the beginning of the list lazily. It does work for list 6 first (list 6) rest (list 6) 10 -+ (list 3) and so on. So it seems to be a problem with h, also suggested by heap profiling, perhaps similar to that of foldl. So is it possible to fix it? The function h x is derived by unwinding the monadic construct corresponding to (for lists) do {i - [x..]; return i} So there are not many possibilities of change. Below, (-+) corresponds, for lists, to :, first to head, and rest to tail. Hans Aberg data List a = List(Integer-a) instance Show a = Show (List a) where show (List f) = [ ++ show (f(0)) ++ concat [, ++ show (f(toInteger i))| i-[1..]] ++ ] list :: Integer - List Integer list x = List(\z - x+z) (-+) :: a - List a - List a x -+ (List y) = List(f) where f 0 = x f k = y(k-1) first :: List a - a first (List f) = f 0 rest :: List a - List a rest (List y) = List(f) where f k = y(k+1) h :: List a - List a h x = (-+) (first x) (h (rest x)) The combination of (-+) and h is too strict, this modification works: (-+) :: a - List a - List a x -+ ~(List y) = List(f) where -- lazy pattern match f 0 = x f k = y(k-1) Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] separate input calculation output
Thomas Engel wrote: inputvalues :: IO Input -- add a return (ve,de,...) to the end compute :: Input - Output How can I combine several function (calculations) in this function? compute1 :: Input - Output1 compute2 :: Input - Output2 combine :: Output1 - Output2 - Output compute input = combine c1 c2 where c1 = compute1 input c2 = compute2 input etc Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] more on FFI build error
Galchin Vasili wrote: line #102 ... allocaBytes (#const sizeof(struct mq_attr)) $ \ p_attrs - do definition of struct mq_attr on Linux ... struct mq_attr { long int mq_flags;/* Message queue flags. */ long int mq_maxmsg; /* Maximum number of messages. */ long int mq_msgsize; /* Maximum message size. */ long int mq_curmsgs; /* Number of messages currently queued. */ long int __pad[4]; }; Did you #include the header file that defines this struct? Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Re: Exporting Haskell Libraries to C Programmers
Bruce, Joseph R (Joe) wrote: [snip] I hadn't looked at CABAL before. It's a very useful tool and met all my library-forming needs. That's only half my problem though. I'm trying to make the use of this Haskell code as transparent as possible to the C programmers on the team. Telling them about the Haskell base libraries dependency or distributing those libraries with my code is not too bad, but the list of -u flags that the linker needs is brutal. Is there a way to avoid it, or embed it in the package? Ex (using ghc-6.8.2 -v): ... gcc -v -o primes Primes.o ./Primes_stub.o hs_primes_wrapper.o cprimes.c [snip] Maybe try compiling cprimes.c to cprimes.o with gcc, then link with GHC? Something like: gcc -o cprimes.o -c cprimes.c ghc -no-hs-main --make -c *.hs ghc -no-hs-main -package foo -o moo *.o I know this isn't ideal, but it's similar to what I used to build a .so from Haskell code that can be loaded by a C app ignorant of Haskell. Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Implicit parameters and Arrows/Yampa?
ChrisK wrote: Could I has one question? What is the purpose of the stream function in the ArrowLoop instance? Is it just to catch an unexpected [] at runtime? instance ArrowLoop SF where loop (SF f) = SF $ \as - let (bs,cs) = unzip (f (zip as (stream cs))) in bs where stream ~(x:xs) = x:stream xs It looks like stream is (almost) an identity which would crash at runtime if it encountered a []. In particular it is equivalent to where stream xs = head xs:stream (tail xs) Not quite that, from a blog post of mine a while back: [snip] But then the trouble arrived - I suddenly noticed that the paper was using infinite sequences, not the finite sequences I was using myself. Trying to implement an ArrowLoop as per the paper led to horrible grief: -- instance ArrowLoop SF where -- loop (SF f) = SF (loop (unzip . f . uncurry zip)) The problem is that this is far too strict - on non-empty input it caused stack overflow crashes, which isn't exactly what I wanted. I found the solution in Programming With Arrows [1] (page 17), which involves some subtlety with lazy patterns: instance ArrowLoop SF where loop (SF f) = SF $ \as - let (bs,cs) = unzip (f (zip as (stream cs))) in bs where stream ~(x:xs) = x:stream xs [unsnip] http://www.haskell.org/arrows/biblio.html#Hug05 [1] Hope this helps, Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Class/Instance : what am I doing wrong in this example ?
david48 wrote: | I'm really inexperienced at this : class Gadget g where fInit :: g - a - g data FString = FString !Int !String deriving Show instance Gadget FString where fInit (FString n _) s = FString n (take n s) The types of: fInit :: g - a - g and: take :: Int - [a] - [a] cause the mismatched types error. You're trying to apply 'take n' to a value of type 'a' ('take n' requires [a]), moreover putting the value of 'take n s' into the FString further constrains its type to be [Char] == String. So either fix the Gadget g class, or fix the Gadget FString instance. Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Point and link
Denis Bueno wrote: On Dec 7, 2007 1:50 PM, Andrew Coppin [EMAIL PROTECTED] wrote: Hi guys. Here's a fairly basic question. I have several ideas for programs that I'd like to write. They all involve placing units of some kind, and then drawing connections between those units. How feasible is it to program such a thing in Haskell? Where would you start? (Gtk2hs is the only graphics API I'm familiar with using at present.) A bit off-topic for this list, but seeing as I'm replying anyway, I've done something like that with SVG and Javascript: http://www.blurty.com/users/claudiusmaximus/day/2007/10/03#419 I imagine (having never used it) Gtk has similar ways to bind event listeners to graphical elements to implement drag and drop type stuff, and it will most likely be much less error-prone than Javascript (I still haven't got the drag and drop right in Graphgrow...). Do you need to update positions of the units in real time? Do they even evolve over time, or are you just trying to visualise? If it's the latter, you might just take a collection of units and connections between them, output them in the graphviz [0] format, and see the resulting drawing. Graphviz is for visualising arbitrary graphs, and it's quite good at it. Coincidentally I tried this the other day: http://www.blurty.com/users/claudiusmaximus/day/2007/12/06#426 Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Haskell code in Wordpress
Paulo J. Matos wrote: Hi all, I'm curious about the best way to typeset haskell code in a wordpress blog. Using blockquote removes all indentation. :-( Cheers, Probably HsColour: http://www.cs.york.ac.uk/fp/darcs/hscolour/ --8-- hscolour is a small Haskell script to colourise Haskell code. It currently has five output formats: ANSI terminal codes, HTML 3.2 with font tags, HTML 4.01 with CSS, LaTeX, and mIRC chat client codes. --8-- Would be nice to have a server-side thing too, though. Thanks, Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Ideas
Evan Laforge wrote: The only thing I'm uncertain about is whether it would have good enough time and space performance. All the real work is writing yet another set of basic envelope, oscillator, and fft primitives. You *should* be able to go all the way down to the samples in pure haskell though, which would be more elegant than those other languages :) I've been playing with using Arrows as stream processors for audio, and the dataflow syntax of arrows is quite nice for sample manipulation: -- low pass filter (1-zero) lop i = proc (x, f) - do sr - readState - SampleRate let c = clip (2 * pi * f / sr) 0 1 rec y - delay i - o let o = x * c + (1 - c) * y returnA - o lop :: (ArrowCircuit a, ArrowReader b a, Floating b, Ord b) = b - a (b, b) b Unfortunately it's *very* slow - to render a 5s sine oscillator sweep from 20Hz to 2Hz through a low pass filter at 44100Hz sampling rate takes around 17s. Admittedly 40% of the time is spent outputting the numbers to a text file, but it's still far far from realtime, and churns through 7GB of memory in the process (the total memory usage at any one time is constant and small, however). Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] renderString problems
Hi Dave, everyone... Dave Tapley wrote: Hi all, I'm having a lot of trouble using renderString from Graphics.UI.GLUT.Fonts. All my attempts to render a StrokeFont have so far failed. Using a BitmapFont I can get strings to appear but they demonstrate the odd behaviour of translating themselves a distance equal to their length every time my displayCallback function is evaluated. My requests are: * Does anyone know how to keep the position fixed? One way is to wrap the drawing action with preservingMatrix : http://cvs.haskell.org/Hugs/pages/libraries/OpenGL/Graphics-Rendering-OpenGL-GL-CoordTrans.html#v%3ApreservingMatrix (re-exported from the main GLUT module, if I read the docs correctly) This pushes the transformation state to a stack, executes the action, and restores the transformation state from the stack. More useful when you have complex scenes/subscenes/subsubscenes etc, but should work here too. [snip] Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] embedding Haskell: problematic polymorphism
Hi people, I'm embedding Haskell into a C program with a stateful objects with message passing paradigm [0]. I want to make boxes with useful functions, then connect them together within the C program. I know how to build a working version using Data.Dynamic, but that loses polymorphism [1]. Say I have 3 boxes: Box 1: [1,2,5,3]:: [Float] Box 2: reverse :: [a] - [a] Box 3: putStrLn . show :: (Show b) = b - IO () I wonder, is it possible to create these boxes separately at runtime (each box being compiled/loaded separately with hsplugins), then connect them together like {Box 1}={Box 2}={Box 3} (with a wrapping layer doing appropriate type checking/error reporting), or does the whole thing need to be compiled statically to generate specialized variants of the polymorphic functions? As hinted in #haskell : quicksilver ClaudiusMaximus: I don't think anything will allow you to pass around polymorphic values. They're an illusion of the type-checker, in a sense. Thanks for any insights, Claude [0] http://puredata.info [1] Data.Dynamic (fromDynamic (toDyn (3.5::Double)))::(Typeable a = Maybe a) interactive:1:0: Ambiguous type variable `a' in the constraint: `Typeable a' arising from instantiating a type signature at interactive:1:0-59 Probable fix: add a type signature that fixes these type variable(s) -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] FunPtr stability - suggested clarification to FFI Addendum
Hi, I think the FFI Addendum should make explicit that FunPtr values are stable (in the sense of StablePtr). I'm writing a Haskell plugin for an application written in C, and the control flow is: C-Haskell-C ; C-Haskell-C ; ... and I was confused by this statement in section 4.1 of the Addendum (under the subheading Dynamic wrapper): --8-- The stub factory mkCallback turns any Haskell computation of type IO () into a C function pointer that can be passed to C routines, which can call back into the Haskell context by invoking the referenced function. --8-- The usage of call back makes it clear that this control flow is ok: Haskell-C-Haskell but I wasn't sure whether the C code could store the pointer and call it later. Apparently it is safe, but it isn't made explicit in the Addendum. As SamB said in #haskell: --8-- The fact that there is something called freeHaskellFunPtr seems to indicate that, yes, those things are stable. It would be totally bonkers to have a free function if the associated memory was not stable (and, therefor, garbage collected). --8-- Thanks for your attention, Claude -- http://claudiusmaximus.goto10.org ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe