[Haskell] lazy printing

Serge D. Mechveliani mechvel at botik.ru
Fri Sep 10 07:50:03 EDT 2004


To my request

> > Can you tell me how to force Haskell to output the results in a
> > `lazy' way? 
> [..]
> >     main = putStr (concat ["\n min1 = ", show min1,
> >                            "\n min2 = ", show min2,
> >                            "\n"
> >                           ]
> >                   )
> [..]
> > This prints       min1 = 1
> > 
> > and hangs silently -- probably, because  min2  takes long to compute.
> > But why does not it print immediately
> >                   
> >                   min1 = 1
> >                   min2 = 


Keith Wansbrough <kw217 at cl.cam.ac.uk>  writes

> 
> Haskell is outputting lazily, but by default stdout is set to 
> LineBuffering - for efficiency, a line is only written to stdout 
> once it is complete.  Try adding "\n" to the end of the "min2 =" 
> line to see what I mean.
> To get the behaviour you describe, add import IO  and 
> hSetBuffering stdout NoBuffering >>  to the start of your main function.
> 


( and what if it also aplies  writeFile "log"  ? )


Thank you. It helps with the contrived examples.

But I cannot do a real example, so far: 

  ---------------------------------------------------------------
  module Main
  where
  import IO
  import Test (test)

  main = hSetBuffering stdout NoBuffering >>
         test 

  -------------------------------
  type ProofTrace = [ProofTraceStep]
  ...
  instance Show ProofTraceStep
    where
    showsPrec _ step =

      ("\n-----------------------------\n"++) .  sho step
      where
      sho (ProveGoal g rc) =
              ("Proving the goal\n "   ++) . shows (goalIndex   g) .
              ("\nwith the formula\n " ++) . shows (goalFormula g) .
              ("\ngiven the resource  "++) . shows rc
 
      sho ...              = ...
   ---------------------------------------------------------------
   
The trace data field is   ptrace :: ProofTrace -> ProofTrace
,
it is accumulated during the program like this:

      \ ... g ptrace ->  ptrace . ((BranchProved [g]) :)

It prints out by  show (ptrace []),  via the above Show instance
for  ProofTraceStep.

Of course, this information may be not sufficient, and probably,
I need further experimenting and investigation.

Copy, please, the answer, if any, to  mechvel at botik.ru

-----------------
Serge Mechveliani
mechvel at botik.ru








More information about the Haskell mailing list