Re: [Haskell-cafe] Bug in writeArray?

2009-09-24 Thread Grzegorz Chrupała
2009/9/23 Bulat Ziganshin bulat.zigans...@gmail.com:
 Hello Grzegorz,

 Wednesday, September 23, 2009, 7:19:59 PM, you wrote:

 This seems like a bug in the implementation of writeArray: when passed
   let (l,u) = ((0,10),(20,20))

 writeArray computes raw index (from 0 to total number of array
 elements) and check that this index is correct. with multi-dimensional
 arrays this approach may lead to wrong results, as you mentioned. it's
 known problem that isn't fixed for a long time probably due to
 efficiency cautions.

Hmm, I understand that efficiency is an issue, but in that case
shouldn't unsafe writing be provided by and unsafeWriteArray function,
while writeArray does proper range checking?

Or at least this problem with writeArray should be clearly indicated
in the documentation. I for one spent several hours debugging before
finding out about this lack of proper range checks so it's not an
imaginary problem.

--
Grzegorz
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Bug in writeArray?

2009-09-23 Thread Grzegorz Chrupała
Hi all,
This seems like a bug in the implementation of writeArray: when passed
an out-of-range index it silently writes to an incorrect index in the
array.
--

import Data.Array.IO
import Data.Array.Unboxed

main = do
  let (l,u) = ((0,10),(20,20))
  marr - newArray (l,u) 0 :: IO (IOUArray (Int,Int) Int)
  let badi = (10,9)
  print (inRange (l,u) badi)
  writeArray marr badi 1
  arr - freeze marr :: IO (UArray (Int,Int) Int)
  print . filter ((/=0) . snd) . assocs  $ arr

--
Grzegorz
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Data.Binary.encode slower than show

2009-07-26 Thread Grzegorz Chrupała
Hi all,
I have a piece of code where I'm serializing a datastructure with the
following type [(Int, (Map DType (IntMap Int)))], using Binary.encode
The thing is it is very slow: actually quite a bit slower than just using
show.
This seems rather suspicious. Any idea what could be going on?

import qualified Data.Map as Map
import qualified Data.IntMap as IntMap
import Data.IntMap ((!))
import Vector
import Atom
import Control.Monad.State
import Indexing (indexBy)
import Data.List (foldl')
import System
import System.IO
import Debug.Trace
import qualified Data.Binary as Binary
import qualified Data.ByteString.Lazy as BS

data DType = Fol | For | Sol | Sor deriving (Eq,Enum,Ord,Show,Read)
instance Binary.Binary DType where
   get = do x - Binary.get
            return (toEnum x)
   put x = Binary.put (fromEnum x)

type Word = Int
type Count = Int
type WordMap = IntMap.IntMap

find = IntMap.findWithDefault IntMap.empty

distReps ::    Map.Map (Word,Word) Count
           - [(Int, (Map.Map DType (WordMap Count)))]
distReps bidict =
   let bigrams = Map.toList bidict
       for = indexBy id bigrams
       fol = indexBy swap bigrams
       ws  = map (fst . fst) bigrams
       r = flip map ws
           $ \w - (w,Map.fromList [ (Fol , find w fol)
                                           , (For , find w for)
                                           , (Sol , sox for fol w )
                                           , (Sor , sox fol for w ) ])
   in trace (show $ r == r)  r
sox :: WordMap (WordMap Count)
   - WordMap (WordMap Count)
   - Word
   - WordMap Count
sox fox foy w =
   let xs = IntMap.keys (find w fox)
       f !z x = let xv = find x foy
                in xv == xv `seq` z `plus` xv
   in foldl' f IntMap.empty xs

swap (!a,!b) = (b,a)

readBigrams = fmap (map (\ [w,w',c] - ((w,w'),read c))
               . map words
                     . lines )
             getContents
main = do
 [f] - getArgs
 bigrams - readBigrams
 let (bigrams',as) = flip runState empty (atomize bigrams)
     dr = distReps (Map.fromList bigrams')
 write f dr
 hPutStrLn stderr Done this
 write (f++.atom) as

--write f d =  writeFile f (show d)
write f d = BS.writeFile f (Binary.encode d)

atomize xs = mapM f xs
   where f ((w,w'),!i) = do
           !i_w - toAtom w'
           !i_w' - toAtom w
           let r = ((i_w,i_w'),i)
           r == r `seq` return r


--
Grzegorz
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Implicit params and typeclasses

2006-11-16 Thread Grzegorz Chrupała

Hi all,
When I try to compile the following in GHC (v 6.6) (with
-fno-monomorphism-restriction -fimplicit-params)
class Configuration a where
   thestring:: a - String
foo c = let { ?c = c } in bar
bar = thestring ?c
I get:
   Ambiguous type variable `a' in the constraint:
 `Configuration a'
arising from use of `thestring' at /home/grzegorz/Foo.hs:9:6-17
   Possible cause: the monomorphism restriction applied to the following:
 bar :: (?c::a) = String (bound at /home/grzegorz/Foo.hs:9:0)
 foo :: a - String (bound at /home/grzegorz/Foo.hs:8:0)
   Probable fix: give these definition(s) an explicit type signature
  or use -fno-monomorphism-restriction

Adding the type signature bar :: (Configuration a,?c :: a) = String
the compiler says:
   Ambiguous constraint `Configuration a'
At least one of the forall'd type variables mentioned by the constraint
must be reachable from the type after the '='
   In the type signature for `bar':
 bar :: (Configuration a, ?c :: a) = String

Similar code without use of implicit params compiles fine:
class Configuration a where
   thestring:: a - String
foo c = thestring c

Why do implicit params cause this error and is there a way to make it work?

Thanks,
--
Grzegorz
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Haskell as embedded DSL

2006-07-06 Thread Grzegorz Chrupała

Perhaps Functional Morphology http://www.cs.chalmers.se/~markus/FM/,
a toolkit for morphology development would be of interest.
--
Grzegorz

On 7/5/06, Joel Reymont [EMAIL PROTECTED] wrote:

Folks,

Do you have examples of using Haskell as a DSL in an environment NOT
targeted at people who know it already?

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe