Re: [Haskell-cafe] Mystery of an Eq instance

2013-09-20 Thread Scott Lawrence

On ghc 7.6.3:

Prelude 3.16227766016837956
3.1622776601683795

So if you specify a number with greater-than-available precision, it will be 
truncated. This isn't an issue with (==), but with the necessary precision 
limitations of Double.


On Fri, 20 Sep 2013, damodar kulkarni wrote:


Hello,
There were some recent discussions on the floating point support in Haskell
and some not-so-pleasant surprises people encountered.

There is an Eq instance defined for these types!

So I tried this:
*Main sqrt (10.0) ==3.1622776601683795
True
*Main sqrt (10.0) ==3.16227766016837956
True
*Main sqrt (10.0) ==3.1622776601683795643
True
*Main sqrt (10.0) ==3.16227766016837956435443343
True

It seems strange.

So my doubts are:
1. I wonder how the Eq instance is defined in case of floating point types
in Haskell?
2. Can the Eq instance for floating point types be considered meaningful?
If yes, how?
In general, programmers are **advised** not to base conditional branching
on tests for **equality** of two floating point values.
3. Is this particular behaviour GHC specific? (I am using GHC 6.12.1)

If there are references on this please share.

Thanks and regards,
-Damodar Kulkarni



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


[Haskell-cafe] Bytestring map/zipWith rationale

2013-09-12 Thread Scott Lawrence

Hello all,

Something's always bothered me about map and zipWith for ByteString. Why is it

map :: (Word8 - Word8) - ByteString - ByteString

but

zipWith :: (Word8 - Word8 - a) - ByteString - ByteString - [a]

? Obviously they can be transformed into each other with pack/unpack, and as I 
understand it, the compiler performs sufficient optimizations so that there's 
no performance hit to doing things like (pack $ zipWith xor a b), but it still 
seems inconsistent. Is there a deep reason for this?


--
Scott Lawrence

Linux baidar 3.10.9-1-ARCH #1 SMP PREEMPT Wed Aug 21 13:49:35 CEST 2013 x86_64 
GNU/Linux
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Bytestring map/zipWith rationale

2013-09-12 Thread Scott Lawrence

On Thu, 12 Sep 2013, Tom Ellis wrote:


On Thu, Sep 12, 2013 at 09:21:20AM -0400, Scott Lawrence wrote:

Something's always bothered me about map and zipWith for ByteString. Why is it

map :: (Word8 - Word8) - ByteString - ByteString

but

zipWith :: (Word8 - Word8 - a) - ByteString - ByteString - [a]


Well, what if you wanted to zipWith a function of type Word8 - Word8 -
Foo instead of Word8 - Word8 - Word8?


Then I would do what I do with map, and call `unpack` first.

Either of the two options is usable:

 map :: (Word8 - Word8) - ByteString - ByteString
 zipWith :: (Word8 - Word8 - Word8) - ByteString - ByteString - ByteString
   (or)
 map :: (Word8 - a) - ByteString - [a]
 zipWith :: (Word8 - Word8 - a) - ByteString - ByteString - [a]

I just don't understand why we have one from each.

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


Re: [Haskell-cafe] remote-build-reporting: cabal phoning home?!

2013-05-01 Thread Scott Lawrence

Hello,

I think (and a quick reading of source seems to bear this out) that that only 
happens when you run cabal report. Which isn't quite undocumented - see 
cabal report --help.


On Wed, 1 May 2013, Ertugrul Söylemez wrote:


Hello there,

could somebody please shed some light on the following line that
appeared in my ~/.cabal/config?

   remote-build-reporting: anonymous

The option doesn't seem to be documented anywhere, and I'm very nervous
about undocumented remote reporting features.  I expect cabal-install
to communicate only to fetch the latest package index and to upload and
download packages, the former only when i actually use the upload
command.

If it performs any other communication, please tell me how to disable
it.  In that case I'd also be interested in an explanation for why this
was enabled in the first place.  I certainly didn't enable it myself,
because it would be a serious security breach in my case.

Thank you.


Greets,
Ertugrul

--
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.



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


Re: [Haskell-cafe] Lazy object deserialization

2013-03-13 Thread Scott Lawrence
I tried it, but it still goes and reads the whole list. Looking at the 
`binary` package source code it seems that strict evaluation is hard-coded in 
a few places, presumably for performance reasons. It also seems to necessarily 
read the bytestring sequentially, so complex tree-like data structures would 
presumably encounter problems even if it worked for a list.


Ah well. As long as I'm not duplicating someone else's work, I'm more than 
happy to go at this from scratch.


On Wed, 13 Mar 2013, Jeff Shaw wrote:


On 3/13/2013 12:15 AM, Scott Lawrence wrote:

Hey all,

All the object serialization/deserialization libraries I could find (pretty 
much just binary and cereal) seem to be strict with respect to the actual 
data being serialized. In particular, if I've serialized a large [Int] to a 
file, and I want to get the first element, it seems I have no choice but to 
deserialize the entire data structure. This is obviously an issue for large 
data sets.


There are obvious workarounds (explicitly fetch elements from the 
database instead of relying on unsafeInterleaveIO to deal with it all 
magically), but it seems like it should be possible to build a cereal-like 
library that allows proper lazy deserialization. Does it exist, and I've 
just missed it?


Thanks,

I haven't tested this, but I suspect something like this could give you lazy 
binary serialization and deserialization. It's not tail recursive, though.


newtype LazyBinaryList a = LazyBinaryList [a]

instance Binary a = LazyBinaryList a where
   put (LazyBinaryList []) = putWord8 0
   put (LazyBinaryList (x:xs)) = putWord8 1  put x  put (LazyBinaryList 
xs)

   get = do
   t - getWord8
   case t of
   0 - return (LazyBinaryList [])
   1 - do
   x - get
   (LazyBinaryList xs) - get
   return $ LazyBinaryList (x:xs)



--
Scott Lawrence

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


[Haskell-cafe] Lazy object deserialization

2013-03-12 Thread Scott Lawrence

Hey all,

All the object serialization/deserialization libraries I could find (pretty 
much just binary and cereal) seem to be strict with respect to the actual data 
being serialized. In particular, if I've serialized a large [Int] to a file, 
and I want to get the first element, it seems I have no choice but to 
deserialize the entire data structure. This is obviously an issue for large 
data sets.


There are obvious workarounds (explicitly fetch elements from the database 
instead of relying on unsafeInterleaveIO to deal with it all magically), but 
it seems like it should be possible to build a cereal-like library that allows 
proper lazy deserialization. Does it exist, and I've just missed it?


Thanks,

--
Scott Lawrence

Linux baidar 3.7.10-1-ARCH #1 SMP PREEMPT Thu Feb 28 09:50:17 CET 2013 x86_64 
GNU/Linux

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


Re: [Haskell-cafe] Makefile for a Haskell Project

2012-12-27 Thread Scott Lawrence

On Fri, 28 Dec 2012, xuan bach wrote:


Hi everyone,
I'm a newbie in Haskell.

I'm wondering that if there is any tool support
creating Makefile for Haskell project like Ocamlbuild
for Ocaml project?


Since ghc handles dependencies automatically, I usually just do,

all:
ghc --make myprog

And then make sure `myprog.hs` is your Main module.



Thank you,
Regards.

--
*Le Dinh Xuan Bach*
*Tel: 01234711869 or +65 86967149
*
*Email: pig28...@gmail.com
School of Information and Communication,
*
*Hanoi University of Science and Technology
-
レ。ディン。スアン。バイック
電話番号:01234711869 or +65 86967149
メール:  pig28...@gmail.com
*



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


Re: [Haskell-cafe] Makefile for a Haskell Project

2012-12-27 Thread Scott Lawrence

On Fri, 28 Dec 2012, xuan bach wrote:


Hi Scott,

Thanks for your kind comment.

By the way, is it possible to use ghc --make for my
project including some external libraries and tools
such as happy, Parsec and haskell stub calling C
library?


I'm not sure about happy and foreign interfaces - as long as parsec is 
installed (via cabal or your distribution's packages), GHC will use it 
correctly.



I also tried out cabal to build my project but it seems
quite complicated to use.


It's worth it if you want a more elaborate build process than ghc itself can 
accomodate, or if you intend to distribute your software on hackage. Running 
cabal init will guide you through creating a stub cabal file, so it's not 
too bad.




Best Regards.

On Fri, Dec 28, 2012 at 2:08 PM, Scott Lawrence byt...@gmail.com wrote:


On Fri, 28 Dec 2012, xuan bach wrote:

 Hi everyone,

I'm a newbie in Haskell.

I'm wondering that if there is any tool support
creating Makefile for Haskell project like Ocamlbuild
for Ocaml project?



Since ghc handles dependencies automatically, I usually just do,

all:
ghc --make myprog

And then make sure `myprog.hs` is your Main module.



Thank you,
Regards.

--
*Le Dinh Xuan Bach*
*Tel: 01234711869 or +65 86967149
*
*Email: pig28...@gmail.com

School of Information and Communication,
*
*Hanoi University of Science and Technology

--**--**-
レ。ディン。スアン。バイック
電話番号:01234711869 or +65 86967149
メール:  pig28...@gmail.com
*



--
Scott Lawrence





--
*Le Dinh Xuan Bach*
*Tel: 01234711869 or +65 86967149
*
*Email: pig28...@gmail.com
School of Information and Communication,
*
*Hanoi University of Science and Technology
-
レ。ディン。スアン。バイック
電話番号:01234711869 or +65 86967149
メール:  pig28...@gmail.com
*



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


Re: [Haskell-cafe] Can cabal be turned into a package manager?

2012-12-12 Thread Scott Lawrence

On Wed, 12 Dec 2012, Janek S. wrote:

In the recent months there was a lot of dicussion about cabal, dependency 
hell and alike. After reading some of these discussions there is a question 
I just have to ask:


Why not create a package manager (like rpm or apt) for Haskell software?

I've been using Linux for years. Software for Linux is mostly written in C 
and C++. There are thousands of libraries with lots of dependencies and yet: 
a) Linux distributions manage to have package repositories that are kept in 
a consistent state b) Linux package managers can avoid dependency hell, 
automatically update to new packages, etc. Linux people did it! Is there any 
technical issue that prevents Haskell people from doing exactly the same 
thing? Or are we just having non-technical problems like lack of money or 
developers?


Linux package managers are so good at avoiding dependency hell because they 
don't have to - they fetch only from repositories that are carefully 
maintained and tested by humans, in a centralized fashion. The problem of 
handling dependencies in a purely automated fashion, with no concerted human 
effort, isn't solved by any of the major linux distros AFAIK.


Which isn't to say that I think it can't be solved; just that I don't know of 
any shining star we can use as an example.


(Incidentally, many linux distros package cabal packages with the same 
centralized-testing methodology under their own package repos, and it avoids 
dependency hell quite nicely. But I think there ought to be a better 
solution.)




Janek

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



--
Scott Lawrence

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


[Haskell-cafe] ANN: format-0.1.0.0

2012-05-26 Thread Scott Lawrence

Hey all,

I've just uploaded to hackage format-0.1, a small library for handling format 
strings similar to those used by nginx's log_format directive. The idea is to 
allow easy parsing of text rendered with such a format string, to make parsing 
simple, automatically generated text files as easy as possible. So for 
example, if you want to parse lines consisting of two bits of text, separated 
by  -- , then


map (scanFormatString $a -- $b) $ lines text

will do so, generating associative arrays with the variable names as keys and 
their content as values. A `renderFormatString` function, doing the obvious, 
is also provided.


Obligatory blog post with some more details: 
http://bytbox.net/blog/2012/05/format-haskell.html


I hope somebody finds this useful. (This seems the sort of thing which should 
already have existed, but I couldn't find anything. Did I miss it?)


I'm using parsec underneath, both for parsing the format string and the 
generated parser, and I've payed no attention to optimization. Some simple 
testing indicates that a reasonably fast computer takes about 1 second to 
parse 5000 lines with nginx's (and apache's) default format string - pretty 
slow. If there's demand, I'll be happy to pay more attention to that.


--
Scott Lawrence

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


Re: [Haskell-cafe] static linking with ghc?

2012-04-22 Thread Scott Lawrence

Adding -optl-pthread fixes it for me.

On Sun, 22 Apr 2012, Johannes Waldmann wrote:


Hi.

I want to produce a statically linked executable.
I am trying 'ghc  --make -fforce-recomp -static -optl-static  Main'

but it gives lots of  errors like
(.text+0xfa): undefined reference to `pthread_mutex_unlock'
collect2: ld returned 1 exit status

A similar thing is mentioned here (see Caveat)
http://www.haskell.org/haskellwiki/Web/Literature/Static_linking

The ghc user guide talks a great length about shared libs
http://www.haskell.org/ghc/docs/latest/html/users_guide/using-shared-libs.html
but I don't see anything on how to switch this off.

- J.W.



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



--
Scott Lawrence

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


[Haskell-cafe] cabal-install package precedence with --extra-lib-dirs

2012-02-01 Thread Scott Lawrence
When running cabal install with --extra-lib-dirs=./lib, if a package is
found both in ~/.cabal/lib and ./lib, cabal seems to favor the
~/.cabal/lib one. Is there some way to specify the correct precedence to
use?

-- 
Scott Lawrence



signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Anonymous, Unique Types, maybe

2011-12-04 Thread Scott Lawrence

On 12/04/11 02:25, Stephen Tetley wrote:

Umm, an obvious point is that if you really are using lists as streams
they should appear infinite to the processing code, so you shouldn't
encounter operations that fail due to incompatible lengths.


Didn't explain myself quite right, I guess.

The lists are infinite; however, when a function (which doesn't call 
`filter`) produces more than one list, the two lists are related in that 
the nth elements of each list came from the same source. Pairing every 
nth element is meaningfull for two such lists/streams. In contrast, a 
list coming out of `filter` isn't related to the list going in in this 
way, and shouldn't be re-paired with that list (or a direct derivative). 
My goal, again, is to represent that distinction in the type system.


--
Scott Lawrence

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


Re: [Haskell-cafe] Anonymous, Unique Types, maybe

2011-12-04 Thread Scott Lawrence
Thanks all; I haven't quite gotten it to work, but I imagine I'll be 
able to now (after reading up on ExistentialQuantification).


--
Scott Lawrence

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


[Haskell-cafe] Anonymous, Unique Types, maybe

2011-12-03 Thread Scott Lawrence
(Sorry if this email is rather unclear - I know my desired end result, 
but neither how to acheive nor explain it well. Here goes.)


I'm processing lists, using them sortof as streams. (Whether that's a 
good idea isn't the issue here - but let me know if it isn't!) 
Fundamentally, there are two types of operations (at least, that are 
relevant here) - those that change the length of the list and those that 
don't.


Some operators might take more than one list/stream as an argument, 
combining them in some way or another. Obviously, if the lists were 
different lengths, the operator would fail. I don't want that to happen 
at run time, so I want to check for it statically, presumably via the 
type system. I could do this manually:


type AList = [Event]
type BList = [Event]
type CList = [Event]

myMapish :: AList - AList
mySelect :: AList - (Event - Bool) - BList
myOtherSelect :: BList - CList

but I'd rather not have to manually define a new type for every new list 
length:


myMapish :: List a - List a
mySelect :: List a - List ?

The '?' would be an anonymous, unique type - unless there's a better way 
to accomplish this.


Hope that was clear, and thanks (as always) for the help (and being 
awesome).


--
Scott Lawrence

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


Re: [Haskell-cafe] Poll: Do you want a mascot?

2011-11-23 Thread Scott Lawrence

No

--
Scott Lawrence

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


[Haskell-cafe] Parsec: non-greedy 'between'

2011-09-11 Thread Scott Lawrence
Hey all,

Trying to match C-style comments, I have:

  between (string /*) (string */) $ many anyChar

Which doesn't work, because it is equivalent (ignoring returned values) to

  do {string /*; many anyChar; string */}

If the termination criterion was a single character, then I could use
noneOf or (satisfy . not), but that doesn't help here.

So... what am I missing?

Thanks in advance.

-- 
Scott Lawrence



signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Parsec: non-greedy 'between'

2011-09-11 Thread Scott Lawrence
On 09/11/11 16:45, Alexander Solla wrote:
 Use manyTill.
Ah, but of course. Thanks again!

-- 
Scott Lawrence



signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Prelude read 1234 :: String - *** Exception: Prelude.read: no parse

2011-08-06 Thread Scott Lawrence
read expects strings to be quoted:

Prelude read \1234\ :: String
1234


On Sat, Aug 6, 2011 at 19:57, michael rice nowg...@yahoo.com wrote:

 Prelude read 1234 :: Int
 1234
 Prelude read 1234 :: String
 *** Exception: Prelude.read: no parse

 Why?

 Michael


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




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


Re: [Haskell-cafe] GHC7 build problem

2011-06-14 Thread Scott Lawrence
It now appears to have been fixed. Thanks.

On Sun, Jun 12, 2011 at 01:13, Karel Gardas karel.gar...@centrum.cz wrote:
 On 06/11/11 09:37 PM, Edward Z. Yang wrote:

 Yes, the tree was broken for some time between yesterday and today, and
 you
 appear to have gotten unlikely.  It should have been fixed now, so you
 should
 try again.

 It's probably not fixed yet, since even last night build fails on
 opensolaris builder:
 http://darcs.haskell.org/ghcBuilder/builders/kgardas-opensolaris-x86-head/256/7.html

 Thanks,
 Karel




-- 
Scott Lawrence

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


[Haskell-cafe] GHC7 build problem

2011-06-11 Thread Scott Lawrence
Trying to compile GHC7 from source (as the ubuntu repository is still on
GHC6), I came across the following error in the final phase:

libraries/base/GHC/ST.lhs:78:1:
You cannot SPECIALISE `forever'
  because its definition has no INLINE/INLINABLE pragma
  (or its defining module `Control.Monad' was compiled without -O)

This was after:

  git clone [...]
  ./sync-all get
  perl boot
  ./configure
  make

Is there some problem with trying to build GHC7 on a machine with GHC6,
or did I just get the build procedure wrong (the last line of the error
looks like a hint that I did).

-- 
Scott Lawrence



signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Building Haskell Platform natively for 64bit Windows

2011-06-08 Thread Scott Lawrence
On 06/09/2011 01:47 AM, Jason Dagit wrote:
 Have you checked this by looking at the generated assembly?  I
 generated some assembly from GHC on windows.  Here is what it looks
 ilke:
 http://hpaste.org/47610
 
 My assembly-fu is not strong enough to tell if it's using 64bit instructions.
 
It would appear to be 32-bit. (pushl instead of pushq  no instances of
aligning to 8-byte boundaries)



signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Cons of -XUndecidableInstances

2011-06-07 Thread Scott Lawrence
I wrote:
  type Model a = (Ord a) = Set a -- the set of lexemes
 - [a] -- the original text to model
 - [a] -- list of previous lexemes
 - ProbDist a -- the next lexeme

 and then

  entropy :: Model a - Set a - [a] - Double

On Mon, Jun 6, 2011 at 03:56, Yitzchak Gale g...@sefer.org wrote:
 If further down the line you need to write a function that is independent
 of the model, the types of its arguments will show you what you
 need to do.

Is there a trick that I'm missing? If I want (as a horribly
constructed hypothetical example) to write a function later on with
type (Model a - Model b) that calls 'entropy' (no idea why), but I
want to use the specialized version of 'entropy' for 'Markov' (which
requires data that isn't even contained in the first argument, which
is really just a function), I don't see any way to do it, without
having two entirely different code paths for 'Markov' and other
models, starting from the point of decision (user input or some other
factor) - an unwieldy solution in case of more than 2 different models
(each, presumably, with their own subset of specializations).



-- 
Scott Lawrence

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


Re: [Haskell-cafe] Cons of -XUndecidableInstances

2011-06-06 Thread Scott Lawrence
On Mon, Jun 6, 2011 at 01:52, Yitzchak Gale g...@sefer.org wrote:
 Scott Lawrence wrote:
 You almost never want to use UndecidableInstances
 when writing practical programs in Haskell.

Ah. That's what I wanted to know :P

(Although it does seem to me - from looking around docs and the source
- that GHC's rules for allowing certain combinations might be a bit
too conservative - but then, I have next to no idea what I'm doing, so
hey.)

 When GHC tells you that you need them, it almost
 always means that your types are poorly designed,
 usually due to influence from previous experience
 with OOP.

* hides behind book


 Your best bet is to step back and think again about
 the problem you are trying to solve. What is the
 best way to formulate the problem functionally?
 That will lead you in the right direction. Please
 feel free to share more details about what you are
 trying to do. We would be happy to help you work out
 some good directions.

I'm modelling text in a markov-model-like way. I have an actual markov
model (albeit one in which X_n depends on a fixed range X_n-1 ..
X-n-k). I'm vaguely anticipating the presence of other models:

  class Model m a | m - a where
lexemes :: m - Set a
genFunc :: m - [a] - ProbDist a

Having that working, I'm trying to estimate the information entropy of a model

  entropy :: (Model m) = m - Double

(This is a slight simplification, since entropy needs a second
argument precision to know when to terminate.)

Which works well and fine - this function is pretty trivial to
implement, on the assumption that Markov (the instance of Model
described above) implements genFunc properly. But it happens not to -
the array argument to genFunc must be the right size, otherwise an
even probability distribution is used. So my OOP-infected mind wants
to specialize 'entropy' for Markov:

  class Entropy d where
entropy :: d - Double -- again, simplified

Note that it's not (Entropy d a) because the type of the lexeme
doesn't matter. Now, the problem code

  instance (Model m a) = Entropy m where
entropy = undefined


As you might have picked up, I suspect the part where I want to
specialize entropy for Markov is where I mess up - but I'm not sure
what to do. (To be clear, I expect to want to specialize entropy for
other models too - the general function I have in mind would be
horribly slow for many reasonable models.)

Thanks.


 Regards,
 Yitz




-- 
Scott Lawrence

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


Re: [Haskell-cafe] Cons of -XUndecidableInstances

2011-06-06 Thread Scott Lawrence
Oops. I can just abandon the Entropy typeclass and put the function
directly into Model, eh? Yeah, I think I'll do that...

Supposing I didn't want to - any alternatives? Other instances of
Entropy I might consider:

  instance (Eq a) = Entropy [a]
  instance (Eq a) = Entropy (Tree a)

On Mon, Jun 6, 2011 at 02:13, Scott Lawrence byt...@gmail.com wrote:
 On Mon, Jun 6, 2011 at 01:52, Yitzchak Gale g...@sefer.org wrote:
 Scott Lawrence wrote:
 You almost never want to use UndecidableInstances
 when writing practical programs in Haskell.

 Ah. That's what I wanted to know :P

 (Although it does seem to me - from looking around docs and the source
 - that GHC's rules for allowing certain combinations might be a bit
 too conservative - but then, I have next to no idea what I'm doing, so
 hey.)

 When GHC tells you that you need them, it almost
 always means that your types are poorly designed,
 usually due to influence from previous experience
 with OOP.

 * hides behind book


 Your best bet is to step back and think again about
 the problem you are trying to solve. What is the
 best way to formulate the problem functionally?
 That will lead you in the right direction. Please
 feel free to share more details about what you are
 trying to do. We would be happy to help you work out
 some good directions.

 I'm modelling text in a markov-model-like way. I have an actual markov
 model (albeit one in which X_n depends on a fixed range X_n-1 ..
 X-n-k). I'm vaguely anticipating the presence of other models:

  class Model m a | m - a where
    lexemes :: m - Set a
    genFunc :: m - [a] - ProbDist a

 Having that working, I'm trying to estimate the information entropy of a model

  entropy :: (Model m) = m - Double

 (This is a slight simplification, since entropy needs a second
 argument precision to know when to terminate.)

 Which works well and fine - this function is pretty trivial to
 implement, on the assumption that Markov (the instance of Model
 described above) implements genFunc properly. But it happens not to -
 the array argument to genFunc must be the right size, otherwise an
 even probability distribution is used. So my OOP-infected mind wants
 to specialize 'entropy' for Markov:

  class Entropy d where
    entropy :: d - Double -- again, simplified

 Note that it's not (Entropy d a) because the type of the lexeme
 doesn't matter. Now, the problem code

  instance (Model m a) = Entropy m where
    entropy = undefined


 As you might have picked up, I suspect the part where I want to
 specialize entropy for Markov is where I mess up - but I'm not sure
 what to do. (To be clear, I expect to want to specialize entropy for
 other models too - the general function I have in mind would be
 horribly slow for many reasonable models.)

 Thanks.


 Regards,
 Yitz




 --
 Scott Lawrence




-- 
Scott Lawrence

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


Re: [Haskell-cafe] Cons of -XUndecidableInstances

2011-06-06 Thread Scott Lawrence
On 06/06/2011 02:57 AM, Yitzchak Gale wrote:
 Generally, we don't start out with a type class. Type classes are
 great for the special situations in which they are needed (although
 you can do pretty well without them even then), but first
 let's get the basic concepts.
 
 Perhaps a model is just a function:
 
 type Model a = Ord a = Set a - [a] - ProbDist a
 
 or something like that.

Erm... yeah, actually.

But... this prevents me from storing more information in a Model in the
future. While I don't really anticipate needing too (I can see this
function covering all likely use cases), it does seem sorta restrictive.

 
  Having that working, I'm trying to estimate the information entropy of a 
  model
 
   entropy :: (Model m) = m - Double
 Perhaps just a function:
 
 entropy :: Model a - Double
 
 I still don't know enough details about what you're doing,
 so my types are probably off. But I hope you get the idea.

No, your types are right.

 
 If that's not general enough, you may introduce more functions, or
 some data types. Those give you a huge amount of power - remember
 that data types can take multiple type parameters (without any
 GHC extension), they can have functions as their parameters, etc.
 
 Or, perhaps you'll even get to the point where you'll need a type class,
 but that's pretty far down the road, and what you would need it
 for is very different than what a class is in OOP - they are different
 concepts.

Oh, I understand the difference between a class and a typeclass. It's
the difference between an interface and a typeclass that I apparently
haven't grasped. Thanks.

 
 Hope this helps,
 Yitz




signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Cons of -XUndecidableInstances

2011-06-06 Thread Scott Lawrence
On 06/06/2011 03:13 AM, Scott Lawrence wrote:
 I still don't know enough details about what you're doing,
  so my types are probably off. But I hope you get the idea.
 No, your types are right.
 

Or not.

  type Model a = (Ord a) = Set a -- the set of lexemes
 - [a] -- the original text to model
 - [a] -- list of previous lexemes
 - ProbDist a -- the next lexeme

and then

  entropy :: Model a - Set a - [a] - Double

or perhaps more simply

  entropy :: [a] - ProbDist a - Double

(Let me know if I'm doing something insane again - thanks.)

But this doesn't allow me to specialize for markov models. Seems to me
that to do that, I'd have to store data - and once I'm using a datatype
for markov models:

  data Markov a = Markov
{ lexemeSet :: Set a
, matrix:: Map [a] (ProbDist a)
}

Then in order to get a consistent interface to various models, I'm going
to need a typeclass. (Which is required to use a single function name on
multiple datatypes, yes?)

I suppose the alternative is something like

  data Model a = Markov {...} | OtherModel

Is that the functional solution? It seems to preclude the possibility of
separating the markov-specialized code and the other specialized code.



signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Cons of -XUndecidableInstances

2011-06-05 Thread Scott Lawrence
According to the haskell-prime wiki[1], -XUndecidableInstances removes
checks on the form of instance declaration, and just impose a depth
limit to ensure termination (of compilation, I assume?). The listed
Con is that this removes the clear boundary between legal and illegal
programs, and behaviour may be implementation-dependent as the edge of
that boundary is reached. How can I tell when I'm nearing that
boundary? (And where are the sorts of things GHC does with types
documented? I can't seem to find any good explanation of these
things.)

More specifically, I have

  class Model m a | m - a where ...
  class Entropy d where ...
  instance (Model m a) = Entropy m where ...

The first line requires MultiParamTypeClasses and
FunctionalDependencies (the two seem to go together) - the third
requires UndecidableInstances (since the type variable 'a' appears on
the left but not the right). Is this likely to cause a problem? My
guess is it shouldn't, since it's equivalent to

  class Model m a | m - a where ...
  class Entropy d a where ...
  instance (Model m a) = Entropy m a where ...

without bothering to actually use 'a' in Entropy - but one never knows...

(Actually, a third type variable has to be introduced to Entropy to
remove the UndecidableInstances dependency - Constraint is no smaller
than the instance head. This only increases the illogic in my humble
eyes. These examples seem simple enough for GHC to handle nicely...)

[1] http://hackage.haskell.org/trac/haskell-prime/wiki/UndecidableInstances

-- 
Scott Lawrence

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


[Haskell-cafe] Lazy Evaluation in Monads

2011-05-31 Thread Scott Lawrence
I was under the impression that operations performed in monads (in this
case, the IO monad) were lazy. (Certainly, every time I make the
opposite assumption, my code fails :P .) Which doesn't explain why the
following code fails to terminate:

  iRecurse :: (Num a) = IO a
  iRecurse = do
recurse - iRecurse
return 1

  main = (putStrLn . show) = iRecurse

Any pointers to a good explanation of when the IO monad is lazy?


=== The long story ===

I wrote a function unfold with type signature (([a] - a) - [a]), for
generating a list in which each element can be calculated from all of
the previous elements.

  unfold :: ([a] - a) - [a]
  unfold f = unfold1 f []

  unfold1 :: ([a] - a) - [a] - [a]
  unfold1 f l = f l : unfold1 f (f l : l)

Now I'm attempting to do the same thing, except where f returns a monad.
(My use case is when f randomly selects the next element, i.e. text
generation from markov chains.) So I want

  unfoldM1 :: (Monad m) = ([a] - m a) - [a] - m [a]

My instinct, then, would be to do something like:

  unfoldM1 f l = do
next - f l
rest - unfoldM1 f (next : l)
return (next : rest)

But that, like iRecurse above, doesn't work.



signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Lazy Evaluation in Monads

2011-05-31 Thread Scott Lawrence
On 05/31/2011 04:20 PM, Artyom Kazak wrote:
 Suppose iRecurse looks like this:
   iRecurse = do
 x - launchMissiles
 r - iRecurse
 return 1
 
 As x is never needed, launchMissiles will never execute. It obviously is
 not what is needed.

Prelude let launchMissiles = putStrLn UH OH  return 1
Prelude let iRecurse = launchMissiles  return 1
Prelude iRecurse
UH OH
1
Prelude

Looks like launchMissiles /does/ execute, even though x is (obviously)
never needed.




signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Lazy Evaluation in Monads

2011-05-31 Thread Scott Lawrence
On 05/31/2011 04:48 PM, Artyom Kazak wrote:
 
 Oh, sorry. I was unclear. I have meant assuming IO is lazy, as Yves
 wrote.

Ah, ok. That makes more sense.

 
 And saying some hacks I meant unsafeInterleaveIO, which lies beneath
 the laziness of, for example, getContents.

Which explains why assuming getContents is strict has never worked for me.

I'm trying to implement unfoldM1 without using unsafeIO, if possible. Since

  unfoldM1 f l = do
next - f l
rest - unfoldM1 f (next : l)
return (next : rest)

obviously won't work, I've been trying to use fmap

  unfoldM1 :: (Functor m, Monad m) = ([a] - m a) - [a] - m [a]
  unfoldM1 f l = do
next - f l
fmap (next :) $ unfoldM1 f (next : l)

Evaluation here also doesn't terminate (or, (head $ unfoldM (return .
head)) doesn't), although I can't figure out why. fmap shouldn't need to
fully evaluate a list to prepend an element, right?



signature.asc
Description: OpenPGP digital signature
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Lazy Evaluation in Monads

2011-05-31 Thread Scott Lawrence
Apparently:

Prelude let r = (fmap (1:) r) :: IO [Integer]
Prelude fmap (take 5) r
*** Exception: stack overflow

Thanks - I'll just have to stay out of IO for this, then.

On Tue, May 31, 2011 at 17:05, Stephen Tetley stephen.tet...@gmail.com wrote:
 2011/5/31 Scott Lawrence byt...@gmail.com:

 Evaluation here also doesn't terminate (or, (head $ unfoldM (return .
 head)) doesn't), although I can't figure out why. fmap shouldn't need to
 fully evaluate a list to prepend an element, right?

 I'm afriad fmap doesn't get to choose - if the monad is strict then
 both definitions are equivalent (probably...).

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




-- 
Scott Lawrence

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