Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do GHC rewrite rules know to recognize syntactic sugar?

Tags:

haskell

ghc

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

like image 655
MasterMastic Avatar asked Dec 05 '14 16:12

MasterMastic


1 Answers

Re-write rules get de-sugared. So a re-write rule like

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

will be stored internally as

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

It's then applied to the de-sugared form of the program. (All optimization in GHC occurs on the de-sugared form of the program).

like image 130
Jonathan Cast Avatar answered Sep 25 '22 23:09

Jonathan Cast