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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T12:11:21+00:00 2026-05-29T12:11:21+00:00

Why does haskell require multiple rewrite rules depending on the function composition technique and

  • 0

Why does haskell require multiple rewrite rules depending on the function composition technique and length? Is there a way to avoid this?

For example, given the following code…

{-# RULES
"f/f" forall a. f ( f a ) = 4*a
  #-}
f a = 2 * a

this works for

test1 = f ( f 1 )

however we need to add a rule for

test2 = f . f $ 1

and

test3 = f $ f 1

leaving us with the following rules

{-# RULES
"f/f1" forall a. f ( f a ) = 4 * a
"f/f2" forall a. f . f $ a  = 4 * a
"f/f3" forall a. f $ f $ a  = 4 * a
   #-}

However, when we string these together or use some other forms of composition the rules do not fire.

test4 = f . f . f $ 1
test5 = f $ f $ f $ 1
test6 = f $ 1

Why is this? Do I have to write rewrite rules for every possible implementation?

  • 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-29T12:11:22+00:00Added an answer on May 29, 2026 at 12:11 pm

    The rule doesn’t fire in many cases because the very simple function f is inlined before the rule had a chance to fire. If you delay the inlining,

    {-# INLINE [1] f #-}
    

    the rule

    {-# RULES "f/f" forall a. f (f a) = 4*a #-}
    

    should fire for all these cases (worked here with 7.2.2 and 7.4.1).

    The reason is that the rule matcher is not overly elaborate, it matches only expressions having the syntactic form of the rule (not entirely true, the rule body undergoes some normalisation too). The expressions f $ f 3 or f . f $ 4 do not match the syntactic form of the rule. For the rule to match, some rewriting has to take place, ($) and (.) have to be inlined before the rule matches the expression. But if you do not prevent f from being inlined in the first phase of the simplifier, it gets replaced by its body in the same run as ($) and (.) are inlined, so in the next iteration, the simplifier doesn’t see f anymore, it only sees 2*(2*x), which doesn’t match the rule.

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

Sidebar

Related Questions

Does anyone know if there's a function in Haskell which does something like this:
Does Haskell standard library have a function that given a list and a predicate,
As i understand Haskell does not have a global state, so is there any
Does anybody know the steps of haskell 'foldr' use of function? GHCI Command Window:
C++ does not have native support for lazy evaluation (as Haskell does). I'm wondering
I am starting to test Haskell for linear algebra. Does anyone have any recommendations
I think I may have asked this on Haskell-Cafe at some point, but damned
Does Haskell have a method for determining the number of CPU cores present on
Why does the following Haskell code not terminate: foldr (||) True $ repeat False
from How does a stackless language work? Haskell (as commonly implemented) does not have

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.