Re: [Haskell-cafe] operations on lists with continuations
Maybe you've invented the ApoPrelude? If I were doing it I'd probably code them in terms of an apomorphism - unfoldr with flush. Unlike regular unfoldr which discards the final state, an apomorphism uses the final state to produce the tail of the output list. See Jeremy Gibbons paper Streaming representation-changers section 4.4. http://www.comlab.ox.ac.uk/jeremy.gibbons/publications/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Rank-2 types in classes
Hello, I'm working on a library which aims to be a generic interface for 2D rendering. To do that, one of my goals is to enable each implementation of this interface to run in its own monad (most of the time an overlay to IO), thus giving me the following class class (Monad (IM i x)) = Impl i x where data IM i x :: * - * (where IM means Implementation Monad) Here, 'x' aims at being a phantom type that ensures a type safe context for some operations. E.g., operations that need to occur in a window will have the type: IM i Window a And will be run by the function: withinWindow :: Window - IM i Window a - IM i x a This makes an operation that doesn't instantiate 'x' context-independent. My problem, then, is that in the definition of class Impl the type variable 'x' is useless, since every implementation must leave uninstantiated. I would like to write something like : class (forall x. Monad (IM i x)) = Impl i where data IM i :: * - * - * But GHC forbids me to do so. Any suggestion would be warmly welcomed. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Rank-2 types in classes
On 2 March 2011 09:11, Yves Parès limestr...@gmail.com wrote: class (forall x. Monad (IM i x)) = Impl i where data IM i :: * - * - * But GHC forbids me to do so. The way I usually work around this is by doing something like the following pattern: {{{ class Monad1 m where return1 :: a - m x a bind1 :: m x a - (a - m x b) - m x b instance Monad1 (IM MyI) where return1 = ... bind1 = ... instance Monad1 m = Monad (m x) where return = return1 (=) = bind1 }}} Your class can now have a (Monad1 (IM i)) superclass context. You will have to enable a few extensions to get this through - most likely FlexibleInstances and OverlappingInstances. Cheers, Max ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] HDF5 binding (was: why is ghci trying to load hsc file ??)
bri...@aracnet.com writes: I worked out a small hdf5 binding using cabal and bindings-DSL and sqlite3 as my example. Hi, I just wanted to add that I also started an HDF5 binding recently (using hsc2hs only). It does more than enough for me ATM, so I don't develop it actively, but if you want to pursue this (and I think it would be a useful addition to Hackage), we may share experience and code. My binding is part of a bigger project, but I meant to split it out anyway. -- Regards, Feri. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Idiomatic error handling in Haskell
There are quite a few exception handling mechanisms provided by Haskell, as can be seen from this post: http://www.randomhacks.net/articles/2007/03/10/haskell-8-ways-to-report-errors I would like to know what is the preferred Haskell mechanism for handling exceptions in the IO monad? I am not concerned with mechanisms such as Maybe / Either, but would like to know about exception mechanisms inside the IO monad. The 2 I know of are: o) throwDyn o) ioError and catch I do need the exceptions to be extendable. So which is the preferred way to handle exceptions in Haskell for new libs? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Rank-2 types in classes
Thank you ! Is what I'm trying to do a common technique to type-ensure contexts or are there simpler methods? 2011/3/2 Max Bolingbroke batterseapo...@hotmail.com On 2 March 2011 09:11, Yves Parès limestr...@gmail.com wrote: class (forall x. Monad (IM i x)) = Impl i where data IM i :: * - * - * But GHC forbids me to do so. The way I usually work around this is by doing something like the following pattern: {{{ class Monad1 m where return1 :: a - m x a bind1 :: m x a - (a - m x b) - m x b instance Monad1 (IM MyI) where return1 = ... bind1 = ... instance Monad1 m = Monad (m x) where return = return1 (=) = bind1 }}} Your class can now have a (Monad1 (IM i)) superclass context. You will have to enable a few extensions to get this through - most likely FlexibleInstances and OverlappingInstances. Cheers, Max ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Idiomatic error handling in Haskell
On Wed, 2 Mar 2011, Rouan van Dalen wrote: I would like to know what is the preferred Haskell mechanism for handling exceptions in the IO monad? I am not concerned with mechanisms such as Maybe / Either, but would like to know about exception mechanisms inside the IO monad. The 2 I know of are: o) throwDyn o) ioError and catch I do need the exceptions to be extendable. So which is the preferred way to handle exceptions in Haskell for new libs? For my taste there is nothing I can really recommend. I cannot recommend the exceptions that are implicit in IO, since if you have an IO action you cannot know on what exceptions you have to react. Can the action throw a FileDoesNotExist or AudioBufferCouldNotBeAllocated? You don't know, because the type does not tell you. I think the mechanisms like IO (Either ErrorMsg a) are the better solution, since they explicitly tell you, what exceptions you have to expect and when you forgot to handle a particular exception. I think the control-monad-exception package is the most advanced one, but it requires some type extensions. I have written explicit-exceptions which is Haskell 98, and you can use more advanced types of exceptions (such as those from control-monad-exception) on top of it. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] HDF5 binding (was: why is ghci trying to load hsc file ??)
What an interesting coincidence, that makes at least three of us. Apparently it's an idea whose time has come. Mine is also an incomplete low-level binding but is currently under semi-active development and I aim to make it cover the entire hdf5.h interface. If anyone is interested in it I've put it on github at: https://github.com/mokus0/bindings-hdf5 -- James On Mar 2, 2011, at 5:12 AM, Ferenc Wagner wf...@niif.hu wrote: bri...@aracnet.com writes: I worked out a small hdf5 binding using cabal and bindings-DSL and sqlite3 as my example. Hi, I just wanted to add that I also started an HDF5 binding recently (using hsc2hs only). It does more than enough for me ATM, so I don't develop it actively, but if you want to pursue this (and I think it would be a useful addition to Hackage), we may share experience and code. My binding is part of a bigger project, but I meant to split it out anyway. -- Regards, Feri. ___ 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
[Haskell-cafe] Examples for the problem
Thank you all for the responses. Here's an example: As I alrerady said, I tried to parse the MMIXAL assembly language. Each instruction has up to three operands, looking like this: @+4 (Jump for bytes forward) foo (the string foo '0'(1+2) etc. A string literal may contain anything but a newline, (there are no escape codes or similar). But when I add a check for a newline, the parser just fails and the next one is tried. This is undesired, as I want to return an error like unexpected newline instead. How is this handled in other parsers? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Examples for the problem
Hi, Robert Clausecker wrote: Each instruction has up to three operands, looking like this: @+4 (Jump for bytes forward) foo (the string foo '0'(1+2) etc. A string literal may contain anything but a newline, (there are no escape codes or similar). But when I add a check for a newline, the parser just fails and the next one is tried. This is undesired, as I want to return an error like unexpected newline instead. How is this handled in other parsers? I would expect that the other parsers are tried, but fail, because they do not accept an initial quotation mark. You get two errors messages then: 1. Unexpected newline after quotation mark 2. Unexpected quotation mark These two error messages reflect the two ways to solve the problem: Either delete the first quotation mark, or add a second one. Tillmann PS. Please use Reply to answer posts, so that they can be put into the same thread. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Examples for the problem
On Wed, 2 Mar 2011 14:14:02 +0100, you wrote: Thank you all for the responses. Here's an example: As I alrerady said, I tried to parse the MMIXAL assembly language. Each instruction has up to three operands, looking like this: @+4 (Jump for bytes forward) foo (the string foo '0'(1+2) etc. A string literal may contain anything but a newline, (there are no escape codes or similar). But when I add a check for a newline, the parser just fails and the next one is tried. This is undesired, as I want to return an error like unexpected newline instead. How is this handled in other parsers? Tillman's reply is absolutely correct. If a particular sequence of characters is invalid according to your grammar, then _all_ of the alternatives in scope at that point should fail to parse that sequence. If that's not happening, then there's something wrong with the way you've expressed your grammar. I don't know how much experience you have with language grammars, but it might be helpful to try to write down MMIXAL's grammar using EBNF notation, as a starting point. -Steve Schafer ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] A practical Haskell puzzle
Thanks to everyone for the nice solutions to this puzzle, here and on reddit: http://www.reddit.com/r/haskell/comments/fu6el/a_practical_haskell_puzzle/ There were two basic approaches. One was to use GADTs and higher-rank types to reduce the amount of type trickery needed. One nice example is apfelmus' solution here in this thread, and several people on reddit suggested using use thrists package: http://hackage.haskell.org/package/thrist The other approach is to use some kind of generics. In any case, there does not appear to be any reasonable way to handle this simple and common situation in Haskell without extensions. I challenge the Haskell community to add these extensions to the Haskell standard in Haskell 2012! Lennart proposed using type-level numbers and reification, but I'm not sure about the full details of that solution. Does it use Haskell extensions, and if so, which ones? Thanks, Yitz ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Examples for the problem
Apologies if this has been answered already (I've got a bit lost with this thread), but the *try* here seems to be giving you precisely the behaviour you don't want. *try* means backtrack on failure, and try the next parser. So if you want ill formed strings to throw an error if they aren't properly enclosed in double quotes don't use try. | try $ (char '' * (StringLit . B.pack $ manyTill (notChar '\n') (char ''))) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Where to put a library
Hi all, I am working on ways to implement local search meta-heuristics in Haskell. I need various test problems to experiment on, and this has resulted in me building various file parsers and data structures for these. Currently I have versions for SAT, TSP and a couple of more private file formats. The file parsers are designed to process files coming out of the TSPLIB and SATLIB repositories. I also want to extend this to cover scheduling and timetabling example problems. Since these are all related I was going to try to put them together into a single library and post them to Hackage, but I am not sure what to put them under. I assume that Data is for data structures, which is not quite right. I was thinking about something like Problems. as a top level point in a hierarchy. Does anyone have any other suggestions, as this seems a little odd as a name to me? Alternatively, does this already exist and I have just missed it? Cheers RS ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Examples for the problem
Actually this is stranger than I thought - from testing it seems like Attoparsec's (|) is different to Parsec's. From what I'm seeing Attoparsec appears to do a full back track for (|) regardless of whether the string lexer is wrapped in try, whereas Parsec needs try to backtrack. On 2 March 2011 16:24, Stephen Tetley stephen.tet...@gmail.com wrote: *try* means backtrack on failure, and try the next parser. So if you want ill formed strings to throw an error if they aren't properly enclosed in double quotes don't use try. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Examples for the problem
Actually, It's not | that's different, it's the string combinator. In Parsec, string matches each character one at a time. If the match fails, any partial input it matched is consumed. In attoparsec, string matches either the entire thing or not, as a single step. If it fails to match, no input is consumed. Carl On Wed, Mar 2, 2011 at 9:51 AM, Stephen Tetley stephen.tet...@gmail.com wrote: Actually this is stranger than I thought - from testing it seems like Attoparsec's (|) is different to Parsec's. From what I'm seeing Attoparsec appears to do a full back track for (|) regardless of whether the string lexer is wrapped in try, whereas Parsec needs try to backtrack. On 2 March 2011 16:24, Stephen Tetley stephen.tet...@gmail.com wrote: *try* means backtrack on failure, and try the next parser. So if you want ill formed strings to throw an error if they aren't properly enclosed in double quotes don't use try. ___ 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
[Haskell-cafe] type safe web forms at compile time Was: Re: Ur vs Haskell
Hi. Some time ago I forgot to forward this message to thie ur versus Haskell http://www.haskell.org/pipermail/haskell-cafe/2011-January/088060.htmldiscussion, (as usual) --- The most impressive feature (of ur) is the compile time checking of conformance between the form and the form results. This can be done in Haskell using HList magic and some class instances, I guess. Since then I have been playing mentally with this. Recently I found something simple an interesting enough to share. (Although crude). It is a kind of typed form fields data Input a= Input String Type a (String - Either String a) and a kind of heterogeneous list to aggregate form fields and results with the operator (:*): Input a :* Input b ;* Input c a :* b :* c and a (simulated for the purpose of demonstration) send-receive function that type match the form fields and the results: *Main let form = Input Text stringdata novalidate :* Input Text (1::Integer) novalidate *Main ask form = \(a :* b) - return $ a ++ b interactive:1:0: No instance for (FormDigest (Input [Char] :* Input Integer) ([a] :* [a])) .. notifying that there is no translation defined , because the result requires two lists of the same type when the form gives a string and an Integer But forcing the correct monomorphic types it does pattern match and return the values. *Main ask form = \ (a :* b) - print ('s':a) print ( fromInteger $ b) sstringdata 1 ask is just a simulation of HTTP one time interaction. It returns the input values. The whole loop involves the rendering of the form, with render: *Main render form input type=Text name=var1 value=stringdata/ input type=Text name=var2 value=1/ In a real case the results are read and validated from the the post values.They are (or can be) ordered sequentially acording with Input field names. The FormDigest instances do this work. There is no need to define new FormDigest instances. (although non one to one field-result can be created) The text is in literate haskell. There is a more elaborate example at the end. I know that the instances are non tail recursive and there are factorization pending but this is just a proof of concept: {-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, TypeOperators #-} import Control.Monad.State The Heterogeneous list agregator. I tried to use GADTs but they does not easily pattern match data x :* xs = x :* xs deriving (Read, Show, Eq) infixr 5 :* data Type= Hidden | Text deriving (Show, Read)-- to add all the types the input field, with text formatting, initial value and runtime validator data Input a = Input String Type a (String - Either [String] a) instance(Show a)= Show (Input a) where show (Input _ _ x _) = show x rendering of the form need a sequentiation of field names. I use a state monad for this class RenderForm a where renderForm :: a - State Int String instance (Show a) = RenderForm (Input a) where renderForm input = do s1 - render1 input n - get put $ n + 1 return s1 HList school here: instance (Show a,RenderForm xs) = RenderForm (Input a :* xs) where renderForm (input :* xs)= do n - get put $ n+1 h - render1 input s - renderForm xs return $ h++s render1 (Input msg t x _)= do n - get put $ n+1 return $ msg ++ input type=\ ++ show t ++ \ name= ++ \var++show n ++ \ value= ++ show x ++/\n render form=putStrLn $ evalState (renderForm form ) 0 processing of the returned form result, in an ordered String list, according with seuquential names of the fields defined in renderForm. class FormDigest a b where formDigest :: a - [String] - Either [String] b Input a is diggested into a type a instance FormDigest (Input a) (a) where formDigest (Input _ _ x f) (s: ss)= case f s of Right x - Right $ x Left x - Left x recursively add instances for any list of inputs Input a's are diggested into a's instance FormDigest as bs = FormDigest (Input a :* as) (a :* bs) where formDigest (input :* fs) es@(s:ss) = let er = formDigest fs ss e = formDigest input es in case (e, er) of (Right x, Right ys) - Right $ x :* ys (Right _, Left errs) - Left errs (Left err, Left errs) - Left (err ++ errs) simulated request-response that returns the entered input values sendRec xs= do let strs = showValues xs return $ formDigest xs strs class ShowValues a where showValues :: a - [String] instance Show x = ShowValues (Input x) where showValues i = [show i ] instance (Show x,ShowValues xs) = ShowValues (Input x :* xs) where showValues (i :* xs)= show i : showValues xs end of simulated request response ask :: (ShowValues a, FormDigest a b) = a - IO b ask form = do er - sendRec form case er of
Re: [Haskell-cafe] type safe web forms at compile time Was: Re: Ur vs Haskell
Excerpts from Alberto G. Corona's message of Wed Mar 02 20:53:28 + 2011: Some time ago I forgot to forward this message to thie ur versus Haskell http://www.haskell.org/pipermail/haskell-cafe/2011-January/088060.htmldiscussion, (as usual) --- The most impressive feature (of ur) is the compile time checking of conformance between the form and the form results. This can be done in See WASH (- hackage). So there is a Haskell implementation. There are small differences though: urweb has nicer URLS which should be much more SEO friendly. From my point of view its not only about forms. Its also about checking SQL queries. And urweb seems to do this very well. Marc Weber ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] type safe web forms at compile time Was: Re: Ur vs Haskell
WASH is full of wonderful ideas . packed in a not so wonderful syntax. It is worth to evolve it. WASH does force form safety in a similar way to Formlets http://hackage.haskell.org/package/formlets: because the form and the form read code are generated automatically by a class instance. So there is no need for, type checking safety The problem comes when the form is generated and/or maintained( edited by some people (some forms have lot of formating in the real world) while the form handling code is .maintained by some other people, the programmers. In this real case , type cheching is very important. 2011/3/2 Marc Weber marco-owe...@gmx.de Excerpts from Alberto G. Corona's message of Wed Mar 02 20:53:28 + 2011: Some time ago I forgot to forward this message to thie ur versus Haskell http://www.haskell.org/pipermail/haskell-cafe/2011-January/088060.html discussion, (as usual) --- The most impressive feature (of ur) is the compile time checking of conformance between the form and the form results. This can be done in See WASH (- hackage). So there is a Haskell implementation. There are small differences though: urweb has nicer URLS which should be much more SEO friendly. From my point of view its not only about forms. Its also about checking SQL queries. And urweb seems to do this very well. Marc Weber ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Where to put a library
Hi Richard, On Thu, Mar 3, 2011 at 1:46 AM, Richard Senington sc06...@leeds.ac.ukwrote: The file parsers are designed to process files coming out of the TSPLIB and SATLIB repositories. [...] Since these are all related I was going to try to put them together into a single library and post them to Hackage, but I am not sure what to put them under. You could place the parsers under Text.TSPLIB Text.SATLIB Text (or maybe only Text.TSP ...?) That would reflect other formats like Text.JSON and so on. Cheers, Sebastian ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Rank-2 types in classes
2011/3/2 Yves Parès limestr...@gmail.com: Is what I'm trying to do a common technique to type-ensure contexts or are there simpler methods? I don't understand your problem well enough to be able to venture a solid opinion on this. Sorry! What you have detailed so far doesn't sound too complex, though. Max ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] A practical Haskell puzzle
Okay thanks I got the difference between both. The 'exists' syntax seems very useful. Is it planned to be added to GHC in a near future? 2011/2/28 Heinrich Apfelmus apfel...@quantentunnel.de Yves Parès wrote: takeC :: Int - Compoz a b - (exists c. Compoz a c) dropC :: Int - Compoz a b - (exists c. Compoz c b) What does 'exists' means? To create a rank-2 type can't you use: takeC :: Int - Compoz a b - (forall c. Compoz a c) ?? Ah, (exists c. Compoz a c) means There exists a type c such that the whole thing has type Compoz a c . What you describe would be the type For any type c the whole thing can be treated as having type Compoz a c which is something entirely different. The point is that in the former version, the function takeC determines what the type c should be and the caller has no clue what it is. In the latter version, the function that calls takeC can choose which type it should be. What I wrote is *not* legal Haskell. (At least not in GHC. If I remember correctly, the EHC from Utrecht supports first-class existential quantification ). You have to encode it in some way, for instance with a data type data Exists f = forall c . Exists (f c) takeC :: Int - Compoz a b - Exists (Compoz a) Regards, Heinrich Apfelmus -- http://apfelmus.nfshost.com ___ 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] Rank-2 types in classes
The trick is to write the rank-2 type in the function that runs the monad, and leave the typeclasses skolemized. Here's an example: -- | Typeclass for monads that write or read to a network. Useful -- if you define operations that need to work for all such monads. -- You're expected to put extra constraints on h. class (Network g, Monad (m g n), Applicative (m g n), Functor (m g n)) = NetworkMonad m g n where -- | Unsafely converts an 'IO' operation that takes an 'AIG' as an -- argument into an operation in some 'NetworkMonad'. unsafeIOToNetwork :: (GEnv g - IO a) - m g n a ... class OpaqueNetwork g = Network g where -- * We cannot put NetworkMonad constraint on GNT g and GNQ g because we -- need to be able to put that constraint as a rank-2 monad. -- * This has a lot of stuff in it, maybe we'll split it up later. data GNode g :: * -- ^ phantom type - * -- ^ data type data GNT g:: * -- ^ phantom type - * - * -- ^ monad data GNQ g:: * -- ^ phantom type - * - * -- ^ monad data GEnv g :: * one:: GNode g n zero :: GNode g n runNT :: (forall n. NetworkMonad GNT g n = GNT g n ()) - g withNT :: g - (forall n. NetworkMonad GNT g n = GNT g n ()) - g There are numerous other problems with this route (can you see them from the sample code?) but I found this solution to be mostly acceptable. Cheers, Edward ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] A practical Haskell puzzle
From: Yitzchak Gale g...@sefer.org To: haskell-cafe@haskell.org Cc: Heinrich Apfelmus apfel...@quantentunnel.de; Lennart Augustsson lennart.augusts...@gmail.com Sent: Wed, March 2, 2011 9:45:15 AM Subject: Re: [Haskell-cafe] A practical Haskell puzzle Thanks to everyone for the nice solutions to this puzzle, here and on reddit: http://www.reddit.com/r/haskell/comments/fu6el/a_practical_haskell_puzzle/ It seems nobody has provided a simple H98 solution. I misread your question as asking for the composition of arbitrary type-compatible subsets of the layers, like runCompose [1,7,4,3] input if it happens fun1 . fun7 . fun4 . fun3 is well typed. This is not easy to do without Dynamic. Now I see you just want contiguous layers, which is easy enough in H98. This code produces and uses a table of all allowed combinations. I think this makes it easier to understand why the code works (and is H98). It's just as easy to make a direct version that produces one requested composition in linear time, so I haven't worried whether lazy evaluation of this table works nicely. \begin{code} runLayers :: Int - Int - String - String runLayers n m = (table !! (n-1)) !! (m-n) table :: [[String - String]] table = close (extend fun1 (extend fun2 (extend fun3 (extend fun4 seed \end{code} Here are some examples with this sequence of layers and transformations (exact type definition and function definitions at the end of the message). Layer1: (Int,Int) --(uncurry(+))-- Layer2: Int --(\x - if even x then Left x else Right x)-- Layer3: Either Int Int --(either (2*) negate)-- Layer4: Int --(`quotRem`14)-- Layer5: (Int,Int) *Main read (runLayers 2 4 (show (Layer2 X 12))) :: Layer4 Layer4 fun3(fun2(X)) 24 *Main read (runLayers 4 5 (show (Layer4 Y 15))) :: Layer5 Layer5 fun4(Y) (1,1) *Main read (runLayers 1 5 (show (Layer1 fullStack (5,6 :: Layer5 Layer5 fun4(fun3(fun2(fun1(fullStack (0,-11) The table also include trivial slices, which might be useful to check the serialization: *Main read (runLayers 3 3 (Layer3 \X\ (Left(12 :: Layer3 Layer3 X (Left 12) The key observation is that if all compositions of functions are followed by the appropriate initialization function, then all the functions starting at the same layer have the same type. With four layers, show . show . fun34 show . fun45 . fun34 all have type Layer3 - String The table construction uses a type \begin{code} data Layered a = Layered [a - String] [[String - String]] \end{code} which stores all sequences beginning at layer a with the uniform type [a - String], and already has all strictly later sequences in the table [[String-String]]. A partial sequences can be extended by precomposing another function, or converted to the unform type by precomposing the deserialization function. To ensure only one type parameter is exposed at a time, the extend function combines both steps. \begin{code} extend :: (Show a, Read b) = (a - b) - Layered b - Layered a extend f (Layered gs tails) = Layered (show:[g . f | g - gs]) ([g . read | g - gs]:tails) \end{code} The final step just closes partial sequences to produce one table, and the seed is a trivial table. \begin{code} close :: (Read a) = Layered a - [[String - String]] close (Layered fs tails) = [f . read | f - fs]:tails seed :: (Show a) = Layered a seed = Layered [show] [] \end{code} Exact definition of the layer types. \begin{code} data Layer1 = Layer1 String (Int,Int) deriving (Read, Show) data Layer2 = Layer2 String Intderiving (Read, Show) data Layer3 = Layer3 String (Either Int Int) deriving (Read, Show) data Layer4 = Layer4 String Intderiving (Read, Show) data Layer5 = Layer5 String (Int,Int) deriving (Read, Show) \end{code} \begin{code} fun1 (Layer1 s x) = Layer2 (fun1(++s++)) (uncurry (+) x) fun2 (Layer2 s x) = Layer3 (fun2(++s++)) (if even x then Left x else Right x) fun3 (Layer3 s x) = Layer4 (fun3(++s++)) (either (2*) negate x) fun4 (Layer4 s x) = Layer5 (fun4(++s++)) (x `quotRem` 14) \end{code} ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Haskell Weekly News: Issue 171
Welcome to issue 171 of the HWN, a newsletter covering developments in the [1]Haskell community. This release covers the week of February 20 - 26, 2011 . Announcements Eric Kow [2]announced that the Darcs project is staring to organize the next hacking sprint, which will be most likely in Paris in this spring. If you fancy a weekend of hacking Haskell on a really neat real world project, come join...! Jose Pedro Magalhaes [3]informed us of the initial release of htime, a library to mimic the unix time utility. Iain Alexander [4]annouced the initial release of data-type, function-combine, and flexiwrap. data-type contains a number of fundamental types and classes useful for type-level programming, such as type-level booleans, natural numbers, and lists. I'd like to once again recognize some of the members of the community that spend their time fielding questions from the new commers. If you haven't been in the #haskell irc channel, you are just missing out! There are also those who spend their time writting great answers to the many questions in haskell's StackOverflow tag. You are what make this community an inviting place for all the people interested in Haskell. Keep it up! Quotes of the Week kmc: but rarely do beginners come to #haskell and say i want to append two lists, but i don't understand all this Monoid business, i hear they're like taco salad Eduard:_Munteanu [In response to GHC can go jump out a window and GHC has already jumped out a window and flied and left you behind] Yes, GHC even implements optimizations such as defenestration. Top Reddit Stories * What kind of things are easy in Haskell and hard in Scala, and vice versa?: From (programmers.stackexchange.com), scored 47 with 34 comments. Read on [5]reddit or the [6]original post. * Multiday Debugging GHC: From (blog.ezyang.com), scored 37 with 2 comments. Read on [7]reddit or the [8]original post. * Making OpenCL Simple with Haskell [pdf]: From (developer.amd.com), scored 35 with 11 comments. Read on [9]reddit or the [10]original post. * GHC 7.0.2 RC 2 available. Please test!: From (haskell.org), scored 33 with 5 comments. Read on [11]reddit or the [12]original post. * Minecraft data API for Haskell: HaskellNBT: From (blog.acfoltzer.net), scored 27 with 4 comments. Read on [13]reddit or the [14]original post. * Dependently Typed Programming: an Agda Introduction: From (youtube.com), scored 25 with 5 comments. Read on [15]reddit or the [16]original post. * Minecraft network protocol and proxy in Haskell: From (github.com), scored 25 with 1 comments. Read on [17]reddit or the [18]original post. * Threading State - a short tutorial for new Haskell programmers: From (mtnviewmark.wordpress.com), scored 22 with 3 comments. Read on [19]reddit or the [20]original post. * Solution (code) XCKD: Nerd Snipping: From (pgraycode.wordpress.com), scored 20 with 8 comments. Read on [21]reddit or the [22]original post. * Agda 2.2 released: From (permalink.gmane.org), scored 18 with 1 comments. Read on [23]reddit or the [24]original post. Top StackOverflow Answers * [25]Feed elements of a tuple to a function as arguments in Haskell? votes: 14 Function uncurry converts a two-argument (curried) function into a function on pairs. Here's its type signature: uncurry :: (a - b - c) - (a, b) - c You need to use it on printf, like this: mapM_ (uncurry $ printf Values: %d %d\n) [(1,100),(2,350),(3,600),(4,200)] Another solution is to use pattern matching to deconstruct the tuple, ... * [26]What is the difference between different orderings of the same monad transformers? votes: 10 Edit: I originally got the cases backwards. Fixed now. The difference between orderings of monad transformer stacks really only matters when you're peeling off layers of the stack. type Procedure a = MaybeT (State ProcedureState) a In this case, you first run the MaybeT, which results in a stateful computation which returns a Maybe a. type Procedure a ... Top StackOverflow Questions * Real-world applications of zygohistomorphic prepromorphisms (votes: 54, answers: 2) [27]read * How to roll a fast BVH representation in Haskell (votes: 10, answers: 2) [28]read * curious about how âloop = loopâ is evaluated in Haskell (votes: 9, answers: 2) [29]read * What is the difference between different orderings of the same monad transformers? (votes: 8, answers: 3) [30]read * haskell function declaration (votes: 6, answers: 3) [31]read About the Haskell Weekly News To help create new editions of this newsletter, please send stories
[Haskell-cafe] Learn You a Haskell for Great Good - a few doubts
Hello, I'm learning Haskell from the extremely well written (and well illustrated as well!) tutorial - http://learnyouahaskell.com/chapters. I have couple of questions from my readings so far. In typeclasses - 101 (http://learnyouahaskell.com/types-and-typeclasses#typeclasses-101), there is a paragraph that reads: Enum members are sequentially ordered types - they can be enumerated. The main advantage of the Enum typeclass is that we can use its types in list ranges. They also have defined successors and predecesors, which you can get with the succ and pred functions. Types in this class: (), Bool, Char, Ordering, Int, Integer, Float and Double. What is the () type? Does it refer to a tuple? How can tuple be ordered, let alone be enum'd? I tried: Prelude take 10 [(1,1) ..] interactive:1:8: No instance for (Enum (t, t1)) arising from the arithmetic sequence `(1, 1) .. ' at interactive:1:8-17 Possible fix: add an instance declaration for (Enum (t, t1)) In the second argument of `take', namely `[(1, 1) .. ]' In the expression: take 10 [(1, 1) .. ] In the definition of `it': it = take 10 [(1, 1) .. ] This is expected and is logical. But, surprise: Prelude (1,1) (1,2) False Prelude (2,2) (1,1) True Prelude (1,2) (2,1) False Prelude (1,2) (2,1) True So tuples are in Ord type class atleast. What is the ordering logic? Another question, on the curried functions - specifically for infix functions. Suppose I need a function that takes an argument and adds five to it. I can do: Prelude let addFive = (+) 5 Prelude addFive 4 9 The paragraph: Infix functions can also be partially applied by using sections. To section an infix function, simply surround it with parentheses and only supply a parameter on one side. That creates a function that takes one parameter and then applies it to the side that's missing an operand: describes a different syntax. I tried that as well: Prelude let addFive' = (+5) Prelude addFive' 3 8 Ok. Works. But on a non-commutative operation like division, we get: Prelude let x = (/) 20.0 Prelude x 10 2.0 Prelude let y = (/20.0) Prelude y 10 0.5 So a curried infix operator fixes the first argument and a sectioned infix operator fixes the second argument? Regards, Karthick ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Learn You a Haskell for Great Good - a few doubts
On Thu, 2011-03-03 at 11:39 +0530, Karthick Gururaj wrote: What is the () type? Does it refer to a tuple? How can tuple be ordered, let alone be enum'd? I tried: The () type is pronounced unit. It is a type with only 1 value, also called () and pronounced unit. Since it only has one possible value, it conveys no information at all, and is sometimes used in situations analogous to C's 'void' keyword. Okay, actually that was a little bit of a lie; () has two values: () and bottom. Bottom is the value that corresponds to the program hanging in an infinite loop or dying with an error message. But if you have an actual, honest-to-goodness value that's not bottom, it has to be (). But, surprise: Prelude (1,1) (1,2) False Prelude (2,2) (1,1) True Prelude (1,2) (2,1) False Prelude (1,2) (2,1) True Okay, so this is no longer Enum, but just Ord. The ordering defined in the Ord instance for tuples is the normal lexicographic order: the comparison between the first elements dominates; but if the first elements coincide, then the second are compared instead. For larger tuple types, the same pattern continues. Think of it like organizing words in alphabetical order, where here you know the words all have the same number of letters. Ok. Works. But on a non-commutative operation like division, we get: Prelude let x = (/) 20.0 Prelude x 10 2.0 Prelude let y = (/20.0) Prelude y 10 0.5 So a curried infix operator fixes the first argument and a sectioned infix operator fixes the second argument? Sections can be either left sections or right sections, so you can pick which argument is provided. Prelude let y = (/ 20.0) Prelude y 10 0.5 Prelude let y = (20.0 /) Prelude y 10 2.0 Hope that helps, -- Chris Smith ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Learning list filtering using puzzles
Hi all, I'm very new to Haskell, having started earlier this week. This is my second attempt after two years :) Currently, I can do simple list processing/recursion/functions. I'm using GHC on Ubuntu. I decided to use puzzles as a way to explore list filtering. For example, while normal solution for solving a quadratic is to compute the solution algebraically, I tried: solvequad :: Int - Int - Int - [Int] solvequad a b c = [x | x - [1..1], a*x*x + b*x + c == 0] This works well, of course assuming the roots are in the given integer range. Next, to solve the puzzle: A stone weighing 40 kg falls from a certain height and is split into four pieces. With the help of these four pieces, one can weigh any quantity from one kg to 40 kg by using these in a scale (both plates of the scale can be used for placing any stone pieces at a time). Find the weight of each piece. I tried: solvepuzzle = [(a,b,c,d) | a - [1..40], b - [a..40], c - [b..40], d - [c..40], a+b+c+d == 40, sort [p*a + q*b + r*c + s*d | p - [-1,0,1], q - [-1,0,1], r - [-1,0,1], s - [-1,0,1], p*a+q*b+r*c+s*d 0] == [1..40]] It works, but I'm not too happy with the last constraint.. Is there a better way to express: ..one can weigh any quantity from 1kg to 40 kgs.. ? A more interesting candidate - considering the previous ones can be also solved with a language like C easily: consider the well know honestans/swindle-cats puzzle: there are two gate keepers guarding two doors - one that leads to hell and the other to heaven. One of these keepers always speaks the truth and the other always lies (and will only answer questions that permit a yes or no for answer). We don't know who is the honest one. If allowed to ask one question to one of the keepers, what would you ask to find out which is the gate to heaven? I'm trying to see how to formulate this in a way Haskell can lead me to a solution.. This is where I'm at now (in psedo Haskell code). --- Oracle knows answers to all questions and will answer truthfully oracle :: Question - Bool -- Ignoring the definition for now --- invOracle knows answers to all questions but will always lie invOracle :: Question - Bool invOracle x = not (oracle x) As far as the answer we are looking for, the truth val of does the first door lead to heaven? is what we want. I represent this Question as 'q'. So in my notation: keeper1 q - ask the question to keeper 1 oracle q - ask oracle the question (not . oracle) q - ask oracle the question, and then negate it (keeper1 . keeper2) q - Ask keeper 1, Suppose I ask keeper 2 the question 'q', what would he say? .. etc Suppose there is a way to generate all possible questions that can be asked to the two gate-keepers.. a generator function (say gen) that takes three parameters: keeper1, keeper2 and q and returns a list of questions (like the examples above). The solution(s) to the puzzle is then (something like..): [x | keeper1 - [oracle, invOracle], keeper2 - [oracle, invOracle], keeper1 /= keeper2, x - gen keeper1 keeper2 q, x q == oracle q] The answer I know is not (keeper1 (keeper2 q)). Thinking through the puzzle now, I realized even keeper1 (keeper1 q) can be an answer. How would I go about defining such a generator function? I'm not looking for a detailed solution, but hints what I can look up/refer. Any other comments on the approach above would be gratefully received :) Cheers, Karthick ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Haskell IDE
Hi Haskellers, whats your Haskell IDE of choise? Currently I use leksah. Is the EclipseFP Plugin for Eclipse a real alternative? Thanks Klaus ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Learn You a Haskell for Great Good - a few doubts
On Thu, Mar 3, 2011 at 11:48 AM, Chris Smith cdsm...@gmail.com wrote: On Thu, 2011-03-03 at 11:39 +0530, Karthick Gururaj wrote: What is the () type? Does it refer to a tuple? How can tuple be ordered, let alone be enum'd? I tried: The () type is pronounced unit. It is a type with only 1 value, also called () and pronounced unit. Since it only has one possible value, it conveys no information at all, and is sometimes used in situations analogous to C's 'void' keyword. Okay, actually that was a little bit of a lie; () has two values: () and bottom. Bottom is the value that corresponds to the program hanging in an infinite loop or dying with an error message. But if you have an actual, honest-to-goodness value that's not bottom, it has to be (). Thanks - is this the same unit that accompanies IO in IO () ? In any case, my question is answered since it is not a tuple. But, surprise: Prelude (1,1) (1,2) False Prelude (2,2) (1,1) True Prelude (1,2) (2,1) False Prelude (1,2) (2,1) True Okay, so this is no longer Enum, but just Ord. The ordering defined in the Ord instance for tuples is the normal lexicographic order: the comparison between the first elements dominates; but if the first elements coincide, then the second are compared instead. For larger tuple types, the same pattern continues. Think of it like organizing words in alphabetical order, where here you know the words all have the same number of letters. Ok. Works. But on a non-commutative operation like division, we get: Prelude let x = (/) 20.0 Prelude x 10 2.0 Prelude let y = (/20.0) Prelude y 10 0.5 So a curried infix operator fixes the first argument and a sectioned infix operator fixes the second argument? Sections can be either left sections or right sections, so you can pick which argument is provided. Prelude let y = (/ 20.0) Prelude y 10 0.5 Prelude let y = (20.0 /) Prelude y 10 2.0 Hope that helps, Thanks, it does! ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Haskell IDE
A year and something ago I used Leksah and I was reasonably satisfied with what it had to offer at that time. If I'm understanding correctly, it has been much improved since. However, now I actually use vim - but that's because I'm scared of trying to install Leksah on Windows (maybe it isn't hard, I haven't tried) and because I'm only doing rather tiny things with Haskell at the moment. 2011/3/3 Hauschild, Klaus (EXT) klaus.hauschild@siemens.com: Hi Haskellers, whats your Haskell IDE of choise? Currently I use leksah. Is the EclipseFP Plugin for Eclipse a real alternative? Thanks Klaus ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe -- Eugene Kirpichov Senior Software Engineer, Mirantis Inc. http://www.mirantis.com/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Haskell IDE
On Thu, Mar 3, 2011 at 8:05 AM, Hauschild, Klaus (EXT) klaus.hauschild@siemens.com wrote: Hi Haskellers, whats your Haskell IDE of choise? Currently I use leksah. Is the EclipseFP Plugin for Eclipse a real alternative? Thanks Klaus Hello, I'm one of the maintainers of EclipseFP. It is a real alternative: it works, it is maintained, supported and enhanced. I use it for my own projects, and of course I use it to work on the version of the scion library that ships with it, so we eat our own dogfood :-). A new minor version is going to come out in the next couple of weeks. Why don't you give it a try? We appreciate any feedback! -- JP Moresmau http://jpmoresmau.blogspot.com/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Learn You a Haskell for Great Good - a few doubts
On 3 March 2011 17:59, Karthick Gururaj karthick.guru...@gmail.com wrote: On Thu, Mar 3, 2011 at 11:48 AM, Chris Smith cdsm...@gmail.com wrote: On Thu, 2011-03-03 at 11:39 +0530, Karthick Gururaj wrote: What is the () type? Does it refer to a tuple? How can tuple be ordered, let alone be enum'd? I tried: The () type is pronounced unit. It is a type with only 1 value, also called () and pronounced unit. Since it only has one possible value, it conveys no information at all, and is sometimes used in situations analogous to C's 'void' keyword. Okay, actually that was a little bit of a lie; () has two values: () and bottom. Bottom is the value that corresponds to the program hanging in an infinite loop or dying with an error message. But if you have an actual, honest-to-goodness value that's not bottom, it has to be (). Thanks - is this the same unit that accompanies IO in IO () ? In any case, my question is answered since it is not a tuple. Yes. -- Ivan Lazar Miljenovic ivan.miljeno...@gmail.com IvanMiljenovic.wordpress.com ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Where to put a library
On 2 Mar 2011, at 22:38, Sebastian Fischer wrote: You could place the parsers under Text.TSPLIB Text.SATLIB Text Some other suggestions might be Codec.TSP Codec.SAT or FileFormat.TSP FileFormat.SAT Regards, Malcolm ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Learn You a Haskell for Great Good - a few doubts
On Thu, Mar 03, 2011 at 12:29:44PM +0530, Karthick Gururaj wrote: Thanks - is this the same unit that accompanies IO in IO () ? In any case, my question is answered since it is not a tuple. It can be viewed as the trivial 0-tuple. -- Antti-Juhani Kaijanaho, Jyväskylä, Finland http://antti-juhani.kaijanaho.fi/newblog/ http://www.flickr.com/photos/antti-juhani/ ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe