Re: [Haskell-cafe] Mapping string to a function
Thanks Oliver. That's good enough. I was ever curious about whether parse String to the function rather than a mapping. -Haisheng On Thu, Mar 15, 2012 at 1:26 PM, Oliver Batchelor saul...@gmail.com wrote: You could store your test data in a named map e.g. import qualified Data.Map as M import System testSets :: M.Map String [Int] testSets = M.fromList [ (testdata, testdata) , (testdata2, testdata2) ] f :: Int - Something f = main = do [arg] - getArgs case M.lookup arg testSets of Just testSet - print (map f testSet) Nothing - print Test set not found! On Thu, Mar 15, 2012 at 12:59 PM, Haisheng Wu fre...@gmail.com wrote: Hi there, Do you have any comments / suggestions for the following scenario? I have two list and a function over list testdata :: [Int] testdata2 :: [Int] f testdata = map g testdata What I like to do is choosing what test data via command line arguments. i.e. test.hs testdata2 will run against testdata2 I could make it using pattern match between argument and data definition but it is annoy. code here: https://github.com/freizl/dive-into-haskell/blob/master/sandbox/one-in-arith-seq.hs I'm wondering it can be done simply in haskell. Thanks a lot. -Haisheng ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Mapping string to a function
On Thu, Mar 15, 2012 at 02:21, Haisheng Wu fre...@gmail.com wrote: Thanks Oliver. That's good enough. I was ever curious about whether parse String to the function rather than a mapping. GHC has the ability to embed an interpreter. You do not want to use it. If you want Perl/Python/Ruby, please use those; they are interpreters and have built-in embedded interpreters for use by programs. (Note that C and C++ are not generally interpreted and also don't have embedded interpreters either.) -- brandon s allbery allber...@gmail.com wandering unix systems administrator (available) (412) 475-9364 vm/sms ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Unable to call function from DLL using FFI
Hi, Thanks for very quick reply Claude. Your suggestion resolved the issue. Using extern C worked for me. Thank you very much. Thanks to all. Regards, Rajendra -- Forwarded message -- From: Claude Heiland-Allen cla...@goto10.org Date: Wed, Mar 14, 2012 at 7:43 PM Subject: Re: [Haskell-cafe] Unable to call function from DLL using FFI To: haskell-cafe@haskell.org On 14/03/12 14:01, rajendra prasad wrote: My c++ code(HelloWorld.cpp) looks like this: Try adding extern C { ... } to use the C ABI instead of a C++ ABI (which usually features symbol name mangling to add type information, among other things). (This may not solve the entire problem, but is an important puzzle-piece). __declspec(dllexport) int doubleMe(int a) { return 2*a; } Claude __**_ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/**mailman/listinfo/haskell-cafehttp://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] HXT: how to get sibling element
Hello, haskellers. Suppose we have this xml doc (maybe, little stupid): div spanSome story/span spanDescription/span: This story about... spanAuthor/span: Tom Smith /div In the end I whant to get list: [(Title, Some story), (Description,This story about...), (Author, Tom Smith)], or, maybe this: Book Some story [(description,This story about...), (Author, Tom Smith)] (Book = Book String [(String, String)]. First span is a special case then others and I undestand how to process it: === import Text.XML.HXT.Core import Text.XML.HXT.Curl import Text.XML.HXT.HTTP pageURL = http://localhost/test.xml; main = do r - runX (configSysVars [withCanonicalize no, withValidate no, withTrace 0, withParseHTML no] readDocument [withErrors no, withWarnings no, withHTTP []] pageURL getChildren isElem hasName div listA (getChildren hasName span) getTitle + getSections) putStrLn Статьи: putStr mapM_ putStr $ map (\i - (fst i) ++ : ++ (snd i) ++ | ) r putStrLn getTitle = arr head getChildren getText arr trim arr (Title,) getSections = arr tail unlistA ((getChildren getText arr trim) (getChildren getText arr trim)) ltrim [] = [] ltrim (' ':x) = ltrim x ltrim ('\n':x) = ltrim x ltrim ('\r':x) = ltrim x ltrim ('\t':x) = ltrim x ltrim x = x rtrim = reverse . ltrim . reverse trim = ltrim . rtrim === And I' get list: [(Title, Some story), (Description,Description), (Author, Author)] (Maybe, there is a better way to get this list?) But I cannot find a way to get text that followes some span. I suppose that I have to use function from Data.Tree.NavigatableTree.XPathAxis, but I don't puzzle out how to do it. Please, help me. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Functional programming podcast
On 15 March 2012 06:53, Clint Moore cl...@ivy.io wrote: We're closing in on a month since this post. Did everyone decide to do their own thing, do nothing, or ? Ah, I'd been traveling after posting this and then settling back in work, this remains on my TODO list in my organizer. I have no plans laid out, it's something I've wanted for some time and this post is the “Hey guys what if?” We should continue—my time zone is UTC+1, I am usually free within 19-23:00 with varying degrees of freeness. Friday and the rest of the weekend is better. We could try out G+ hangout and or I can setup a Mumble on hpaste.org. These are good for group chats, and if the quality isn't too great we can each record locally also and synchronize the audio later. Anyway, must dash. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] HXT: how to get sibling element
I'm am not really familiar with XML parsing in Haskell, but I am wondering why, if you have an xml file, you not simply name the element after the type of contents: book titleSome Story/title descriptionThis story about../description authorTom Smith/author /book Alternatively you could use the class or some type attribute to indicate the type. Regards, Wilfried van Asten 2012/3/15 Никитин Лев leon.v.niki...@pravmail.ru: Hello, haskellers. Suppose we have this xml doc (maybe, little stupid): div spanSome story/span spanDescription/span: This story about... spanAuthor/span: Tom Smith /div In the end I whant to get list: [(Title, Some story), (Description,This story about...), (Author, Tom Smith)], or, maybe this: Book Some story [(description,This story about...), (Author, Tom Smith)] (Book = Book String [(String, String)]. First span is a special case then others and I undestand how to process it: === import Text.XML.HXT.Core import Text.XML.HXT.Curl import Text.XML.HXT.HTTP pageURL = http://localhost/test.xml; main = do r - runX (configSysVars [withCanonicalize no, withValidate no, withTrace 0, withParseHTML no] readDocument [withErrors no, withWarnings no, withHTTP []] pageURL getChildren isElem hasName div listA (getChildren hasName span) getTitle + getSections) putStrLn Статьи: putStr mapM_ putStr $ map (\i - (fst i) ++ : ++ (snd i) ++ | ) r putStrLn getTitle = arr head getChildren getText arr trim arr (Title,) getSections = arr tail unlistA ((getChildren getText arr trim) (getChildren getText arr trim)) ltrim [] = [] ltrim (' ':x) = ltrim x ltrim ('\n':x) = ltrim x ltrim ('\r':x) = ltrim x ltrim ('\t':x) = ltrim x ltrim x = x rtrim = reverse . ltrim . reverse trim = ltrim . rtrim === And I' get list: [(Title, Some story), (Description,Description), (Author, Author)] (Maybe, there is a better way to get this list?) But I cannot find a way to get text that followes some span. I suppose that I have to use function from Data.Tree.NavigatableTree.XPathAxis, but I don't puzzle out how to do it. Please, help me. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Functional programming podcast
On Thu, Mar 15, 2012 at 6:53 AM, Christopher Done chrisd...@googlemail.comwrote: On 15 March 2012 06:53, Clint Moore cl...@ivy.io wrote: We're closing in on a month since this post. Did everyone decide to do their own thing, do nothing, or ? Ah, I'd been traveling after posting this and then settling back in work, this remains on my TODO list in my organizer. I have no plans laid out, it's something I've wanted for some time and this post is the “Hey guys what if?” We should continue—my time zone is UTC+1, I am usually free within 19-23:00 with varying degrees of freeness. Friday and the rest of the weekend is better. We could try out G+ hangout and or I can setup a Mumble on hpaste.org. These are good for group chats, and if the quality isn't too great we can each record locally also and synchronize the audio later. Anyway, must dash. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe hmm... UTC-5 here ( -4 right now with DST... or whatever excuse it is to deprive me of an hour of sleep!) fridays the weekend are good for me too. i work til 18:00 on fri 16:00 on sat... also, google hangouts would (should) work for me. i know skype is *really* flaky, but the google video chat has been pretty ok. lets hope my newbishness dosn't turn anybody off :P me -- * The wise man said: Never argue with an idiot. They bring you down to their level and beat you with experience. * As a programmer, it is your job to put yourself out of business. What you do today can be automated tomorrow. ~Doug McIlroy No snowflake in an avalanche ever feels responsible. --- CFO: “What happens if we train people and they leave?” CTO: “What if we don’t and they stay?” ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] HXT: how to get sibling element
I absolutly agree with you but unfortunetly, it is not my xml file.It is extraction from html page of public web server. I cannot to change format of this html page.Sorry. I had to explain it in first letter. But than what about to get sibling text (geting sibling is an separate interesting tasks with no matter for my contrete case). ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] HXT: how to get sibling element
Oh, yes! In this situation with so poor structured source I can try to use tagsoup. (or I'll take a look at xml-conduit). Nevertheless for better undestanding HXT it will be interesting to solve this problem in HXT. Or is it impossible? 15.03.2012, 20:08, Asten, W.G.G. van (Wilfried, Student B-TI) w.g.g.vanas...@student.utwente.nl: You might want to check out the xml-conduit package. It has preceding and following sibling Axis. I am not sure how the package works exactly, but it seems to be a good starting point. 2012/3/15 Никитин Лев leon.v.niki...@pravmail.ru: I absolutly agree with you but unfortunetly, it is not my xml file. It is extraction from html page of public web server. I cannot to change format of this html page. Sorry. I had to explain it in first letter. But than what about to get sibling text (geting sibling is an separate interesting tasks with no matter for my contrete case). ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] HXT: how to get sibling element
ArrowNavigatableTree can also get a following sibling Axis: http://hackage.haskell.org/packages/archive/hxt/9.2.2/doc/html/Control-Arrow-ArrowNavigatableTree.html Wilfried 2012/3/15 Никитин Лев leon.v.niki...@pravmail.ru: Oh, yes! In this situation with so poor structured source I can try to use tagsoup. (or I'll take a look at xml-conduit). Nevertheless for better undestanding HXT it will be interesting to solve this problem in HXT. Or is it impossible? 15.03.2012, 20:08, Asten, W.G.G. van (Wilfried, Student B-TI) w.g.g.vanas...@student.utwente.nl: You might want to check out the xml-conduit package. It has preceding and following sibling Axis. I am not sure how the package works exactly, but it seems to be a good starting point. 2012/3/15 Никитин Лев leon.v.niki...@pravmail.ru: I absolutly agree with you but unfortunetly, it is not my xml file. It is extraction from html page of public web server. I cannot to change format of this html page. Sorry. I had to explain it in first letter. But than what about to get sibling text (geting sibling is an separate interesting tasks with no matter for my contrete case). ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] puzzling polymorphism behavior (7.0.3 windows)
Why does the following program compile and produce the results it does? It seems like 3 and x got interpreted as the same type, (). Thanks in advance for your help. import Data.IORef import System.IO.Unsafe cell = unsafePerformIO $ newIORef [] push i = modifyIORef cell (++ [i]) main = do push 3 push x readIORef cell = return *Main :browse cell :: GHC.IORef.IORef [a] push :: a - IO () main :: IO [a] *Main main [(),()] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] puzzling polymorphism behavior (7.0.3 windows)
Maybe everytime you use 'cell' you tell haskell to create NEW cell. Try this: push' i cell = modifyIORef cell (++ [i]) main = do cell - newIORef [] push' x cell {- push' 3 cell will be incorrect in this case -} push' o cell readIORef cell = return Why the original code porduces [(),()] but not [] I cannot undestand. 15.03.2012, 23:53, gladst...@gladstein.com gladst...@gladstein.com: Why does the following program compile and produce the results it does? It seems like 3 and x got interpreted as the same type, (). Thanks in advance for your help. import Data.IORef import System.IO.Unsafe cell = unsafePerformIO $ newIORef [] push i = modifyIORef cell (++ [i]) main = do push 3 push x readIORef cell = return *Main :browse cell :: GHC.IORef.IORef [a] push :: a - IO () main :: IO [a] *Main main [(),()] ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] puzzling polymorphism behavior (7.0.3 windows)
Hi, this is one of the reasons why unsafePerformIO is not type-safe. Lets see what's going on by figuring out the types of the various definitions. cell = unsafePerformIO $ newIORef [] newIORef returns a cell which can hold values of the same type as its arguments. The type of the empty list is [a], because an empty list could be a list of arbitrary elements. So the overall type of cell is: cell :: IORef [a] cell returns a cell which can hold lists of arbitrary elements. push i = modifyIORef cell (++ [i]) Lets say i is of some type b. Then cell needs to hold lists of the type b. So in this use of cell, the type variable is instantiated to b, and the overall type of push is: push :: b - IO () So push can accept arbitrary values, and appends them to the list hold by cell. (Note that ghc reports the type as (a - IO ()), but that really means the same thing as (b - IO ()). main = do push 3 Here, since you call push with 3, b is chosen to be Int. After this line, the cell holds the list [3]. push x Here, since you call push with x, b is chosen to be String. After this line, the cell holds the list [3, x], which is not well-typed. You tricked Haskell to produce an ill-typed list by using unsafePerformIO. readIORef cell = return Here, it is not clear how you want to instantiate the type variable a in the type of cell. So lets assume that we want to return a value of type c, and instantiate a with c. So even though at this point, the list contains an Int and a String, we can (try to) extract whatever type we want from the list. Therefore, the overall type of main is: main :: IO [c] *Main main [(),()] Now once more, it is not clear how you want to instantiate c, so, by default, () is chosen. That default is only active in ghci, by the way. main will extract the Int 3 and the String x from the list, but treat them as if they were of type (). Here you get lucky: Since there's only one value of type (), ghci can show () without looking at it too deeply, so even though this program is not type-safe in a sense, it works fine in practice. But try forcing ghci to consider a more interesting type instead of (): *Main main :: IO [Int] [3,738467421] The string x is reinterpreted as a number and shown as such. You can try other types instead of Int until your ghci crashes because you access some memory you shouldn't have looked at or try to execute some random part of your memory as code. So to summarize, your code exhibits the (well-known) fact that unsafePerformIO is not type-safe, because it can be used to implement a polymorphic reference, which is a Bad Thing. Tillmann ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Trying to use more than one array in runSTUArray
Hello, café: I am trying to use more than one array with runSTUArray but I don't seem to be able to understand how it works. My first try is this: test1 n = runSTUArray $ do a - newArray (1, n) (2::Int) b - newArray (1, n) (3::Int) forM_ [1..n] $ \i - do v - readArray a i writeArray b i (v+1) return b but it does not work. However, when I write test2 n = runSTUArray $ do let createArray v n = newArray (1, n) (v::Int) a - createArray 2 n b - createArray 0 n forM_ [1..n] $ \i - do v - readArray a i writeArray b i (v+1) return b everything is fine although I expected the two versions to be equivalent. To further complicate matters, the following createArray v n = newArray (1, n) (v::Int) test3 n = runSTUArray $ do a - createArray 2 n b - createArray 3 n forM_ [1..n] $ \i - do v - readArray a i writeArray b i (v+1) return b does not work either. Where can I find an explanation for this behaviour? Furthermore, what I am after is to use two arrays with different types (Int and Bool), is it possible? Thanks in advance, Juan Miguel -- Juan Miguel Vilar Torres Profesor titular de universidad Vicedirector de la ESTCE para ITIG e ITIS Departamento de Lenguajes y Sistemas Informáticos Escuela Superior de Tecnología y Ciencias Experimentales Universitat Jaume I Av. de Vicent Sos Baynat s/n 12071 Castelló de la Plana (Spain) Tel: +34 964 72 8365 Fax: +34 964 72 8435 jvi...@lsi.uji.es ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Trying to use more than one array in runSTUArray
On Thursday 15 March 2012, 19:27:18, Juan Miguel Vilar wrote: Hello, café: I am trying to use more than one array with runSTUArray but I don't seem to be able to understand how it works. My first try is this: test1 n = runSTUArray $ do a - newArray (1, n) (2::Int) b - newArray (1, n) (3::Int) forM_ [1..n] $ \i - do v - readArray a i writeArray b i (v+1) return b but it does not work. The compiler can infer the type of b (STUArray s Integer Int), since that is returned (and then frozen to a UArray Integer Int), but it cannot infer what array type to use for a. Thus that function does not compile. However, when I write test2 n = runSTUArray $ do let createArray v n = newArray (1, n) (v::Int) Here you create a local binding for createArray that gets a monomorphic type, that type is the fixed by the returning of b to createArray :: Int - Integer - ST s (STUArray s Integer Int) you can make that fail too with enabling {-# LANGUAGE NoMonoLocalBinds #-} a - createArray 2 n b - createArray 0 n forM_ [1..n] $ \i - do v - readArray a i writeArray b i (v+1) return b everything is fine although I expected the two versions to be equivalent. To further complicate matters, the following createArray v n = newArray (1, n) (v::Int) This is a top-level definition, createArray is bound by a function binding, hence it is polymorphic again, and as in the first case, the type of a cannot be inferred. Give it a type signature createArray :: Int - Int - ST s (STUArray s Int Int) (I chose Int for the indices here instead of the default Integer) test3 n = runSTUArray $ do a - createArray 2 n b - createArray 3 n forM_ [1..n] $ \i - do v - readArray a i writeArray b i (v+1) return b does not work either. Where can I find an explanation for this behaviour? Furthermore, what I am after is to use two arrays with different types (Int and Bool), is it possible? Sure, you need to use type signatures. With expression type signatures, it would look like test1 n = runSTUArray $ do a - newArray (1, n) 2 :: ST s (STUArray s Int Int) b - newArray (1, n) 3 :: ST s (STUArray s Int Int) forM_ [1..n] $ \i - do v - readArray a i writeArray b i (v+1) return b If you don't want to give expression type signatures at every use, you can create a top-level function {-# LANGUAGE FlexibleContexts #-} createArray :: (Marray (STUArray s) a (ST s)) = a - Int - ST s (STUArray s Int a) createArray v n = newArray (1,n) v and you have to deal with only one type signature. Thanks in advance, Juan Miguel ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Trying to use more than one array in runSTUArray
On Thursday, March 15, 2012 at 2:27 PM, Juan Miguel Vilar wrote: Hello, café: I am trying to use more than one array with runSTUArray but I don't seem to be able to understand how it works. My first try is this: test1 n = runSTUArray $ do a - newArray (1, n) (2::Int) b - newArray (1, n) (3::Int) forM_ [1..n] $ \i - do v - readArray a i writeArray b i (v+1) return b but it does not work. However, when I write The problem is that GHC doesn't know what type of array a is. If you provide an annotation, you can resolve the ambiguity: a - newArray (1,n) (2::Int) :: ST s (STUArray s Int Int) However, this is somewhat ugly, so we should look at your next example: test2 n = runSTUArray $ do let createArray v n = newArray (1, n) (v::Int) a - createArray 2 n b - createArray 0 n forM_ [1..n] $ \i - do v - readArray a i writeArray b i (v+1) return b Note that the type of the b array was never in doubt thanks to runSTUArray. What you've done here is said that the same function that creates b also creates a, and since we know b's type, we now know a's type because GHC doesn't make createArray's type as polymorphic as it might. Another approach to resolving the types is to essentially do what you've done in your second example, but give createArray a type that is as polymorphic as you need: {-# LANGUAGE FlexibleContexts #-} newSTUArray :: (MArray (STUArray s) e (ST s), Ix i) = (i,i) - e - ST s (STUArray s i e) newSTUArray = newArray test3 n = runSTUArray $ do a - newSTUArray (1, n) False b - newSTUArray (1, n) (3::Int) forM_ [1..n] $ \i - do v - readArray a i writeArray b i (fromEnum v+1) return b I hope that helps clear things up. The issue to be aware of, particularly with the Array types, is just how polymorphic the interfaces you rely upon are. The best approach to figuring these problems out is to add type annotations to see where your intuition diverged from the type checker's reality. Anthony___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Trying to use more than one array in runSTUArray
On Thursday 15 March 2012, 19:53:56, Daniel Fischer wrote: On Thursday 15 March 2012, 19:27:18, Juan Miguel Vilar wrote: Hello, café: However, when I write test2 n = runSTUArray $ do let createArray v n = newArray (1, n) (v::Int) Here you create a local binding for createArray that gets a monomorphic type, that type is the fixed by the returning of b to createArray :: Int - Integer - ST s (STUArray s Integer Int) you can make that fail too with enabling {-# LANGUAGE NoMonoLocalBinds #-} Hmm, what compiler version are you using? When I actually tried to compile that, it failed with No instance for (MArray a0 Int (ST s)) without language extensions. After enabling MonoLocalBinds, however, it compiled with 6.12.3, 7.0.2, 7.0.4, 7.2.1 and 7.2.2, but 7.4.1 still refused to compile it. a - createArray 2 n b - createArray 0 n forM_ [1..n] $ \i - do v - readArray a i writeArray b i (v+1) return b everything is fine although I expected the two versions to be equivalent. ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Trying to use more than one array in runSTUArray
El 15/03/12 20:07, Daniel Fischer escribió: On Thursday 15 March 2012, 19:53:56, Daniel Fischer wrote: On Thursday 15 March 2012, 19:27:18, Juan Miguel Vilar wrote: Hello, café: However, when I write test2 n = runSTUArray $ do let createArray v n = newArray (1, n) (v::Int) Here you create a local binding for createArray that gets a monomorphic type, that type is the fixed by the returning of b to createArray :: Int - Integer - ST s (STUArray s Integer Int) you can make that fail too with enabling {-# LANGUAGE NoMonoLocalBinds #-} Hmm, what compiler version are you using? When I actually tried to compile that, it failed with No instance for (MArray a0 Int (ST s)) without language extensions. After enabling MonoLocalBinds, however, it compiled with 6.12.3, 7.0.2, 7.0.4, 7.2.1 and 7.2.2, but 7.4.1 still refused to compile it. I am using 7.0.3. Adding type signatures solved the problems. And with respect to MonoLocalBinds, it failed after adding NoMonoLocalBinds, without it, everything went fine. Thanks a lot, Juan Miguel -- Juan Miguel Vilar Torres Profesor titular de universidad Vicedirector de la ESTCE para ITIG e ITIS Departamento de Lenguajes y Sistemas Informáticos Escuela Superior de Tecnología y Ciencias Experimentales Universitat Jaume I Av. de Vicent Sos Baynat s/n 12071 Castelló de la Plana (Spain) Tel: +34 964 72 8365 Fax: +34 964 72 8435 jvi...@lsi.uji.es ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] Trying to use more than one array in runSTUArray
El 15/03/12 19:53, Anthony Cowley escribió: On Thursday, March 15, 2012 at 2:27 PM, Juan Miguel Vilar wrote: Hello, café: I am trying to use more than one array with runSTUArray but I don't seem to be able to understand how it works. My first try is this: test1 n = runSTUArray $ do a - newArray (1, n) (2::Int) b - newArray (1, n) (3::Int) forM_ [1..n] $ \i - do v - readArray a i writeArray b i (v+1) return b but it does not work. However, when I write The problem is that GHC doesn't know what type of array a is. If you provide an annotation, you can resolve the ambiguity: a - newArray (1,n) (2::Int) :: ST s (STUArray s Int Int) However, this is somewhat ugly, so we should look at your next example: test2 n = runSTUArray $ do let createArray v n = newArray (1, n) (v::Int) a - createArray 2 n b - createArray 0 n forM_ [1..n] $ \i - do v - readArray a i writeArray b i (v+1) return b Note that the type of the b array was never in doubt thanks to runSTUArray. What you've done here is said that the same function that creates b also creates a, and since we know b's type, we now know a's type because GHC doesn't make createArray's type as polymorphic as it might. Another approach to resolving the types is to essentially do what you've done in your second example, but give createArray a type that is as polymorphic as you need: {-# LANGUAGE FlexibleContexts #-} newSTUArray :: (MArray (STUArray s) e (ST s), Ix i) = (i,i) - e - ST s (STUArray s i e) newSTUArray = newArray test3 n = runSTUArray $ do a - newSTUArray (1, n) False b - newSTUArray (1, n) (3::Int) forM_ [1..n] $ \i - do v - readArray a i writeArray b i (fromEnum v+1) return b I hope that helps clear things up. The issue to be aware of, particularly with the Array types, is just how polymorphic the interfaces you rely upon are. The best approach to figuring these problems out is to add type annotations to see where your intuition diverged from the type checker's reality. Anthony Thanks a lot, it is much clear now. Regards, Juan Miguel -- Juan Miguel Vilar Torres Profesor titular de universidad Vicedirector de la ESTCE para ITIG e ITIS Departamento de Lenguajes y Sistemas Informáticos Escuela Superior de Tecnología y Ciencias Experimentales Universitat Jaume I Av. de Vicent Sos Baynat s/n 12071 Castelló de la Plana (Spain) Tel: +34 964 72 8365 Fax: +34 964 72 8435 jvi...@lsi.uji.es ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
Re: [Haskell-cafe] puzzling polymorphism behavior (7.0.3 windows)
Just a little more interesting information: This is why impure languages like OCaml have the value restriction. Haskell doesn't need it because it is pure, but of course unsafePerformIO thwarts that. On Mar 15, 2012 1:34 PM, Tillmann Rendel ren...@informatik.uni-marburg.de wrote: Hi, this is one of the reasons why unsafePerformIO is not type-safe. Lets see what's going on by figuring out the types of the various definitions. cell = unsafePerformIO $ newIORef [] newIORef returns a cell which can hold values of the same type as its arguments. The type of the empty list is [a], because an empty list could be a list of arbitrary elements. So the overall type of cell is: cell :: IORef [a] cell returns a cell which can hold lists of arbitrary elements. push i = modifyIORef cell (++ [i]) Lets say i is of some type b. Then cell needs to hold lists of the type b. So in this use of cell, the type variable is instantiated to b, and the overall type of push is: push :: b - IO () So push can accept arbitrary values, and appends them to the list hold by cell. (Note that ghc reports the type as (a - IO ()), but that really means the same thing as (b - IO ()). main = do push 3 Here, since you call push with 3, b is chosen to be Int. After this line, the cell holds the list [3]. push x Here, since you call push with x, b is chosen to be String. After this line, the cell holds the list [3, x], which is not well-typed. You tricked Haskell to produce an ill-typed list by using unsafePerformIO. readIORef cell = return Here, it is not clear how you want to instantiate the type variable a in the type of cell. So lets assume that we want to return a value of type c, and instantiate a with c. So even though at this point, the list contains an Int and a String, we can (try to) extract whatever type we want from the list. Therefore, the overall type of main is: main :: IO [c] *Main main [(),()] Now once more, it is not clear how you want to instantiate c, so, by default, () is chosen. That default is only active in ghci, by the way. main will extract the Int 3 and the String x from the list, but treat them as if they were of type (). Here you get lucky: Since there's only one value of type (), ghci can show () without looking at it too deeply, so even though this program is not type-safe in a sense, it works fine in practice. But try forcing ghci to consider a more interesting type instead of (): *Main main :: IO [Int] [3,738467421] The string x is reinterpreted as a number and shown as such. You can try other types instead of Int until your ghci crashes because you access some memory you shouldn't have looked at or try to execute some random part of your memory as code. So to summarize, your code exhibits the (well-known) fact that unsafePerformIO is not type-safe, because it can be used to implement a polymorphic reference, which is a Bad Thing. Tillmann __**_ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/**mailman/listinfo/haskell-cafehttp://www.haskell.org/mailman/listinfo/haskell-cafe ___ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
[Haskell-cafe] Haskell Weekly News: Issue 218
Welcome to issue 218 of the HWN, an issue covering crowd-sourced bits of information about Haskell from around the web. This issue cover the weeks of February 26 to March 10, 2012. You can find the HTML version at: http://contemplatecode.blogspot.com/2012/03/haskell-weekly-news-issue-218.html Quotes of the Week * monochrom: shapr is not normal. he got a decent job, then went to school. * quicksilver: typeclasses are absolutely not sugar for a record of functions quicksilver: they're unsugar. quicksilver: they're a benighted bitter poison. * edwardk: i'm not willing to give up orders of magnitude worth of performance to get a bad hack edwardk: if you want that, go code in scheme * (on pointless black magic) mgsloan: welcome to excessively pointless plumbing operators :) byorgey: mgsloan: that's... terrifying DanBurton: you should put it on hackage * cheater_: maybe you should try /part since we're talking about perfect-world situations * edwardk: i used to be a huge proponent of unicode syntax everywhere, then i used agda ;) * cmccann: free advice is, of course, any advice which satisfies the properties expected given the definition of advice, but nothing further ;] * shachaf: magic is just another word for primitive * dylukes: I'm getting way more acquainted with the GHC build system than I'd like to be. * JoeyA: Inform7 is a programming language. A programming language tells a computer what to do. The computer is in a room. You are facing West. * mauke: sorry, the current time is not a constant lukish: It's a pity * dfletcher: ((++ Batman!) . take 48 . cycle . show) (0/0) Top Reddit Stories * New unordered-containers release improves performance by as much as 2x, using HAMTs Domain: blog.johantibell.com, Score: 64, Comments: 46 On Reddit: [1] http://goo.gl/D6yTb Original: [2] http://goo.gl/5YUph * A humble suggestion for visual pure functional programming Domain: i.imgur.com, Score: 59, Comments: 59 On Reddit: [3] http://goo.gl/eUMrk Original: [4] http://goo.gl/DSFnj * Spike: experimental web browser in Haskell Domain: self.haskell, Score: 53, Comments: 22 On Reddit: [5] http://goo.gl/ZTR1j Original: [6] http://goo.gl/ZTR1j * GHC 7.4.1 speeds up arbtt by a factor of 22 Domain: joachim-breitner.de, Score: 51, Comments: 18 On Reddit: [7] http://goo.gl/XVBDt Original: [8] http://goo.gl/IL6EL * Eff 3.0 is out, a functional language with first-class computational effects and handlers (an alternative to monads) Domain: math.andrej.com, Score: 49, Comments: 12 On Reddit: [9] http://goo.gl/1Idw0 Original: [10] http://goo.gl/iwHlG * What's the most clever piece of Haskell code you know? Domain: self.haskell, Score: 47, Comments: 64 On Reddit: [11] http://goo.gl/T1u9t Original: [12] http://goo.gl/T1u9t * Diagrams 0.5 released: powerful, flexible embedded domain-specific language for creating vector graphics Domain: byorgey.wordpress.com, Score: 40, Comments: 7 On Reddit: [13] http://goo.gl/e75Yx Original: [14] http://goo.gl/565c1 * Faster Javascript Through Category Theory Domain: johnbender.us, Score: 39, Comments: 5 On Reddit: [15] http://goo.gl/UgvTr Original: [16] http://goo.gl/TLK4k * I'm writing a Haskell book. Almost finished the 3rd chapter. Would like feedback. Domain: bit.ly, Score: 36, Comments: 48 On Reddit: [17] http://goo.gl/AzwhR Original: [18] http://goo.gl/AzwhR * Level 0 -- a Snake clone using SDL, with a nice interactive map editor Domain: quasimal.com, Score: 28, Comments: 5 On Reddit: [19] http://goo.gl/x9gZk Original: [20] http://goo.gl/meSPo * Parallel Haskell Digest 8: MVar, summer school, Simon PJ @ YOW, and more Domain: well-typed.com, Score: 27, Comments: 1 On Reddit: [21] http://goo.gl/MDzrI Original: [22] http://goo.gl/OC6O6 * Avoid cabal hell: find nirvana Domain: yesodweb.com, Score: 27, Comments: 30 On Reddit: [23] http://goo.gl/ryd5e Original: [24] http://goo.gl/Yyzqd * Improvements to HashMap and HashSet creation Domain: blog.johantibell.com, Score: 26, Comments: 1 On Reddit: [25] http://goo.gl/yh2zs Original: [26] http://goo.gl/u9uhg * The Day Python Embarassed Imperative Programming » What, then, Shall We Say? Domain: the-27th-comrade.appspot.com, Score: 25, Comments: 20 On Reddit: [27] http://goo.gl/5KIOx Original: [28] http://goo.gl/Wi47v * Improbable uses of unsafeCoerce Domain: gist.github.com, Score: 25, Comments: 25 On Reddit: [29] http://goo.gl/cN0LE Original: [30] http://goo.gl/cxfSc Top StackOverflow Questions * Haskell: Lists, Arrays, Vectors, Sequences votes: 32, answers: 1