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 7078667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:34:34+00:00 2026-05-28T06:34:34+00:00

I read this: http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns I like the idea, want to use the extension. I

  • 0

I read this:

http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns

I like the idea, want to use the extension. I however would like to make sure as to one thing: whether the view function is evaluated once for a single matching.

So let’s say we have:

{-# LANGUAGE ViewPatterns #-}
...

f (view -> Nothing) = ...
f (view -> Just x) = ...

view :: a -> Maybe b

Now let’s say I invoke f a. Is view invoked twice or just once for the given argument a?

EDIT:

I tried to find out whether this is the case and wrote the following:

{-# LANGUAGE ViewPatterns #-}

import System.IO.Unsafe

blah (ble -> Nothing) = 123
blah (ble -> Just x) = x

ble x = unsafePerformIO $ do
    putStrLn $ "Inside ble: " ++ show x
    return x

main :: IO ()
main = do
    putStrLn $ "Main: " ++ show (blah $ Just 234)

Output using GHC:

Inside ble: Just 234
Inside ble: Just 234
Main: 234

Output using GHC (with optimization)

Inside ble: Just 234
Main: 234

Output using GHCi:

Main: Inside ble: Just 234
Inside ble: Just 234
234
  • 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-05-28T06:34:34+00:00Added an answer on May 28, 2026 at 6:34 am

    Just once:

    Efficiency: When the same view function is applied in
    multiple branches of a function definition or a case expression (e.g.,
    in size above), GHC makes an attempt to collect these
    applications into a single nested case expression, so that the view
    function is only applied once. Pattern compilation in GHC follows the
    matrix algorithm described in Chapter 4 of The
    Implementation of Functional Programming Languages
    . When the
    top rows of the first column of a matrix are all view patterns with the
    “same” expression, these patterns are transformed into a single nested
    case. This includes, for example, adjacent view patterns that line up
    in a tuple, as in

    f ((view -> A, p1), p2) = e1
    f ((view -> B, p3), p4) = e2
    

    The current notion of when two view pattern expressions are “the
    same” is very restricted: it is not even full syntactic equality.
    However, it does include variables, literals, applications, and tuples;
    e.g., two instances of view ("hi", "there") will be
    collected. However, the current implementation does not compare up to
    alpha-equivalence, so two instances of (x, view x -> y) will not be coalesced.

    — The GHC manual

    As for your snippet, the problem is that you’re not compiling with optimisation; with both ghc -O and ghc -O2, the line is only printed once. That’s always the first thing to check when you have performance-related problems when using GHC 🙂

    (By the way, Debug.Trace lets you check these kinds of things without having to write manual unsafePerformIO hacks.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I read this page: http://www.opengl.org/wiki/GlGetString For example, if the extension GL_EXT_pixel_transform_color_table is listed, doing
I read this documentation: http://docs.python.org/reference/simple_stmts.html Now, I want to create statements like it describes.
I already read this trick in the cookbook: http://book.cakephp.org/2.0/en/models/model-attributes.html#usetable Now I would like to
So basiclly I read this, http://www.gdcl.co.uk/2011/June/UnregisteredFilters.htm . Which tells you how to use filters
I read this http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/ and in nested set model I want retrieve from parent
I've had a read of this http://docs.b-list.org/django-registration/0.8/backend-api.html and i've had a shot at making
I am learning php and read this example in this http://www.php.net/manual/en/language.types.boolean.php tutorial, I want
I've read this: http://java.sun.com/products/javamail/javadocs/com/sun/mail/imap/package-summary.html I want to write an imap server rather than imap
I have read this thread: http://jasperforge.org/plugins/espforum/view.php?group_id=83&forumid=101&topicid=67923 I would rather not repeat the page header
I am trying to setup Jeash for Haxe, so I read this: http://old.haxe.org/com/libs/jeash/firststeps Now,

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.