packages feed

phino-0.0.79 / src / Rewriter.hs

{-# LANGUAGE DeriveAnyClass #-}{-# LANGUAGE DerivingStrategies #-}{-# LANGUAGE DuplicateRecordFields #-}{-# LANGUAGE LambdaCase #-}{-# LANGUAGE OverloadedRecordDot #-}{-# LANGUAGE RecordWildCards #-}{-# OPTIONS_GHC -Wno-name-shadowing #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Rewriter (rewrite, RewriteContext (..), Rewritten, Rewrittens, Rewrittens') whereimport ASTimport Builderimport Control.Exception (Exception, throwIO)import Data.List.NonEmpty (NonEmpty (..))import qualified Data.List.NonEmpty as NEimport qualified Data.Set as Setimport Depsimport Locator (locatedExpression, withLocatedExpression)import Logger (logDebug)import Matcher (Subst)import Misc (recoverFormations)import Must (Must (..), exceedsUpperBound, inRange)import Printer (printExpression)import Replacer (ReplaceContext (ReplaceCtx), ReplaceExpressionFunc, replaceExpression, replaceExpressionFast)import Rule (RuleContext (RuleContext))import qualified Rule as Rimport Text.Printf (printf)import qualified Yaml as Ytype RewriteState = (NonEmpty Rewritten, Set.Set Expression, Bool)type Rewritten = (Program, Maybe String)type Rewrittens = (NonEmpty Rewritten, Bool)type Rewrittens' = ([Rewritten], Bool)type ToReplace = (Expression, Expression, Expression, [Subst])data RewriteContext = RewriteContext  { _locator :: Expression  , _maxDepth :: Int  , _maxCycles :: Int  , _depthSensitive :: Bool  , _buildTerm :: BuildTermFunc  , _must :: Must  , _breakpoint :: Maybe String  , _saveStep :: SaveStepFunc  }data RewriteException  = MustBeGoing Must Int  | MustStopBefore Must Int  | StoppedOnLimit String Int  | LoopingRewriting String String Int  deriving (Exception)instance Show RewriteException where  show (MustBeGoing mst cnt) =    printf      "With option --must=%s it's expected rewriting cycles to be in range [%s], but rewriting stopped after %d cycles"      (show mst)      (show mst)      cnt  show (MustStopBefore mst cnt) =    printf      "With option --must=%s it's expected rewriting cycles to be in range [%s], but rewriting has already reached %d cycles and is still going"      (show mst)      (show mst)      cnt  show (StoppedOnLimit flg lim) =    printf      "With option --depth-sensitive it's expected rewriting iterations amount does not reach the limit: --%s=%d"      flg      lim  show (LoopingRewriting expr rul stp) =    printf      "On rewriting step '%d' of rule '%s' we got the same expression as we got at one of the previous step, it seems rewriting is looping\nExpression: %s"      stp      rul      expr-- Build pattern and result expression and replace patterns to results in given expressionbuildAndReplace' :: ToReplace -> ReplaceExpressionFunc -> IO ExpressionbuildAndReplace' (expr, ptn, res, substs) func = do  ptns <- buildExpressionsThrows ptn substs  repls <- buildExpressionsThrows res substs  let repls' = map const repls  pure (func (expr, ptns, repls'))-- If pattern and replacement are appropriate for fast replacing - does it.-- Pattern and replacement expressions can be used in fast replacing only if-- 1. they are both formations-- 2. they start and end with the same meta bindings, e.g. [!B1, ..., !B2]-- 3. the does not have meta bindings between first and last meta bindings-- In such case we can just replace bindings one by one without building whole expression.-- You can find more details in this ticket: https://github.com/objectionary/phino/issues/321-- If we don't meet the conditions above - just do a regular replacingtryBuildAndReplaceFast :: ToReplace -> ReplaceContext -> IO ExpressiontryBuildAndReplaceFast state@(expr, ExFormation _pbds@(pbd : pbds), ExFormation _rbds@(rbd : rbds), substs) ctx =  let pbds' = init pbds      rbds' = init rbds   in if startsAndEndsWithMeta _pbds        && startsAndEndsWithMeta _rbds        && pbd == rbd        && last pbds == last rbds        && not (hasMetaBindings pbds')        && not (hasMetaBindings rbds')        then do          logDebug "Applying fast replacing since 'pattern' and 'result' are suitable for this..."          buildAndReplace' (expr, ExFormation pbds', ExFormation rbds', substs) (replaceExpressionFast ctx)        else do          logDebug "Applying regular replacing..."          buildAndReplace' state replaceExpression  where    startsAndEndsWithMeta :: [Binding] -> Bool    startsAndEndsWithMeta [] = False    startsAndEndsWithMeta bds@(bd : _) =      length bds > 1        && isMetaBinding bd        && isMetaBinding (last bds)    hasMetaBindings :: [Binding] -> Bool    isMetaBinding :: Binding -> Bool    isMetaBinding = \case      BiMeta _ -> True      _ -> False    hasMetaBindings = foldl (\acc bd -> acc || isMetaBinding bd) FalsetryBuildAndReplaceFast state _ = buildAndReplace' state replaceExpression-- The function returns tuple (X, Y, Z) where-- - X is sequence of programs;-- - Y is Set of unique programs after each rule application. It allows to stop the rewriting if we're getting--   into loop and get back to program which we've already got before-- - Z is boolean flag which tells us if we reach breakpoint. If unmatched rule is equal to breakpoint rule - entire--   rewriting must be stopped and original program must be returnedrewrite' :: RewriteState -> [Y.Rule] -> Int -> RewriteContext -> IO RewriteStaterewrite' state [] _ _ = pure staterewrite' state (rule : rest) iteration ctx@RewriteContext{..} = do  state' <- _rewrite state 1  case state' of    (_, _, True) -> pure state'    _ -> rewrite' state' rest iteration ctx  where    _rewrite :: RewriteState -> Int -> IO RewriteState    _rewrite (_rewrittens@((program, _) :| _), _unique, _) _count =      let ruleName = rule.name          ptn = rule.pattern          res = rule.result       in if _count - 1 == _maxDepth            then do              logDebug (printf "Max amount of rewriting cycles (%d) for rule '%s' has been reached, rewriting is stopped" _maxDepth ruleName)              if _depthSensitive                then throwIO (StoppedOnLimit "max-depth" _maxDepth)                else pure (_rewrittens, _unique, False)            else do              logDebug (printf "Starting rewriting cycle for rule '%s': %d out of %d" ruleName _count _maxDepth)              expression <- locatedExpression _locator program              R.matchExpressionWithRule expression rule (RuleContext _buildTerm) >>= \case                [] -> do                  logDebug (printf "Rule '%s' does not match, rewriting is stopped" ruleName)                  if _breakpoint == Just ruleName                    then do                      logDebug (printf "Rule '%s' is a breakpoint, dropping down all the previous rewritings..." ruleName)                      pure (_rewrittens, _unique, True)                    else pure (_rewrittens, _unique, False)                matched -> do                  logDebug (printf "Rule '%s' has been matched, applying..." ruleName)                  expr <- recoverFormations <$> tryBuildAndReplaceFast (expression, ptn, res, matched) (ReplaceCtx _maxDepth)                  if expression == expr                    then do                      logDebug (printf "Applied '%s', no changes made" ruleName)                      pure (_rewrittens, _unique, False)                    else                      if Set.member expr _unique                        then throwIO (LoopingRewriting (printExpression expr) ruleName _count)                        else do                          logDebug                            ( printf                                "Applied '%s' (%d nodes -> %d nodes)\n%s"                                ruleName                                (countNodes expression)                                (countNodes expr)                                (printExpression expr)                            )                          prog <- withLocatedExpression _locator expr program                          _saveStep prog (((iteration - 1) * _maxDepth) + _count)                          _rewrite (leadsTo prog, Set.insert expr _unique, False) (_count + 1)      where        leadsTo :: Program -> NonEmpty Rewritten        leadsTo _prog =          let (program, _) :| rest = _rewrittens           in (_prog, Nothing) :| (program, Just rule.name) : rest-- Rewrite program by provided locator from RewriteContextrewrite :: Program -> [Y.Rule] -> RewriteContext -> IO Rewrittensrewrite prog rules ctx@RewriteContext{..} = do  (rewrittens, exceeded) <- _rewrite ((prog, Nothing) :| [], Set.empty, False) 0  pure (NE.reverse rewrittens, exceeded)  where    _rewrite :: RewriteState -> Int -> IO Rewrittens    _rewrite state@(rewrittens@((program, _) :| _), _, _) count      | not (inRange _must count) && count > 0 && exceedsUpperBound _must count = throwIO (MustStopBefore _must count)      | count == _maxCycles = do          logDebug (printf "Max amount of rewriting cycles for all rules (%d) has been reached, rewriting is stopped" _maxCycles)          if _depthSensitive            then throwIO (StoppedOnLimit "max-cycles" _maxCycles)            else pure (rewrittens, True)      | otherwise = do          logDebug (printf "Starting rewriting cycle for all rules: %d out of %d" count _maxCycles)          rewrite' state rules count ctx >>= \case            (_, _, True) -> pure ((prog, Nothing) :| [], False) -- breakpoint, return original program            state'@(rewrittens'@((program', _) :| _), _, False) ->              if program' == program                then do                  logDebug "Rewriting is stopped since it has no effect"                  if not (inRange _must count)                    then throwIO (MustBeGoing _must count)                    else pure (rewrittens', False)                else _rewrite state' (count + 1)