Wednesday, October 6, 2010

Verbose show and the missing preprocessor

Verbose show

Say we want a function vshow which describes an expression before showing its value. For example:

vshow (2 + 3)          = "2 + 3 = 5"
vshow (product [1..5]) = "product [1..5] = 120"

Clearly vshow can't be an ordinary Haskell function. We just don't have that kind of runtime access to program source.

All sorts of workarounds come to mind: simple-reflect, feeding strings to hint, Template Haskell… But it's hard to beat the simplicity of the humble C preprocessor:

{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -pgmP cpp #-}

#define vshow(e) (#e ++ " = " ++ show (e))

main :: IO ()
main = do
putStrLn vshow(2 + 3)
putStrLn vshow(product [1..5])

GHC's built-in CPP can't handle the stringify operator, so we invoke the "real" C preprocessor with -pgmP cpp. Testing it:

$ runhaskell vshow.hs
2 + 3 = 5
product [1..5] = 120

Like the other solutions I mentioned, this has its drawbacks. vshow isn't a first-class function, and we have to use C rather than Haskell function-call syntax. It's not terribly flexible, but it is one line of reasonably clear code, and I can think of a few small use cases.

The missing preprocessor

Besides sharing a cute little trick, I wanted to start a conversation about text-level preprocessors for Haskell. CPP is used throughout the standard library for conditional compilation, boilerplate class instances, etc. At the same time, it's quite limited, and not a good syntactic fit for Haskell.

Template Haskell is at the opposite end of the power/complexity spectrum. It might be what you want for programmatically generating complicated abstract syntax trees. It's not a good fit for simply "copy-pasting" some text into a few locations. For those tasks we shy away from the enormous complexity of TH. We use CPP or, well, copy-paste. The latter is not acceptable for a language that's advertising such a high level of abstraction. We may write less boilerplate than in Java or C++, but it still gets on the nerves.

I think there's an unfilled niche for lightweight text-level preprocessing, with just a bit more flexibility than CPP. GHC makes it easy to plug in any preprocessor you like, using -F -pgmF. We can start experimenting today.

m4 is a powerful and venerable preprocessor. It comes with most UNIX systems and has an open-source implementation. Yet I've not seen any Haskell project use it for Haskell source. Has anyone used m4 in this role?

I found an old paper (.ps.gz) with some proposals. Anyone have comments?

There's a number of preprocessors for implementing specific language features, including arrows, indexed monads, C bindings, and Conor McBride's intriguing Strathclyde Haskell Enhancement. The package preprocessor-tools seems to be a framework for writing this sort of preprocessor.

So what have you used for preprocessing? What features would you like to see? What design philosophy?

16 comments:

  1. Take a look at Racket's 'macro' system. It's an API for the front-end and you'll find it helps you with a lot more than the simple example above.

    Oh and Racket does come with a lazy dialect, and perhaps one day with typed/lazy.

    -- Matthias

    ReplyDelete
  2. Liskell?

    http://www.liskell.org/fifteen-minute-tour

    ReplyDelete
  3. The students will be able to check their UP Board Result for Class 12 via SMS also. The UP Board 12th Result 2021 would comprise the student's roll RBSE 8th Board Result 2021 Around 25 lakh students will be able to check their UP Board Result 2021 Class 12 for the UP 12th examination which will be conducted in April and May 2021.

    ReplyDelete
  4. The Broker Forex Net Is A Perfect Place To Discover The News Of Great Brokers In The Industry. Along With Our Review Of Popular Broker Sites, You Can Also Find Useful Articles That Will Help You Learn More About Trading And Try Out Some Strategies On Your Own. Many Informative Video Tutorials Are Also Included.

    ReplyDelete
  5. Market Live Rates Provides You With Honest Reviews And Strategies For Each Website. Our Site Is Updated Frequently And New Strategies Are Tested Daily. Be Sure To Check Our Facebook Page As Well As Our Youtube To Stay In Touch With Market Live Rates Latest News And Updates!

    ReplyDelete
  6. ในยุคเศรษฐกิจตกต่ำเเบบนี้อาจจะทำให้ใครหลายคนมีปัญหาติดขัดเรื่องการเงิน เพราะมีรายรับน้อยกว่ารายจ่าย ทำให้จพเป็นจะต้องใช้เงินอย่างระมัดระวัง การมีบัตรเครดิตสักใบก็อาจจะช่วยเเบ่งเยาภาระต่างๆ ให้กับชาวพนักงานออฟฟิศมากยิ่งขึ้น ช้อปปิ้ง กินข้าว เที่ยว ก็จะเป็นเรื่องง่าย เราขอเเนะนำบัตรเครดิต ที่อนุบัติไว เข้าชม ไม่ต้องรอนาน เเถมยังมีสิทธิพิเศษมากมาย รับรองว่าเเบ่งเบาภาระทางการเงินได้อย่างเเน่นอน.

    ReplyDelete
  7. Gutt Websäit : Zonahobisaya
    Gutt Websäit : Zonahobisaya
    Gutt Websäit : lambang
    Gutt Websäit : Sinopsis Film
    Gutt Websäit : Zonahobisaya
    Gutt Websäit : Zonahobisaya
    Gutt Websäit : Zonahobisaya
    Gutt Websäit : Zonahobisaya

    ReplyDelete
  8. This post is really astounding

    ReplyDelete
  9. This website is so great, Cheers!

    ReplyDelete
  10. Marvelous post. Very useful!!

    ReplyDelete
  11. Experience the evolution of R&M Healing Buddha, from coincidental discovery to life-altering calling, as it touches hearts and lives.
    healingbuddha

    ReplyDelete
  12. Beyond business, The Global Hues dives into holistic well-being. Explore articles on health, wealth management, work-life balance, and personal growth.

    theglobalhues

    ReplyDelete
  13. YesMovies, established in the early 2010s, has become a popular and accessible online streaming platform for movie and TV enthusiasts. With its user-friendly interface, viewers can easily explore a diverse range of content based on genres, release years, and IMDb ratings. The platform provides a convenient and enjoyable way to discover and watch a wide array of movies. While YesMovies is favored for its extensive collection, users are encouraged to consider legal and authorized streaming alternatives for a secure and ethical entertainment experience. Embracing legitimate options ensures continued support for the creative industry and enhances the overall streaming landscape positively.
    Yesmovies

    ReplyDelete
  14. pgaztec เกมแตกดี ที่สุด ในตอนนี้ เป็นหนึ่งในเกมสล็อตที่ได้รับความนิยมจากผู้เล่นมากมาย เกมนี้มีธีมที่น่าสนใจจากชุดหุ่นของชนบทและวัฒนธรรมอะซเทคจาก pg slot ซึ่งนำเสนอบรรยากาศ

    ReplyDelete