Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7796367
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:21:15+00:00 2026-06-01T23:21:15+00:00

I have a project for Uni to write a compiler (in Haskell) for a

  • 0

I have a project for Uni to write a compiler (in Haskell) for a simple made-up imperative language. One of the requirements is printing debug statements on entering a function call, leaving a function and assigning variables.

Printing messages when entering functions is easy, I just use Debug.trace, eg:

functionValue = trace "Entering function" (evaluateFunction functionArguments)

The same process applies when assigning to variables. What I can’t figure out is how to print when returning from a function call and have the output timed correctly with the other outputs. Every attempt I’ve made so far has resulted in “Leaving function” being printed immediately after “Entering function” – I need the function’s internal debug statements (assigning and nested function calls) to be printed before “Leaving function” is printed.

My imperative habits tell me that I need a way to force execution of (evaluateFunction functionArguments) before the leave-function output, but this seems impossible and wrong in Haskell.

Example output I get now:

Entering main function...
Leaving main function...
Entering fn1 function...
Leaving fn1 function...
Assigning value1 to A.
Assigning value2 to C.
Entering fn2 function...
Leaving fn2 function...
Assigning value3 to B.
Assigning value4 to C.

Same program’s output the way I need it to look:

Entering main function...
Entering fn1 function...
Assigning value1 to A.
Leaving fn1 function...
Assigning value2 to C.
Entering fn2 function...
Assigning value3 to B.
Assigning value4 to C.
Leaving fn2 function...
Leaving main function...

So, what’s Haskell idiom for ‘run myFunctionWithTraces then print myString’?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T23:21:16+00:00Added an answer on June 1, 2026 at 11:21 pm

    If you want to immediately print traces, you can lift the function to IO monad, and put it between two putStrs, e.g.

    trace :: String -> IO () -> IO ()
    trace name f = do
        putStrLn $ "Entering " ++ name
        f
        putStrLn $ "Leaving " ++ name
    

    And then:

    main = trace "main" $ do
        fn1
        fn2
    
    fn1 = trace "fn1" $ do
        return ()
    
    fn2 = trace "fn2" $ do
        return ()
    

    This also can be done purely, with the Writer monad (i.e. don’t print, but just accumulate debugging output as you go). trace would then look more like this:

    trace :: String -> Writer String () -> Writer String ()
    trace name f = do
        tell $ "Entering " ++ name ++ "\n"
        f
        tell $ "Leaving " ++ name ++ "\n"
    

    and with additional step of unwrapping the debug output with runWriter or execWriter.

    Edit: generalising trace to IO a is not too difficult:

    trace :: String -> IO a -> IO a
    trace name f = do
        putStrLn $ "Entering " ++ name
        ret <- f
        putStrLn $ "Leaving " ++ name
        return ret
    
    main = trace "main" $ do
        a <- fn1
        b <- fn2
        print $ a + b
    
    fn1 = trace "fn1" $ do
        return 42
    
    fn2 = trace "fn2" $ do
        return 69
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using PIL for a uni project and we have one task where we
I have project in Dropbox and two running laptops: one with Ubuntu and one
As a project over summer while I have some downtime from Uni I am
I have VS2010 Pro - at my uni I can start a Modelling Project,
I'm creating a web project for one of my uni modules using Visual studio
On my earlier projects (school and side projects) I have mainly used one language
I have project with third part library, this library made for 32-bit systems. But
I have project group in Eclipse. But I use debug as --> on server
I have project on github.com. All summer i made commits. Now i need to
I have project Emle in Launchpad . I set it to import from emle.svn.sourceforge.net

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.