Skip to content

Latest commit

 

History

History
15 lines (10 loc) · 1.2 KB

Responsibility of evaluation in Haskell functions.md

File metadata and controls

15 lines (10 loc) · 1.2 KB

Let Haskell level functions take responsibility of evaluation

We've discussed how a Haskell level function that operates on Plutarch level terms needs to be careful about work duplication. Related to this point, it's good practice to design your Haskell level functions so that it takes responsibility for evaluation.

The user of your Haskell level function doesn't know how many times it uses the argument it has been passed! If it uses the argument multiple times without pleting it - there's duplicate work! There are two solutions here:

  • The user plets the argument before passing it to the Haskell level function.
  • The Haskell level function takes responsibility of its argument and plets it itself.

The former is problematic since it's based on assumption. What if the Haskell level function is a good rule follower, and correctly plets its argument if using it multiple times? Well, then there's a redundant plet (though back-to-back plets will be optimized away into one).

Instead, try to offload the responsibility for evaluation to the Haskell level function - so that it only plets when it needs to.