Do GHC rewrite rules know syntactic sugar?

For example, with lists, if I write a rule with LHS myFn [x] , will it also be able to run when the programmer writes myFn (x:[]) ? or do I need to write a separate rule for each possible syntax?

+6
source share
2 answers

The rewriting rules get de-sugared. Therefore, a rewrite rule, for example

 {-# RULES "myFn/singleton" forall x. myFn [x] = myOtherFn x #-} 

will be stored inside

 forall x. myFn (x:[]) = myOtherFn x 

Then it was applied to the de-sagal form of the program. (All optimization in the GHC takes place in a de-sagal form of the program).

+7
source

I understand that purely syntactic things like this do not matter; the rule will work anyway.

What you find is a problem is that myFn may have been myFn by the time the GHC tries to use the rule (so there is nothing to run for it). In general, find out exactly when your rule (s) are the hard part!

+4
source

Source: https://habr.com/ru/post/979181/


All Articles