Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog.d/20260625_203341_unisay_notes_low_items.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
### Changed

- Documented the optimizer's `Note [IR is assumed well-typed]`, cited from the
constant-folding and beta-reduction rewrites that rely on it, and made the
conscious calls on the remaining low-priority candidates from #44 (leaving the
already-adequate inline comments in place). This closes out the GHC-style
`Note` documentation pass (#44). Comments only; no change to generated code.
12 changes: 11 additions & 1 deletion lib/Language/PureScript/Backend/IR/Optimizer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,22 @@ optimizedExpression =
`thenRewrite` removeIfWithEqualBranches
`thenRewrite` inlineLocalBindings

{- Note [IR is assumed well-typed]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The optimizer assumes its input IR is well-typed -- it has already passed the
PureScript type checker -- and several rewrites rely on that rather than
re-checking. 'constantFolding' rewrites @Eq True b@ to @b@ on the grounds that
@b@ must then be a 'Bool', and 'betaReduce' substitutes an argument for a
parameter without checking their types match. A rewrite must not introduce an
assumption the type checker would not already guarantee.
-}
constantFolding ∷ RewriteRule Ann
constantFolding =
pure . \case
Eq _ (LiteralBool _ a) (LiteralBool _ b) →
Rewritten Stop $ literalBool $ a == b
Eq _ (LiteralBool _ True) b →
-- 'b' must be of type Bool
-- 'b' must be of type Bool; see Note [IR is assumed well-typed]
Rewritten Stop b
Eq _ (LiteralInt _ a) (LiteralInt _ b) →
Rewritten Stop $ literalBool $ a == b
Expand All @@ -319,6 +328,7 @@ constantFolding =
_ → NoChange

-- (λx. M) N ===> M[x := N]
-- See Note [IR is assumed well-typed]
betaReduce ∷ RewriteRule Ann
betaReduce =
pure . \case
Expand Down
Loading