Rewrite rules with newtype

Rewriting rules can help optimize your program. And I wonder if they will work if I wrap my objects in newtype . As you know, newtype does not lead to performance penalties; it is a compile-time wrapper that leaves at runtime. So I wonder if rewriting rules with newtype will still run. For example, there are many rules for Data.Text ( What is Haskell Data.Text ? ).

GHC User Guide warns of the unthinkability of the algorithm:

The GHC currently uses a very simple syntax algorithm to map the LHS rule to the expression. He is looking for a replacement that makes LHS and expression syntactically uniform alpha conversion. A pattern (rule), but not an expression, is, if necessary, an extension of eta. (An Eta extension of the expression can lead to laziness errors.) But not a beta conversion (this is called higher order matching).

And now I am working with Identity Text (for some reason). Can I get these benefits? I am not an expert in rewriting rules for running tests on my own and guarantee their results.

+5
source share
1 answer

Rewriting rules apply to the GHC Core intermediate language (and not to Haskell). By this time, newtype basically disappeared. For example, Identity x becomes x |> c , where c is a constraint that distinguishes x :: Text from the type Identity Text .

These drops can cancel each other, and then the rules can work, as usual. Thus, you should still benefit from the rules for rewriting text, if everything falls into place.

Unfortunately, there are no serious guarantees, and despite the intermediate code (and you know what to look for, for example, if you know a certain place where the merger is supposed to be), you cannot be sure that using Identity does not come here at a price.

+6
source

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


All Articles