From Graham.Hutton at nottingham.ac.uk Thu Sep 1 08:59:09 2016 From: Graham.Hutton at nottingham.ac.uk (Graham Hutton) Date: Thu, 1 Sep 2016 08:59:09 +0000 Subject: [Haskell-beginners] New edition of "Programming in Haskell" Message-ID: Dear all, I'm delighted to announce that the new edition of "Programming in Haskell" is now available! Further details are provided below, and are also available from: http://tinyurl.com/hnfjdgc. Best wishes, Graham ================================================================= *** BOOK ANNOUNCEMENT *** Programming in Haskell - 2nd Edition Graham Hutton, University of Nottingham Cambridge University Press, 1st September 2016 320 pages, 120 exercises, ISBN 9781316626221 http://tinyurl.com/hnfjdgc ================================================================= DESCRIPTION: Haskell is a purely functional language that allows programmers to rapidly develop clear, concise, and correct software. The language has grown in popularity in recent years, both in teaching and in industry. This book is based on the author's experience of teaching Haskell for more than twenty years. All concepts are explained from first principles and no programming experience is required, making this book accessible to a broad spectrum of readers. While Part I focuses on basic concepts, Part II introduces the reader to more advanced topics. This new edition has been extensively updated and expanded to include recent and more advanced features of Haskell, new examples and exercises, selected solutions, and freely downloadable lecture slides and example code. The presentation is clean and simple, while also being fully compliant with the latest version of the language, including recent changes concerning applicative, monadic, foldable, and traversable types. ================================================================= CONTENTS: Foreword Preface Part I. Basic Concepts: 1. Introduction 2. First steps 3. Types and classes 4. Defining functions 5. List comprehensions 6. Recursive functions 7. Higher-order functions 8. Declaring types and classes 9. The countdown problem Part II. Going Further: 10. Interactive programming 11. Unbeatable tic-tac-toe 12. Monads and more 13. Monadic parsing 14. Foldables and friends 15. Lazy evaluation 16. Reasoning about programs 17. Calculating compilers Appendix A. Selected solutions Appendix B. Standard prelude Bibliography Index ================================================================= AUTHOR: Graham Hutton is Professor of Computer Science at the University of Nottingham. He has taught Haskell to thousands of students and received numerous best lecturer awards. Hutton has served as an editor of the Journal of Functional Programming, Chair of the Haskell Symposium and the International Conference on Functional Programming, and Vice-Chair of the ACM Special Interest Group on Programming Languages, and he is an ACM Distinguished Scientist. ================================================================= This message and any attachment are intended solely for the addressee and may contain confidential information. If you have received this message in error, please send it back to me, and immediately delete it. Please do not use, copy or disclose the information contained in this message or in any attachment. Any views or opinions expressed by the author of this email do not necessarily reflect the views of the University of Nottingham. This message has been checked for viruses but the contents of an attachment may still contain software viruses which could damage your computer system, you are advised to perform your own checks. Email communications with the University of Nottingham may be monitored as permitted by UK legislation. From defigueiredo at ucdavis.edu Thu Sep 1 14:29:08 2016 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Thu, 1 Sep 2016 11:29:08 -0300 Subject: [Haskell-beginners] New edition of "Programming in Haskell" (Graham Hutton) In-Reply-To: References: Message-ID: <935be72a-9238-16e8-5074-ea996e30f040@ucdavis.edu> This book is the best introduction to Haskell I know. The chapter on lazy evaluation was so compelling I am thoroughly convinced I have been thinking about evaluation the wrong way all my life (minus space complexity issues ;-). As much as I prefer short books (such as Hudak et al's "A Gentle Introduction to Haskell" -- which is also good, but *not* gentle once you get far enough into it) the main critique I had to the 1st edition is that it only scratched the surface and did not go far enough. It seems this has been thoroughly addressed in this edition. Thanks for making this a reality! :-) Dimitri On 01/09/16 9:00 AM, beginners-request at haskell.org wrote: > Send Beginners mailing list submissions to > beginners at haskell.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > or, via email, send a message with subject or body 'help' to > beginners-request at haskell.org > > You can reach the person managing the list at > beginners-owner at haskell.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Beginners digest..." > > > Today's Topics: > > 1. New edition of "Programming in Haskell" (Graham Hutton) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Thu, 1 Sep 2016 08:59:09 +0000 > From: Graham Hutton > To: "beginners at haskell.org" > Subject: [Haskell-beginners] New edition of "Programming in Haskell" > Message-ID: > > Content-Type: text/plain; charset="us-ascii" > > Dear all, > > I'm delighted to announce that the new edition of "Programming in > Haskell" is now available! Further details are provided below, > and are also available from: http://tinyurl.com/hnfjdgc. > > Best wishes, > > Graham > > ================================================================= > > *** BOOK ANNOUNCEMENT *** > > Programming in Haskell - 2nd Edition > > Graham Hutton, University of Nottingham > > Cambridge University Press, 1st September 2016 > > 320 pages, 120 exercises, ISBN 9781316626221 > > http://tinyurl.com/hnfjdgc > > ================================================================= > > DESCRIPTION: > > Haskell is a purely functional language that allows programmers > to rapidly develop clear, concise, and correct software. The > language has grown in popularity in recent years, both in teaching > and in industry. This book is based on the author's experience > of teaching Haskell for more than twenty years. All concepts > are explained from first principles and no programming experience > is required, making this book accessible to a broad spectrum > of readers. While Part I focuses on basic concepts, Part II > introduces the reader to more advanced topics. > > This new edition has been extensively updated and expanded to > include recent and more advanced features of Haskell, new examples > and exercises, selected solutions, and freely downloadable lecture > slides and example code. The presentation is clean and simple, > while also being fully compliant with the latest version of > the language, including recent changes concerning applicative, > monadic, foldable, and traversable types. > > ================================================================= > > CONTENTS: > > Foreword > Preface > Part I. Basic Concepts: > 1. Introduction > 2. First steps > 3. Types and classes > 4. Defining functions > 5. List comprehensions > 6. Recursive functions > 7. Higher-order functions > 8. Declaring types and classes > 9. The countdown problem > Part II. Going Further: > 10. Interactive programming > 11. Unbeatable tic-tac-toe > 12. Monads and more > 13. Monadic parsing > 14. Foldables and friends > 15. Lazy evaluation > 16. Reasoning about programs > 17. Calculating compilers > Appendix A. Selected solutions > Appendix B. Standard prelude > Bibliography > Index > > ================================================================= > > AUTHOR: > > Graham Hutton is Professor of Computer Science at the University > of Nottingham. He has taught Haskell to thousands of students > and received numerous best lecturer awards. Hutton has served as > an editor of the Journal of Functional Programming, Chair of the > Haskell Symposium and the International Conference on Functional > Programming, and Vice-Chair of the ACM Special Interest Group on > Programming Languages, and he is an ACM Distinguished Scientist. > > ================================================================= > > > > > This message and any attachment are intended solely for the addressee > and may contain confidential information. If you have received this > message in error, please send it back to me, and immediately delete it. > > Please do not use, copy or disclose the information contained in this > message or in any attachment. Any views or opinions expressed by the > author of this email do not necessarily reflect the views of the > University of Nottingham. > > This message has been checked for viruses but the contents of an > attachment may still contain software viruses which could damage your > computer system, you are advised to perform your own checks. Email > communications with the University of Nottingham may be monitored as > permitted by UK legislation. > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > ------------------------------ > > End of Beginners Digest, Vol 99, Issue 1 > **************************************** -- 2E45 D376 A744 C671 5100 A261 210B 8461 0FB0 CA1F From frederic-emmanuel.picca at synchrotron-soleil.fr Tue Sep 6 10:17:08 2016 From: frederic-emmanuel.picca at synchrotron-soleil.fr (PICCA Frederic-Emmanuel) Date: Tue, 6 Sep 2016 10:17:08 +0000 Subject: [Haskell-beginners] GADTs and method signature In-Reply-To: References: , Message-ID: Thanks for your answer, I will try to find some time to test your proposition. Frederic From uneeb.agha at gmail.com Sat Sep 10 21:07:48 2016 From: uneeb.agha at gmail.com (Uneeb Adeel Agha) Date: Sat, 10 Sep 2016 21:07:48 +0000 Subject: [Haskell-beginners] Stack implementation Message-ID: Hi, I'm very, very new to Haskell. I am trying to understand the following error, but I'm having a hard time wrapping my head around the whole type system. Stack module Stack (Stack(..)) where import Prelude hiding (head, tail) class Stack s where empty :: s a isEmpty :: s a -> Bool cons :: s a -> a -> s a head :: s a -> a tail :: s a -> s a data ListStack a = LS [a] deriving (Show) instance Stack ListStack where empty = LS [] isEmpty (LS s) = null s cons (LS s) x = LS(x:s) head(LS(x:_)) = x tail(LS(x:xs)) = LS xs Not using Mixmax yet? The problem is that when I load this code in the interpreter and write>emptyI get the following error :108:1:    No instance for (Show (s0 a0)) arising from a use of ‘print’    The type variables ‘s0’, ‘a0’ are ambiguous I mean when I write "LS []" it works just fine. But something is wrong with empty. Thanks, Uneeb -------------- next part -------------- An HTML attachment was scrubbed... URL: From imantc at gmail.com Sat Sep 10 21:47:43 2016 From: imantc at gmail.com (Imants Cekusins) Date: Sat, 10 Sep 2016 23:47:43 +0200 Subject: [Haskell-beginners] Stack implementation In-Reply-To: References: Message-ID: Hello Uneeb, instances of class Stack are defined for *s*. empty's *return* type is *s a* *empty* does not take any arguments. GHCi is confused about *empty*'s *return* type. option 1: try adding *return* type at prompt e.g.: empty::ListStack Int instead of just empty option 2: ​add *a* as Stack class parameter i.e.: class Stack s *a* where ... then if you define only 1 instance, interactive will probably assume that you expect *empty* to return that (the only one defined) type see if this works -------------- next part -------------- An HTML attachment was scrubbed... URL: From dennis.raddle at gmail.com Sun Sep 11 22:49:55 2016 From: dennis.raddle at gmail.com (Dennis Raddle) Date: Sun, 11 Sep 2016 15:49:55 -0700 Subject: [Haskell-beginners] help with optimizing memory usage of thunk size of Monte Carlo backtracking search algorithm Message-ID: I'm a Haskell semi-beginner, and I've implemented a crude Monte Carlo search optimization algorithm, and I want to know how to use strict evaluation to prevent it from consuming too much memory. I am willing to learn new things to do this---if you want to point me in the right direction and maybe give some references, I'll take it from there. However, if there is something simple that can take care of the whole problem, please let me know. I have not run this with the profiler so I don't actually know precisely what it is doing now. I have not run out of memory using it in small cases, but I hope to use it on much larger cases. Here is the code so far: import qualified Data.List as L import Control.Monad import Data.Function -- Suppose that we do backtracking search for an optimal arrangement of -- elements in some kind of "structure". The "structure" is built one step at -- a time. -- -- An example would be searching for an optimal arrangement of furniture. The -- structure is a represenation of the room and all items that have been -- placed in it so far. We could list the options (or "steps") available to us -- at any point in building the structure, that is a list of furniture items -- and locations. -- -- We have the notion that the structure, at some point in adding steps, -- becomes complete. We have an evaluation function providing a "goodness -- score" on either a partially built or complete structure. -- -- The search will optimize the goodness score over all possible complete -- structures. (Or perhaps with Monte Carlo search, an approximate optimal -- value.) -- -- Class Opt (for "optimization") defines a structure type "struct" and a step -- type "step". class Opt struct step | struct -> step where -- number of steps chosen so far in current state of 'struct': oSize :: struct -> Int -- list all the available steps to choose next. If this list is null, then -- the structure is complete. oList :: struct -> [step] -- apply a step to the structure to create a new structure: oApply :: struct -> step -> struct -- evaluate the "goodness" of the current state of a structure. Higher is -- better. oEval :: struct -> Double -- Implement a kind of Monte Carlo search. (I have a vague idea of the -- literature on Monte Carlo; this algorithm is my guess at something that -- does the job). We work in a state monad of class -- "RandMonad" which holds the StdGen data and provides several methods for -- accessing it. The only method we need from RandMonad is -- -- rChooseItem :: RandMonad m => [a] -> m a -- -- which chooses a random item from a non-null list. -- -- The basic algorithm is this: at each point in building the structure, we -- have a structure S and a list of next steps step_list. We apply each step -- in step_list to S, in turn. After applying a step x to S, call the result -- S_x. We evaluate the "monte carlo fitness" of S_x by making random -- completions of it---that is, choosing a bunch of additional steps -- randomly---doing 'nExper' completions (nExper might be 1000 to 10000). The -- fitness as measured by oEval of the very best random completion becomes -- the "monte carlo fitness" of S_x. We then choose the step from step_list, -- x, that maximizes the "monte carlo fitness" of S_x. -- -- We then repeat this process until S is complete. -- Function monteCarlo will take a partially complete structure, and optimize -- it over an investigation of 'nExper' possible "completions". monteCarlo :: (RandMonad r, Opt a b) => Int -> a -> r a monteCarlo nExper struct = case oList struct of -- If structure is complete, then it is its own optimization. [] -> return struct -- Otherwise find optimal step according to "monte carlo fitness" and -- recursively call 'monteCarlo' steps -> do let doStep step = do let newStruct = oApply struct step score <- monteCarloEval nExper newStruct return (score,newStruct) (_,winner) <- L.maximumBy (compare `on` fst) `liftM` mapM doStep steps monteCarlo nExper winner -- monteCarloEval -- -- Evaluate the "monte carlo fitness" of a structure 'struct' by completing it -- in nExper random ways (that is, make all remaining choices in purely random -- way) and finding the maximum value of the evaluated final state among all -- nExper ways. -- monteCarloEval :: (RandMonad r, Opt a b) => Int -> a -> r Double monteCarloEval nExper struct = case oList struct of [] -> return $ oEval struct _ -> do scores <- replicateM nExper (randomComplete struct) return . maximum . map oEval $ scores -- Make random choices of steps until a structure 's' is complete. randomComplete :: (RandMonad r, Opt a b) => a -> r a randomComplete s = case oList s of [] -> return s steps -> rChooseItem steps >>= (randomComplete . oApply s) -------------- next part -------------- An HTML attachment was scrubbed... URL: From kc1956 at gmail.com Sun Sep 11 23:06:27 2016 From: kc1956 at gmail.com (KC) Date: Sun, 11 Sep 2016 16:06:27 -0700 Subject: [Haskell-beginners] help with optimizing memory usage of thunk size of Monte Carlo backtracking search algorithm In-Reply-To: References: Message-ID: Please run with a profiler Programmers are notoriously bad at guessing where the bottlenecks are Unless looking for beer 😃 For strictness there is the bang notation ! 😃 -- -- Sent from an expensive device which will be obsolete in a few months! :D Casey On Sep 11, 2016 3:49 PM, "Dennis Raddle" wrote: > I'm a Haskell semi-beginner, and I've implemented a crude Monte Carlo > search optimization algorithm, and I want to know how to use strict > evaluation to prevent it from consuming too much memory. > > I am willing to learn new things to do this---if you want to point me in > the right direction and maybe give some references, I'll take it from > there. However, if there is something simple that can take care of the > whole problem, please let me know. > > I have not run this with the profiler so I don't actually know precisely > what it is doing now. I have not run out of memory using it in small cases, > but I hope to use it on much larger cases. > > Here is the code so far: > > > > import qualified Data.List as L > import Control.Monad > import Data.Function > > > -- Suppose that we do backtracking search for an optimal arrangement of > -- elements in some kind of "structure". The "structure" is built one step > at > -- a time. > -- > -- An example would be searching for an optimal arrangement of furniture. > The > -- structure is a represenation of the room and all items that have been > -- placed in it so far. We could list the options (or "steps") available > to us > -- at any point in building the structure, that is a list of furniture > items > -- and locations. > -- > -- We have the notion that the structure, at some point in adding steps, > -- becomes complete. We have an evaluation function providing a "goodness > -- score" on either a partially built or complete structure. > -- > -- The search will optimize the goodness score over all possible complete > -- structures. (Or perhaps with Monte Carlo search, an approximate optimal > -- value.) > -- > -- Class Opt (for "optimization") defines a structure type "struct" and a > step > -- type "step". > > class Opt struct step | struct -> step where > -- number of steps chosen so far in current state of 'struct': > oSize :: struct -> Int > -- list all the available steps to choose next. If this list is null, > then > -- the structure is complete. > oList :: struct -> [step] > -- apply a step to the structure to create a new structure: > oApply :: struct -> step -> struct > -- evaluate the "goodness" of the current state of a structure. Higher is > -- better. > oEval :: struct -> Double > > > > -- Implement a kind of Monte Carlo search. (I have a vague idea of the > -- literature on Monte Carlo; this algorithm is my guess at something that > -- does the job). We work in a state monad of class > -- "RandMonad" which holds the StdGen data and provides several methods for > -- accessing it. The only method we need from RandMonad is > -- > -- rChooseItem :: RandMonad m => [a] -> m a > -- > -- which chooses a random item from a non-null list. > -- > -- The basic algorithm is this: at each point in building the structure, we > -- have a structure S and a list of next steps step_list. We apply each > step > -- in step_list to S, in turn. After applying a step x to S, call the > result > -- S_x. We evaluate the "monte carlo fitness" of S_x by making random > -- completions of it---that is, choosing a bunch of additional steps > -- randomly---doing 'nExper' completions (nExper might be 1000 to 10000). > The > -- fitness as measured by oEval of the very best random completion becomes > -- the "monte carlo fitness" of S_x. We then choose the step from > step_list, > -- x, that maximizes the "monte carlo fitness" of S_x. > -- > -- We then repeat this process until S is complete. > > > -- Function monteCarlo will take a partially complete structure, and > optimize > -- it over an investigation of 'nExper' possible "completions". > > monteCarlo :: (RandMonad r, Opt a b) => Int -> a -> r a > monteCarlo nExper struct = case oList struct of > -- If structure is complete, then it is its own optimization. > [] -> return struct > -- Otherwise find optimal step according to "monte carlo fitness" and > -- recursively call 'monteCarlo' > steps -> do > let doStep step = do > let newStruct = oApply struct step > score <- monteCarloEval nExper newStruct > return (score,newStruct) > (_,winner) <- L.maximumBy (compare `on` fst) `liftM` mapM doStep steps > monteCarlo nExper winner > > > > -- monteCarloEval > -- > -- Evaluate the "monte carlo fitness" of a structure 'struct' by > completing it > -- in nExper random ways (that is, make all remaining choices in purely > random > -- way) and finding the maximum value of the evaluated final state among > all > -- nExper ways. > -- > monteCarloEval :: (RandMonad r, Opt a b) => Int -> a -> r Double > monteCarloEval nExper struct = case oList struct of > [] -> return $ oEval struct > _ -> do > scores <- replicateM nExper (randomComplete struct) > return . maximum . map oEval $ scores > > > -- Make random choices of steps until a structure 's' is complete. > randomComplete :: (RandMonad r, Opt a b) => a -> r a > randomComplete s = case oList s of > [] -> return s > steps -> rChooseItem steps >>= (randomComplete . oApply s) > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From dennis.raddle at gmail.com Mon Sep 12 04:58:04 2016 From: dennis.raddle at gmail.com (Dennis Raddle) Date: Sun, 11 Sep 2016 21:58:04 -0700 Subject: [Haskell-beginners] help with optimizing memory usage of thunk size of Monte Carlo backtracking search algorithm In-Reply-To: References: Message-ID: You are right. :) Will run with a profiler and get back to you. I think I might want to learn Stack before I do that, it's supposed to be the best way to manage profiling libraries and non-profiling libraries on the same machine. D -------------- next part -------------- An HTML attachment was scrubbed... URL: From toad3k at gmail.com Mon Sep 12 12:31:53 2016 From: toad3k at gmail.com (David McBride) Date: Mon, 12 Sep 2016 08:31:53 -0400 Subject: [Haskell-beginners] help with optimizing memory usage of thunk size of Monte Carlo backtracking search algorithm In-Reply-To: References: Message-ID: While profiling is important, I think there is a lot of low hanging fruit to be had. Any time you have a loop like let doStep step = This is 90% of space leaks in any program. Look at the step variable, is it ever fully evaluated? It gets oApply run on it, then a little later it gets oEval on it. If neither of those are strict, then that will leak. It is extremely easy to just go let doStep !step = run it again, and look at its max memory usage. Another example is monteCarloEval nExper struct = I think whether nExpr and struct will be fully evaluated is dependent on what r ends up being. If r is IO it will be fully evaluated, I think. In any case, it is really easy to just go monteCarloEval !nExper !struct = and find out. Then if you are still having trouble, profiling is always an option. There is a point where excessive strictness annotations can degrade performance, but honestly, I've never gotten to that point. On Sun, Sep 11, 2016 at 6:49 PM, Dennis Raddle wrote: > I'm a Haskell semi-beginner, and I've implemented a crude Monte Carlo > search optimization algorithm, and I want to know how to use strict > evaluation to prevent it from consuming too much memory. > > I am willing to learn new things to do this---if you want to point me in > the right direction and maybe give some references, I'll take it from > there. However, if there is something simple that can take care of the > whole problem, please let me know. > > I have not run this with the profiler so I don't actually know precisely > what it is doing now. I have not run out of memory using it in small cases, > but I hope to use it on much larger cases. > > Here is the code so far: > > > > import qualified Data.List as L > import Control.Monad > import Data.Function > > > -- Suppose that we do backtracking search for an optimal arrangement of > -- elements in some kind of "structure". The "structure" is built one step > at > -- a time. > -- > -- An example would be searching for an optimal arrangement of furniture. > The > -- structure is a represenation of the room and all items that have been > -- placed in it so far. We could list the options (or "steps") available > to us > -- at any point in building the structure, that is a list of furniture > items > -- and locations. > -- > -- We have the notion that the structure, at some point in adding steps, > -- becomes complete. We have an evaluation function providing a "goodness > -- score" on either a partially built or complete structure. > -- > -- The search will optimize the goodness score over all possible complete > -- structures. (Or perhaps with Monte Carlo search, an approximate optimal > -- value.) > -- > -- Class Opt (for "optimization") defines a structure type "struct" and a > step > -- type "step". > > class Opt struct step | struct -> step where > -- number of steps chosen so far in current state of 'struct': > oSize :: struct -> Int > -- list all the available steps to choose next. If this list is null, > then > -- the structure is complete. > oList :: struct -> [step] > -- apply a step to the structure to create a new structure: > oApply :: struct -> step -> struct > -- evaluate the "goodness" of the current state of a structure. Higher is > -- better. > oEval :: struct -> Double > > > > -- Implement a kind of Monte Carlo search. (I have a vague idea of the > -- literature on Monte Carlo; this algorithm is my guess at something that > -- does the job). We work in a state monad of class > -- "RandMonad" which holds the StdGen data and provides several methods for > -- accessing it. The only method we need from RandMonad is > -- > -- rChooseItem :: RandMonad m => [a] -> m a > -- > -- which chooses a random item from a non-null list. > -- > -- The basic algorithm is this: at each point in building the structure, we > -- have a structure S and a list of next steps step_list. We apply each > step > -- in step_list to S, in turn. After applying a step x to S, call the > result > -- S_x. We evaluate the "monte carlo fitness" of S_x by making random > -- completions of it---that is, choosing a bunch of additional steps > -- randomly---doing 'nExper' completions (nExper might be 1000 to 10000). > The > -- fitness as measured by oEval of the very best random completion becomes > -- the "monte carlo fitness" of S_x. We then choose the step from > step_list, > -- x, that maximizes the "monte carlo fitness" of S_x. > -- > -- We then repeat this process until S is complete. > > > -- Function monteCarlo will take a partially complete structure, and > optimize > -- it over an investigation of 'nExper' possible "completions". > > monteCarlo :: (RandMonad r, Opt a b) => Int -> a -> r a > monteCarlo nExper struct = case oList struct of > -- If structure is complete, then it is its own optimization. > [] -> return struct > -- Otherwise find optimal step according to "monte carlo fitness" and > -- recursively call 'monteCarlo' > steps -> do > let doStep step = do > let newStruct = oApply struct step > score <- monteCarloEval nExper newStruct > return (score,newStruct) > (_,winner) <- L.maximumBy (compare `on` fst) `liftM` mapM doStep steps > monteCarlo nExper winner > > > > -- monteCarloEval > -- > -- Evaluate the "monte carlo fitness" of a structure 'struct' by > completing it > -- in nExper random ways (that is, make all remaining choices in purely > random > -- way) and finding the maximum value of the evaluated final state among > all > -- nExper ways. > -- > monteCarloEval :: (RandMonad r, Opt a b) => Int -> a -> r Double > monteCarloEval nExper struct = case oList struct of > [] -> return $ oEval struct > _ -> do > scores <- replicateM nExper (randomComplete struct) > return . maximum . map oEval $ scores > > > -- Make random choices of steps until a structure 's' is complete. > randomComplete :: (RandMonad r, Opt a b) => a -> r a > randomComplete s = case oList s of > [] -> return s > steps -> rChooseItem steps >>= (randomComplete . oApply s) > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwyang at aranetworks.com Wed Sep 14 10:22:34 2016 From: cwyang at aranetworks.com (Chul-Woong Yang) Date: Wed, 14 Sep 2016 19:22:34 +0900 Subject: [Haskell-beginners] how to enter EOF in emacs haskell mode Message-ID: Hi, all. I use emacs haskell mode for editing and testing simple programs. I have no success in finding out how to enter EOF to haskell program in emacs "Interactive-Haskell" buffer. So I am unable to test haskell program which reads from stdin. :-( Any help would be appreciated deeply. Regards, Chul-Woong Yang -------------- next part -------------- An HTML attachment was scrubbed... URL: From shrvtsnvs at gmail.com Wed Sep 14 11:01:29 2016 From: shrvtsnvs at gmail.com (Shrivats) Date: Wed, 14 Sep 2016 16:31:29 +0530 Subject: [Haskell-beginners] how to enter EOF in emacs haskell mode In-Reply-To: References: Message-ID: Hi, If it's anything like other interactive modes, does C-c C-d do the job of signalling EOF? Shrivats On Sep 14, 2016 15:53, "Chul-Woong Yang" wrote: Hi, all. I use emacs haskell mode for editing and testing simple programs. I have no success in finding out how to enter EOF to haskell program in emacs "Interactive-Haskell" buffer. So I am unable to test haskell program which reads from stdin. :-( Any help would be appreciated deeply. Regards, Chul-Woong Yang _______________________________________________ Beginners mailing list Beginners at haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwyang at aranetworks.com Wed Sep 14 15:06:12 2016 From: cwyang at aranetworks.com (Chul-Woong Yang) Date: Thu, 15 Sep 2016 00:06:12 +0900 Subject: [Haskell-beginners] how to enter EOF in emacs haskell mode In-Reply-To: References: Message-ID: No. Usual `C-c C-d' (comint-send-eof) does not work in haskell-mode's interactive buffer. It is not defined, emacs responds as follows: > C-c C-d is undefined 2016-09-14 20:01 GMT+09:00 Shrivats : > Hi, > > If it's anything like other interactive modes, does > C-c C-d do the job of signalling EOF? > > Shrivats > > On Sep 14, 2016 15:53, "Chul-Woong Yang" wrote: > > Hi, all. > > I use emacs haskell mode for editing and testing simple programs. > I have no success in finding out how to enter EOF to haskell program > in emacs "Interactive-Haskell" buffer. > So I am unable to test haskell program which reads from stdin. :-( > > Any help would be appreciated deeply. > > Regards, > Chul-Woong Yang > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From timmelzer at gmail.com Wed Sep 14 15:46:53 2016 From: timmelzer at gmail.com (Norbert Melzer) Date: Wed, 14 Sep 2016 15:46:53 +0000 Subject: [Haskell-beginners] how to enter EOF in emacs haskell mode In-Reply-To: References: Message-ID: So have you tried `M-x comint-send-eof RET` then? If that works, it is just a matter of binding it to whatever you want. Chul-Woong Yang schrieb am Mi., 14. Sep. 2016 um 17:06 Uhr: > No. Usual `C-c C-d' (comint-send-eof) does not work in haskell-mode's > interactive buffer. > It is not defined, emacs responds as follows: > > > C-c C-d is undefined > > 2016-09-14 20:01 GMT+09:00 Shrivats : > >> Hi, >> >> If it's anything like other interactive modes, does >> C-c C-d do the job of signalling EOF? >> >> Shrivats >> >> On Sep 14, 2016 15:53, "Chul-Woong Yang" wrote: >> >> Hi, all. >> >> I use emacs haskell mode for editing and testing simple programs. >> I have no success in finding out how to enter EOF to haskell program >> in emacs "Interactive-Haskell" buffer. >> So I am unable to test haskell program which reads from stdin. :-( >> >> Any help would be appreciated deeply. >> >> Regards, >> Chul-Woong Yang >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> >> > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cwyang at aranetworks.com Wed Sep 14 16:17:53 2016 From: cwyang at aranetworks.com (Chul-Woong Yang) Date: Thu, 15 Sep 2016 01:17:53 +0900 Subject: [Haskell-beginners] how to enter EOF in emacs haskell mode In-Reply-To: References: Message-ID: Yes, I've tried comint-send-eof with no success: ​​> Current buffer has no process haskell-mode's comint-interface is deprecated. No success with googling and browsing of documentions on haskell-mode. 2016-09-15 0:46 GMT+09:00 Norbert Melzer : > So have you tried `M-x comint-send-eof RET` then? If that works, it is > just a matter of binding it to whatever you want. > > Chul-Woong Yang schrieb am Mi., 14. Sep. 2016 um > 17:06 Uhr: > >> No. Usual `C-c C-d' (comint-send-eof) does not work in haskell-mode's >> interactive buffer. >> It is not defined, emacs responds as follows: >> >> > C-c C-d is undefined >> >> 2016-09-14 20:01 GMT+09:00 Shrivats : >> >>> Hi, >>> >>> If it's anything like other interactive modes, does >>> C-c C-d do the job of signalling EOF? >>> >>> Shrivats >>> >>> On Sep 14, 2016 15:53, "Chul-Woong Yang" wrote: >>> >>> Hi, all. >>> >>> I use emacs haskell mode for editing and testing simple programs. >>> I have no success in finding out how to enter EOF to haskell program >>> in emacs "Interactive-Haskell" buffer. >>> So I am unable to test haskell program which reads from stdin. :-( >>> >>> Any help would be appreciated deeply. >>> >>> Regards, >>> Chul-Woong Yang >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>> >>> >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners at haskell.org >>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >>> >>> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From hasquehl at gmx.de Fri Sep 16 02:24:18 2016 From: hasquehl at gmx.de (Gunnar Quehl) Date: Fri, 16 Sep 2016 04:24:18 +0200 Subject: [Haskell-beginners] why isnt this optimized? Message-ID: Hi, I wrote a recursive fibonacci function in ghci: :set +m let f 0 = 0 f 1 = 1 f n = f (n-1) + f (n-2) As expected calculation f 30 takes a while. About 5s on my machine. What I don't understand is that let x = f 30 in (x,x) takes twice as long. Why is ghci reevaluating the x twice? Isn't in always said, that it can optimize because we can replace same by same and so on. Actually if compiled the behaviour is as expected. main = print $ let x = f 34 in (x) and main = print $ let x = f 34 in (x,x) both take about 3s. Why is this not the case in the interactive enviroment? Thanks Gunnar From guillaum.bouchard+haskell at gmail.com Fri Sep 16 08:54:05 2016 From: guillaum.bouchard+haskell at gmail.com (Guillaume Bouchard) Date: Fri, 16 Sep 2016 10:54:05 +0200 Subject: [Haskell-beginners] why isnt this optimized? In-Reply-To: References: Message-ID: Hello, Observe this example : Prelude> let x = f 30 in (x,x) (832040,832040) (3.70 secs, 1,649,318,104 bytes) Prelude> let x = (f 30) :: Int in (x,x) (832040,832040) (1.77 secs, 854,498,640 bytes) If we force the result type of f 30 to Int, the result is shared as expected. This is related to the monomorphism restriction https://wiki.haskell.org/Monomorphism_restriction We can observe the type of (x, x) : Prelude> let y = (let x = f 30 in (x, x)) Prelude> :t y y :: (Num t1, Num t) => (t, t1) Both value does not have the same type. Actually `f` is a polymorphic function which can compute the result for any type t as long as t is a `Num`. So we can compute it for `Double`, `Float`, `BigMatriceOf100GigaBytesOfInfinitePrecisionRationalNumbers`. The compiler will only know the final type when needed, later. Actually, the (x, x) tuple is already computed (but `x` are unevaluated) when, by displaying it, you decide to fix `x` as `Integer` for both (the default `Num`). The setting is a bit different for compiled code, because in this case, the compiler choose the type earlier. For example, in a file `Foo.hs` a = 10 y = (a, a) :: (Float, Integer) BlorkBlork.hs:2:9: error: • Couldn't match expected type ‘Integer’ with actual type ‘Float’ • In the expression: a In the expression: (a, a) :: (Float, Integer) In an equation for ‘z’: z = (a, a) :: (Float, Integer) Failed, modules loaded: none. Because the compiler takes the first encountered type (Float) as the type of a. However you can keep the polymorphism with a type signature : a :: Num t => t a = 10 y = (a, a) :: (Float, Integer) This is one of the rare case where adding a type signature adds polymorphism compared to the infered type. Will works. Hope this help. -- G. On Fri, Sep 16, 2016 at 4:24 AM, Gunnar Quehl wrote: > Hi, > > I wrote a recursive fibonacci function in ghci: > > :set +m > > let > f 0 = 0 > f 1 = 1 > f n = f (n-1) + f (n-2) > > As expected calculation f 30 takes a while. About 5s on my machine. > What I don't understand is that > > let x = f 30 in (x,x) > > takes twice as long. Why is ghci reevaluating the x twice? Isn't in always > said, > that it can optimize because we can replace same by same and so on. > > Actually if compiled the behaviour is as expected. > > main = print $ let x = f 34 in (x) > > and > > main = print $ let x = f 34 in (x,x) > > both take about 3s. > > Why is this not the case in the interactive enviroment? > > Thanks > Gunnar > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners From hasquehl at gmx.de Fri Sep 16 10:01:12 2016 From: hasquehl at gmx.de (Gunnar Quehl) Date: Fri, 16 Sep 2016 12:01:12 +0200 Subject: [Haskell-beginners] why isnt this optimized? In-Reply-To: References: Message-ID: Am 16.09.2016 um 10:54 schrieb Guillaume Bouchard: > Hello, > > Observe this example : > > Prelude> let x = f 30 in (x,x) > (832040,832040) > (3.70 secs, 1,649,318,104 bytes) > > Prelude> let x = (f 30) :: Int in (x,x) > (832040,832040) > (1.77 secs, 854,498,640 bytes) > > If we force the result type of f 30 to Int, the result is shared as expected. > > This is related to the monomorphism restriction > https://wiki.haskell.org/Monomorphism_restriction > > We can observe the type of (x, x) : > > Prelude> let y = (let x = f 30 in (x, x)) > Prelude> :t y > y :: (Num t1, Num t) => (t, t1) > > > Both value does not have the same type. Actually `f` is a polymorphic > function which can compute the result for any type t as long as t is a > `Num`. So we can compute it for `Double`, `Float`, > `BigMatriceOf100GigaBytesOfInfinitePrecisionRationalNumbers`. The > compiler will only know the final type when needed, later. Actually, > the (x, x) tuple is already computed (but `x` are unevaluated) when, > by displaying it, you decide to fix `x` as `Integer` for both (the > default `Num`). > > The setting is a bit different for compiled code, because in this > case, the compiler choose the type earlier. For example, in a file > `Foo.hs` > > a = 10 > y = (a, a) :: (Float, Integer) > > > BlorkBlork.hs:2:9: error: > • Couldn't match expected type ‘Integer’ with actual type ‘Float’ > • In the expression: a > In the expression: (a, a) :: (Float, Integer) > In an equation for ‘z’: z = (a, a) :: (Float, Integer) > Failed, modules loaded: none. > > Because the compiler takes the first encountered type (Float) as the type of a. > > However you can keep the polymorphism with a type signature : > > a :: Num t => t > a = 10 > > y = (a, a) :: (Float, Integer) > > This is one of the rare case where adding a type signature adds > polymorphism compared to the infered type. > > Will works. > > Hope this help. > Wow, this reply was much more than I expected. You are my heroe. I actually started off with the definition let fibs = 0:1:zipWith (+) fibs (tail fibs) and wondered why looking at a certain cell with for example fibs !! 20000 always took some amount of time to evaluation, as I expected the second time it should be already there. Now I can explain (and do something about it, add a type signature). Thanks that starts to become a good day From guillaum.bouchard+haskell at gmail.com Fri Sep 16 11:52:39 2016 From: guillaum.bouchard+haskell at gmail.com (Guillaume Bouchard) Date: Fri, 16 Sep 2016 13:52:39 +0200 Subject: [Haskell-beginners] why isnt this optimized? In-Reply-To: References: Message-ID: Glad I was able to help you, some added informations : On Fri, Sep 16, 2016 at 12:01 PM, Gunnar Quehl wrote: > Wow, this reply was much more than I expected. You are my heroe. > I actually started off with the definition > > let fibs = 0:1:zipWith (+) fibs (tail fibs) > > and wondered why looking at a certain cell with for example > > fibs !! 20000 > > always took some amount of time to evaluation, as I expected the > second time it should be already there. Now I can explain (and do > something about it, add a type signature). You can use :set +s in ghci to get the time of the line. Also, you can use :sprint to display the evaluation status of the thunk, really useful to debug / understand lazyness issues. Prelude> let l = map (*2) [1::Int ..10] Prelude> :sprint l l = _ Prelude> length l 10 Prelude> :sprint l l = [_,_,_,_,_,_,_,_,_,_] Prelude> take 3 l [2,4,6] Prelude> Prelude> :sprint l l = [2,4,6,_,_,_,_,_,_,_] Finally, recal that (!!) is still an O(n) operator on the fibs list, but you can build an infinite fibs list with O(n log n) query using an infinite Tree see https://wiki.haskell.org/Memoization#Efficient_tree_data_structure_for_maps_from_Int_to_somewhere > Thanks that starts to become a good day ;) -- G. From dennis.raddle at gmail.com Thu Sep 22 09:57:17 2016 From: dennis.raddle at gmail.com (Dennis Raddle) Date: Thu, 22 Sep 2016 02:57:17 -0700 Subject: [Haskell-beginners] is this right? Message-ID: I want to evaluate a function on a series of inputs: f :: a -> Maybe b then collect the results [b] if they are all Just, or terminate the computation immediately upon hitting Nothing. This is exactly what mapM does in the Maybe monad, correct? In particular I want to make sure that it will not try to evaluate anything past the first 'Nothing' result as the efficiency of my design is based on that. D -------------- next part -------------- An HTML attachment was scrubbed... URL: From sylvain at haskus.fr Thu Sep 22 10:33:11 2016 From: sylvain at haskus.fr (Sylvain Henry) Date: Thu, 22 Sep 2016 12:33:11 +0200 Subject: [Haskell-beginners] is this right? In-Reply-To: References: Message-ID: <44e48242-044f-6d01-4488-8e06cdc54282@haskus.fr> Hi, Yes it's correct. You can check this with ghci: f :: Integer -> Maybe Integer f 5 = Nothing f x = Just x > let xs = [1..] :: [Integer] > mapM f xs Nothing (it doesn't loop forever) > :sprint xs xs = 1 : 2 : 3 : 4 : 5 : _ (the tail after 5 is not evaluated) Cheers Sylvain On 22/09/2016 11:57, Dennis Raddle wrote: > I want to evaluate a function on a series of inputs: > > f :: a -> Maybe b > > then collect the results [b] if they are all Just, or terminate the > computation immediately upon hitting Nothing. > > This is exactly what mapM does in the Maybe monad, correct? > > In particular I want to make sure that it will not try to evaluate > anything past the first 'Nothing' result as the efficiency of my > design is based on that. > > D > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From laiboonh at gmail.com Thu Sep 22 13:19:12 2016 From: laiboonh at gmail.com (Lai Boon Hui) Date: Thu, 22 Sep 2016 21:19:12 +0800 Subject: [Haskell-beginners] Newbie question about function type constraints Message-ID: Hi, can someone explain to me why i cannot define meanList as: meanList :: (Integral a, Fractional b) => [a] -> a meanList xs = (sumList xs) / (lengthList xs) I want to restrict the function to only accept lists like [1,2,3] and return answer 2.0 sumList :: (Num a) => [a] -> a sumList [] = 0 sumList (x:xs) = x + (sumList xs) lengthList :: (Num a) => [t] -> a lengthList [] = 0 lengthList (_:xs) = 1 + (lengthList xs) -------------- next part -------------- An HTML attachment was scrubbed... URL: From tushar4r at gmail.com Thu Sep 22 14:06:04 2016 From: tushar4r at gmail.com (Tushar Tyagi) Date: Thu, 22 Sep 2016 19:36:04 +0530 Subject: [Haskell-beginners] Newbie question about function type constraints In-Reply-To: References: Message-ID: What happens if you change the signature of meanList to meanList :: ( Fractional b) => [b] ->b The integrals in [1,2,3] would be converted to [1.0, 2.0, 3.0] before you act upon them. On 22 Sep 2016 6:49 p.m., "Lai Boon Hui" wrote: Hi, can someone explain to me why i cannot define meanList as: meanList :: (Integral a, Fractional b) => [a] -> a meanList xs = (sumList xs) / (lengthList xs) I want to restrict the function to only accept lists like [1,2,3] and return answer 2.0 sumList :: (Num a) => [a] -> a sumList [] = 0 sumList (x:xs) = x + (sumList xs) lengthList :: (Num a) => [t] -> a lengthList [] = 0 lengthList (_:xs) = 1 + (lengthList xs) _______________________________________________ Beginners mailing list Beginners at haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From imantc at gmail.com Thu Sep 22 14:06:52 2016 From: imantc at gmail.com (Imants Cekusins) Date: Thu, 22 Sep 2016 16:06:52 +0200 Subject: [Haskell-beginners] Newbie question about function type constraints In-Reply-To: References: Message-ID: Hello, this works too: ​meanList :: (Fractional a) => [a] -> a meanList xs = (sumList xs) / (lengthList xs) sumList :: (Fractional a) => [a] -> a sumList [] = 0 sumList (x:xs) = x + (sumList xs) lengthList :: (Fractional a) => [t] -> a lengthList [] = 0 lengthList (_:xs) = 1 + (lengthList xs) -------------- next part -------------- An HTML attachment was scrubbed... URL: From bo at ct.de Thu Sep 22 14:08:56 2016 From: bo at ct.de (=?UTF-8?Q?Harald_B=c3=b6geholz?=) Date: Thu, 22 Sep 2016 16:08:56 +0200 Subject: [Haskell-beginners] Newbie question about function type constraints In-Reply-To: References: Message-ID: Am 22.09.16 um 15:19 schrieb Lai Boon Hui: > Hi, can someone explain to me why i cannot define meanList as: > > meanList :: (Integral a, Fractional b) => [a] -> a > meanList xs = (sumList xs) / (lengthList xs) > > I want to restrict the function to only accept lists like [1,2,3] and > return answer 2.0 It will work like this: meanList :: (Integral a, Fractional b) => [a] -> b meanList xs = fromIntegral (sumList xs) / (lengthList xs) You probably meant -> b in the type signature, that was a typo. And you need to insert fromIntegral to convert to Fractional before you can divide. Now that I see it I am beginning to wonder why it works, though, because I was just about to insert another fromIntegral before lengthList ... > sumList :: (Num a) => [a] -> a > sumList [] = 0 > sumList (x:xs) = x + (sumList xs) > > lengthList :: (Num a) => [t] -> a > lengthList [] = 0 > lengthList (_:xs) = 1 + (lengthList xs) Hope this helps -- Harald Bögeholz (PGP key available from servers) Redaktion c't Tel.: +49 511 5352-300 Fax: +49 511 5352-417 int f[9814],b,c=9814,g,i;long a=1e4,d,e,h; main(){for(;b=c,c-=14;i=printf("%04d",e+d/a),e=d%a) while(g=--b*2)d=h*b+a*(i?f[b]:a/5),h=d/--g,f[b]=d%g;} (Arndt/Haenel) Affe Apfel Vergaser /*Heise Medien GmbH & Co. KG * Karl-Wiechert-Allee 10 * 30625 Hannover Registergericht: Amtsgericht Hannover HRA 26709 Persönlich haftende Gesellschafterin: Heise Medien Geschäftsführung GmbH Registergericht: Amtsgericht Hannover, HRB 60405 Geschäftsführer: Ansgar Heise, Dr. Alfons Schräder*/ From sylvain at haskus.fr Thu Sep 22 14:10:17 2016 From: sylvain at haskus.fr (Sylvain Henry) Date: Thu, 22 Sep 2016 16:10:17 +0200 Subject: [Haskell-beginners] Newbie question about function type constraints In-Reply-To: References: Message-ID: <66b119b3-ff51-74d7-80c5-d19450164508@haskus.fr> Hi, You can define it, but in practice there is no instance of "a" that satisfies both constraints: Integral a and Fractional a > meanList ([1,2,3] :: [Int]) :4:1: error: • No instance for (Fractional Int) arising from a use of ‘meanList’ > meanList ([1,2,3] :: [Float]) :5:1: error: • No instance for (Integral Float) arising from a use of ‘meanList’ What you probably want is: meanList :: (Integral a, Fractional b) => [a] -> b meanList xs = fromIntegral (sumList xs) / fromIntegral (lengthList xs) Where we convert from the integral type "a" to the fractional type "b" before performing the division. > meanList ([1,2,3] :: [Int]) 2.0 Cheers Sylvain On 22/09/2016 15:19, Lai Boon Hui wrote: > Hi, can someone explain to me why i cannot define meanList as: > > meanList :: (Integral a, Fractional b) => [a] -> a > meanList xs = (sumList xs) / (lengthList xs) > > I want to restrict the function to only accept lists like [1,2,3] and > return answer 2.0 > > > sumList :: (Num a) => [a] -> a > sumList [] = 0 > sumList (x:xs) = x + (sumList xs) > > lengthList :: (Num a) => [t] -> a > lengthList [] = 0 > lengthList (_:xs) = 1 + (lengthList xs) > > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From sylvain at haskus.fr Thu Sep 22 14:14:48 2016 From: sylvain at haskus.fr (Sylvain Henry) Date: Thu, 22 Sep 2016 16:14:48 +0200 Subject: [Haskell-beginners] Newbie question about function type constraints In-Reply-To: References: Message-ID: <074cbf97-2316-9d70-3fb8-7c8c9904c602@haskus.fr> On 22/09/2016 16:08, Harald Bögeholz wrote: > It will work like this: > meanList :: (Integral a, Fractional b) => [a] -> b > meanList xs = fromIntegral (sumList xs) / (lengthList xs) > > You probably meant -> b in the type signature, that was a typo. > > And you need to insert fromIntegral to convert to Fractional before you > can divide. Now that I see it I am beginning to wonder why it works, > though, because I was just about to insert another fromIntegral before > lengthList ... It works because in this case lengthList uses the fractional type b to perfom its summation (it doesn't care about the type of the elements in xs). Cheers Sylvain From defigueiredo at ucdavis.edu Thu Sep 22 23:26:36 2016 From: defigueiredo at ucdavis.edu (Dimitri DeFigueiredo) Date: Thu, 22 Sep 2016 17:26:36 -0600 Subject: [Haskell-beginners] The meaning of categories constructed from HASK In-Reply-To: References: Message-ID: <36383e22-0b32-426d-7c9f-5e611bbca233@ucdavis.edu> In category theory, there are many ways one can make new categories out of an old one. In particular, given a category C one can construct: 1. The arrows category of C: arrows in C become objects and commutative squares in C become arrows 2. The slice category of C given an object A: arrows into a distinguished object A become objects in the slice commutative triangles become arrows There are also functors going from C to these new categories (and back). Are these constructed categories useful when C = `Hask` (the category of haskell types and functions)? What do they represent in programming terms? In other words, is there intuition for what the arrows category of Hask is? What about the slice category of Hask over a specific type? Do the functors between these match some programming abstractions? Any pointers are much appreciated. Thanks, Dimitri From laiboonh at gmail.com Fri Sep 23 00:44:15 2016 From: laiboonh at gmail.com (Lai Boon Hui) Date: Fri, 23 Sep 2016 08:44:15 +0800 Subject: [Haskell-beginners] Beginners Digest, Vol 99, Issue 13 In-Reply-To: References: Message-ID: Hi All, i am overwhelmed by all the helpful responses. Thanks guys. I am more curious about why meanList :: (Num a, Fractional b) => [a] -> b meanList xs = (sumList xs) / (lengthList xs) does not compile. 'a' being a Num type seems perfectly fine, (/) returns a Fractional type hence 'b' being Fractional seems also fine. On Fri, Sep 23, 2016 at 7:13 AM, wrote: > Send Beginners mailing list submissions to > beginners at haskell.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > or, via email, send a message with subject or body 'help' to > beginners-request at haskell.org > > You can reach the person managing the list at > beginners-owner at haskell.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Beginners digest..." > > > Today's Topics: > > 1. Newbie question about function type constraints (Lai Boon Hui) > 2. Re: Newbie question about function type constraints > (Tushar Tyagi) > 3. Re: Newbie question about function type constraints > (Imants Cekusins) > 4. Re: Newbie question about function type constraints > (Harald Bögeholz) > 5. Re: Newbie question about function type constraints > (Sylvain Henry) > 6. Re: Newbie question about function type constraints > (Sylvain Henry) > 7. The meaning of categories constructed from HASK > (Dimitri DeFigueiredo) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Thu, 22 Sep 2016 21:19:12 +0800 > From: Lai Boon Hui > To: beginners at haskell.org > Subject: [Haskell-beginners] Newbie question about function type > constraints > Message-ID: > mail.gmail.com> > Content-Type: text/plain; charset="utf-8" > > Hi, can someone explain to me why i cannot define meanList as: > > meanList :: (Integral a, Fractional b) => [a] -> a > meanList xs = (sumList xs) / (lengthList xs) > > I want to restrict the function to only accept lists like [1,2,3] and > return answer 2.0 > > > sumList :: (Num a) => [a] -> a > sumList [] = 0 > sumList (x:xs) = x + (sumList xs) > > lengthList :: (Num a) => [t] -> a > lengthList [] = 0 > lengthList (_:xs) = 1 + (lengthList xs) > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: attachments/20160922/45389bc3/attachment-0001.html> > > ------------------------------ > > Message: 2 > Date: Thu, 22 Sep 2016 19:36:04 +0530 > From: Tushar Tyagi > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: Re: [Haskell-beginners] Newbie question about function type > constraints > Message-ID: > mail.gmail.com> > Content-Type: text/plain; charset="utf-8" > > What happens if you change the signature of meanList to > > meanList :: ( Fractional b) => [b] ->b > > The integrals in [1,2,3] would be converted to [1.0, 2.0, 3.0] before you > act upon them. > > On 22 Sep 2016 6:49 p.m., "Lai Boon Hui" wrote: > > Hi, can someone explain to me why i cannot define meanList as: > > meanList :: (Integral a, Fractional b) => [a] -> a > meanList xs = (sumList xs) / (lengthList xs) > > I want to restrict the function to only accept lists like [1,2,3] and > return answer 2.0 > > > sumList :: (Num a) => [a] -> a > sumList [] = 0 > sumList (x:xs) = x + (sumList xs) > > lengthList :: (Num a) => [t] -> a > lengthList [] = 0 > lengthList (_:xs) = 1 + (lengthList xs) > > > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: attachments/20160922/a092897b/attachment-0001.html> > > ------------------------------ > > Message: 3 > Date: Thu, 22 Sep 2016 16:06:52 +0200 > From: Imants Cekusins > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: Re: [Haskell-beginners] Newbie question about function type > constraints > Message-ID: > gmail.com> > Content-Type: text/plain; charset="utf-8" > > Hello, > > this works too: > > ​meanList :: (Fractional a) => [a] -> a > meanList xs = (sumList xs) / (lengthList xs) > > > sumList :: (Fractional a) => [a] -> a > sumList [] = 0 > sumList (x:xs) = x + (sumList xs) > > > lengthList :: (Fractional a) => [t] -> a > lengthList [] = 0 > lengthList (_:xs) = 1 + (lengthList xs) > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: attachments/20160922/4c967a0d/attachment-0001.html> > > ------------------------------ > > Message: 4 > Date: Thu, 22 Sep 2016 16:08:56 +0200 > From: Harald Bögeholz > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: Re: [Haskell-beginners] Newbie question about function type > constraints > Message-ID: > Content-Type: text/plain; charset=utf-8 > > Am 22.09.16 um 15:19 schrieb Lai Boon Hui: > > Hi, can someone explain to me why i cannot define meanList as: > > > > meanList :: (Integral a, Fractional b) => [a] -> a > > meanList xs = (sumList xs) / (lengthList xs) > > > > I want to restrict the function to only accept lists like [1,2,3] and > > return answer 2.0 > > It will work like this: > > meanList :: (Integral a, Fractional b) => [a] -> b > meanList xs = fromIntegral (sumList xs) / (lengthList xs) > > You probably meant -> b in the type signature, that was a typo. > > And you need to insert fromIntegral to convert to Fractional before you > can divide. Now that I see it I am beginning to wonder why it works, > though, because I was just about to insert another fromIntegral before > lengthList ... > > > > sumList :: (Num a) => [a] -> a > > sumList [] = 0 > > sumList (x:xs) = x + (sumList xs) > > > > lengthList :: (Num a) => [t] -> a > > lengthList [] = 0 > > lengthList (_:xs) = 1 + (lengthList xs) > > Hope this helps > > > -- > Harald Bögeholz (PGP key available from servers) > Redaktion c't Tel.: +49 511 5352-300 Fax: +49 511 5352-417 > > int f[9814],b,c=9814,g,i;long a=1e4,d,e,h; > main(){for(;b=c,c-=14;i=printf("%04d",e+d/a),e=d%a) > while(g=--b*2)d=h*b+a*(i?f[b]:a/5),h=d/--g,f[b]=d%g;} > (Arndt/Haenel) > > Affe Apfel Vergaser > > /*Heise Medien GmbH & Co. KG * Karl-Wiechert-Allee 10 * 30625 Hannover > Registergericht: Amtsgericht Hannover HRA 26709 > Persönlich haftende Gesellschafterin: Heise Medien Geschäftsführung GmbH > Registergericht: Amtsgericht Hannover, HRB 60405 > Geschäftsführer: Ansgar Heise, Dr. Alfons Schräder*/ > > > ------------------------------ > > Message: 5 > Date: Thu, 22 Sep 2016 16:10:17 +0200 > From: Sylvain Henry > To: beginners at haskell.org > Subject: Re: [Haskell-beginners] Newbie question about function type > constraints > Message-ID: <66b119b3-ff51-74d7-80c5-d19450164508 at haskus.fr> > Content-Type: text/plain; charset="utf-8"; Format="flowed" > > Hi, > > You can define it, but in practice there is no instance of "a" that > satisfies both constraints: Integral a and Fractional a > > meanList ([1,2,3] :: [Int]) > > :4:1: error: > • No instance for (Fractional Int) arising from a use of ‘meanList’ > > > meanList ([1,2,3] :: [Float]) > > :5:1: error: > • No instance for (Integral Float) arising from a use of ‘meanList’ > > What you probably want is: > meanList :: (Integral a, Fractional b) => [a] -> b > meanList xs = fromIntegral (sumList xs) / fromIntegral (lengthList xs) > > Where we convert from the integral type "a" to the fractional type "b" > before performing the division. > > > meanList ([1,2,3] :: [Int]) > 2.0 > > Cheers > Sylvain > > > On 22/09/2016 15:19, Lai Boon Hui wrote: > > Hi, can someone explain to me why i cannot define meanList as: > > > > meanList :: (Integral a, Fractional b) => [a] -> a > > meanList xs = (sumList xs) / (lengthList xs) > > > > I want to restrict the function to only accept lists like [1,2,3] and > > return answer 2.0 > > > > > > sumList :: (Num a) => [a] -> a > > sumList [] = 0 > > sumList (x:xs) = x + (sumList xs) > > > > lengthList :: (Num a) => [t] -> a > > lengthList [] = 0 > > lengthList (_:xs) = 1 + (lengthList xs) > > > > > > > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: attachments/20160922/1be73068/attachment-0001.html> > > ------------------------------ > > Message: 6 > Date: Thu, 22 Sep 2016 16:14:48 +0200 > From: Sylvain Henry > To: beginners at haskell.org > Subject: Re: [Haskell-beginners] Newbie question about function type > constraints > Message-ID: <074cbf97-2316-9d70-3fb8-7c8c9904c602 at haskus.fr> > Content-Type: text/plain; charset=utf-8; format=flowed > > On 22/09/2016 16:08, Harald Bögeholz wrote: > > It will work like this: > > meanList :: (Integral a, Fractional b) => [a] -> b > > meanList xs = fromIntegral (sumList xs) / (lengthList xs) > > > > You probably meant -> b in the type signature, that was a typo. > > > > And you need to insert fromIntegral to convert to Fractional before you > > can divide. Now that I see it I am beginning to wonder why it works, > > though, because I was just about to insert another fromIntegral before > > lengthList ... > It works because in this case lengthList uses the fractional type b to > perfom its summation (it doesn't care about the type of the elements in > xs). > > Cheers > Sylvain > > > ------------------------------ > > Message: 7 > Date: Thu, 22 Sep 2016 17:26:36 -0600 > From: Dimitri DeFigueiredo > To: Haskell Cafe , The Haskell-Beginners > Mailing List - Discussion of primarily beginner-level topics > related > to Haskell > Subject: [Haskell-beginners] The meaning of categories constructed > from HASK > Message-ID: <36383e22-0b32-426d-7c9f-5e611bbca233 at ucdavis.edu> > Content-Type: text/plain; charset=utf-8 > > In category theory, there are many ways one can make new categories out > of an old one. > > In particular, given a category C one can construct: > > 1. The arrows category of C: > arrows in C become objects and > commutative squares in C become arrows > 2. The slice category of C given an object A: > arrows into a distinguished object A become objects in the slice > commutative triangles become arrows > > There are also functors going from C to these new categories (and back). > > Are these constructed categories useful when C = `Hask` (the category of > haskell types and functions)? > What do they represent in programming terms? > > In other words, is there intuition for what the arrows category of Hask is? > What about the slice category of Hask over a specific type? > Do the functors between these match some programming abstractions? > > Any pointers are much appreciated. > > Thanks, > > Dimitri > > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > ------------------------------ > > End of Beginners Digest, Vol 99, Issue 13 > ***************************************** > -- Best Regards, Boon Hui -------------- next part -------------- An HTML attachment was scrubbed... URL: From tushar4r at gmail.com Fri Sep 23 03:31:18 2016 From: tushar4r at gmail.com (Tushar Tyagi) Date: Fri, 23 Sep 2016 09:01:18 +0530 Subject: [Haskell-beginners] Beginners Digest, Vol 99, Issue 13 In-Reply-To: References: Message-ID: You can read more about Numbers here: https://www.haskell.org/tutorial/numbers.html In your implementation sumList and lengthList both return 'Num' which doesn't define a division operator. So you have to convert them into fractional by either changing the signatures of these 2 functions from Num to Fractional, or use fromIntegral function, (or something else) . Two of these approaches have been suggested by people here. :) Typed using my phone, so excuse my brevity. On 23 Sep 2016 6:14 a.m., "Lai Boon Hui" wrote: > Hi All, > > i am overwhelmed by all the helpful responses. Thanks guys. > > I am more curious about why > > meanList :: (Num a, Fractional b) => [a] -> b > meanList xs = (sumList xs) / (lengthList xs) > > does not compile. > > 'a' being a Num type seems perfectly fine, (/) returns a Fractional type > hence 'b' being Fractional seems also fine. > > > > On Fri, Sep 23, 2016 at 7:13 AM, wrote: > >> Send Beginners mailing list submissions to >> beginners at haskell.org >> >> To subscribe or unsubscribe via the World Wide Web, visit >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> or, via email, send a message with subject or body 'help' to >> beginners-request at haskell.org >> >> You can reach the person managing the list at >> beginners-owner at haskell.org >> >> When replying, please edit your Subject line so it is more specific >> than "Re: Contents of Beginners digest..." >> >> >> Today's Topics: >> >> 1. Newbie question about function type constraints (Lai Boon Hui) >> 2. Re: Newbie question about function type constraints >> (Tushar Tyagi) >> 3. Re: Newbie question about function type constraints >> (Imants Cekusins) >> 4. Re: Newbie question about function type constraints >> (Harald Bögeholz) >> 5. Re: Newbie question about function type constraints >> (Sylvain Henry) >> 6. Re: Newbie question about function type constraints >> (Sylvain Henry) >> 7. The meaning of categories constructed from HASK >> (Dimitri DeFigueiredo) >> >> >> ---------------------------------------------------------------------- >> >> Message: 1 >> Date: Thu, 22 Sep 2016 21:19:12 +0800 >> From: Lai Boon Hui >> To: beginners at haskell.org >> Subject: [Haskell-beginners] Newbie question about function type >> constraints >> Message-ID: >> > gmail.com> >> Content-Type: text/plain; charset="utf-8" >> >> Hi, can someone explain to me why i cannot define meanList as: >> >> meanList :: (Integral a, Fractional b) => [a] -> a >> meanList xs = (sumList xs) / (lengthList xs) >> >> I want to restrict the function to only accept lists like [1,2,3] and >> return answer 2.0 >> >> >> sumList :: (Num a) => [a] -> a >> sumList [] = 0 >> sumList (x:xs) = x + (sumList xs) >> >> lengthList :: (Num a) => [t] -> a >> lengthList [] = 0 >> lengthList (_:xs) = 1 + (lengthList xs) >> -------------- next part -------------- >> An HTML attachment was scrubbed... >> URL: > 20160922/45389bc3/attachment-0001.html> >> >> ------------------------------ >> >> Message: 2 >> Date: Thu, 22 Sep 2016 19:36:04 +0530 >> From: Tushar Tyagi >> To: The Haskell-Beginners Mailing List - Discussion of primarily >> beginner-level topics related to Haskell >> Subject: Re: [Haskell-beginners] Newbie question about function type >> constraints >> Message-ID: >> > gmail.com> >> Content-Type: text/plain; charset="utf-8" >> >> What happens if you change the signature of meanList to >> >> meanList :: ( Fractional b) => [b] ->b >> >> The integrals in [1,2,3] would be converted to [1.0, 2.0, 3.0] before you >> act upon them. >> >> On 22 Sep 2016 6:49 p.m., "Lai Boon Hui" wrote: >> >> Hi, can someone explain to me why i cannot define meanList as: >> >> meanList :: (Integral a, Fractional b) => [a] -> a >> meanList xs = (sumList xs) / (lengthList xs) >> >> I want to restrict the function to only accept lists like [1,2,3] and >> return answer 2.0 >> >> >> sumList :: (Num a) => [a] -> a >> sumList [] = 0 >> sumList (x:xs) = x + (sumList xs) >> >> lengthList :: (Num a) => [t] -> a >> lengthList [] = 0 >> lengthList (_:xs) = 1 + (lengthList xs) >> >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> -------------- next part -------------- >> An HTML attachment was scrubbed... >> URL: > 20160922/a092897b/attachment-0001.html> >> >> ------------------------------ >> >> Message: 3 >> Date: Thu, 22 Sep 2016 16:06:52 +0200 >> From: Imants Cekusins >> To: The Haskell-Beginners Mailing List - Discussion of primarily >> beginner-level topics related to Haskell >> Subject: Re: [Haskell-beginners] Newbie question about function type >> constraints >> Message-ID: >> > ail.com> >> Content-Type: text/plain; charset="utf-8" >> >> Hello, >> >> this works too: >> >> ​meanList :: (Fractional a) => [a] -> a >> meanList xs = (sumList xs) / (lengthList xs) >> >> >> sumList :: (Fractional a) => [a] -> a >> sumList [] = 0 >> sumList (x:xs) = x + (sumList xs) >> >> >> lengthList :: (Fractional a) => [t] -> a >> lengthList [] = 0 >> lengthList (_:xs) = 1 + (lengthList xs) >> -------------- next part -------------- >> An HTML attachment was scrubbed... >> URL: > 20160922/4c967a0d/attachment-0001.html> >> >> ------------------------------ >> >> Message: 4 >> Date: Thu, 22 Sep 2016 16:08:56 +0200 >> From: Harald Bögeholz >> To: The Haskell-Beginners Mailing List - Discussion of primarily >> beginner-level topics related to Haskell >> Subject: Re: [Haskell-beginners] Newbie question about function type >> constraints >> Message-ID: >> Content-Type: text/plain; charset=utf-8 >> >> Am 22.09.16 um 15:19 schrieb Lai Boon Hui: >> > Hi, can someone explain to me why i cannot define meanList as: >> > >> > meanList :: (Integral a, Fractional b) => [a] -> a >> > meanList xs = (sumList xs) / (lengthList xs) >> > >> > I want to restrict the function to only accept lists like [1,2,3] and >> > return answer 2.0 >> >> It will work like this: >> >> meanList :: (Integral a, Fractional b) => [a] -> b >> meanList xs = fromIntegral (sumList xs) / (lengthList xs) >> >> You probably meant -> b in the type signature, that was a typo. >> >> And you need to insert fromIntegral to convert to Fractional before you >> can divide. Now that I see it I am beginning to wonder why it works, >> though, because I was just about to insert another fromIntegral before >> lengthList ... >> >> >> > sumList :: (Num a) => [a] -> a >> > sumList [] = 0 >> > sumList (x:xs) = x + (sumList xs) >> > >> > lengthList :: (Num a) => [t] -> a >> > lengthList [] = 0 >> > lengthList (_:xs) = 1 + (lengthList xs) >> >> Hope this helps >> >> >> -- >> Harald Bögeholz (PGP key available from servers) >> Redaktion c't Tel.: +49 511 5352-300 Fax: +49 511 5352-417 >> >> int f[9814],b,c=9814,g,i;long a=1e4,d,e,h; >> main(){for(;b=c,c-=14;i=printf("%04d",e+d/a),e=d%a) >> while(g=--b*2)d=h*b+a*(i?f[b]:a/5),h=d/--g,f[b]=d%g;} >> (Arndt/Haenel) >> >> Affe Apfel Vergaser >> >> /*Heise Medien GmbH & Co. KG * Karl-Wiechert-Allee 10 * 30625 Hannover >> Registergericht: Amtsgericht Hannover HRA 26709 >> Persönlich haftende Gesellschafterin: Heise Medien Geschäftsführung GmbH >> Registergericht: Amtsgericht Hannover, HRB 60405 >> Geschäftsführer: Ansgar Heise, Dr. Alfons Schräder*/ >> >> >> ------------------------------ >> >> Message: 5 >> Date: Thu, 22 Sep 2016 16:10:17 +0200 >> From: Sylvain Henry >> To: beginners at haskell.org >> Subject: Re: [Haskell-beginners] Newbie question about function type >> constraints >> Message-ID: <66b119b3-ff51-74d7-80c5-d19450164508 at haskus.fr> >> Content-Type: text/plain; charset="utf-8"; Format="flowed" >> >> Hi, >> >> You can define it, but in practice there is no instance of "a" that >> satisfies both constraints: Integral a and Fractional a >> > meanList ([1,2,3] :: [Int]) >> >> :4:1: error: >> • No instance for (Fractional Int) arising from a use of ‘meanList’ >> >> > meanList ([1,2,3] :: [Float]) >> >> :5:1: error: >> • No instance for (Integral Float) arising from a use of ‘meanList’ >> >> What you probably want is: >> meanList :: (Integral a, Fractional b) => [a] -> b >> meanList xs = fromIntegral (sumList xs) / fromIntegral (lengthList xs) >> >> Where we convert from the integral type "a" to the fractional type "b" >> before performing the division. >> >> > meanList ([1,2,3] :: [Int]) >> 2.0 >> >> Cheers >> Sylvain >> >> >> On 22/09/2016 15:19, Lai Boon Hui wrote: >> > Hi, can someone explain to me why i cannot define meanList as: >> > >> > meanList :: (Integral a, Fractional b) => [a] -> a >> > meanList xs = (sumList xs) / (lengthList xs) >> > >> > I want to restrict the function to only accept lists like [1,2,3] and >> > return answer 2.0 >> > >> > >> > sumList :: (Num a) => [a] -> a >> > sumList [] = 0 >> > sumList (x:xs) = x + (sumList xs) >> > >> > lengthList :: (Num a) => [t] -> a >> > lengthList [] = 0 >> > lengthList (_:xs) = 1 + (lengthList xs) >> > >> > >> > >> > >> > _______________________________________________ >> > Beginners mailing list >> > Beginners at haskell.org >> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> >> -------------- next part -------------- >> An HTML attachment was scrubbed... >> URL: > 20160922/1be73068/attachment-0001.html> >> >> ------------------------------ >> >> Message: 6 >> Date: Thu, 22 Sep 2016 16:14:48 +0200 >> From: Sylvain Henry >> To: beginners at haskell.org >> Subject: Re: [Haskell-beginners] Newbie question about function type >> constraints >> Message-ID: <074cbf97-2316-9d70-3fb8-7c8c9904c602 at haskus.fr> >> Content-Type: text/plain; charset=utf-8; format=flowed >> >> On 22/09/2016 16:08, Harald Bögeholz wrote: >> > It will work like this: >> > meanList :: (Integral a, Fractional b) => [a] -> b >> > meanList xs = fromIntegral (sumList xs) / (lengthList xs) >> > >> > You probably meant -> b in the type signature, that was a typo. >> > >> > And you need to insert fromIntegral to convert to Fractional before you >> > can divide. Now that I see it I am beginning to wonder why it works, >> > though, because I was just about to insert another fromIntegral before >> > lengthList ... >> It works because in this case lengthList uses the fractional type b to >> perfom its summation (it doesn't care about the type of the elements in >> xs). >> >> Cheers >> Sylvain >> >> >> ------------------------------ >> >> Message: 7 >> Date: Thu, 22 Sep 2016 17:26:36 -0600 >> From: Dimitri DeFigueiredo >> To: Haskell Cafe , The Haskell-Beginners >> Mailing List - Discussion of primarily beginner-level topics >> related >> to Haskell >> Subject: [Haskell-beginners] The meaning of categories constructed >> from HASK >> Message-ID: <36383e22-0b32-426d-7c9f-5e611bbca233 at ucdavis.edu> >> Content-Type: text/plain; charset=utf-8 >> >> In category theory, there are many ways one can make new categories out >> of an old one. >> >> In particular, given a category C one can construct: >> >> 1. The arrows category of C: >> arrows in C become objects and >> commutative squares in C become arrows >> 2. The slice category of C given an object A: >> arrows into a distinguished object A become objects in the slice >> commutative triangles become arrows >> >> There are also functors going from C to these new categories (and back). >> >> Are these constructed categories useful when C = `Hask` (the category of >> haskell types and functions)? >> What do they represent in programming terms? >> >> In other words, is there intuition for what the arrows category of Hask >> is? >> What about the slice category of Hask over a specific type? >> Do the functors between these match some programming abstractions? >> >> Any pointers are much appreciated. >> >> Thanks, >> >> Dimitri >> >> >> >> >> ------------------------------ >> >> Subject: Digest Footer >> >> _______________________________________________ >> Beginners mailing list >> Beginners at haskell.org >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners >> >> >> ------------------------------ >> >> End of Beginners Digest, Vol 99, Issue 13 >> ***************************************** >> > > > > -- > Best Regards, > Boon Hui > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From laiboonh at gmail.com Fri Sep 23 12:53:04 2016 From: laiboonh at gmail.com (Lai Boon Hui) Date: Fri, 23 Sep 2016 20:53:04 +0800 Subject: [Haskell-beginners] Beginners Digest, Vol 99, Issue 15 In-Reply-To: References: Message-ID: Hi, still does not compile sumList :: (Fractional a) => [a] -> a sumList [] = 0 sumList (x:xs) = x + (sumList xs) lengthList :: (Fractional a) => [t] -> a lengthList [] = 0 lengthList (_:xs) = 1 + (lengthList xs) meanList :: (Num a, Fractional b) => [a] -> b meanList xs = (sumList xs) / (lengthList xs) i know meanList :: (Fractional b) => [b] -> b meanList xs = (sumList xs) / (lengthList xs) compiles. But why do i have to restrict the inputs to be a list of Fractionals and not Nums?? On Fri, Sep 23, 2016 at 11:18 AM, wrote: > Send Beginners mailing list submissions to > beginners at haskell.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > or, via email, send a message with subject or body 'help' to > beginners-request at haskell.org > > You can reach the person managing the list at > beginners-owner at haskell.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Beginners digest..." > > > Today's Topics: > > 1. Re: Beginners Digest, Vol 99, Issue 13 (Tushar Tyagi) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Fri, 23 Sep 2016 09:01:18 +0530 > From: Tushar Tyagi > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > Subject: Re: [Haskell-beginners] Beginners Digest, Vol 99, Issue 13 > Message-ID: > rEuMNN_Kg at mail.gmail.com> > Content-Type: text/plain; charset="utf-8" > > You can read more about Numbers here: > https://www.haskell.org/tutorial/numbers.html > > In your implementation sumList and lengthList both return 'Num' which > doesn't define a division operator. So you have to convert them into > fractional by either changing the signatures of these 2 functions from Num > to Fractional, or use fromIntegral function, (or something else) . Two of > these approaches have been suggested by people here. :) > > Typed using my phone, so excuse my brevity. > > On 23 Sep 2016 6:14 a.m., "Lai Boon Hui" wrote: > > > Hi All, > > > > i am overwhelmed by all the helpful responses. Thanks guys. > > > > I am more curious about why > > > > meanList :: (Num a, Fractional b) => [a] -> b > > meanList xs = (sumList xs) / (lengthList xs) > > > > does not compile. > > > > 'a' being a Num type seems perfectly fine, (/) returns a Fractional type > > hence 'b' being Fractional seems also fine. > > > > > > > > On Fri, Sep 23, 2016 at 7:13 AM, wrote: > > > >> Send Beginners mailing list submissions to > >> beginners at haskell.org > >> > >> To subscribe or unsubscribe via the World Wide Web, visit > >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > >> or, via email, send a message with subject or body 'help' to > >> beginners-request at haskell.org > >> > >> You can reach the person managing the list at > >> beginners-owner at haskell.org > >> > >> When replying, please edit your Subject line so it is more specific > >> than "Re: Contents of Beginners digest..." > >> > >> > >> Today's Topics: > >> > >> 1. Newbie question about function type constraints (Lai Boon Hui) > >> 2. Re: Newbie question about function type constraints > >> (Tushar Tyagi) > >> 3. Re: Newbie question about function type constraints > >> (Imants Cekusins) > >> 4. Re: Newbie question about function type constraints > >> (Harald Bögeholz) > >> 5. Re: Newbie question about function type constraints > >> (Sylvain Henry) > >> 6. Re: Newbie question about function type constraints > >> (Sylvain Henry) > >> 7. The meaning of categories constructed from HASK > >> (Dimitri DeFigueiredo) > >> > >> > >> ---------------------------------------------------------------------- > >> > >> Message: 1 > >> Date: Thu, 22 Sep 2016 21:19:12 +0800 > >> From: Lai Boon Hui > >> To: beginners at haskell.org > >> Subject: [Haskell-beginners] Newbie question about function type > >> constraints > >> Message-ID: > >> >> gmail.com> > >> Content-Type: text/plain; charset="utf-8" > >> > >> Hi, can someone explain to me why i cannot define meanList as: > >> > >> meanList :: (Integral a, Fractional b) => [a] -> a > >> meanList xs = (sumList xs) / (lengthList xs) > >> > >> I want to restrict the function to only accept lists like [1,2,3] and > >> return answer 2.0 > >> > >> > >> sumList :: (Num a) => [a] -> a > >> sumList [] = 0 > >> sumList (x:xs) = x + (sumList xs) > >> > >> lengthList :: (Num a) => [t] -> a > >> lengthList [] = 0 > >> lengthList (_:xs) = 1 + (lengthList xs) > >> -------------- next part -------------- > >> An HTML attachment was scrubbed... > >> URL: >> 20160922/45389bc3/attachment-0001.html> > >> > >> ------------------------------ > >> > >> Message: 2 > >> Date: Thu, 22 Sep 2016 19:36:04 +0530 > >> From: Tushar Tyagi > >> To: The Haskell-Beginners Mailing List - Discussion of primarily > >> beginner-level topics related to Haskell > > >> Subject: Re: [Haskell-beginners] Newbie question about function type > >> constraints > >> Message-ID: > >> >> gmail.com> > >> Content-Type: text/plain; charset="utf-8" > >> > >> What happens if you change the signature of meanList to > >> > >> meanList :: ( Fractional b) => [b] ->b > >> > >> The integrals in [1,2,3] would be converted to [1.0, 2.0, 3.0] before > you > >> act upon them. > >> > >> On 22 Sep 2016 6:49 p.m., "Lai Boon Hui" wrote: > >> > >> Hi, can someone explain to me why i cannot define meanList as: > >> > >> meanList :: (Integral a, Fractional b) => [a] -> a > >> meanList xs = (sumList xs) / (lengthList xs) > >> > >> I want to restrict the function to only accept lists like [1,2,3] and > >> return answer 2.0 > >> > >> > >> sumList :: (Num a) => [a] -> a > >> sumList [] = 0 > >> sumList (x:xs) = x + (sumList xs) > >> > >> lengthList :: (Num a) => [t] -> a > >> lengthList [] = 0 > >> lengthList (_:xs) = 1 + (lengthList xs) > >> > >> > >> > >> _______________________________________________ > >> Beginners mailing list > >> Beginners at haskell.org > >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > >> -------------- next part -------------- > >> An HTML attachment was scrubbed... > >> URL: >> 20160922/a092897b/attachment-0001.html> > >> > >> ------------------------------ > >> > >> Message: 3 > >> Date: Thu, 22 Sep 2016 16:06:52 +0200 > >> From: Imants Cekusins > >> To: The Haskell-Beginners Mailing List - Discussion of primarily > >> beginner-level topics related to Haskell > > >> Subject: Re: [Haskell-beginners] Newbie question about function type > >> constraints > >> Message-ID: > >> >> ail.com> > >> Content-Type: text/plain; charset="utf-8" > >> > >> Hello, > >> > >> this works too: > >> > >> ​meanList :: (Fractional a) => [a] -> a > >> meanList xs = (sumList xs) / (lengthList xs) > >> > >> > >> sumList :: (Fractional a) => [a] -> a > >> sumList [] = 0 > >> sumList (x:xs) = x + (sumList xs) > >> > >> > >> lengthList :: (Fractional a) => [t] -> a > >> lengthList [] = 0 > >> lengthList (_:xs) = 1 + (lengthList xs) > >> -------------- next part -------------- > >> An HTML attachment was scrubbed... > >> URL: >> 20160922/4c967a0d/attachment-0001.html> > >> > >> ------------------------------ > >> > >> Message: 4 > >> Date: Thu, 22 Sep 2016 16:08:56 +0200 > >> From: Harald Bögeholz > >> To: The Haskell-Beginners Mailing List - Discussion of primarily > >> beginner-level topics related to Haskell > > >> Subject: Re: [Haskell-beginners] Newbie question about function type > >> constraints > >> Message-ID: > >> Content-Type: text/plain; charset=utf-8 > >> > >> Am 22.09.16 um 15:19 schrieb Lai Boon Hui: > >> > Hi, can someone explain to me why i cannot define meanList as: > >> > > >> > meanList :: (Integral a, Fractional b) => [a] -> a > >> > meanList xs = (sumList xs) / (lengthList xs) > >> > > >> > I want to restrict the function to only accept lists like [1,2,3] and > >> > return answer 2.0 > >> > >> It will work like this: > >> > >> meanList :: (Integral a, Fractional b) => [a] -> b > >> meanList xs = fromIntegral (sumList xs) / (lengthList xs) > >> > >> You probably meant -> b in the type signature, that was a typo. > >> > >> And you need to insert fromIntegral to convert to Fractional before you > >> can divide. Now that I see it I am beginning to wonder why it works, > >> though, because I was just about to insert another fromIntegral before > >> lengthList ... > >> > >> > >> > sumList :: (Num a) => [a] -> a > >> > sumList [] = 0 > >> > sumList (x:xs) = x + (sumList xs) > >> > > >> > lengthList :: (Num a) => [t] -> a > >> > lengthList [] = 0 > >> > lengthList (_:xs) = 1 + (lengthList xs) > >> > >> Hope this helps > >> > >> > >> -- > >> Harald Bögeholz (PGP key available from servers) > >> Redaktion c't Tel.: +49 511 5352-300 Fax: +49 511 5352-417 > >> > >> int f[9814],b,c=9814,g,i;long a=1e4,d,e,h; > >> main(){for(;b=c,c-=14;i=printf("%04d",e+d/a),e=d%a) > >> while(g=--b*2)d=h*b+a*(i?f[b]: > a/5),h=d/--g,f[b]=d%g;} > >> (Arndt/Haenel) > >> > >> Affe Apfel Vergaser > >> > >> /*Heise Medien GmbH & Co. KG * Karl-Wiechert-Allee 10 * 30625 Hannover > >> Registergericht: Amtsgericht Hannover HRA 26709 > >> Persönlich haftende Gesellschafterin: Heise Medien Geschäftsführung GmbH > >> Registergericht: Amtsgericht Hannover, HRB 60405 > >> Geschäftsführer: Ansgar Heise, Dr. Alfons Schräder*/ > >> > >> > >> ------------------------------ > >> > >> Message: 5 > >> Date: Thu, 22 Sep 2016 16:10:17 +0200 > >> From: Sylvain Henry > >> To: beginners at haskell.org > >> Subject: Re: [Haskell-beginners] Newbie question about function type > >> constraints > >> Message-ID: <66b119b3-ff51-74d7-80c5-d19450164508 at haskus.fr> > >> Content-Type: text/plain; charset="utf-8"; Format="flowed" > >> > >> Hi, > >> > >> You can define it, but in practice there is no instance of "a" that > >> satisfies both constraints: Integral a and Fractional a > >> > meanList ([1,2,3] :: [Int]) > >> > >> :4:1: error: > >> • No instance for (Fractional Int) arising from a use of ‘meanList’ > >> > >> > meanList ([1,2,3] :: [Float]) > >> > >> :5:1: error: > >> • No instance for (Integral Float) arising from a use of ‘meanList’ > >> > >> What you probably want is: > >> meanList :: (Integral a, Fractional b) => [a] -> b > >> meanList xs = fromIntegral (sumList xs) / fromIntegral (lengthList xs) > >> > >> Where we convert from the integral type "a" to the fractional type "b" > >> before performing the division. > >> > >> > meanList ([1,2,3] :: [Int]) > >> 2.0 > >> > >> Cheers > >> Sylvain > >> > >> > >> On 22/09/2016 15:19, Lai Boon Hui wrote: > >> > Hi, can someone explain to me why i cannot define meanList as: > >> > > >> > meanList :: (Integral a, Fractional b) => [a] -> a > >> > meanList xs = (sumList xs) / (lengthList xs) > >> > > >> > I want to restrict the function to only accept lists like [1,2,3] and > >> > return answer 2.0 > >> > > >> > > >> > sumList :: (Num a) => [a] -> a > >> > sumList [] = 0 > >> > sumList (x:xs) = x + (sumList xs) > >> > > >> > lengthList :: (Num a) => [t] -> a > >> > lengthList [] = 0 > >> > lengthList (_:xs) = 1 + (lengthList xs) > >> > > >> > > >> > > >> > > >> > _______________________________________________ > >> > Beginners mailing list > >> > Beginners at haskell.org > >> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > >> > >> -------------- next part -------------- > >> An HTML attachment was scrubbed... > >> URL: >> 20160922/1be73068/attachment-0001.html> > >> > >> ------------------------------ > >> > >> Message: 6 > >> Date: Thu, 22 Sep 2016 16:14:48 +0200 > >> From: Sylvain Henry > >> To: beginners at haskell.org > >> Subject: Re: [Haskell-beginners] Newbie question about function type > >> constraints > >> Message-ID: <074cbf97-2316-9d70-3fb8-7c8c9904c602 at haskus.fr> > >> Content-Type: text/plain; charset=utf-8; format=flowed > >> > >> On 22/09/2016 16:08, Harald Bögeholz wrote: > >> > It will work like this: > >> > meanList :: (Integral a, Fractional b) => [a] -> b > >> > meanList xs = fromIntegral (sumList xs) / (lengthList xs) > >> > > >> > You probably meant -> b in the type signature, that was a typo. > >> > > >> > And you need to insert fromIntegral to convert to Fractional before > you > >> > can divide. Now that I see it I am beginning to wonder why it works, > >> > though, because I was just about to insert another fromIntegral before > >> > lengthList ... > >> It works because in this case lengthList uses the fractional type b to > >> perfom its summation (it doesn't care about the type of the elements in > >> xs). > >> > >> Cheers > >> Sylvain > >> > >> > >> ------------------------------ > >> > >> Message: 7 > >> Date: Thu, 22 Sep 2016 17:26:36 -0600 > >> From: Dimitri DeFigueiredo > >> To: Haskell Cafe , The Haskell-Beginners > >> Mailing List - Discussion of primarily beginner-level topics > >> related > >> to Haskell > >> Subject: [Haskell-beginners] The meaning of categories constructed > >> from HASK > >> Message-ID: <36383e22-0b32-426d-7c9f-5e611bbca233 at ucdavis.edu> > >> Content-Type: text/plain; charset=utf-8 > >> > >> In category theory, there are many ways one can make new categories out > >> of an old one. > >> > >> In particular, given a category C one can construct: > >> > >> 1. The arrows category of C: > >> arrows in C become objects and > >> commutative squares in C become arrows > >> 2. The slice category of C given an object A: > >> arrows into a distinguished object A become objects in the slice > >> commutative triangles become arrows > >> > >> There are also functors going from C to these new categories (and back). > >> > >> Are these constructed categories useful when C = `Hask` (the category of > >> haskell types and functions)? > >> What do they represent in programming terms? > >> > >> In other words, is there intuition for what the arrows category of Hask > >> is? > >> What about the slice category of Hask over a specific type? > >> Do the functors between these match some programming abstractions? > >> > >> Any pointers are much appreciated. > >> > >> Thanks, > >> > >> Dimitri > >> > >> > >> > >> > >> ------------------------------ > >> > >> Subject: Digest Footer > >> > >> _______________________________________________ > >> Beginners mailing list > >> Beginners at haskell.org > >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > >> > >> > >> ------------------------------ > >> > >> End of Beginners Digest, Vol 99, Issue 13 > >> ***************************************** > >> > > > > > > > > -- > > Best Regards, > > Boon Hui > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: attachments/20160923/7b6523b3/attachment.html> > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > ------------------------------ > > End of Beginners Digest, Vol 99, Issue 15 > ***************************************** > -- Best Regards, Boon Hui -------------- next part -------------- An HTML attachment was scrubbed... URL: From arjanen.loic at gmail.com Fri Sep 23 14:00:45 2016 From: arjanen.loic at gmail.com (=?UTF-8?Q?ARJANEN_Lo=c3=afc_Jean_David?=) Date: Fri, 23 Sep 2016 21:00:45 +0700 Subject: [Haskell-beginners] Newbie question about function type constraints In-Reply-To: References: Message-ID: Because if you say that your function accept all types which are instances of Num, then it must accept *all* of them. Even those who do not define a division, or whose division has a different behaviour than the fractional one. Le 23/09/2016 à 19:53, Lai Boon Hui a écrit : > Hi, > > still does not compile > > sumList :: (Fractional a) => [a] -> a > sumList [] = 0 > sumList (x:xs) = x + (sumList xs) > > lengthList :: (Fractional a) => [t] -> a > lengthList [] = 0 > lengthList (_:xs) = 1 + (lengthList xs) > > meanList :: (Num a, Fractional b) => [a] -> b > meanList xs = (sumList xs) / (lengthList xs) > > i know > > meanList :: (Fractional b) => [b] -> b > meanList xs = (sumList xs) / (lengthList xs) > > compiles. But why do i have to restrict the inputs to be a list of > Fractionals and not Nums?? > > > On Fri, Sep 23, 2016 at 11:18 AM, > wrote: > > Send Beginners mailing list submissions to > beginners at haskell.org > > To subscribe or unsubscribe via the World Wide Web, visit > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > or, via email, send a message with subject or body 'help' to > beginners-request at haskell.org > > You can reach the person managing the list at > beginners-owner at haskell.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Beginners digest..." > > > Today's Topics: > > 1. Re: Beginners Digest, Vol 99, Issue 13 (Tushar Tyagi) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Fri, 23 Sep 2016 09:01:18 +0530 > From: Tushar Tyagi > > To: The Haskell-Beginners Mailing List - Discussion of primarily > beginner-level topics related to Haskell > > > Subject: Re: [Haskell-beginners] Beginners Digest, Vol 99, Issue 13 > Message-ID: > > > > Content-Type: text/plain; charset="utf-8" > > You can read more about Numbers here: > https://www.haskell.org/tutorial/numbers.html > > > In your implementation sumList and lengthList both return 'Num' which > doesn't define a division operator. So you have to convert them into > fractional by either changing the signatures of these 2 functions > from Num > to Fractional, or use fromIntegral function, (or something else) . > Two of > these approaches have been suggested by people here. :) > > Typed using my phone, so excuse my brevity. > > On 23 Sep 2016 6:14 a.m., "Lai Boon Hui" > wrote: > > > Hi All, > > > > i am overwhelmed by all the helpful responses. Thanks guys. > > > > I am more curious about why > > > > meanList :: (Num a, Fractional b) => [a] -> b > > meanList xs = (sumList xs) / (lengthList xs) > > > > does not compile. > > > > 'a' being a Num type seems perfectly fine, (/) returns a > Fractional type > > hence 'b' being Fractional seems also fine. > > > > > > > > On Fri, Sep 23, 2016 at 7:13 AM, > wrote: > > > >> Send Beginners mailing list submissions to > >> beginners at haskell.org > >> > >> To subscribe or unsubscribe via the World Wide Web, visit > >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > >> or, via email, send a message with subject or body 'help' to > >> beginners-request at haskell.org > > >> > >> You can reach the person managing the list at > >> beginners-owner at haskell.org > >> > >> When replying, please edit your Subject line so it is more specific > >> than "Re: Contents of Beginners digest..." > >> > >> > >> Today's Topics: > >> > >> 1. Newbie question about function type constraints (Lai > Boon Hui) > >> 2. Re: Newbie question about function type constraints > >> (Tushar Tyagi) > >> 3. Re: Newbie question about function type constraints > >> (Imants Cekusins) > >> 4. Re: Newbie question about function type constraints > >> (Harald Bögeholz) > >> 5. Re: Newbie question about function type constraints > >> (Sylvain Henry) > >> 6. Re: Newbie question about function type constraints > >> (Sylvain Henry) > >> 7. The meaning of categories constructed from HASK > >> (Dimitri DeFigueiredo) > >> > >> > >> > ---------------------------------------------------------------------- > >> > >> Message: 1 > >> Date: Thu, 22 Sep 2016 21:19:12 +0800 > >> From: Lai Boon Hui > > >> To: beginners at haskell.org > >> Subject: [Haskell-beginners] Newbie question about function type > >> constraints > >> Message-ID: > >> >> gmail.com > > >> Content-Type: text/plain; charset="utf-8" > >> > >> Hi, can someone explain to me why i cannot define meanList as: > >> > >> meanList :: (Integral a, Fractional b) => [a] -> a > >> meanList xs = (sumList xs) / (lengthList xs) > >> > >> I want to restrict the function to only accept lists like > [1,2,3] and > >> return answer 2.0 > >> > >> > >> sumList :: (Num a) => [a] -> a > >> sumList [] = 0 > >> sumList (x:xs) = x + (sumList xs) > >> > >> lengthList :: (Num a) => [t] -> a > >> lengthList [] = 0 > >> lengthList (_:xs) = 1 + (lengthList xs) > >> -------------- next part -------------- > >> An HTML attachment was scrubbed... > >> URL: > >> 20160922/45389bc3/attachment-0001.html> > >> > >> ------------------------------ > >> > >> Message: 2 > >> Date: Thu, 22 Sep 2016 19:36:04 +0530 > >> From: Tushar Tyagi > > >> To: The Haskell-Beginners Mailing List - Discussion of primarily > >> beginner-level topics related to Haskell > > > >> Subject: Re: [Haskell-beginners] Newbie question about function > type > >> constraints > >> Message-ID: > >> >> gmail.com > > >> Content-Type: text/plain; charset="utf-8" > >> > >> What happens if you change the signature of meanList to > >> > >> meanList :: ( Fractional b) => [b] ->b > >> > >> The integrals in [1,2,3] would be converted to [1.0, 2.0, 3.0] > before you > >> act upon them. > >> > >> On 22 Sep 2016 6:49 p.m., "Lai Boon Hui" > wrote: > >> > >> Hi, can someone explain to me why i cannot define meanList as: > >> > >> meanList :: (Integral a, Fractional b) => [a] -> a > >> meanList xs = (sumList xs) / (lengthList xs) > >> > >> I want to restrict the function to only accept lists like > [1,2,3] and > >> return answer 2.0 > >> > >> > >> sumList :: (Num a) => [a] -> a > >> sumList [] = 0 > >> sumList (x:xs) = x + (sumList xs) > >> > >> lengthList :: (Num a) => [t] -> a > >> lengthList [] = 0 > >> lengthList (_:xs) = 1 + (lengthList xs) > >> > >> > >> > >> _______________________________________________ > >> Beginners mailing list > >> Beginners at haskell.org > >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > >> -------------- next part -------------- > >> An HTML attachment was scrubbed... > >> URL: > >> 20160922/a092897b/attachment-0001.html> > >> > >> ------------------------------ > >> > >> Message: 3 > >> Date: Thu, 22 Sep 2016 16:06:52 +0200 > >> From: Imants Cekusins > > >> To: The Haskell-Beginners Mailing List - Discussion of primarily > >> beginner-level topics related to Haskell > > > >> Subject: Re: [Haskell-beginners] Newbie question about function > type > >> constraints > >> Message-ID: > >> > > >> ail.com > > >> Content-Type: text/plain; charset="utf-8" > >> > >> Hello, > >> > >> this works too: > >> > >> ​meanList :: (Fractional a) => [a] -> a > >> meanList xs = (sumList xs) / (lengthList xs) > >> > >> > >> sumList :: (Fractional a) => [a] -> a > >> sumList [] = 0 > >> sumList (x:xs) = x + (sumList xs) > >> > >> > >> lengthList :: (Fractional a) => [t] -> a > >> lengthList [] = 0 > >> lengthList (_:xs) = 1 + (lengthList xs) > >> -------------- next part -------------- > >> An HTML attachment was scrubbed... > >> URL: > >> 20160922/4c967a0d/attachment-0001.html> > >> > >> ------------------------------ > >> > >> Message: 4 > >> Date: Thu, 22 Sep 2016 16:08:56 +0200 > >> From: Harald Bögeholz > > >> To: The Haskell-Beginners Mailing List - Discussion of primarily > >> beginner-level topics related to Haskell > > > >> Subject: Re: [Haskell-beginners] Newbie question about function > type > >> constraints > >> Message-ID: > > >> Content-Type: text/plain; charset=utf-8 > >> > >> Am 22.09.16 um 15:19 schrieb Lai Boon Hui: > >> > Hi, can someone explain to me why i cannot define meanList as: > >> > > >> > meanList :: (Integral a, Fractional b) => [a] -> a > >> > meanList xs = (sumList xs) / (lengthList xs) > >> > > >> > I want to restrict the function to only accept lists like > [1,2,3] and > >> > return answer 2.0 > >> > >> It will work like this: > >> > >> meanList :: (Integral a, Fractional b) => [a] -> b > >> meanList xs = fromIntegral (sumList xs) / (lengthList xs) > >> > >> You probably meant -> b in the type signature, that was a typo. > >> > >> And you need to insert fromIntegral to convert to Fractional > before you > >> can divide. Now that I see it I am beginning to wonder why it > works, > >> though, because I was just about to insert another fromIntegral > before > >> lengthList ... > >> > >> > >> > sumList :: (Num a) => [a] -> a > >> > sumList [] = 0 > >> > sumList (x:xs) = x + (sumList xs) > >> > > >> > lengthList :: (Num a) => [t] -> a > >> > lengthList [] = 0 > >> > lengthList (_:xs) = 1 + (lengthList xs) > >> > >> Hope this helps > >> > >> > >> -- > >> Harald Bögeholz > (PGP key > available from servers) > >> Redaktion c't Tel.: +49 511 5352-300 > Fax: +49 511 5352-417 > > >> > >> int f[9814],b,c=9814,g,i;long a=1e4,d,e,h; > >> > main(){for(;b=c,c-=14;i=printf("%04d",e+d/a),e=d%a) > >> while(g=--b*2)d=h*b+a*(i?f[b]:a/5),h=d/--g,f[b]=d%g;} > >> (Arndt/Haenel) > >> > >> Affe Apfel Vergaser > >> > >> /*Heise Medien GmbH & Co. KG * Karl-Wiechert-Allee 10 * 30625 > Hannover > >> Registergericht: Amtsgericht Hannover HRA 26709 > >> Persönlich haftende Gesellschafterin: Heise Medien > Geschäftsführung GmbH > >> Registergericht: Amtsgericht Hannover, HRB 60405 > >> Geschäftsführer: Ansgar Heise, Dr. Alfons Schräder*/ > >> > >> > >> ------------------------------ > >> > >> Message: 5 > >> Date: Thu, 22 Sep 2016 16:10:17 +0200 > >> From: Sylvain Henry > > >> To: beginners at haskell.org > >> Subject: Re: [Haskell-beginners] Newbie question about function > type > >> constraints > >> Message-ID: <66b119b3-ff51-74d7-80c5-d19450164508 at haskus.fr > > > >> Content-Type: text/plain; charset="utf-8"; Format="flowed" > >> > >> Hi, > >> > >> You can define it, but in practice there is no instance of "a" that > >> satisfies both constraints: Integral a and Fractional a > >> > meanList ([1,2,3] :: [Int]) > >> > >> :4:1: error: > >> • No instance for (Fractional Int) arising from a use of > ‘meanList’ > >> > >> > meanList ([1,2,3] :: [Float]) > >> > >> :5:1: error: > >> • No instance for (Integral Float) arising from a use of > ‘meanList’ > >> > >> What you probably want is: > >> meanList :: (Integral a, Fractional b) => [a] -> b > >> meanList xs = fromIntegral (sumList xs) / fromIntegral > (lengthList xs) > >> > >> Where we convert from the integral type "a" to the fractional > type "b" > >> before performing the division. > >> > >> > meanList ([1,2,3] :: [Int]) > >> 2.0 > >> > >> Cheers > >> Sylvain > >> > >> > >> On 22/09/2016 15:19, Lai Boon Hui wrote: > >> > Hi, can someone explain to me why i cannot define meanList as: > >> > > >> > meanList :: (Integral a, Fractional b) => [a] -> a > >> > meanList xs = (sumList xs) / (lengthList xs) > >> > > >> > I want to restrict the function to only accept lists like > [1,2,3] and > >> > return answer 2.0 > >> > > >> > > >> > sumList :: (Num a) => [a] -> a > >> > sumList [] = 0 > >> > sumList (x:xs) = x + (sumList xs) > >> > > >> > lengthList :: (Num a) => [t] -> a > >> > lengthList [] = 0 > >> > lengthList (_:xs) = 1 + (lengthList xs) > >> > > >> > > >> > > >> > > >> > _______________________________________________ > >> > Beginners mailing list > >> > Beginners at haskell.org > >> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > >> > >> -------------- next part -------------- > >> An HTML attachment was scrubbed... > >> URL: > >> 20160922/1be73068/attachment-0001.html> > >> > >> ------------------------------ > >> > >> Message: 6 > >> Date: Thu, 22 Sep 2016 16:14:48 +0200 > >> From: Sylvain Henry > > >> To: beginners at haskell.org > >> Subject: Re: [Haskell-beginners] Newbie question about function > type > >> constraints > >> Message-ID: <074cbf97-2316-9d70-3fb8-7c8c9904c602 at haskus.fr > > > >> Content-Type: text/plain; charset=utf-8; format=flowed > >> > >> On 22/09/2016 16:08, Harald Bögeholz wrote: > >> > It will work like this: > >> > meanList :: (Integral a, Fractional b) => [a] -> b > >> > meanList xs = fromIntegral (sumList xs) / (lengthList xs) > >> > > >> > You probably meant -> b in the type signature, that was a typo. > >> > > >> > And you need to insert fromIntegral to convert to Fractional > before you > >> > can divide. Now that I see it I am beginning to wonder why it > works, > >> > though, because I was just about to insert another > fromIntegral before > >> > lengthList ... > >> It works because in this case lengthList uses the fractional > type b to > >> perfom its summation (it doesn't care about the type of the > elements in > >> xs). > >> > >> Cheers > >> Sylvain > >> > >> > >> ------------------------------ > >> > >> Message: 7 > >> Date: Thu, 22 Sep 2016 17:26:36 -0600 > >> From: Dimitri DeFigueiredo > > >> To: Haskell Cafe >, The Haskell-Beginners > >> Mailing List - Discussion of primarily beginner-level > topics > >> related > >> to Haskell > > >> Subject: [Haskell-beginners] The meaning of categories constructed > >> from HASK > >> Message-ID: <36383e22-0b32-426d-7c9f-5e611bbca233 at ucdavis.edu > > > >> Content-Type: text/plain; charset=utf-8 > >> > >> In category theory, there are many ways one can make new > categories out > >> of an old one. > >> > >> In particular, given a category C one can construct: > >> > >> 1. The arrows category of C: > >> arrows in C become objects and > >> commutative squares in C become arrows > >> 2. The slice category of C given an object A: > >> arrows into a distinguished object A become objects in > the slice > >> commutative triangles become arrows > >> > >> There are also functors going from C to these new categories > (and back). > >> > >> Are these constructed categories useful when C = `Hask` (the > category of > >> haskell types and functions)? > >> What do they represent in programming terms? > >> > >> In other words, is there intuition for what the arrows category > of Hask > >> is? > >> What about the slice category of Hask over a specific type? > >> Do the functors between these match some programming abstractions? > >> > >> Any pointers are much appreciated. > >> > >> Thanks, > >> > >> Dimitri > >> > >> > >> > >> > >> ------------------------------ > >> > >> Subject: Digest Footer > >> > >> _______________________________________________ > >> Beginners mailing list > >> Beginners at haskell.org > >> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > >> > >> > >> ------------------------------ > >> > >> End of Beginners Digest, Vol 99, Issue 13 > >> ***************************************** > >> > > > > > > > > -- > > Best Regards, > > Boon Hui > > > > _______________________________________________ > > Beginners mailing list > > Beginners at haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > > > -------------- next part -------------- > An HTML attachment was scrubbed... > URL: > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > ------------------------------ > > End of Beginners Digest, Vol 99, Issue 15 > ***************************************** > > > > > -- > Best Regards, > Boon Hui -- ARJANEN Loïc Jean David http://blog.luigiscorner.com --- “Computer science is no more about computers than astronomy is about telescopes, biology is about microscopes, or chemistry is about beakers and test tubes. Science is not about tools. It is about how we use them, and what we find out when we do.” Michael R. Fellows and Ian Parberry -------------- next part -------------- An HTML attachment was scrubbed... URL: From frederic-emmanuel.picca at synchrotron-soleil.fr Sat Sep 24 11:15:55 2016 From: frederic-emmanuel.picca at synchrotron-soleil.fr (PICCA Frederic-Emmanuel) Date: Sat, 24 Sep 2016 11:15:55 +0000 Subject: [Haskell-beginners] cabal optional build-depends Message-ID: Hello the hmatrix package was splitte hmatrix -> hmatrix, hmatrix-gsl, ... for version > 0.16 I use Debian stable and need to support hmatrix 0.15 (with no split) and hmatrix >= 0.17 (splitted) I would like to know how to suport this in the cabal file only hmatrix if >= 0.15 && < 0.16 and hmatrix + hmatrix-gsl if hmatrix > 0.17 thanks for your help Frederic From 50295 at web.de Sat Sep 24 13:54:08 2016 From: 50295 at web.de (Olumide) Date: Sat, 24 Sep 2016 14:54:08 +0100 Subject: [Haskell-beginners] Why is :t 42 ::Num a => a -> a ? Message-ID: <5463264a-8806-1473-4835-6438a6da0d3e@web.de> Is 42 a function? And if it is why does 42 + 24 work, if both are functions. I know there's something I'm getting wrong here. Also, is the expression abs -42 an error. I initially thought that abs to the operator (-) first but there is no space between - and 42. Or is there some associativity issue going on here. - Olumide From sylvain at haskus.fr Sat Sep 24 14:05:37 2016 From: sylvain at haskus.fr (Sylvain Henry) Date: Sat, 24 Sep 2016 16:05:37 +0200 Subject: [Haskell-beginners] Why is :t 42 ::Num a => a -> a ? In-Reply-To: <5463264a-8806-1473-4835-6438a6da0d3e@web.de> References: <5463264a-8806-1473-4835-6438a6da0d3e@web.de> Message-ID: Hi, > :t 42 42 :: Num t => t The parser has to make some choices to distinguish between negate and subtraction. In ambiguous cases, subtraction is chosen: > abs -42 :2:1: error: • Non type-variable argument in the constraint: Num (a -> a) (Use FlexibleContexts to permit this) • When checking the inferred type it :: forall a. (Num (a -> a), Num a) => a -> a > abs (-42) 42 > let abs=10 > abs-42 -32 > abs -42 -32 > abs - 42 -32 > abs (-42) :15:1: error: • Non type-variable argument in the constraint: Num (t -> t1) (Use FlexibleContexts to permit this) • When checking the inferred type it :: forall t t1. (Num (t -> t1), Num t) => t1 It leads to some confusion: > fmap (+ 42) [1,2] [43,44] > fmap (- 42) [1,2] :12:1: error: • Non type-variable argument in the constraint: Num (a -> b) (Use FlexibleContexts to permit this) • When checking the inferred type it :: forall a b. (Num (a -> b), Num a) => [b] > fmap (flip (-) 42) [1,2] [-41,-40] Cheers Sylvain On 24/09/2016 15:54, Olumide wrote: > Is 42 a function? And if it is why does 42 + 24 work, if both are > functions. I know there's something I'm getting wrong here. > > Also, is the expression abs -42 an error. I initially thought that abs > to the operator (-) first but there is no space between - and 42. Or > is there some associativity issue going on here. > > - Olumide > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners From hillaryryan92 at gmail.com Sun Sep 25 04:49:48 2016 From: hillaryryan92 at gmail.com (Hillary Ryan) Date: Sat, 24 Sep 2016 23:49:48 -0500 Subject: [Haskell-beginners] Exception/Error Handling Best Practices Message-ID: Dear Haskellers, I've been working on variants of the following code: connect_from_file :: String -> IO Connection connect_from_file fi = do s <- BL.readFile fi cred <- (decode s :: Maybe Db_cred) c <- connect . reformat_cred $ cred return c What this code intends to do is read a config from a file in JSON, decode the JSON, and then open a connection based the decoded info. This code doesn't compile for several reasons, but I've left it in this form because I believe it is the most convenient for approaching my question: How should I handle errors here? Both readFile (from Bytestring) and connect (from mysql.simple) throw exceptions when things don't go their way, and decode uses maybe. I see four ways of handling these errors: 0) *All typed errors: *I could "capture" all the exceptions in Either types and use an EitherIO monad or transformer to automatically abort when a Left value appears. Now my function looks like: String -> IO (Either MyErrorType Connection) 1) *All exceptions: *I could throw an exception if Nothing appears during decoding. The function looks the same: String -> IO Connection 2) *A mix*: I could leave the connect and readFile functions alone, but return IO (Nothing) if the decoding doesn't work out. Now we have: String -> IO (Maybe Connection) 3) Not 0)-2) So what is the proper way to do things? Many Thanks, Hillary PS For what it is worth, I have read: 0) https://www.schoolofhaskell.com/user/commercial/content/ exceptions-best-practices . I'd love to get feedback on how to apply this article to my question if it is appropriate. 1) https://lexi-lambda.github.io/blog/2016/06/12/four-months-with-haskell/ . From this article and others, I get the sense that there is great confusion in the community as to how to handle errors correctly, and worse yet, that different approaches are not easily compatible. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tanuki at gmail.com Sun Sep 25 11:53:45 2016 From: tanuki at gmail.com (Theodore Lief Gannon) Date: Sun, 25 Sep 2016 04:53:45 -0700 Subject: [Haskell-beginners] Exception/Error Handling Best Practices In-Reply-To: References: Message-ID: I recently had to deal with a situation like this in a small web service which acts as an on-demand scraper for other sites. It runs on Servant, which uses ExceptT as of a few versions ago (EitherT before that), but for the remote calls it uses Wreq, which throws HttpException values. I needed to be able to report the details of remote errors rather than having them become a generic unhandled exception, so I was stuck with your option 0 -- but with ExceptT rather than Either, and it turns out ExceptT is pretty slick! Here's the resultant code: https://github.com/tejon/Meeseeks/blob/master/src/bin/MeeseeksBox.hs#L42-L48 It's a one-route service so there's a bit of clutter in there that really should be abstracted out and/or done better. (Let's just pretend logE doesn't exist -- semantically, it doesn't!) But other than the bit I documented with a comment, it's entirely straightforward; and IIRC omitting that signature defaulted the type to IOException, which looks like what you want anyway. tl;dr 0.5) ExceptT. On Sat, Sep 24, 2016 at 9:49 PM, Hillary Ryan wrote: > Dear Haskellers, > > I've been working on variants of the following code: > > connect_from_file :: String -> IO Connection > connect_from_file fi = do > s <- BL.readFile fi > cred <- (decode s :: Maybe Db_cred) > c <- connect . reformat_cred $ cred > return c > > What this code intends to do is read a config from a file in JSON, decode > the JSON, and then open a connection based the decoded info. This code > doesn't compile for several reasons, but I've left it in this form because > I believe it is the most convenient for approaching my question: > > How should I handle errors here? > > Both readFile (from Bytestring) and connect (from mysql.simple) throw > exceptions when things don't go their way, and decode uses maybe. I see > four ways of handling these errors: > > 0) *All typed errors: *I could "capture" all the exceptions in Either > types and use an EitherIO monad or transformer to automatically abort when > a Left value appears. Now my function looks like: String -> IO (Either > MyErrorType Connection) > 1) *All exceptions: *I could throw an exception if Nothing appears during > decoding. The function looks the same: String -> IO Connection > 2) *A mix*: I could leave the connect and readFile functions alone, but > return IO (Nothing) if the decoding doesn't work out. Now we have: String > -> IO (Maybe Connection) > 3) Not 0)-2) > > So what is the proper way to do things? > > Many Thanks, > Hillary > > > PS For what it is worth, I have read: > 0) https://www.schoolofhaskell.com/user/commercial/content/exce > ptions-best-practices . I'd love to get feedback on how to apply this > article to my question if it is appropriate. > 1) https://lexi-lambda.github.io/blog/2016/06/12/four-months-with-haskell/ > . From this article and others, I get the sense that there is great > confusion in the community as to how to handle errors correctly, and worse > yet, that different approaches are not easily compatible. > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From K.Bleijenberg at lijbrandt.nl Tue Sep 27 09:14:12 2016 From: K.Bleijenberg at lijbrandt.nl (Kees Bleijenberg) Date: Tue, 27 Sep 2016 11:14:12 +0200 Subject: [Haskell-beginners] ftp-client for Windows Message-ID: <001801d2189f$82f77d90$88e678b0$@lijbrandt.nl> Is there a library with a ftp client that works on Windows? I tried ftphs but I get segmentation faults. Kees -------------- next part -------------- An HTML attachment was scrubbed... URL: From mlang at delysid.org Wed Sep 28 13:06:53 2016 From: mlang at delysid.org (Mario Lang) Date: Wed, 28 Sep 2016 15:06:53 +0200 Subject: [Haskell-beginners] A seemingly simple use-case for Template Haskell Message-ID: <87shsk9nrm.fsf@fx.delysid.org> Hi. As a long-term Lisp fan, and someone who always admired compile-time code-generation, I wanted to at least do something simple with Template Haskell once. In a small project of mine, I have this basically auto-generated data type: -- Braille music code only uses the old 6-dot system. We enumerate all -- possible dot patterns to use the type system to avoid accidentally -- specifying invalid dot patterns in the source code. -- -- gen :: String -- gen = -- "data Braille = " ++ intercalate " | " ctors ++ " deriving (Enum, Eq)" where -- ctors = "NoDots" : map ctorName [1..63] where -- ctorName :: Int -> String -- ctorName = (++) "Dot" . concatMap (show . succ) . flip filter [0..5] . testBit data SixDots = NoDots | Dot1 | Dot2 | Dot12 | Dot3 | Dot13 | Dot23 | Dot123 | Dot4 | Dot14 | Dot24 | Dot124 | Dot34 | Dot134 | Dot234 | Dot1234 | Dot5 | Dot15 | Dot25 | Dot125 | Dot35 | Dot135 | Dot235 | Dot1235 | Dot45 | Dot145 | Dot245 | Dot1245 | Dot345 | Dot1345 | Dot2345 | Dot12345 | Dot6 | Dot16 | Dot26 | Dot126 | Dot36 | Dot136 | Dot236 | Dot1236 | Dot46 | Dot146 | Dot246 | Dot1246 | Dot346 | Dot1346 | Dot2346 | Dot12346 | Dot56 | Dot156 | Dot256 | Dot1256 | Dot356 | Dot1356 | Dot2356 | Dot12356 | Dot456 | Dot1456 | Dot2456 | Dot12456 | Dot3456 | Dot13456 | Dot23456 | Dot123456 deriving (Bounded, Enum, Eq, Read, Show) So, while actually quite simple, this looks like an opportunity to use Template Haskell for me. In other words, I want to figure out what is necessary to generate this data type with TH, instead of the gen function that basically generates a piece of plain Haskell code. I have been reading "A practical Template Haskell Tutorial"[1] but I find it a little bit too terse to actually solve this very little riddle on my own. For one, I find it confusing that some TH functions return "Q Dec" while others just return Dec. I am aware that this is some sort of Monad for the TH system, but I have never seen it explained anywhere. Also, all the examples I can find seem to be mostly focused in generating Q Exp or similar, but I didn't really find an example for Q Dec. I realize this should be simple to figure out on my own, but it apparently is not. I have tried to wrap my head around this on my own at least three times now, but always stopped after an hour or two due to frustration. Is there some comprehensive TH documentation I haven't seen yet? Could you please give me enough of a head-start that I actually manage to write something which can generate this simple data type above? [1] https://wiki.haskell.org/A_practical_Template_Haskell_Tutorial -- CYa, ⡍⠁⠗⠊⠕ From ryan.trinkle at gmail.com Wed Sep 28 14:10:38 2016 From: ryan.trinkle at gmail.com (Ryan Trinkle) Date: Wed, 28 Sep 2016 10:10:38 -0400 Subject: [Haskell-beginners] A seemingly simple use-case for Template Haskell In-Reply-To: <87shsk9nrm.fsf@fx.delysid.org> References: <87shsk9nrm.fsf@fx.delysid.org> Message-ID: Why not just use a datastructure with 6 Bools? E.g.: data SixDots = SixDots { dot1 :: Bool , dot2 :: Bool , dot3 :: Bool , dot4 :: Bool , dot5 :: Bool , dot6 :: Bool } There may be even better ways to do this, but I would consider something like this if I were working on this problem. Although I do end up using TH sometimes, I usually find that it is better to use non-meta-level solutions when practical. Best, Ryan On Wed, Sep 28, 2016 at 9:06 AM, Mario Lang wrote: > Hi. > > As a long-term Lisp fan, and someone who always admired compile-time > code-generation, I wanted to at least do something simple with Template > Haskell once. > > In a small project of mine, I have this basically auto-generated data > type: > > -- Braille music code only uses the old 6-dot system. We enumerate all > -- possible dot patterns to use the type system to avoid accidentally > -- specifying invalid dot patterns in the source code. > -- > -- gen :: String > -- gen = > -- "data Braille = " ++ intercalate " | " ctors ++ " deriving (Enum, > Eq)" where > -- ctors = "NoDots" : map ctorName [1..63] where > -- ctorName :: Int -> String > -- ctorName = (++) "Dot" . concatMap (show . succ) . flip filter > [0..5] . testBit > > data SixDots = NoDots | Dot1 | Dot2 | Dot12 | Dot3 | Dot13 | Dot23 | Dot123 > | Dot4 | Dot14 | Dot24 | Dot124 | Dot34 | Dot134 | Dot234 > | Dot1234 | Dot5 | Dot15 | Dot25 | Dot125 | Dot35 | Dot135 > | Dot235 | Dot1235 | Dot45 | Dot145 | Dot245 | Dot1245 | > Dot345 > | Dot1345 | Dot2345 | Dot12345 | Dot6 | Dot16 | Dot26 | Dot126 > | Dot36 | Dot136 | Dot236 | Dot1236 | Dot46 | Dot146 | Dot246 > | Dot1246 | Dot346 | Dot1346 | Dot2346 | Dot12346 | Dot56 | > Dot156 > | Dot256 | Dot1256 | Dot356 | Dot1356 | Dot2356 | Dot12356 > | Dot456 | Dot1456 | Dot2456 | Dot12456 | Dot3456 | Dot13456 > | Dot23456 | Dot123456 > deriving (Bounded, Enum, Eq, Read, Show) > > So, while actually quite simple, this looks like an opportunity to use > Template Haskell for me. In other words, I want to figure out what is > necessary to generate this data type with TH, instead of the gen > function that basically generates a piece of plain Haskell code. > > I have been reading "A practical Template Haskell Tutorial"[1] but I find > it a little bit too terse to actually solve this very little riddle on > my own. > > For one, I find it confusing that some TH functions return "Q Dec" while > others just return Dec. I am aware that this is some sort of Monad for > the TH system, but I have never seen it explained anywhere. > > Also, all the examples I can find seem to be mostly focused in > generating Q Exp or similar, but I didn't really find an example > for Q Dec. > > I realize this should be simple to figure out on my own, but it > apparently is not. I have tried to wrap my head around this on my own > at least three times now, but always stopped after an hour or two due to > frustration. Is there some comprehensive TH documentation I haven't > seen yet? Could you please give me enough of a head-start that I > actually manage to write something which can generate this simple data > type above? > > [1] https://wiki.haskell.org/A_practical_Template_Haskell_Tutorial > -- > CYa, > ⡍⠁⠗⠊⠕ > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: From simon at mintsource.org Wed Sep 28 14:18:28 2016 From: simon at mintsource.org (Simon Peter Nicholls) Date: Wed, 28 Sep 2016 16:18:28 +0200 Subject: [Haskell-beginners] A seemingly simple use-case for Template Haskell In-Reply-To: <87shsk9nrm.fsf@fx.delysid.org> References: <87shsk9nrm.fsf@fx.delysid.org> Message-ID: <8298E68A-D98E-44DF-89D7-50029D20D7C4@mintsource.org> Hi Mario, I had a similar itch to scratch when I first started noodling with Haskell, coming from Lisp, and wrote the following for generating enums from a convenient notation: https://github.com/yesodweb/yesod/wiki/Create-String-Based-Enums-With-Template-Haskell It’s approaching four years old, so likely no longer works as is, but I hope it is helpful. Even more than with Lisp macros, TH should be seen as a last resort. However, sometimes you gotta scratch. I was frustrated at the time that I couldn’t find any decent examples, and I specifically wanted to learn some TH - regardless of whether there was a better way available. In that wiki page you’ll find code for a custom quasi quoter, and code for generating declarations. I mostly just relied on the template haskell docs: https://hackage.haskell.org/package/template-haskell which I found to have everything I needed, though of course there is a lot to digest in there. After gaining some familiarity, the types and functions for creating those types feel a natural match to regular Haskell source, and Q is used because otherwise your declarations would be just side effect free values, unable to affect your program. For your needs, find “data Dec” in the template haskell docs, and you’ll see that `DataD` matches what you need at term level. Then take a look at `dataD`, a function that will construct your declaration, using Q. Cheers, Si > On 28 Sep 2016, at 15:06, Mario Lang wrote: > > Hi. > > As a long-term Lisp fan, and someone who always admired compile-time > code-generation, I wanted to at least do something simple with Template > Haskell once. > > In a small project of mine, I have this basically auto-generated data > type: > > -- Braille music code only uses the old 6-dot system. We enumerate all > -- possible dot patterns to use the type system to avoid accidentally > -- specifying invalid dot patterns in the source code. > -- > -- gen :: String > -- gen = > -- "data Braille = " ++ intercalate " | " ctors ++ " deriving (Enum, Eq)" where > -- ctors = "NoDots" : map ctorName [1..63] where > -- ctorName :: Int -> String > -- ctorName = (++) "Dot" . concatMap (show . succ) . flip filter [0..5] . testBit > > data SixDots = NoDots | Dot1 | Dot2 | Dot12 | Dot3 | Dot13 | Dot23 | Dot123 > | Dot4 | Dot14 | Dot24 | Dot124 | Dot34 | Dot134 | Dot234 > | Dot1234 | Dot5 | Dot15 | Dot25 | Dot125 | Dot35 | Dot135 > | Dot235 | Dot1235 | Dot45 | Dot145 | Dot245 | Dot1245 | Dot345 > | Dot1345 | Dot2345 | Dot12345 | Dot6 | Dot16 | Dot26 | Dot126 > | Dot36 | Dot136 | Dot236 | Dot1236 | Dot46 | Dot146 | Dot246 > | Dot1246 | Dot346 | Dot1346 | Dot2346 | Dot12346 | Dot56 | Dot156 > | Dot256 | Dot1256 | Dot356 | Dot1356 | Dot2356 | Dot12356 > | Dot456 | Dot1456 | Dot2456 | Dot12456 | Dot3456 | Dot13456 > | Dot23456 | Dot123456 > deriving (Bounded, Enum, Eq, Read, Show) > > So, while actually quite simple, this looks like an opportunity to use > Template Haskell for me. In other words, I want to figure out what is > necessary to generate this data type with TH, instead of the gen > function that basically generates a piece of plain Haskell code. > > I have been reading "A practical Template Haskell Tutorial"[1] but I find > it a little bit too terse to actually solve this very little riddle on > my own. > > For one, I find it confusing that some TH functions return "Q Dec" while > others just return Dec. I am aware that this is some sort of Monad for > the TH system, but I have never seen it explained anywhere. > > Also, all the examples I can find seem to be mostly focused in > generating Q Exp or similar, but I didn't really find an example > for Q Dec. > > I realize this should be simple to figure out on my own, but it > apparently is not. I have tried to wrap my head around this on my own > at least three times now, but always stopped after an hour or two due to > frustration. Is there some comprehensive TH documentation I haven't > seen yet? Could you please give me enough of a head-start that I > actually manage to write something which can generate this simple data > type above? > > [1] https://wiki.haskell.org/A_practical_Template_Haskell_Tutorial > -- > CYa, > ⡍⠁⠗⠊⠕ > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: From mlang at delysid.org Wed Sep 28 15:12:15 2016 From: mlang at delysid.org (Mario Lang) Date: Wed, 28 Sep 2016 17:12:15 +0200 Subject: [Haskell-beginners] A seemingly simple use-case for Template Haskell In-Reply-To: (Ryan Trinkle's message of "Wed, 28 Sep 2016 10:10:38 -0400") References: <87shsk9nrm.fsf@fx.delysid.org> Message-ID: <87wphw6ots.fsf@fx.delysid.org> Ryan Trinkle writes: > Why not just use a datastructure with 6 Bools? E.g.: > > data SixDots = SixDots > { dot1 :: Bool > , dot2 :: Bool > , dot3 :: Bool > , dot4 :: Bool > , dot5 :: Bool > , dot6 :: Bool > } Because it is convenient to have an Enum instance, and because braille dot patterns are used as sort of constants in the code. So if I had a data structure like you described above, I'd still need 64 functions with convenient names that return such a data structure with the appropriate bits set. -- eeek! dot123 :: SixDots' dot123 = SixDots' True True True False False False It seems *a lot* easier to just use a sum type and enumerate all the possibilities. Besides, it seems overly inefficient to use 6 Bools for something that actually just needs 6 bits. Also, the resulting enumeration actually matches the Unicode standard. To illustrate, I have a function to convert Braille dot patterns to Char: toChar :: SixDots -> Char toChar = toEnum . (+ 0x2800) . fromEnum That said, I am not really looking for advice on the example at hand, I am more interested in using that example as a motivation to learn TH. [...] > On Wed, Sep 28, 2016 at 9:06 AM, Mario Lang wrote: > >> Hi. >> >> As a long-term Lisp fan, and someone who always admired compile-time >> code-generation, I wanted to at least do something simple with Template >> Haskell once. >> >> In a small project of mine, I have this basically auto-generated data >> type: >> >> -- Braille music code only uses the old 6-dot system. We enumerate all >> -- possible dot patterns to use the type system to avoid accidentally >> -- specifying invalid dot patterns in the source code. >> -- >> -- gen :: String >> -- gen = >> -- "data Braille = " ++ intercalate " | " ctors ++ " deriving (Enum, >> Eq)" where >> -- ctors = "NoDots" : map ctorName [1..63] where >> -- ctorName :: Int -> String >> -- ctorName = (++) "Dot" . concatMap (show . succ) . flip filter >> [0..5] . testBit >> >> data SixDots = NoDots | Dot1 | Dot2 | Dot12 | Dot3 | Dot13 | Dot23 | Dot123 >> | Dot4 | Dot14 | Dot24 | Dot124 | Dot34 | Dot134 | Dot234 >> | Dot1234 | Dot5 | Dot15 | Dot25 | Dot125 | Dot35 | Dot135 >> | Dot235 | Dot1235 | Dot45 | Dot145 | Dot245 | Dot1245 | Dot345 >> | Dot1345 | Dot2345 | Dot12345 | Dot6 | Dot16 | Dot26 | Dot126 >> | Dot36 | Dot136 | Dot236 | Dot1236 | Dot46 | Dot146 | Dot246 >> | Dot1246 | Dot346 | Dot1346 | Dot2346 | Dot12346 | Dot56 | Dot156 >> | Dot256 | Dot1256 | Dot356 | Dot1356 | Dot2356 | Dot12356 >> | Dot456 | Dot1456 | Dot2456 | Dot12456 | Dot3456 | Dot13456 >> | Dot23456 | Dot123456 >> deriving (Bounded, Enum, Eq, Read, Show) >> >> So, while actually quite simple, this looks like an opportunity to use >> Template Haskell for me. In other words, I want to figure out what is >> necessary to generate this data type with TH, instead of the gen >> function that basically generates a piece of plain Haskell code. >> >> I have been reading "A practical Template Haskell Tutorial"[1] but I find >> it a little bit too terse to actually solve this very little riddle on >> my own. >> >> For one, I find it confusing that some TH functions return "Q Dec" while >> others just return Dec. I am aware that this is some sort of Monad for >> the TH system, but I have never seen it explained anywhere. >> >> Also, all the examples I can find seem to be mostly focused in >> generating Q Exp or similar, but I didn't really find an example >> for Q Dec. >> >> I realize this should be simple to figure out on my own, but it >> apparently is not. I have tried to wrap my head around this on my own >> at least three times now, but always stopped after an hour or two due to >> frustration. Is there some comprehensive TH documentation I haven't >> seen yet? Could you please give me enough of a head-start that I >> actually manage to write something which can generate this simple data >> type above? >> >> [1] https://wiki.haskell.org/A_practical_Template_Haskell_Tutorial -- CYa, ⡍⠁⠗⠊⠕ From raabe at froglogic.com Thu Sep 29 08:39:14 2016 From: raabe at froglogic.com (Frerich Raabe) Date: Thu, 29 Sep 2016 10:39:14 +0200 Subject: [Haskell-beginners] A seemingly simple use-case for Template Haskell In-Reply-To: <87shsk9nrm.fsf@fx.delysid.org> References: <87shsk9nrm.fsf@fx.delysid.org> Message-ID: <2f0d2bcda1b619f8284c0891002f928b@roundcube.froglogic.com> On 2016-09-28 15:06, Mario Lang wrote: > In a small project of mine, I have this basically auto-generated data > type: > > -- Braille music code only uses the old 6-dot system. We enumerate all > -- possible dot patterns to use the type system to avoid accidentally > -- specifying invalid dot patterns in the source code. > -- > -- gen :: String > -- gen = > -- "data Braille = " ++ intercalate " | " ctors ++ " deriving (Enum, > Eq)" where > -- ctors = "NoDots" : map ctorName [1..63] where > -- ctorName :: Int -> String > -- ctorName = (++) "Dot" . concatMap (show . succ) . flip filter [0..5] > . testBit > > data SixDots = NoDots | Dot1 | Dot2 | Dot12 | Dot3 | Dot13 | Dot23 | Dot123 > | Dot4 | Dot14 | Dot24 | Dot124 | Dot34 | Dot134 | Dot234 > | Dot1234 | Dot5 | Dot15 | Dot25 | Dot125 | Dot35 | Dot135 > | Dot235 | Dot1235 | Dot45 | Dot145 | Dot245 | Dot1245 | Dot345 > | Dot1345 | Dot2345 | Dot12345 | Dot6 | Dot16 | Dot26 | Dot126 > | Dot36 | Dot136 | Dot236 | Dot1236 | Dot46 | Dot146 | Dot246 > | Dot1246 | Dot346 | Dot1346 | Dot2346 | Dot12346 | Dot56 | > Dot156 > | Dot256 | Dot1256 | Dot356 | Dot1356 | Dot2356 | Dot12356 > | Dot456 | Dot1456 | Dot2456 | Dot12456 | Dot3456 | Dot13456 > | Dot23456 | Dot123456 > deriving (Bounded, Enum, Eq, Read, Show) > > So, while actually quite simple, this looks like an opportunity to use > Template Haskell for me. In other words, I want to figure out what is > necessary to generate this data type with TH, instead of the gen > function that basically generates a piece of plain Haskell code. Here's one way to do it (the 'ctorNames' definition is copied out of your comment): --- Mario.hs --- module Mario (makeDotsType) where import Data.Bits (testBit) import Language.Haskell.TH ctorNames :: [String] ctorNames = "NoDots" : map ctorName [1..63] where ctorName :: Int -> String ctorName = (++) "Dot" . concatMap (show . succ) . flip filter [0..5] . testBit makeDotsType :: Q [Dec] makeDotsType = do let ctors = map (\n -> NormalC (mkName n) []) ctorNames let instances = map mkName ["Bounded", "Enum", "Eq", "Read", "Show"] return [DataD [] (mkName "SixDots") [] ctors instances] --- --- Main.hs --- {-# LANGUAGE TemplateHaskell #-} import Mario $(makeDotsType) --- If you compile this with $ ghc -ddump-splices Main.hs You can see what type definition that '$(makeDotsType)' expands to. For what it's worth, this may not compile with all versions of the TH support in GHC; I wrote the above code using GHC 7.10.2. In general, I find -ddump-splices invaluable when using TH. I use it every minute or so to see what code I'm currently generating. What's noteworthy is that (as mentioned in the 'Using Template Haskell' section of the GHC user guide) that You can only run a function at compile time if it is imported from another module. That is, you can't define a function in a module, and call it from within a splice in the same module. That's why I used a separate 'Mario' module above. > > Also, all the examples I can find seem to be mostly focused in > generating Q Exp or similar, but I didn't really find an example > for Q Dec. I uploaded a couple of my own exercises for using TH on GitHub: https://github.com/frerich/random-derive https://github.com/frerich/catamorphism https://github.com/frerich/smartconstructor All of them deal with generating a 'Dec' at https://hackage.haskell.org/package/template-haskell-2.11.0.0/docs/Language-Haskell-TH-Syntax.html#t:Dec ...and then work my way down. Hope that helps! -- Frerich Raabe - raabe at froglogic.com www.froglogic.com - Multi-Platform GUI Testing From mlang at delysid.org Thu Sep 29 14:54:13 2016 From: mlang at delysid.org (Mario Lang) Date: Thu, 29 Sep 2016 16:54:13 +0200 Subject: [Haskell-beginners] A seemingly simple use-case for Template Haskell In-Reply-To: <2f0d2bcda1b619f8284c0891002f928b@roundcube.froglogic.com> (Frerich Raabe's message of "Thu, 29 Sep 2016 10:39:14 +0200") References: <87shsk9nrm.fsf@fx.delysid.org> <2f0d2bcda1b619f8284c0891002f928b@roundcube.froglogic.com> Message-ID: <87k2du3gfe.fsf@fx.delysid.org> Frerich Raabe writes: > On 2016-09-28 15:06, Mario Lang wrote: >> In a small project of mine, I have this basically auto-generated data >> type: >> >> -- Braille music code only uses the old 6-dot system. We enumerate all >> -- possible dot patterns to use the type system to avoid accidentally >> -- specifying invalid dot patterns in the source code. >> -- >> -- gen :: String >> -- gen = >> -- "data Braille = " ++ intercalate " | " ctors ++ " deriving >> (Enum, Eq)" where >> -- ctors = "NoDots" : map ctorName [1..63] where >> -- ctorName :: Int -> String >> -- ctorName = (++) "Dot" . concatMap (show . succ) . flip filter >> [0..5] . testBit >> >> data SixDots = NoDots | Dot1 | Dot2 | Dot12 | Dot3 | Dot13 | Dot23 | Dot123 >> | Dot4 | Dot14 | Dot24 | Dot124 | Dot34 | Dot134 | Dot234 >> | Dot1234 | Dot5 | Dot15 | Dot25 | Dot125 | Dot35 | Dot135 >> | Dot235 | Dot1235 | Dot45 | Dot145 | Dot245 | Dot1245 | Dot345 >> | Dot1345 | Dot2345 | Dot12345 | Dot6 | Dot16 | Dot26 | Dot126 >> | Dot36 | Dot136 | Dot236 | Dot1236 | Dot46 | Dot146 | Dot246 >> | Dot1246 | Dot346 | Dot1346 | Dot2346 | Dot12346 | Dot56 | Dot156 >> | Dot256 | Dot1256 | Dot356 | Dot1356 | Dot2356 | Dot12356 >> | Dot456 | Dot1456 | Dot2456 | Dot12456 | Dot3456 | Dot13456 >> | Dot23456 | Dot123456 >> deriving (Bounded, Enum, Eq, Read, Show) >> >> So, while actually quite simple, this looks like an opportunity to use >> Template Haskell for me. In other words, I want to figure out what is >> necessary to generate this data type with TH, instead of the gen >> function that basically generates a piece of plain Haskell code. > > Here's one way to do it (the 'ctorNames' definition is copied out of > your comment): > > --- Mario.hs --- > module Mario (makeDotsType) where > > import Data.Bits (testBit) > import Language.Haskell.TH > > ctorNames :: [String] > ctorNames = "NoDots" : map ctorName [1..63] > where > ctorName :: Int -> String > ctorName = (++) "Dot" . concatMap (show . succ) . flip filter > [0..5] . testBit > > makeDotsType :: Q [Dec] > makeDotsType = do > let ctors = map (\n -> NormalC (mkName n) []) ctorNames > let instances = map mkName ["Bounded", "Enum", "Eq", "Read", "Show"] > return [DataD [] (mkName "SixDots") [] ctors instances] > --- > > --- Main.hs --- > {-# LANGUAGE TemplateHaskell #-} > > import Mario > > $(makeDotsType) > --- > > If you compile this with > > $ ghc -ddump-splices Main.hs > > You can see what type definition that '$(makeDotsType)' expands to. Oh, thank you! It could have been so simple... > For what it's worth, this may not compile with all versions of the TH > support in GHC; I wrote the above code using GHC 7.10.2. Works here. > In general, I find -ddump-splices invaluable when using TH. I use it > every minute or so to see what code I'm currently generating. What's > noteworthy is that (as mentioned in the 'Using Template Haskell' > section of the GHC user guide) that > > You can only run a function at compile time if it is imported from > another module. That is, you can't define > a function in a module, and call it from within a splice in the same > module. > > That's why I used a separate 'Mario' module above. Yes, I was aware of the need to put the function in a different module. Thanks again, a working example is really nice to play with. -- CYa, ⡍⠁⠗⠊⠕ From dennis.raddle at gmail.com Thu Sep 29 20:35:49 2016 From: dennis.raddle at gmail.com (Dennis Raddle) Date: Thu, 29 Sep 2016 13:35:49 -0700 Subject: [Haskell-beginners] Haskell Platform on OS X, Sierra Message-ID: I have been running the Haskell Platform on OS X El Capitan for a few months. I just upgraded to Sierra and I notice it broke something. For instance when I run ghci I get xcrun: error: invalid active developer path ... missing xcrun .... What's the best way to recover? Should I uninstall and reinstall Haskell Platform? BTW, how DO you uninstall the Haskell Platform on OS X? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dennis.raddle at gmail.com Thu Sep 29 21:10:52 2016 From: dennis.raddle at gmail.com (Dennis Raddle) Date: Thu, 29 Sep 2016 14:10:52 -0700 Subject: [Haskell-beginners] Haskell Platform on OS X, Sierra In-Reply-To: References: Message-ID: Solved, I think. I think the fix is to install the command-line developer tools. On Thu, Sep 29, 2016 at 1:35 PM, Dennis Raddle wrote: > I have been running the Haskell Platform on OS X El Capitan for a few > months. I just upgraded to Sierra and I notice it broke something. For > instance when I run ghci I get > > xcrun: error: invalid active developer path ... missing xcrun .... > > What's the best way to recover? Should I uninstall and reinstall Haskell > Platform? > > BTW, how DO you uninstall the Haskell Platform on OS X? > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From laiboonh at gmail.com Thu Sep 29 23:48:32 2016 From: laiboonh at gmail.com (Lai Boon Hui) Date: Fri, 30 Sep 2016 07:48:32 +0800 Subject: [Haskell-beginners] Why isn't :t (1+1) Integer Message-ID: Hi, i read that the default type for Num is Integer. If we specify a type for one of the arguments: :t (1 + 1::Double) is Double If we don't, i expect the default type for Num to come into play: However :t (1 + 1) is Num a => a Why is that so? -- Best Regards, Boon Hui -------------- next part -------------- An HTML attachment was scrubbed... URL: From i.caught.air at gmail.com Fri Sep 30 03:47:17 2016 From: i.caught.air at gmail.com (Alex Belanger) Date: Thu, 29 Sep 2016 23:47:17 -0400 Subject: [Haskell-beginners] Why isn't :t (1+1) Integer In-Reply-To: References: Message-ID: Those are not demonstrations of the defaulting rules in action though. As far as I know, :t is agnostic to those and will keep showing the original polymorphic version, since you're not in a context where GHC has to sacrifice the polymorphic type and commit to a monomorphic one using a default, like when expressions are evaluated. Also I think that's effective just for the REPL (it uses extended defaulting rules that are more lax) and is done for convenience. If it was during a GHC build, you'd get loud warning when something is defaulted. Try `reverse []` for fun vs. `:t reverse []`. On Sep 29, 2016 7:48 PM, "Lai Boon Hui" wrote: > Hi, > i read that the default type for Num is Integer. > If we specify a type for one of the arguments: > :t (1 + 1::Double) is Double > > If we don't, i expect the default type for Num to come into play: > However :t (1 + 1) is Num a => a > > Why is that so? > > -- > Best Regards, > Boon Hui > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From frederic-emmanuel.picca at synchrotron-soleil.fr Fri Sep 30 14:18:14 2016 From: frederic-emmanuel.picca at synchrotron-soleil.fr (PICCA Frederic-Emmanuel) Date: Fri, 30 Sep 2016 14:18:14 +0000 Subject: [Haskell-beginners] Could not deduce (Frame a r0) arising from a use of `len' Message-ID: Hello I try to write something like this {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE RankNTypes #-} module Hkl.Frame ( Frame(..) , frames , frames' ) where import Pipes import Control.Monad class (Frame a r) where len :: a -> IO (Maybe Int) row :: a -> Int -> IO r frames :: (Frame a r) => Pipe a r IO () frames = do d <- await (Just n) <- lift $ len d forM_ [0..n-1] (\i' -> do f <- lift $ row d i' yield f) But when I try to compile this I get this error message. Could not deduce (Frame a r0) arising from a use of `len' from the context (Frame a r) bound by the type signature for frames :: Frame a r => Pipe a r IO () at src/Hkl/Frame.hs:17:11-39 The type variable `r0' is ambiguous Possible fix: add a type signature that fixes these type variable(s) In the second argument of `($)', namely `len d' In a stmt of a 'do' block: (Just n) <- lift $ len d In the expression: do { d <- await; (Just n) <- lift $ len d; forM_ [0 .. n - 1] (\ i' -> do { f <- lift $ row d i'; .... }) } What should I do in order to solve this problem. Thanks Frederic From dennis.raddle at gmail.com Fri Sep 30 20:28:03 2016 From: dennis.raddle at gmail.com (Dennis Raddle) Date: Fri, 30 Sep 2016 13:28:03 -0700 Subject: [Haskell-beginners] short-circuit like Maybe monad with a difference Message-ID: I have a function 'evalRule' that applies rules to steps in a backtracking search optimization problem and evaluates the step's fitness or decides that it must be eliminated entirely. In the following, a result of Nothing means "eliminate the step," Just x means the step has fitness score x. evalRule :: Rule -> Step -> Maybe Double I would like to write a function that applies a bunch of rules but short-circuits the computation if it hits Nothing. However, unlike the way the Maybe monad works, I want to know the partial results. In the following, the rules are applied in order and the "Just" results are collected up the point where a rule returns Nothing. evalRules :: [Rule] -> Step -> [Double] What's a nice way to do this? D -------------- next part -------------- An HTML attachment was scrubbed... URL: From imantc at gmail.com Fri Sep 30 20:32:53 2016 From: imantc at gmail.com (Imants Cekusins) Date: Fri, 30 Sep 2016 22:32:53 +0200 Subject: [Haskell-beginners] short-circuit like Maybe monad with a difference In-Reply-To: References: Message-ID: > short-circuits the computation if it hits Nothing. However, unlike the way the Maybe monad works, I want to know the partial results. ​ could store partial results in a State monad.. Maybe would short circuit but the values would stay in the State. would this work? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dennis.raddle at gmail.com Fri Sep 30 20:56:03 2016 From: dennis.raddle at gmail.com (Dennis Raddle) Date: Fri, 30 Sep 2016 13:56:03 -0700 Subject: [Haskell-beginners] short-circuit like Maybe monad with a difference In-Reply-To: References: Message-ID: I just tried something evalRules :: [Rule] -> Step -> [Double] evalRules rules step = catMaybes . takeWhile isJust . map (flip evalRule step) $ rules This seems to work according to my testing. On Fri, Sep 30, 2016 at 1:32 PM, Imants Cekusins wrote: > > short-circuits the computation if it hits Nothing. However, unlike the > way the Maybe monad works, I want to know the partial results. > ​ > could store partial results in a State monad.. Maybe would short circuit > but the values would stay in the State. > > would this work? > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From imantc at gmail.com Fri Sep 30 21:12:44 2016 From: imantc at gmail.com (Imants Cekusins) Date: Fri, 30 Sep 2016 23:12:44 +0200 Subject: [Haskell-beginners] short-circuit like Maybe monad with a difference In-Reply-To: References: Message-ID: it is better alright. is it necessary to know if all rules were evaluated? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dennis.raddle at gmail.com Fri Sep 30 22:07:30 2016 From: dennis.raddle at gmail.com (Dennis Raddle) Date: Fri, 30 Sep 2016 15:07:30 -0700 Subject: [Haskell-beginners] short-circuit like Maybe monad with a difference In-Reply-To: References: Message-ID: It is necessary to know if all the rules are evaluated but that can be done by checking the length of the output. Also I could do something like zip the rule list with the [Double] output to tuple the rules together with their scores. D On Fri, Sep 30, 2016 at 2:12 PM, Imants Cekusins wrote: > it is better alright. > > is it necessary to know if all rules were evaluated? > > _______________________________________________ > Beginners mailing list > Beginners at haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > -------------- next part -------------- An HTML attachment was scrubbed... URL: