[Haskell-cafe] [OT]Help with translating Error Monads to C++
Currently in the c++ developers ml, there's a discussion of a proposed ExpectedT or an EitherL,R template: http://article.gmane.org/gmane.comp.lib.boost.devel/242496 A lot of the discussion appears, at least to me, to not be aware of how haskell handles similar situations. The above link was to my post suggesting they try to emulate the way haskell solves the problem using Monads. However, I've only briefly dabbled in haskell; hence, my responses on that thread are probably missing something. I invite anyone on this list to please provide insights on this particular thread in the boost.devel mailing list. -kind regards, Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] ; in do
On 12/29/10 22:40, Daryoush Mehrtash wrote: Why do people put ; in do {}, or , in data fields, at the beginning of the line? -- It reflects the parse tree better by putting the combining operators (e.g. ';' and ',') at the left and their operands (or combined subtrees) indented to the right. IOW, this formating style rotates the parse tree: o_ / \ /\ s1s2 for operator o_ and subtrees, s1 and s2, -90 degrees and replaces the connecting edges with indentation: s1 o_ s2 now, it surrounds that with the begin(b_) and end(e_) delimiters: b_ s1 o_ s2 e_ For example, in the case of a tuple with arguments, a1 and a2, this would appear: ( a1 , a2 ) This also improves readability in a similar way that bulleted list items in a text document improve readability. For example: * s1 * s2 is easier to read than: s1 s2 because the reader knows that * begins an item and he only has to search a given column for the beginning of the next item. However, some people object to this style because it requires too much vertical space as compared to: ( a1, a2 ) HTH -regards, Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Formatting function types
On 12/30/10 08:17, Henning Thielemann wrote: On Thu, 30 Dec 2010, Lauri Alanko wrote: Even nowadays, Haddock deliberately generates the following layout for long function types: openTempFile :: FilePath - String - IO (FilePath, Handle) The layout draws special attention to the first argument type, whereas the other argument types are indistinguishable from the return type. Lauri, I assume then that you want to draw special attention to the return type instead of the first argument type. The following is much clearer: openTempFile :: FilePath - String - IO (FilePath, Handle) (Possibly with the arrows aligned.) So, with this operator postfix formatting, the special attention to the return type is achieved by *not* suffixing it with -. However, to see that it's not suffixed with -, your eyes have to scan to the right for the whole line until you don't find the -. Oh, but wait, if the return type is an elaborate type expression, that takes up more than say, 60 spaces, then I would want to format it over more than one line. Yes, that would be rare, but possible. Now you'd have to scan not 1 but 2 lines to find (or rather not find) the -. OK, but then why not just find the last argument and forget about finding the missing postfix -? Well, in that case, the operator prefix formatting would serve just as well. The attachment contains ret_{post,pre} declaration which provide a concrete example. Maybe I'm just used to the prefix formatting, but I do find ret_pre easier to read than the ret_post. I find it more readable because I just have to search, in a given column, for the last -, and then I know the following is the return type. +1 GHC also formats type signatures in errors and warnings in the misleading way. In case of Haddock comments I understand that the comment must be close to the argument type. Henning, I guess you're saying Haddock puts - first on the line in order to make the comment as close as possible to the argument type. That is openTempFile FilePath -- ^ filename, of course - String - IO (FilePath, Handle) would not work. But {- ^ filename -} would work. So, Haddock would format this(after adding comments for other args) as: [1]: openTempFile :: FilePath -- ^ filename, of course - String -- ^ comment for 2nd arg. - IO (FilePath, Handle) -- ^ comment for 3ird arg However, you and Lauri would prefer (IIUC): [2]: openTempFile:: FilePath {- ^ filename, of course -} - String {- ^ comment for 2nd arg. -} - IO (FilePath, Handle) -- ^ comment for 3ird arg or, to retain the same comment types: [3]: openTempFile:: FilePath -- ^ filename, of course - String -- ^ comment for 2nd arg. - IO (FilePath, Handle) -- ^ comment for 3ird arg OK, but then there's more vertical space used, so maybe [2] is better; however, [2] separates the operator, - from it's operands by the the comment. Maybe you could justify this by saying the operator is not important for readability; however, in my reply to Lauri, I indicated it was easier to find the last argument if the operators were prefixed, as was illustrated by the ret_{post,pre} declarations in the attachment. Likewise, I find it easier to find the last argument with [1] rather than either [2] or [3]. -regards, Larry module Format where --{return attention ret_post :: Int - ( Int , Int ) --more than 1 line arg - ( Int , Int ) --more than 1 line return type ret_pre :: Int - ( Int , Int ) --more than 1 line arg - ( Int , Int ) --more than 1 line return type --}return attention --{comments com_post :: Int {-arg1-} - Int {-arg2-} com_pre :: Int --arg1 - Int --arg2 --}comments ret_post x _ = (x,x) ret_pre x _ = (x,x) com_post x = x com_pre x = x ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Is there a tutorial interpreter to teach haskell?
On 11/30/10 16:46, Stephen Tetley wrote: Andy Gill developed HERA which sounds somewhat similar to what you are asking, but I don't know that it would be particularly beginner friendly and I think it was static - i.e. the reduction rules were applied to program source code rather than within an interactive evaluation of a running program. I thought I'd seen more information about it on the web, but all I can seem to find at the moment is this page: http://www.haskell.org/haskellwiki/Haskell_Equational_Reasoning_Assistant [snip] Thanks for that link Stephen, unfortunately when I, as instructed by that link, did: darcs get http://code.haskell.org/HERA and then, as instructed by the just gotten HERA/README, did: make boot I got errors as shown in the attachment. I did post these results on the HERA discussion page: http://www.haskell.org/haskellwiki /Talk:Haskell_Equational_Reasoning_Assistant However, apparently I didn't post it right because it's all formatted wrong, making it hard to read :( -regards, Larry /home/evansl/prog_dev/haskell/my-code/hera/HERA $ make boot make -C engine make[1]: Entering directory `/home/evansl/prog_dev/haskell/my-code/hera/HERA/engine' runhaskell Setup.hs configure --user --prefix=/home/evansl/prog_dev/haskell/my-code/hera/HERA/engine/inplace Setup.hs:3:0: Warning: In the use of `defaultUserHooks' (imported from Distribution.Simple): Deprecated: Use simpleUserHooks or autoconfUserHooks, unless you need Cabal-1.2 compatibility in which case you must stick with defaultUserHooks Warning: defaultUserHooks in Setup script is deprecated. Configuring hera-engine-2.1... Warning: No 'build-type' specified. If you do not need a custom Setup.hs or ./configure script then use 'build-type: Simple'. Warning: Instead of 'ghc-options: -fth -cpp' use 'extensions: TemplateHaskell CPP' runhaskell Setup.hs build Setup.hs:3:0: Warning: In the use of `defaultUserHooks' (imported from Distribution.Simple): Deprecated: Use simpleUserHooks or autoconfUserHooks, unless you need Cabal-1.2 compatibility in which case you must stick with defaultUserHooks Preprocessing library hera-engine-2.1... Building hera-engine-2.1... [ 1 of 10] Compiling Language.Haskell.ER.HughesPJ ( Language/Haskell/ER/HughesPJ.hs, dist/build/Language/Haskell/ER/HughesPJ.o ) on the commandline: Warning: -fth is deprecated: use -XTemplateHaskell or pragma {-# LANGUAGE TemplateHaskell #-} instead on the commandline: Warning: -Onot is deprecated: Use -O0 instead [ 2 of 10] Compiling Language.Haskell.ER.PprLib ( Language/Haskell/ER/PprLib.hs, dist/build/Language/Haskell/ER/PprLib.o ) [ 3 of 10] Compiling Language.Haskell.ER.Utils ( Language/Haskell/ER/Utils.hs, dist/build/Language/Haskell/ER/Utils.o ) [ 4 of 10] Compiling Language.Haskell.ER.Frees ( Language/Haskell/ER/Frees.hs, dist/build/Language/Haskell/ER/Frees.o ) [ 5 of 10] Compiling Language.Haskell.ER.Syntax ( Language/Haskell/ER/Syntax.hs, dist/build/Language/Haskell/ER/Syntax.o ) Language/Haskell/ER/Syntax.hs:46:32: No instance for (Show ModName) arising from a use of `show' at Language/Haskell/ER/Syntax.hs:46:32-37 Possible fix: add an instance declaration for (Show ModName) In the second argument of `(++)', namely `show n' In the expression: NameQ ++ show n In the definition of `show': show (NameQ n) = NameQ ++ show n Language/Haskell/ER/Syntax.hs:52:55: No instance for (Show PkgName) arising from a use of `show' at Language/Haskell/ER/Syntax.hs:52:55-60 Possible fix: add an instance declaration for (Show PkgName) In the first argument of `(++)', namely `show n' In the second argument of `(++)', namely `show n ++ ++ show x' In the second argument of `(++)', namely ` ++ show n ++ ++ show x' Language/Haskell/ER/Syntax.hs:71:43: No instance for (Lift Pred) arising from a use of `cxt' at Language/Haskell/ER/Syntax.hs:71:43-45 Possible fix: add an instance declaration for (Lift Pred) In the first argument of `DataD', namely `cxt' In the Template Haskell quotation [| DataD cxt n ns1 cs ns2 |] In the expression: [| DataD cxt n ns1 cs ns2 |] Language/Haskell/ER/Syntax.hs:71:49: No instance for (Lift TyVarBndr) arising from a use of `ns1' at Language/Haskell/ER/Syntax.hs:71:49-51 Possible fix: add an instance declaration for (Lift TyVarBndr) In the third argument of `DataD', namely `ns1' In the Template Haskell quotation [| DataD cxt n ns1 cs ns2 |] In the expression: [| DataD cxt n ns1 cs ns2 |] Language/Haskell/ER/Syntax.hs:262:36: No instance for (Lift OccName) arising from a use of `name' at Language/Haskell/ER/Syntax.hs:262:36-39 Possible fix: add an instance declaration for (Lift OccName) In the first argument
Re: [Haskell-cafe] Bifold: a simultaneous foldr and foldl
On 12/07/10 12:36, Henning Thielemann wrote: Noah Easterly wrote: Somebody suggested I post this here if I wanted feedback. So I was thinking about the ReverseState monad I saw mentioned on r/haskell a couple days ago, and playing around with the concept of information flowing two directions when I came up with this function: bifold :: (l - a - r - (r,l)) - (l,r) - [a] - (r,l) bifold _ (l,r) [] = (r,l) bifold f (l,r) (a:as) = (ra,las) where (ras,las) = bifold f (la,r) as (ra,la) = f l a ras (I'm sure someone else has come up with this before, so I'll just say I discovered it, not invented it). Basically, it's a simultaneous left and right fold, passing one value from the start of the list toward the end, and one from the end toward the start. I also needed a bidirectional fold in the past. See foldl'r in http://code.haskell.org/~thielema/utility/src/Data/List/HT/Private.hs You can express it using foldr alone, since 'foldl' can be written as 'foldr': http://www.haskell.org/haskellwiki/Foldl_as_foldr You may add your example to the Wiki. Hi Henning, I tried to understand the Foldl_as_foldr method by actually manually tracing the the execution. The result is in the attachment 1(Bifold.Thielemann.txt). Then I tried to implement the equivalent of the foldl'r in your Private.hs with the if_recur mentioned in my other post: http://www.mail-archive.com/haskell-cafe@haskell.org/msg84793.html This code is in attachment 2(HutBacFoldlr.hs). It uses a small help module to make explicit the associativity of the folded values. This help module is in attachment 3(Assoc.hs). The emacs eshell output shows: --{--eshell output-- /home/evansl/prog_dev/haskell/my-code $ ghci HutBacFoldlr.hs GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. [1 of 2] Compiling Assoc( Assoc.hs, interpreted ) [2 of 2] Compiling HutBacFoldlr ( HutBacFoldlr.hs, interpreted ) Ok, modules loaded: HutBacFoldlr, Assoc. *HutBacFoldlr test Loading package array-0.3.0.0 ... linking ... done. Loading package containers-0.3.0.0 ... linking ... done. Loading package mtl-1.1.0.2 ... linking ... done. Loading package fgl-5.4.2.2 ... linking ... done. ***test_inp: [(1,'a'),(2,'b'),(3,'c')] ***foldl'r AsLeft AsNull AsRight AsNull test_inp: (AsLeft (AsLeft (AsLeft AsNull 1) 2) 3,AsRight 'a' (AsRight 'b' (AsRight 'c' AsNull))) ***if_recur_foldlr AsLeft AsNull AsRight AsNull test_inp: (AsLeft (AsLeft (AsLeft AsNull 1) 2) 3,AsRight 'a' (AsRight 'b' (AsRight 'c' AsNull))) ---foldl'r_fst: AsLeft (AsLeft (AsLeft AsNull 1) 2) 3 ---foldl'r_snd: AsRight 'a' (AsRight 'b' (AsRight 'c' AsNull)) *HutBacFoldlr --}--eshell output-- The two outputs preceded by the *** lines show both foldl'r and if_recur_foldlr compute the same result. The two outputs preceded by the --- lines show that part of the result of foldl'r output is a tuple whose fst is a function and whose 2nd is an already calculated value. The manual trace in attachment 1 shows how this fst function is calculated. I think the last step in foldl'r, the mapFst call, corresponds to applying the last argument in the foldl_as_foldr method as shown at (1.1.rhs.8) of attachment 1. IOW, the application of v in the rhs of (1.1) in attachment 1 performs the same role (as far as the foldl calculation is concerned) the mapFst ($b0) . in foldl'r rhs. What's also interesting is that with if_recur_foldlr, the foldr calculations (i.e. calls to fr argument) are delayed until going up the call stack; whereas in foldl'r the foldl calculations (i.e. calls to fl argument) are delayed by composing a sequence of functions with the: \b - k $! fl b a expression. I'm not real sure if these observations have any importantance, but maybe there's a difference of performance between composing a sequence of functions (as in the foldl'r function) as opposed to storing values on the call stack (as in the if_record_foldlr function). Maybe you have some insight on this. -regards, Larry On 12/07/10 12:36, Henning Thielemann wrote: Noah Easterly wrote: Somebody suggested I post this here if I wanted feedback. So I was thinking about the ReverseState monad I saw mentioned on r/haskell a couple days ago, and playing around with the concept of information flowing two directions when I came up with this function: bifold :: (l - a - r - (r,l)) - (l,r) - [a] - (r,l) bifold _ (l,r) [] = (r,l) bifold f (l,r) (a:as) = (ra,las) where (ras,las) = bifold f (la,r) as (ra,la) = f l a ras (I'm sure someone else has come up with this before, so I'll just say I discovered it, not invented it). Basically, it's a simultaneous left and right fold, passing one value from the start of the list toward the end, and one from the end toward the start. I also needed a bidirectional fold in the past. See
Re: [Haskell-cafe] OT: Monad co-tutorial: the Compilation Monad
On 12/17/10 07:07, Daniel Fischer wrote: On Friday 17 December 2010 13:45:38, Larry Evans wrote: WARNING: I clicked on that link in my thunderbird news reader and got a page which was something about registering domains. It was nothing about Neil's slides. I then tried directing my Firfox browser to: http://community.haskell.org/ but got the same web page. Am I doing something wrong or has somehow community.haskell.org been hijacked somehow? -Larry It seems the haskell.org domain hasn't been renewed on time. Yes, it works for me now. Thanks for all the replies explaining the reason. BTW, I found the video for the talk here: http://vimeo.com/15465133 -regards, Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] OT: Monad co-tutorial: the Compilation Monad
On 12/17/10 01:32, Max Bolingbroke wrote: [snip] I can't speak for your monad based approach, but you may be interested in Neil Mitchell's Haskell DSL for build systems, called Shake: http://community.haskell.org/~ndm/downloads/slides-shake_a_better_make-01_oct_2010.pdf WARNING: I clicked on that link in my thunderbird news reader and got a page which was something about registering domains. It was nothing about Neil's slides. I then tried directing my Firfox browser to: http://community.haskell.org/ but got the same web page. Am I doing something wrong or has somehow community.haskell.org been hijacked somehow? -Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] ANN: Digestive functors 0.0.2.0
On 12/09/10 16:46, Jasper Van der Jeugt wrote: Hello all, I'm very glad to announce the 0.0.2.0 release of the digestive functors library. The library provides a general API to input consumption, and is an upgrade of formlets. I've written an announcing blogpost and tutorial with more information here: http://jaspervdj.be/posts/2010-12-09-digestive-functors-0.0.2.html Hi jasper. Following the instructions on the .html page, I did: cabal update cabal install snap-server cabal install digestive-functors-blaze cabal install digestive-functors-snap However, when I tried: runghc -v 2010-12-09-digestive-functors-0.0.2.lhs I got: --{--cut here-- *** Chasing dependencies: Chasing modules from: *2010-12-09-digestive-functors-0.0.2.lhs Created temporary directory: /tmp/ghc7611_0 *** Literate pre-processor: /usr/lib/ghc-6.12.1/unlit -h 2010-12-09-digestive-functors-0.0.2.lhs 2010-12-09-digestive-functors-0.0.2.lhs /tmp/ghc7611_0/ghc7611_0.lpp 2010-12-09-digestive-functors-0.0.2.lhs:15:9: Could not find module `Text.Digestive.Blaze.Html5': locations searched: Text/Digestive/Blaze/Html5.hs Text/Digestive/Blaze/Html5.lhs Failed, modules loaded: none. *** Deleting temp files: Deleting: /tmp/ghc7611_0/ghc7611_0.lpp --}--cut here-- In directory: /home/evansl/.cabal/lib/digestive-functors-blaze-0.0.2.0/ghc-6.12.1/Text/Digestive/Blaze there is: -rw-r--r-- 1 evansl evansl 13984 Dec 10 09:18 Html5.hi Please, what should I do? -regards, Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] ANN: Digestive functors 0.0.2.0
On 12/10/10 10:38, Jasper Van der Jeugt wrote: Hello, Thanks for the error report. Is blaze-html installed correctly? Could you cabal install blaze-html and verify that you can import Text.Blaze in ghci? Here's the terminal session: --{--cut here-- /home/evansl/prog_dev/haskell/my-code/digestive-functors $ cabal install blaze-html Resolving dependencies... No packages to be installed. All the requested packages are already installed. If you want to reinstall anyway then use the --reinstall flag. /home/evansl/prog_dev/haskell/my-code/digestive-functors $ cat TextBlaze.hs module TextBlaze where import Text.Blaze /home/evansl/prog_dev/haskell/my-code/digestive-functors $ ghci TextBlaze.hs GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling TextBlaze( TextBlaze.hs, interpreted ) Ok, modules loaded: TextBlaze. *TextBlaze --}--cut here-- HTH. -Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Bifold: a simultaneous foldr and foldl
On 12/01/10 21:35, Larry Evans wrote: [snip] Hi Noah, The attached is my attempt at reproducing your code [snip] However, ghci compilation of bifold produces an error message: BifoldIfRecur.hs:20:19: parse error on input `=' [snip] Apparently I had some extra leading spaces in the last line. Taking those out (as shown in attached) results in a good reading of the file by ghci. -Larry {- Purpose: Test bifold code shown in post: http://article.gmane.org/gmane.comp.lang.haskell.cafe/83874 -} module Bifold where {--} bifold :: (l - a - r - (r,l)) - (l,r) - [a] - (r,l) bifold _ (l,r) [] = (r,l) bifold f (l,r) (a:as) = (ra,las) where (ras,las) = bifold f (la,r) as (ra,la) = f l a ras {--} ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Bifold: a simultaneous foldr and foldl
On 12/01/10 21:35, Larry Evans wrote: On 11/30/10 13:43, Noah Easterly wrote: [snip] Thanks, Larry, this is some interesting stuff. I'm not sure yet whether Q is equivalent - it may be, but I haven't been able to thoroughly grok it yet. [snip] Hi Noah, The attached is my attempt at reproducing your code and also contains an alternative attempt at emulating the code in section 12.5 of: http://www.thocp.net/biographies/papers/backus_turingaward_lecture.pdf The attached code is a revision of my previous if_recur attempt at emulated the section 12.5 code. This revision added the i function from the seciton 12.5 (instead of delegating that task to the h function). The 2nd attachment shows the output. It shows that by modifying the args to if_recur, you can reproduce the output from foldl or foldr. {- Purpose: create a function, if_recur, like the f in section 12.5 of: [BAC77] http://www.thocp.net/biographies/papers/backus_turingaward_lecture.pdf -} module IfRecur where -- {*if_recur if_recur :: state_down - (state_down - Bool) - (state_down - state_down) - (state_down - state_saved) - ((state_saved,state_up) - state_up) - (state_down - state_up) - state_up if_recur state_now -- current state recur_ -- continue recursion? then_down -- ::state_down - state_down save_state -- ::state_down - state_saved now_up -- ::((state_saved,state_up)-state_up else_ -- ::state_down - state_up {- The following table shows the corresponndence between the f in section 12.5 of [BAC77] and the arguments to this function: [BAC77] [if_recur] === == p recur_ g else_ j then_down i save_state h now_up -} = if recur_ state_now then now_up ( save_state state_now , if_recur (then_down state_now) recur_ then_down save_state now_up else_ ) else else_ state_now -- }*if_recur {--} palindrome :: [a] - [a] palindrome x = if_recur (x,[]) --state_now (not.null.fst) --recur_ (\(sn,cd) - (tail sn,(head sn):cd)) --then_down (\(sn,cd) - head sn) --save_state (\(ss,cu) - ss:cu) --now_up (\(sn,cd) - cd) --else_ if_recur_foldl :: [a] - [a] if_recur_foldl x = if_recur (x,[]) --state_now (not.null.fst) --recur_ (\(sn,cd) - (tail sn,(head sn):cd)) --then_down (\(sn,cd) - ()) --save_state (\(ss,cu) - cu) --now_up (\(sn,cd) - cd) --else_ if_recur_foldr :: [a] - [a] if_recur_foldr x = if_recur (x,[]) --state_now (not.null.fst) --recur_ (\(sn,cd) - (tail sn,cd)) --then_down (\(sn,cd) - head sn) --save_state (\(ss,cu) - ss:cu) --now_up (\(sn,cd) - cd) --else_ test = sequence [ print palindrome [1,2,3]: , print (palindrome [1,2,3]) , print if_recur_foldl [1,2,3]: , print (if_recur_foldl [1,2,3]) , print (foldl (flip(:)) [] [1,2,3]): , print (foldl (flip(:)) [] [1,2,3]) , print if_recur_foldr [1,2,3]: , print (if_recur_foldr [1,2,3]) , print (foldr (:) [] [1,2,3]): , print (foldr (:) [] [1,2,3]) ] {--} /home/evansl/prog_dev/haskell/my-code $ ghci IfRecur.hs GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help Loading package ghc-prim ... linking ... done. Loading package integer-gmp ... linking ... done. Loading package base ... linking ... done. [1 of 1] Compiling IfRecur ( IfRecur.hs, interpreted ) Ok, modules loaded: IfRecur. *IfRecur test palindrome [1,2,3]: [1,2,3,3,2,1] if_recur_foldl [1,2,3]: [3,2,1] (foldl (flip(:)) [] [1,2,3]): [3,2,1] if_recur_foldr [1,2,3]: [1,2,3] (foldr (:) [] [1,2,3]): [1,2,3] [(),(),(),(),(),(),(),(),(),()] *IfRecur ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Categorical description of systems with dependent types
On 12/02/10 09:13, Petr Pudlak wrote: Hi, recently, I was studying how cartesian closed categories can be used to describe typed functional languages. Types are objects and morphisms are functions from one type to another. Since I'm also interested in systems with dependent types, I wonder if there is a categorical description of such systems. The problem (as I see it) is that the codomain of a function depends on a value passed to the function. I'd happy if someone could give me some pointers to some papers or other literature. Thanks, Petr Hi Petr, Although it's not a categorical description of dependent types (AFAICT, but I've almost no experience in category theory), dependent types are described by Nuprl: http://www.cs.cornell.edu/Info/People/sfa/Nuprl/NuprlPrimitives/Xfunctionality2_doc.html For example, on that page, there's this: Actually, A-B is a special form of the more general x:A-C(x). When A is a type and C(x) is a type-valued function (in x) over domain A, then a member of x:A-C(x) is a function which for an argument, a:A takes a value in the type C(a). HTH. -Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Categorical description of systems with dependent types
On 12/02/10 11:19, Iavor Diatchki wrote: Hi, Bart Jacobs's book Categorical Logic and Type Theory has a categorical description of a system with dependent types (among others). The book is fairly advanced but it has lots of details about the constructions. Hope this helps, -Iavor Page 586 of Jacobs' book mentions dependent products and dependent sums. What confuses me is that Nuprl defines the dependent product as a dependent function: http://www.cs.cornell.edu/Info/People/sfa/Nuprl/NuprlPrimitives/Xfunctionality2_doc.html and the dependent sum as the dependent product: http://www.cs.cornell.edu/Info/People/sfa/Nuprl/NuprlPrimitives/Xpairs_doc.html I sorta see that because the disjoint sum (i.e. the dependent product in Nuprl terms) is actually a pair of values, the discriminant (1st part) and the value whose type depends on the value of the discriminant. And I can see Nuprl's choice to call the dependent product as a dependent function because passing an index to this function returns a value whose type is dependent on the index. This is just like the value constructed by a haskell datatypes with field labels: data Record = MkRec { f1::T1, f2::T2, ..., fn::Tn } r = MkRec{ f1 = t1, f2 = t2,..., fn = tn} However, instead of r as the dependent function, the fields are the functions: fi r :: Ti, for i=1...n instead of Nuprl's notion: r fi :: Ti, for i=1...n Anybody know a good reason why the categorical and nuprl terms differ, leading, to (at least in my case) a bit of confusion? -Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Categorical description of systems with dependent types
On 12/02/10 15:47, Iavor Diatchki wrote: Hi, You have it exactly right, and I don't think that there's a particularly deep reason to prefer the one over the other. It seems that computer science people tend to go with the (product-function) terminology, while math people seem to prefer the (sum-product) version, but it is all largely a matter of taste. -Iavor [snip] *Maybe* the computer science people are trying to minimize the concepts. In this case, the one concept common to both the sum and product ( in the math peoples viewpoint) is there's a function: field2type: field_name - Type in the case of a product or record, r, it's: product = f:fieldname - field2type(f) in the case of a disjoint sum its: sum = (f:fieldname, field2type(f)) or something like that. -Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Bifold: a simultaneous foldr and foldl
On 11/30/10 13:43, Noah Easterly wrote: On Tue, Nov 30, 2010 at 9:37 AM, Larry Evans cppljev...@suddenlink.net mailto:cppljev...@suddenlink.net wrote: suggested to me that bifold might be similar to the function, Q, of section 12.5 equation 1) on p. 15 of: http://www.thocp.net/biographies/papers/backus_turingaward_lecture.pdf [snip] [snip] Thanks, Larry, this is some interesting stuff. I'm not sure yet whether Q is equivalent - it may be, but I haven't been able to thoroughly grok it yet. For uniformity, I shifted the notation you gave to Haskell: (.^) :: (a - a) - Int - a - a f .^ 0 = id f .^ n = f . (f .^ (n - 1)) (./) :: (b - c - c) - [a - b] - (a-c) - a - c (./) = flip . foldr . \h f g - h $ f * g _Q_ :: (b - c - c) - (a - b) - (a - a) - (a - c) - a - c _Q_ h i j k = h $ i * (k . j) So the shorthand just states the equivalence of (_Q_ h i j) .^ n and (./) h [ i . (j .^ m) | m - [0 .. n-1] ] . ( . (j .^ n)) Looking at it that way, we can see that (_Q_ h i j) .^ n takes some initial value, unpacks it into a list of size n+1 (using i as the iterate function), derives a base case value from the final value (and some function k) maps the initial values into a new list, then foldrs over them. The _f_ function seems to exist to repeat _Q_ until we reach some stopping condition (rather than n times) _f_ :: (b - c - c) - (a - b) - (a - a) - (a - Bool) - (a - c) - a - c _f_ h i j p q a = if p a then q a else _Q_ h i j (_f_ h i j p q) a No simple way to pass values from left to right pops out at me, but I don't doubt that bifold could be implemented in foldr, and therefore there should be *some* way. Hi Noah, The attached is my attempt at reproducing your code and also contains an alternative attempt at emulating the code in section 12.5 of: http://www.thocp.net/biographies/papers/backus_turingaward_lecture.pdf However, ghci compilation of bifold produces an error message: BifoldIfRecur.hs:20:19: parse error on input `=' OTOH, when this code is commented out and the test variable is printed, the output is: [1,2,3,999,3,2,1] [3,2,1,999] [1,2,3,999] [(),(),()] The first line is for a call to if_recur. The other two are for foldl and foldr where the binary operator is (flip (:)) and (:), respectively. The suffix after 999 of the 1st line suggests to me that if_recur does something like foldr with the else_ function is called, after which something like foldr is done, as indicated by the [1,2,3] prefix before 999 of the 1st line. So it seems that both foldr and foldl are being done during if_recur, IOW, it's a kinda bifold also. Hopefully this sheds some light on how section 12.5 is related to bifold; however, I'm still not completely sure what that relation is :( -regards, Larry {- Purpose: Further develop idea that Bifold in posts: http://article.gmane.org/gmane.comp.lang.haskell.cafe/83874 http://article.gmane.org/gmane.comp.lang.haskell.cafe/83883 if somehow like the f in section 12.5 of: http://www.thocp.net/biographies/papers/backus_turingaward_lecture.pdf -} module BifoldIfRecur where import Monad; import Control.Applicative; -- {*83874 article {- bifold :: (l - a - r - (r,l)) - (l,r) - [a] - (r,l) bifold _ (l,r) [] = (r,l) bifold f (l,r) (a:as) = (ra,las) where (ras,las) = bifold f (la,r) as (ra,la) = f l a ras -} -- }*83874 article -- {*83883 article {--} (.^) :: (a - a) - Int - a - a f .^ 0 = id f .^ n = f . (f .^ (n - 1)) (./) :: (b - c - c) - [a - b] - (a-c) - a - c (./) = flip . foldr . \h f g - h $ f * g _Q_ :: (b - c - c) - (a - b) - (a - a) - (a - c) - a - c _Q_ h i j k = h $ i * (k . j) {--} -- }*83883 article -- {*if_recur.hpp from http://svn.boost.org/svn/boost/sandbox/variadic_templates/boost/mpl/ if_recur :: state_down - (state_down - Bool) - (state_down - state_down) - ((state_down,state_up) - state_up) - (state_down - state_up) - state_up if_recur state_now -- current state recur_ -- continue recursion? then_down -- ::state_down - state_down now_up -- ::((state_down,state_up)-state_up else_ -- ::state_down - state_up = if recur_ state_now then now_up ( state_now , if_recur (then_down state_now) recur_ then_down now_up else_ ) else else_ state_now -- }*if_recur.hpp from http://svn.boost.org/svn/boost/sandbox/variadic_templates/boost/mpl/ {--} palindrome_btm :: [a] - a - [a] palindrome_btm x b = if_recur (x,[]) --state_now (not.null.fst) --recur_ (\(sn,cd) - (tail sn,(head sn):cd)) --then_down
[Haskell-cafe] Re: Bifold: a simultaneous foldr and foldl
On 11/29/10 21:41, Noah Easterly wrote: Somebody suggested I post this here if I wanted feedback. So I was thinking about the ReverseState monad I saw mentioned on r/haskell a couple days ago, and playing around with the concept of information flowing two directions when I came up with this function: bifold :: (l - a - r - (r,l)) - (l,r) - [a] - (r,l) bifold _ (l,r) [] = (r,l) bifold f (l,r) (a:as) = (ra,las) where (ras,las) = bifold f (la,r) as (ra,la) = f l a ras (I'm sure someone else has come up with this before, so I'll just say I discovered it, not invented it). Basically, it's a simultaneous left and right fold, passing one value from the start of the list toward the end, and one from the end toward the start. [snip] Hi Noah, I've not examined bifold real close, but the description: passing one value from the start of the list toward the end, and one from the end toward the start. suggested to me that bifold might be similar to the function, Q, of section 12.5 equation 1) on p. 15 of: http://www.thocp.net/biographies/papers/backus_turingaward_lecture.pdf Now Q takes just 1 argument, a function. Also, the f function defined with Q can be short-circuited, IOW, it may not process all the elements is a list. Also, it may not even act on a list; however, that just means it's more general and could be easily specialized to just act on lists. Anyway, I'll reproduce some of the reference's section 12.5 here: f == p - q; Q(f) where: Q(k) == h * [i, k*j] and where(by p. 4 of reference): * is the function composition operator: (f * g) x == f(g(x)) and where(by p. 9 of reference): [f,g] x = f x, g x and where(by p. 8 of reference) x1,x2,...,xn is a sequence of objects, x1,x2,...xn (like haskell's tuple: (x1,x2,...,xn) and where(by p. 8 of reference): (p - g; h)(x) means if p(x) then do g(x) else do h(x) for any functions, k, p, g, h, i, j. p. 16 provides a nice shorthand for the result of that function: Q^n(g) = /h*[i,i*j,...,i*j^(n-1),g*J^n where: f^n = f*f** f for n applications of f /h*[f1,f2,...,fn] is defined on p. 13 of reference. Again, I'm not sure it's the same a bifold, but it seems pretty close. -Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Is there a tutorial interpreter to teach haskell?
By tutorial interpreter, I means something like an expert system having a list of rules and than a problem which is solved by using those list of rules. The tutorial means the trace of the problem state before and after each rule is applied along with which parts of the rule are matched with which part of the problem state. W.R.T. haskell, the problem state would be a haskell expression and each rule application would be simply reducing the haskell expression (using some rule, which would be cited during the reduction) to a simpler form until the final answer was achieved. Obviously the rule applications to be trace should be user selectable to avoid way too much output, but that seems similar to setting breakpoints in selected functions; hence, I guess it wouldn't be hard to do. The attached file illustrates what I'm after. I laboriously composed that attached to enable me to understand what the haskell code was doing. It would help other novices if such a trace could be automated. I'm currently trying to understand: sequence (c:cs) = return (:) `ap` c `ap` sequence cs from p. 2 of: http://www.soi.city.ac.uk/~ross/papers/Applicative.pdf and again I'm having a lot of difficulty what the code is doing. So far I've got: --{--eshell-- Prelude Monad :t ap ap :: (Monad m) = m (a - b) - m a - m b Prelude Monad :t return (:) return (:) :: (Monad m) = m (a - [a] - [a]) Prelude Monad (:) 'a' bc abc Prelude Monad :t return (:) `ap` a return (:) `ap` a :: [[Char] - [Char]] --}--eshell-- so now I must manually figure out what the a and b in the ap declaration correspond to in the return(:) type: m( a - b ) __ _ _ 1: [] Char - [Char]-[Char] 2: [] Char-[Char] - [Char] IOW, is it choice 1: in the above table for choice 2:? I'd guess, since application associates to the left, that it's choice 1:. But you see, I'm not sure, and I have to work too hard to figure it all out. I *may* get it eventually, but it's a *lot* of work. I tutorial interpreter would make this so much easier! -regards, Larry Purpose: Trace the execution of cross function in: http://www.muitovar.com/monad/moncow.xhtml#list Trace: By: http://www.muitovar.com/monad/moncow.xhtml#list Gives: cross AB [1,2] = do { x - AB ; y - [1,2] ; return (x,y) } By: http://www.haskell.org/onlinereport/exps.html#sect3.14 .Translation .do{p-e;stmts} Where: p == x e == AB stmts = y-[1,2];return(x,y) Gives: AB=\x- do { y-[1,2] ; return (x,y) } By: http://www.haskell.org/onlinereport/exps.html#sect3.14 .Translation .do{p-e;stmts} Where: p == y e == [1,2] stmts = return(x,y) Gives: AB=\x- [1,2]=\y- { return (x,y) } By: http://www.haskell.org/onlinereport/exps.html#sect3.14 .Translation .do{e} Where: e == return(x,y) Gives: AB=\x- [1,2]=\y- return (x,y) By: http://www.haskell.org/onlinereport/standard-prelude.html .instance Monad [] where .return x Where: x == (x,y) Gives: AB=\x- [1,2]=\y- [(x,y)] By: http://www.haskell.org/onlinereport/standard-prelude.html .instance Monad [] where .m = k Where: m == [1,2] k == \y - [(x,y)] Gives: AB=\x- concat ( map (\y-[(x,y)]) [1,2] ) By: http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map .map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn] Where: f == \y-[(x,y)] [x1, x2, ..., xn] == [1,2] Gives: AB=\x- concat ( [(\y-[(x,y)]) 1 ,(\y-[(x,y)]) 2 ] ) By: http://en.wikipedia.org/wiki/Lambda_calculus#.CE.B2-reduction Where: V == y E == [(x,y)] E' == 1 E' == 2 Gives: AB=\x- concat ( [ [(x,1)] , [(x,2)] ] ) By: http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v%3Aconcat Where: type a = (Char,Int) Gives: AB=\x- [ (x,1) , (x,2) ] By: http://www.haskell.org/onlinereport/standard-prelude.html .instance Monad [] where .m = k Where: m == AB k == \x- [ (x,1) , (x,2) ] Gives: concat ( map (\x - [ (x,1) , (x,2) ] ) AB ) By: http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#v:map .map f [x1, x2, ..., xn] == [f x1, f x2, ..., f xn] Where: f == \x-[(x,1),(x,2)] [x1, x2, ..., xn] == ['A','B'] Gives: concat ( [ (\x - [ (x,1) , (x,2) ] ) 'A' , (\x - [ (x,1) , (x,2) ] ) 'B' ] ) By: http://en.wikipedia.org/wiki/Lambda_calculus#.CE.B2-reduction Where: V == x E == [(x,1),(x,2)]
[Haskell-cafe] type universes (was Re: MPTC inheritance
On 02/24/09 13:41, Paul Johnson wrote: Derek Gladding wrote: Please forgive me if I'm still mentally contaminated by the OO way of seeing (and discussing) the universe, but I'm trying to figure out how to inherit an interface from a multi-parameter type class. [...] but this isn't allowed (kind mismatch). Kinds are a meta-type system for types. Because Haskell has such a rich type system, the types themselves need a type-like system. These are kinds. You never declare kinds (apart from certain language extensions not in use here): the compiler infers them. Is a kind sorta like a type universe as in nuprl: http://www.cs.cornell.edu/home/sfa/Nuprl/NuprlPrimitives/Xuniverse_doc.html Except that a kind sounds like a universe at level 2 or 3. IOW, I guess haskell types are at level 1, and kines at level 2? Then I guess values would be at level 0? Is there some version of haskell that has more levels in its type universe. If not, it there some reason for that limitation? Is there some reference explaining the relationship of haskell types to nuprl type universes? TIA. -Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: howto tuple fold to do n-ary cross product?
On 11/30/08 12:49, Larry Evans wrote: [snip] You'll see Domains can be an mpl::vector of any length. The cross_nproduct_view_test.cpp tests with a 3 element Domains: typedef mpl::vector mpl::range_cint,0,4 , mpl::range_cint,100,103 , mpl::range_cint,2000,2002 domains; OOPS. That's in another test driver. The one in the cross_nproduct_view_test.cpp has: typedef range_cint,0,1 seq0; typedef range_cint, 100, 102 seq1; typedef range_cint, 2000, 2002 seq2; typedef range_cint,3,30003 seq3; typedef list seq0 , seq1 , seq2 , seq3 domains; The range_cint, 100, 102 template instance: http://www.boost.org/doc/libs/1_37_0/libs/mpl/doc/refmanual/range-c.html produces a type sequence of length 2. So mpl::listseq0,...,seq3 is a sequence of sequences similar to haskell's [[a]] except that it's a sequence of a sequences of types instead of a sequence of sequences of values. The cross_nproduct_view template and test driver are found in the cross_nproduct_view.zip file here: http://preview.tinyurl.com/5ar9g4 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: howto tuple fold to do n-ary cross product?
On 11/23/08 13:52, Luke Palmer wrote: 2008/11/23 Larry Evans [EMAIL PROTECTED]: http://www.muitovar.com/monad/moncow.xhtml#list contains a cross function which calculates the cross product of two lists. That attached does the same but then used cross on 3 lists. Naturally, I thought use of fold could generalize that to n lists; however, I'm getting error: You should try writing this yourself, it would be a good exercise. To begin with, you can mimic the structure of cross in that tutorial, but make it recursive. After you have a recursive version, you might try switching to fold or foldM. The type of the function will not involve tuples, since they can be arbitrary length (dynamic-length tuples are not supported in Haskell; we use lists for that). cross :: [[a]] - [[a]] However, list's contain elements all of the same type. What the following boost post: http://thread.gmane.org/gmane.comp.lib.boost.devel/182797/focus=182915 demonstrated was, AFAICT, the c++ template metaprogramming counterpart to the moncow haskell cross. Now, AFAICT, the boost vault directory: http://www.boostpro.com/vault/index.php?PHPSESSID=ab51206c9d980155d142f5bcef8e00eedirection=0order=directory=Template%20Metaprogramming in the cross_nproduct_view_test.zip, contains what I'm looking for in haskell. I'm guessing that: templateclass Row, class Columnstruct row_view; corresponds to the haskell tuple type (row,column) I'm trying to confirm that by printing out the typename in a formated form, but I'm having trouble doing that at the moment: http://preview.tinyurl.com/66x4nx Is there some version of haskell, maybe template haskell, that can do that, i.e. instead of: cross::[[a]] - [[a]] have: crossn::[a0]-[a1]-...-[an] - [(a0,a1,...,an)] ? ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: howto tuple fold to do n-ary cross product?
On 11/30/08 11:30, Luke Palmer wrote: On Sun, Nov 30, 2008 at 10:25 AM, Larry Evans [EMAIL PROTECTED] wrote: Is there some version of haskell, maybe template haskell, that can do that, i.e. instead of: cross::[[a]] - [[a]] have: crossn::[a0]-[a1]-...-[an] - [(a0,a1,...,an)] Ah yes! This is straightforward usage of the list monad. I suggest applicative notation: import Control.Applicative (,,,) $ xs0 * xs1 * xs2 * xs3 Or alternatively: import Control.Monad liftM4 (,,,) xs0 xs1 xs2 xs3 (I would have used liftA4, but it's not defined. The definition looks a lot like the first example :-) This notation seems a bit magical, but you can build what you want using a simple binary cross: cross :: [a] - [b] - [(a,b)] It's just kind of a pain (you build [(a,(b,(c,d)))] and then flatten out the tuples). The applicative notation is a neat little trick which does this work for you. Thanks Luke. I'll try that. If you're asking whether crossn, as a single function which handles arbitrarily many arguments, can be defined, the short answer is no. I dare you to come up with a case in which such function adds more than cursory convenience. The following post: http://thread.gmane.org/gmane.comp.lib.boost.devel/182797 shows at least one person that would find it useful, at least in c++. Of course maybe it would be less useful in haskell. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: howto tuple fold to do n-ary cross product?
On 11/30/08 12:04, Larry Evans wrote: [snip] The following post: http://thread.gmane.org/gmane.comp.lib.boost.devel/182797 shows at least one person that would find it useful, at least in c++. Of course maybe it would be less useful in haskell. One thing that maybe confusing things is that the c++ template code calculated a crossproduct of types, not values. The haskell code: cross::[[a]]-[[a]] calculate a cross product of values. Sorry if that was unclear. I was trying to use haskell to guide me in doing something similar with c++ template metaprogramming. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: howto tuple fold to do n-ary cross product?
On 11/30/08 12:27, Luke Palmer wrote: On Sun, Nov 30, 2008 at 11:04 AM, Larry Evans [EMAIL PROTECTED] wrote: The following post: http://thread.gmane.org/gmane.comp.lib.boost.devel/182797 shows at least one person that would find it useful, at least in c++. Of course maybe it would be less useful in haskell. The line: typedef boost::mpl::vector T1Variants, T2Variants, T3Variants TT; Has the number of lists hard-coded as 3, and does not abstract over it. This corresponds to the 3 in liftA3, or the number of *s in the expression. Abstracting over the number and types of arguments is something neither C++ nor Haskell is very good at. But in order to be able to do any abstraction using such a variable-argument function, the type systems of these languages would have to increase in complexity by quite a lot. Luke True, but if you look at the cross_nproduct_view template: {--cut here-- template class Domains struct cross_nproduct_view : fold Domains , cross_product_one , cross_product_viewarg2,arg1 ::type { }; }--cut here-- You'll see Domains can be an mpl::vector of any length. The cross_nproduct_view_test.cpp tests with a 3 element Domains: typedef mpl::vector mpl::range_cint,0,4 , mpl::range_cint,100,103 , mpl::range_cint,2000,2002 domains; The cross_nproduct_view template and test driver are found in the cross_nproduct_view.zip file here: http://preview.tinyurl.com/5ar9g4 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: howto tuple fold to do n-ary cross product?
On 11/24/08 00:40, Andrea Vezzosi wrote: It's more natural to consider the cross product of no sets to be [[]] so your crossr becomes: crossr [] = [[]] crossr (x:xs) = concat (map (\h -map (\t - h:t) (crossr tail)) hd) which we can rewrite with list comprehensions for conciseness: crossr [] = [[]] crossr (x:xs) = [ a:as | a - x, as - crossr xs ] then look at the definition of foldr: foldr f z [] = z foldr f z (x:xs) = f x (foldr f z xs) and, considering (foldr f z) == crossr, you should derive the definition of f and z. THANK YOU Andrea (and Luke) for prompting me to a solution: crossf::[[a]] - [[a]] crossf lls = foldr (\hd tail - concat (map (\h -map (\t - h:t) tail) hd)) [[]] lls The reason I'm interested in this is that the cross product problem came up in the boost newsgroup: http://thread.gmane.org/gmane.comp.lib.boost.devel/182797/focus=182915 I believe programming the solution in a truly functional language might help a boost mpl programmer to see a solution in mpl. I expect there's some counterpart to haskell's map, concat, and foldr in mpl and so the mpl solution would be similar to the above crossf solution. -kind regards to both of you, Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] howto tuple fold to do n-ary cross product?
http://www.muitovar.com/monad/moncow.xhtml#list contains a cross function which calculates the cross product of two lists. That attached does the same but then used cross on 3 lists. Naturally, I thought use of fold could generalize that to n lists; however, I'm getting error: {-- cut here Compilation started at Sun Nov 23 13:15:24 make runghc -XMultiParamTypeClasses -XFunctionalDependencies -XFlexibleInstances cross.hs cross.hs:23:30: Occurs check: cannot construct the infinite type: a = (a, a) Expected type: [a] - [a] - [a] Inferred type: [a] - [a] - [(a, a)] In the first argument of `Data.Foldable.foldl1', namely `cross' In the first argument of `print', namely `(Data.Foldable.foldl1 cross (list2, list3, list1))' make: *** [run] Error 1 }-- cut here How do I apply fold to cross and n lists, for n=0? TIA. -regards, Larry {- Purpose: Demonstrate http://www.muitovar.com/monad/moncow.xhtml#list -} import Data.Foldable cross::[a]-[b]-[(a,b)] cross ls1 ls2 = do { x- ls1 ; y- ls2 ; return (x,y) } list1=[1,2] list2=[10,20] list3=[100,200] main = do putStrLn cross list2 list3= print (cross list2 list3) putStrLn cross list1 (cross list2 list3)= print (cross list1 (cross list2 list3)) print (list2,list3,list1) print (Data.Foldable.foldl1 cross (list2,list3,list1)) ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: howto tuple fold to do n-ary cross product?
On 11/23/08 13:52, Luke Palmer wrote: 2008/11/23 Larry Evans [EMAIL PROTECTED]: http://www.muitovar.com/monad/moncow.xhtml#list contains a cross function which calculates the cross product of two lists. That attached does the same but then used cross on 3 lists. Naturally, I thought use of fold could generalize that to n lists; however, I'm getting error: You should try writing this yourself, it would be a good exercise. To begin with, you can mimic the structure of cross in that tutorial, but make it recursive. After you have a recursive version, you might try switching to fold or foldM. Thanks. The recursive method worked with: -{--cross.hs-- crossr::[[a]] - [[a]] crossr lls = case lls of { [] - [] ; [hd]- map return hd ; hd:tail - concat (map (\h -map (\t - h:t) (crossr tail)) hd) } -}--cross.hs-- However, I'm not sure fold will work because fold (or rather foldr1) from: http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html#12 has signature: (a-a-a)-[a]-a and in the cross product case, a is [a1]; so, the signature would be ([a1]-[a1]-[a1]-[[a1]]-[a1] but what's needed as the final result is [[a1]]. Am I missing something? -Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: [off-topic] OOPer?
On 11/17/08 18:24, Daniel Yokomizo wrote: On Mon, Nov 17, 2008 at 9:49 PM, Maurício [EMAIL PROTECTED] mailto:[EMAIL PROTECTED] wrote: (...) I don't recall where I found the following example, but copied it locally as compelling evidence that the functional solution can be much clearer and shorter than the same solution modeled with objects and inheritance. [snip] -- Arithmetic expression forms data Expr = Num Int | Add Expr Expr -- Evaluate expressions eval :: Expr - Int (...) public abstract class Expr { public abstract int eval (); public abstract void modn(int v); [snip] when the first standard was set, and I can tell you for sure that, even at that time, no one who knows at least the basics of C++ would ever write that problem like this. Mauri, I'm not sure what you mean. Do you mean: 1) No C++er would ever structure the problem like: -- Arithmetic expression forms data Expr = Num Int | Add Expr Expr -- Evaluate expressions eval :: Expr - Int eval (Num i) = i eval (Add l r ) = eval l + eval r If so, then I'm unsure what you could mean since the closest counterpart to: date Expr = Num Int | Add Expr Expr in c++ is an abstract Expr class with derived classes, Int and Add, just as shown in Greg's Java counterpart to the haskell Expr. 2) No C++er would every solve the problem with a heirarchy of Expr classes with virtual functions. If so, then I'm really confused because that's exactly the way I would do it *except* if I wanted to avoid the overhead of virtual function dispatch. In this case, I would use template metaprogramming (WARNING: not for c++ template metaprogramming novices): http://www.boost.org/doc/libs/1_37_0/doc/html/proto/users_guide.html#boost_proto.users_guide.intermediate_form.expression_introspection.defining_dsel_grammars In the proto metaprogramming, AFAICT, the 1st element of the proto::expr template, the tag, corresponds to Expr constructor's, Num and Add, of Greg's haskell example code. The | separating the Expr constructor variants corresponds to the proto::or_ template. So, if template metaprogramming were used, then there are some very good c++ programmer which would structure their c++ code like the haskell code (although, as seen by the #boost_proto.users_guide.intermediate_form.expression_introspection.defining_dsel_grammars reference, it's a bit obscured by all the scaffolding needed to make it work). Another reference which *may* reflect the haskell structure is: http://research.microsoft.com/~akenn/generics/gadtoop.pdf I must admit I don't really understand it, but it seems to have some similarities to haskell's structure. The author even uses haskell code to compare with his corresponding extension to c#. In particular, page 9 contains an example use of his proposed switch statement extension which looks very similar to the way haskell would pattern match on an expression to dispatch to the appropriate case. [snip] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Are arbitrary rank types and existentials equivalent?
On 11/09/08 17:04, Loup Vaillant wrote: [snip] Err, where can I find such texts? I don't even understand intuitionistic predicate logic :-( I just googled that phrase and got many hits. I think metaprl implements something like that: http://metaprl.org/default.html I've often wondered about haskell and metaprl. I know metprl has something like a hierarchy of types and that's used to avoid Russell's paradox. I also remember reading somewhere (can't remember where) that there was something in category theory that had a hierarchy of categories. Maybe someone else can provide more details. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Examples of Mutually Recursive Types
On 10/26/08 17:06, Hugo Pacheco wrote: Probably I overdid the real part. I was thinking of examples such as ASTs (such as the Haskell one), trees and imagining more fancy things, maybe L-systems and fractal processing. I will have a look at the Haskell sources and the previous papers from Tim Sheard. One example is a language grammar. Each non-terminal in the grammar can reference any other non-terminal in the grammar. Thus, if the non-terminals are considered types, then each non-terminal in a grammar is mutually recursive with respect to other non-terminals in that grammar. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: ghc error: requested module name differs from name found in interface file
On 10/21/08 18:55, Larry Evans wrote: On 10/21/08 17:55, Duncan Coutts wrote: On Tue, 2008-10-21 at 09:41 -0500, Larry Evans wrote: Just that one little piece of information, that |cabal install| , by default, installs in ~/.cabal and then enables ghc to look there for packages, would have saved an awful lot of time :( Where would you like that information to have been presented? Perhaps something the first time you used the cabal command to say what configuration it was using? Duncan I'd suggest putting this information after the brief description of the install option here: http://hackage.haskell.org/trac/hackage/wiki/CabalInstall [snip] I'm pretty sure another reason why I had difficulty was that I followed the instructions here: http://www.haskell.org/haskellwiki/Cabal/How_to_install_a_Cabal_package to install cabal itself. Those instructions install globally. I just assumed that |cabal install| would do the same. Maybe if the default install destinations for both manual and |cabal install| were the same, I wouldn't have had this problem. Although I see lower down on that CabalInstall page there's instructions for a user install, I didn't bother to read that far since I was only interested in just getting the package to install ASAP. HTH. -Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: ghc error: requested module name differs from name found in interface file
On 10/21/08 07:35, Bertram Felgenhauer wrote: Larry Evans wrote: On 10/20/08 12:33, Larry Evans wrote: With a file containing: module Main where import Array import Control.Functor.Fix I get: make ghc -i/root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2 -c catamorphism.example.hs Yes, using -i to give paths to installed packages does not work - you really have to tell ghc about the corresponding package.conf file, using -package-conf file. See also http://www.haskell.org/ghc/docs/latest/html/users_guide/packages.html#package-databases catamorphism.example.hs:19:0: Bad interface file: /root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2/Control/Functor/Fix.hi Something is amiss; requested module main:Control.Functor.Fix differs from name found in the interface file category-extras-0.53.5:Control.Functor.Fix make: *** [all] Error 1 The problem is that all modules found by -i are expected to be in the current package - which is 'main' by default. (Build tools like Cabal specify a different package name for libraries; for example the Control.Functor.Fix is in the 'category-extras-0.53.5' package here.) So, I've got to figure how to tell cabal install to install in right place :( Have you tried 'cabal install --global'? To make it stick, put 'user-install: False' in root's .cabal/config file. HTH, Bertram THANK YOU! I finally understand what happened. To manually install cabal-install, I had to change to root. I just assumed I had to stay as root to install category-extras. Since the default for |cabal install| was --user, it put the files in /root/.cabal. I just did |cabal install category-extras| as myself and now: ---cut here --- module Main where import Data.Generics.Biplate import Control.Functor.Fix main = do putStr hello world\n ---cut here--- compiles without error. Just that one little piece of information, that |cabal install| , by default, installs in ~/.cabal and then enables ghc to look there for packages, would have saved an awful lot of time :( -regards, Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: ghc error: requested module name differs from name found in interface file
On 10/21/08 17:55, Duncan Coutts wrote: On Tue, 2008-10-21 at 09:41 -0500, Larry Evans wrote: Just that one little piece of information, that |cabal install| , by default, installs in ~/.cabal and then enables ghc to look there for packages, would have saved an awful lot of time :( Where would you like that information to have been presented? Perhaps something the first time you used the cabal command to say what configuration it was using? Duncan I'd suggest putting this information after the brief description of the install option here: http://hackage.haskell.org/trac/hackage/wiki/CabalInstall For example: Commands: install Installs a list of packages. The 'visibility' of the install depends on whether --user or --global FLAG is used. With --user, the package is only visible to the user invoking the cabal command. With --global, the package is visible to all users; however, this requires root authority. list List available packages on the server (cached). ... I'm at fault for not reading: For more information about a command, try 'cabal COMMAND --help'. lower down on that page. I do remember actually doing cabal install --help but I can't remember if that was after or before Bertrand's post. However, even that command's description of the meaning of --user and --global is obscured by so many other options, that it's easy to miss. Also their descriptions: --user Enable doing a per-user installation --global Disable doing a per-user installation doesn't explain what 'per-user installation' means. If it just said: A per-user installation means the installed package is only seen by the haskell compiler if the compiler is invoked by the same user which issued the 'cabal install' command. That would have clearly indicated to me that the root doing 'cabal install' would not make the installed package available to any other user. (I probably should have figured this out by noting the location was /root/.cabal/...; however, that just didn't happen.) HTH and thanks for your interest. -regards, Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] ghc error: requested module name differs from name found in interface file
With a file containing: module Main where import Array import Control.Functor.Fix I get: make ghc -i/root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2 -c catamorphism.example.hs catamorphism.example.hs:19:0: Bad interface file: /root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2/Control/Functor/Fix.hi Something is amiss; requested module main:Control.Functor.Fix differs from name found in the interface file category-extras-0.53.5:Control.Functor.Fix make: *** [all] Error 1 I used cabal to install category-extras: /home/evansl/download/haskell/libs # cabal install category-extras Resolving dependencies... Downloading category-extras-0.53.5... Configuring category-extras-0.53.5... Preprocessing library category-extras-0.53.5... ... /usr/bin/ar: creating dist/build/libHScategory-extras-0.53.5.a Installing library in /root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2 Registering category-extras-0.53.5... Reading package info from dist/installed-pkg-config ... done. Saving old package config file... done. Writing new package config file... done. /home/evansl/download/haskell/libs # What should I do to import the Functor.Fix as shown here: http://comonad.com/reader/2008/recursion-schemes/ TIA. -Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: ghc error: requested module name differs from name found in interface file
On 10/20/08 12:33, Larry Evans wrote: With a file containing: module Main where import Array import Control.Functor.Fix I get: make ghc -i/root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2 -c catamorphism.example.hs catamorphism.example.hs:19:0: Bad interface file: /root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2/Control/Functor/Fix.hi Something is amiss; requested module main:Control.Functor.Fix differs from name found in the interface file category-extras-0.53.5:Control.Functor.Fix make: *** [all] Error 1 I used cabal to install category-extras: /home/evansl/download/haskell/libs # cabal install category-extras Resolving dependencies... Downloading category-extras-0.53.5... Configuring category-extras-0.53.5... Preprocessing library category-extras-0.53.5... ... /usr/bin/ar: creating dist/build/libHScategory-extras-0.53.5.a Installing library in /root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2 [snip] The problem must be where the library was installed. A manual install: http://www.haskell.org/haskellwiki/Cabal/How_to_install_a_Cabal_package of http://hackage.haskell.org/cgi-bin/hackage-scripts/package/uniplate followed by compile of: --- cut here --- module Main where import Data.Generics.Biplate main = do putStr hello world\n --- cut here --- showed no errors: --- cut here --- make ghc -C import.test.hs Compilation finished at Mon Oct 20 18:47:54 --- cut here --- The key difference is the manual installed as: --- cut here --- /home/evansl/download/haskell/libs/uniplate-1.2.0.1 # runhaskell Setup install Installing library in /usr/local/lib/uniplate-1.2.0.1/ghc-6.8.2 Registering uniplate-1.2.0.1... Reading package info from dist/installed-pkg-config ... done. Saving old package config file... done. Writing new package config file... done. /home/evansl/download/haskell/libs/uniplate-1.2.0.1 # --- cut here --- whereas the cabal install of category-extras installed the library here: --- cut here --- /root/.cabal/lib/category-extras-0.53.5/ghc-6.8.2: total used in directory 2812 available 148773984 drwxr-xr-x 4 root root4096 Oct 19 15:09 . drwxr-xr-x 3 root root4096 Oct 19 15:09 .. drwxr-xr-x 9 root root4096 Oct 19 15:09 Control drwxr-xr-x 2 root root4096 Oct 19 15:09 Data -rw-r--r-- 1 root root 1120345 Oct 19 15:09 HScategory-extras-0.53.5.o -rw-r--r-- 1 root root 1728590 Oct 19 15:09 libHScategory-extras-0.53.5.a --- cut here --- So, I've got to figure how to tell cabal install to install in right place :( ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: Is there already an abstraction for this?
On 09/23/08 01:01, Jake Mcarthur wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 The first thing I thought of was to try to apply one of the recursion schemes in the category-extras package. Here is what I managed using catamorphism. - - Jake - data Expr' a = Quotient a a | Product a a | Sum a a | Difference a a | Lit Double | Var Char type Expr = FixF Expr' instance Functor Expr' where fmap f (a `Quotient` b) = f a `Quotient` f b fmap f (a `Product` b) = f a `Product` f b fmap f (a `Sum` b) = f a `Sum` f b fmap f (a `Difference` b) = f a `Difference` f b fmap _ (Lit x) = Lit x fmap _ (Var x) = Var x identity = cata ident where ident (a `Quotient` InF (Lit 1)) = a ident (a `Product` InF (Lit 1)) = a ident (InF (Lit 1) `Product` b) = b ident (a `Sum` InF (Lit 0)) = a ident (InF (Lit 0) `Sum` b) = b ident (a `Difference` InF (Lit 0)) = a ident (Lit x) = InF $ Lit x ident (Var x) = InF $ Var x According to: cata :: Functor f = Algebra f a - FixF f - a from: http://comonad.com/reader/2008/catamorphism ident must be: Algebra f a for some Functor f; however, I don't see any declaration of ident as an Algebra f a. Could you please elaborate. I'm trying to apply this to a simple boolean simplifier shown in the attachement. As near as I can figure, maybe the f could be the ArityN in the attachment and maybe the a would be (Arity0 ConBool var). The output of the last line of attachment is: bool_eval:f+f+v0=(:+) (Op0 (OpCon BoolFalse)) (Op0 (OpVar V0)) however, what I want is a complete reduction to: (OpVar V0) How can this be done using catamorphisms? {-# LANGUAGE PatternSignatures #-} {- Purpose: Try out the use of catamorphism to simplify an expression as far as possible. Reference: Post: http://www.nabble.com/Re%3A-Is-there-already-an-abstraction-for-this--p19641692.html Headers: From: wren ng thornton Newsgroups: gmane.comp.lang.haskell.cafe Subject: Re: Is there already an abstraction for this? Date: Wed, 24 Sep 2008 00:10:29 -0400 -} module Main where import Array data Arity0 con var --nullary operators = OpCon con -- constant | OpVar var -- variable deriving(Show) data ArityN arity0 = Op0 arity0 | (:+) (ArityN arity0) (ArityN arity0) | (:*) (ArityN arity0) (ArityN arity0) deriving(Show) infixl 6 :+ infixl 7 :* instance Functor ArityN where fmap f (Op0 e) = Op0 (f e) fmap f ((:+) e0 e1) = (:+) (fmap f e0) (fmap f e1) fmap f ((:*) e0 e1) = (:*) (fmap f e0) (fmap f e1) data ConBool --boolean constants = BoolFalse | BoolTrue deriving(Enum,Show,Ord,Eq,Bounded,Ix) data VarName --varable names = V0 | V1 | V2 deriving(Enum,Show,Ord,Eq,Bounded,Ix) bool_eval :: ArityN (Arity0 ConBool var) - ArityN (Arity0 ConBool var) bool_eval e = case e of { (Op0 (OpCon BoolTrue ) :+ _ ) - Op0 (OpCon BoolTrue) ; (_ :+ Op0 (OpCon BoolTrue ) ) - Op0 (OpCon BoolTrue) ; (Op0 (OpCon BoolFalse) :+ e1) - e1 ; (e0:+ Op0 (OpCon BoolFalse) ) - e0 ; (Op0 (OpCon BoolFalse) :* _ ) - Op0 (OpCon BoolFalse) ; (_ :* Op0 (OpCon BoolFalse) ) - Op0 (OpCon BoolFalse) ; (e0:+ e1) - (bool_eval e0) :+ (bool_eval e1) ; (e0:* e1) - (bool_eval e0) :* (bool_eval e1) ; e - e } main = do let bool_f::ArityN (Arity0 ConBool VarName) = Op0 (OpCon BoolFalse) let bool_expr_f_plus_v0 = bool_f :+ Op0 (OpVar V0) putStr bool_expr:f+v0= print bool_expr_f_plus_v0 let bool_eval_f_plus_v0 = bool_eval bool_expr_f_plus_v0 putStr bool_eval:f+v0= print bool_eval_f_plus_v0 let bool_expr_f_plus_f_plus_v0 = bool_f :+ bool_expr_f_plus_v0 putStr bool_expr:f+f+f+v0= print bool_expr_f_plus_f_plus_v0 let bool_eval_f_plus_f_plus_v0 = bool_eval bool_expr_f_plus_f_plus_v0 putStr bool_eval:f+f+v0= print bool_eval_f_plus_f_plus_v0 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] howto catamorph monoid (was Re: Is there already an abstraction for this?
On 10/18/08 16:48, Larry Evans wrote: [snip] I'm trying to apply this to a simple boolean simplifier shown in the attachment. This attachment is the same as the previous except, instead of a boolean algebra, an monoid is used. [snip] The output of the last line of attachment is: [snip] mon_eval:1*1*v0=(:*) (Op0 (OpCon MonoidOne)) (Op0 (OpVar V0)) however, what I want is a complete reduction to: (OpVar V0) As in the previous line beginning with mon_eval: mon_eval:1*v0=Op0 (OpVar V0) How can this be done using catamorphisms? Same question w.r.t. this attachment. TIA. -Larry {-# LANGUAGE PatternSignatures #-} {- Purpose: Try out the use of catamorphism to simplify an expression as far as possible. Reference: Post: http://www.nabble.com/Re%3A-Is-there-already-an-abstraction-for-this--p19641692.html Headers: From: wren ng thornton Newsgroups: gmane.comp.lang.haskell.cafe Subject: Re: Is there already an abstraction for this? Date: Wed, 24 Sep 2008 00:10:29 -0400 -} module Main where import Array data Arity0 con var --nullary operators = OpCon con -- constant | OpVar var -- variable deriving(Show) data ArityN arity0 = Op0 arity0 | (:*) (ArityN arity0) (ArityN arity0) deriving(Show) infixl 7 :* instance Functor ArityN where fmap f (Op0 e) = Op0 (f e) fmap f ((:*) e0 e1) = (:*) (fmap f e0) (fmap f e1) data ConMonoid --boolean constants = MonoidOne deriving(Enum,Show,Ord,Eq,Bounded,Ix) data VarName --varable names = V0 | V1 | V2 deriving(Enum,Show,Ord,Eq,Bounded,Ix) mon_eval :: ArityN (Arity0 ConMonoid var) - ArityN (Arity0 ConMonoid var) mon_eval e = case e of { (Op0 (OpCon MonoidOne) :* e1) - e1 ; (e0:* Op0 (OpCon MonoidOne) ) - e0 ; e - e } main = do let mon_1::ArityN (Arity0 ConMonoid VarName) = Op0 (OpCon MonoidOne) let mon_expr_1_v0 = mon_1 :* Op0 (OpVar V0) putStr mon_expr:1*v0= print mon_expr_1_v0 let mon_eval_1_v0 = mon_eval mon_expr_1_v0 putStr mon_eval:1*v0= print mon_eval_1_v0 let mon_expr_1_1_v0 = mon_1 :* mon_expr_1_v0 putStr mon_expr:1*1*1*v0= print mon_expr_1_1_v0 let mon_eval_1_1_v0 = mon_eval mon_expr_1_1_v0 putStr mon_eval:1*1*v0= print mon_eval_1_1_v0 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] howto mix - within do?
The attached code produces error: -- cut here -- runghc -dcore-lint do_with_assignment.proto.hs do_with_assignment.proto.hs:30:2: Couldn't match expected type `[]' against inferred type `IO' Expected type: [t] Inferred type: IO () In the expression: putStr v0= In a 'do' expression: putStr v0= -- cut here-- However, similar code here: http://www.nabble.com/List-as-input-p19987726.html apparently does work. Is there some compiler option I need to use? The compiler is ghc: ghc --version The Glorious Glasgow Haskell Compilation System, version 6.8.2 ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] OPPS, missing attachment (was Re: howto mix - within do?
On 10/17/08 07:39, Larry Evans wrote: The attached code produces error: -- cut here -- [snip] {- Purpose: Explore how to mix 'assignments' inside do. Motivation: Instead of: let { v0 = e0 ; v1 = e1 } in do { print v0 ; print v1 } which is hard to read, recode above as: do { v0 = e0 ; print v0 ; v1 = e1 ; print v1 } which is easier to read and more intuitive for c++ programmers. Example_code_suggesting_should_work: http://www.nabble.com/List-as-input-p19987726.html -} module Main where import Data.List main = do { v0 - [999] ; putStr v0= ; print v0 } ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: OPPS, missing attachment (was Re: howto mix - within do?
On 10/17/08 08:12, Daniel Fischer wrote: Thanks very much Daniel. Am Freitag, 17. Oktober 2008 14:42 schrieb Larry Evans: On 10/17/08 07:39, Larry Evans wrote: The attached code produces error: -- cut here -- [snip] {- Purpose: Explore how to mix 'assignments' inside do. Motivation: Instead of: let { v0 = e0 ; v1 = e1 } in do { print v0 ; print v1 } which is hard to read, recode above as: do { v0 = e0 ; print v0 ; v1 = e1 ; print v1 } That would have to be do let v0 = e0 print v0 let v1 = e1 print v1 or do v0 - return e0 print v0 v1 - return e1 print v1 (better (?): do print $ e0 print $ e1 ) At first, the |let v = e| combination looked best (less typing). However, my actual e0 was more complicated: array_complete [ Expr == Op0 GramOne , Term == Op0 GramOne , Factor == Op0 GramOne ] where == was a binary operator which just produced a tuple pair. I wanted to keep the expression on multiple lines because it's easier to read. The == was defined to make the expression look more like grammar productions (the Op0 GramOne is not the final rhs. That's only for testing purposes.). Unfortunately, using let with the multiline rhs failed to compile (due, I guess, to some layout rules, which I find difficult to fathom). So, I tried the | v - return e| combination. This worked although I had to surround e with parenthesis to enable compilation with multiple lines: ; gram_eqs::GramEqs ArithInp ArithVar - return (array_complete [ Expr == Op0 GramOne , Term == Op0 GramOne , Factor == Op0 GramOne ]) ; putStr gram_eqs= ; print gram_eqs I didn't use the last combination |print $ e0| because e1 may use v0. IOW: v0 - return e0 v1 - return ...v0... which is easier to read and more intuitive for c++ programmers. intuitive for c++ programmers is dangerous, the languages are very different, and one shouldn't gloss over that. The different syntax should help to not confound the languages. OK, but I'm finding it very difficult to figure out how to do something that's very simple in c++: ; calculate e0 ; store e0 in v0 ; print v0 ; calculate e1 using possibly v0 ; store e1 in v1 ; print v1 However, apparently, in haskell, the operands on each side of the ';' must be compatible. Hmmm, I remember seeing a translation of more complex haskell statments/expression to simpler ones... looking... http://haskell.org/onlinereport/exps.html#sect3.14 Mea culpa :( I should have read that first. Then a would have realized: (v0 - e0) putStr v0 would not have worked because, I guess: e0 putStr v0 would not work. OOPS, sect3.14 has a specialization on the pattern p-e. That specialization is harder to understand. I'll work on it :) [snip] remember that in do value - action statements -- using value (or not) action and statements must belong to the same monad. In your code above, the 'action' [999] has type Num a = [a], so the monad is [], but putStr v0= has type IO (), the monad is IO. So the binding v0 - [999] and the statement putStr v0= can't appear in the same do-block. If what you want is for every member of some list, do some monadic action, you need mapM/mapM_ resp forM(_): Nope. I just want to trace through various stages in translation of a language grammar(a set of grammar equationss) to a corresponding set of equations defining the lookahead sets for that grammar. So, I want the do{...} to just be a sequence of (calculate value; print value} where calculate value may use previously calculated values). Thanks for you help. -regards, Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Re: [Haskell] Compiler Construction course using Haskell?
On 08/20/08 11:43, Derek Elkins wrote: On Wed, 2008-08-20 at 16:03 +0200, Johannes Waldmann wrote: Hello. I plan to give a course in compiler construction, using Haskell as the implementation language (not as source or target language). Something along these lines: 1. combinator parsers (Parsec), 2. simple interpreter (arithmetical expressions) 3. add algebraic data types, functions 4. type checker 5. code generator. Ideally, 2..5 would be using the very same tree traversal code and just change the monad for evaluation. Any comments appreciated. Have you given such a course? Taken? [I've replied to Haskell-Cafe which is a better list for this discussion.] 2 3 are going to have different trees so using the same tree traversal code would require some finesse, though the code for 2 should only need extension not change. One thing you may want to look at is attribute grammars which can be cast into a monadic framework and gives a natural example of using the backward state monad and allows you to connect to other formalisms used for compiler construction. Could you give some examples or links or references? Another possibility is abstract interpretation. Both code generation and type checking can be viewed as examples of abstract interpretation (as well as, of course, actual interpretation.) Also many analyses fit into the model of abstract interpretation. Links or references? [snip] I'd also would like to see First and Follow sets: http://www.jambe.co.nz/UNI/FirstAndFollowSets.html calculated in haskell. I'd think it would be natural since, AFAICT, the calculation of the First sets is a homomorphism on the algebra of grammar expressions. IOW FIRST( x | y ) = set_union(FIRST(x),FIRST(y)) and I *think* maybe a similar definition can be made for the sequencing operator. Since I've seen haskell associated with algebras and expecially monads, I guess haskell would be especially adept at this. WARNING: I've not written a line of haskell code. -regards Larry ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe