[Haskell-beginners] truncate results depend on strict/lazy

Bryan Vicknair bryanvick at gmail.com
Tue Sep 10 00:08:34 CEST 2013


> On Mon, Sep 9, 2013 at 7:26 PM, Bryan Vicknair <bryanvick at gmail.com> wrote:
> > Deep in a WAI web app, I have a function that converts a String from a web
> > form
> > like "0.12" to an Int 12.  Converting "0.12" to the Float 0.12 is working.
> > However, converting the Float 0.12 to the Int 12 does not work as expected
> > unless I use the trace function.

> On Mon, Sep 9, 2013 Bryan Vicknair <bryanvick at gmail.com> wrote:
> I put together a simple library and web app to demonstrate the behavior I'm
> seeing: git clone git at bitbucket.org:bryanvick/truncate.git
> 
> The README has simple instructions to view the behavior in a repl or in a web
> app.  The Lib.hs file is where the parsing code is.
> 
> I swear I saw different behaviour between separate cabal sandboxes while I was
> testing this.  Sometimes the parsing would work as expected, sometimes it
> wouldn't.  That made me think that maybe different versions of dependencies are
> being installed for different runs.  I'll start paying attention to
> "cabal sandbox hc-pkg" to see if this is the case.

I just witnessed the parsing code in question giving different results in
different invocations of a repl in the same cabal sandbox.

I started a cabal sandbox and installed dependencies:

  > cabal sandbox init
  > cabal install --only-dependencies
  ... <bunch of compiling>

I started a repl...

  > cabal repl
  Package has never been configured. Configuring with default flags. If this
  fails, please run configure manually.
  Resolving dependencies...
  Configuring app-0...
  Preprocessing library app-0...
  GHCi, version 7.4.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.
  Loading package bytestring-0.9.2.1 ... linking ... done.
  Loading package array-0.4.0.0 ... linking ... done.
  Loading package deepseq-1.3.0.0 ... linking ... done.
  Loading package containers-0.4.2.1 ... linking ... done.
  Loading package transformers-0.3.0.0 ... linking ... done.
  Loading package mtl-2.1.2 ... linking ... done.
  Loading package text-0.11.3.1 ... linking ... done.
  Loading package digestive-functors-0.6.1.0 ... linking ... done.
  [1 of 1] Compiling Lib              ( Lib.hs, interpreted )
  Ok, modules loaded: Lib.
  *Lib> import Data.Text (pack)

And this time, the parsing works!

  *Lib Data.Text> validateVal $ pack "0.12"
  Success (Just 12)
  *Lib Data.Text> :q
  Leaving GHCi.

I did a dump of the libraries installed to compare to later sandboxes that don't work:

  > cabal sandbox hc-pkg list > /tmp/working-deps

I ran the web app:

  > cabal run
  Building app-0...
  Preprocessing library app-0...
  [1 of 1] Compiling Lib              ( Lib.hs, dist/build/Lib.o )
  In-place registering app-0...
  Preprocessing executable 'app' for app-0...
  [1 of 2] Compiling Lib              ( Lib.hs, dist/build/app/app-tmp/Lib.o )
  [2 of 2] Compiling Main             ( app.hs, dist/build/app/app-tmp/Main.o )
  
  app.hs:67:21: Warning: Defined but not used: `name'
  
  app.hs:67:37: Warning: Defined but not used: `val'
  Linking dist/build/app/app ...
  running at port 8005
  Thing {name = "name", val = Just 11}

The parsing didn't work there, as usual.  So I went back into the repl:

  > cabal repl
  Preprocessing library app-0...
  GHCi, version 7.4.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.
  Loading package bytestring-0.9.2.1 ... linking ... done.
  Loading package array-0.4.0.0 ... linking ... done.
  Loading package deepseq-1.3.0.0 ... linking ... done.
  Loading package containers-0.4.2.1 ... linking ... done.
  Loading package transformers-0.3.0.0 ... linking ... done.
  Loading package mtl-2.1.2 ... linking ... done.
  Loading package text-0.11.3.1 ... linking ... done.
  Loading package digestive-functors-0.6.1.0 ... linking ... done.
  Ok, modules loaded: Lib.
  Prelude Lib> import Data.Text (pack)

And all of a sudden, the parsing code doesn't work again!:

  Prelude Data.Text Lib> validateVal $ pack "0.12"
  Success (Just 11)

I'm loosing my mind!




More information about the Beginners mailing list