packages feed

phino-0.0.170 / src / CLI / Helpers.hs

-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MIT{-# LANGUAGE OverloadedRecordDot #-}{-# LANGUAGE RecordWildCards #-}{-# LANGUAGE ScopedTypeVariables #-}{-# LANGUAGE TupleSections #-}module CLI.Helpers whereimport ASTimport Abridge (abridged)import CLI.Typesimport CLI.Validators (invalidCLIArguments)import CST (EXPRESSION)import Canonizer (CanonState, canonize, canonizeExpr, canonizeExpression, emptyCanon, lambdaNames)import Compiled (compiled)import Control.Exceptionimport Control.Monad (mfilter, when, zipWithM, (>=>))import qualified Data.ByteString as BSimport Data.Char (isDigit)import Data.Functor ((<&>))import Data.IORefimport Data.List (intercalate, nub)import qualified Data.Map.Strict as Mimport Data.Maybeimport qualified Data.Text as Timport Data.Text.Encoding (encodeUtf8)import Data.Time (getZonedTime)import Data.Tuple (swap)import Data.Version (showVersion)import qualified Data.Yaml as Yamlimport Deps (Evaluation (EvHeader, EvRun), Header (Header), Judgment, SaveEvalFunc, SaveStepFunc, World (World), dontSaveEval, emptyNesting, emptyProgress, emptyProtocol, endEval, endEvalXml, profiling, progressed, resited, saveEval, saveEvalXml, saveStep)import Encodingimport Engine (Engine, fresh, yaml)import Files (ensuredFile, overwrite)import qualified Filter as Fimport Functions (buildFunctions, execFunctions)import GHC.Clock (getMonotonicTime)import LaTeX (LatexContext (LatexContext), defaultMeetLength, defaultMeetPopularity, expressionToLaTeX, rewrittensToLatex)import Lambdas (Lambdas, emptyLambdas, readLambdas, sized, taken)import Lining (LineFormat (SINGLELINE))import Locator (LocatorException, locatedExpression, withLocatedExpression)import Loggerimport Morph (ReduceContext (..), insideUniverse)import Parser (Form, parseExpressionThrows, parseExpressionThrows')import Paths_phino (version)import qualified Printer as Pimport qualified Random as Rimport Rewriter (Rewrittens', stepHeaders)import Sugar (SugarType (SALTY), withoutRho)import System.Directory (createDirectoryIfMissing, doesDirectoryExist, doesFileExist, listDirectory, removeFile)import System.FilePath (splitExtension, takeDirectory, (</>))import System.IO (Handle, IOMode (WriteMode), getContents', hClose, hSetEncoding, openFile, utf8)import Text.Printf (printf)import XMIR (Atoms, expressionToXMIR, gitRevision, parseXMIRThrows, printXMIR, renameAtoms, xmirAtoms, xmirToPhi)import Yaml (normalizationRules)import qualified Yaml as YjustMeetPopularity :: Maybe Int -> IntjustMeetPopularity = fromMaybe defaultMeetPopularityjustMeetLength :: Maybe Int -> IntjustMeetLength = fromMaybe defaultMeetLengthisStepFileName :: FilePath -> BoolisStepFileName name = length base == 5 && all isDigit base && ext `elem` [".phi", ".xmir", ".tex"]  where    (base, ext) = splitExtension nameclearedStepsDir :: Maybe FilePath -> IO ()clearedStepsDir Nothing = pure ()clearedStepsDir (Just dir) = do  exists <- doesDirectoryExist dir  when exists (listDirectory dir >>= mapM_ removedIfStepFile)  where    removedIfStepFile :: FilePath -> IO ()    removedIfStepFile name = when (isStepFileName name) (removedFile (dir </> name))    removedFile :: FilePath -> IO ()    removedFile path = do      isFile <- doesFileExist path      when isFile (removeFile path)saveStepFunc :: Maybe FilePath -> PrintContext -> [Expression] -> [Expression] -> Expression -> IO SaveStepFuncsaveStepFunc stepsDir ctx@PrintCtx{..} included excluded start = do  clearedStepsDir stepsDir  counter <- newIORef (0 :: Int)  names <- newIORef =<< seeded  let ioToExt :: String      ioToExt        | _outputFormat == LATEX = "tex"        | otherwise = show _outputFormat      render :: Expression -> IO String      render expr = do        visible <- shown expr        if _canonize          then atomicModifyIORef' names (swap . canonizeExpression visible) >>= printCanonized ctx visible          else printAnswer ctx visible      save :: SaveStepFunc      save expr = do        step <- atomicModifyIORef' counter (\value -> (value + 1, value + 1))        saveStep stepsDir ioToExt render step expr  pure save  where    shown :: Expression -> IO Expression    shown expr = F.exclude' expr excluded >>= (`F.include'` included)    seeded :: IO CanonState    seeded      | _canonize && isJust stepsDir = (snd . (`canonizeExpression` emptyCanon) <$> shown start) `catch` (\(_ :: LocatorException) -> pure emptyCanon)      | otherwise = pure emptyCanonwithEvalFunc :: forall a. Maybe FilePath -> Maybe FilePath -> Bool -> PrintContext -> (SaveEvalFunc -> IO a) -> IO awithEvalFunc xml text profile ctx action = withEvalFunc' xml text profile ctx (tracked >=> action)  where    tracked :: SaveEvalFunc -> IO SaveEvalFunc    tracked record = do      enabled <- logging INFO      if enabled        then do          cursor <- newIORef . emptyProgress =<< getMonotonicTime          pure (progressed cursor 5 (flattened ctx) record)        else pure recordwithEvalFunc' :: forall a. Maybe FilePath -> Maybe FilePath -> Bool -> PrintContext -> (SaveEvalFunc -> IO a) -> IO awithEvalFunc' xml text profile ctx action =  markedUp xml (\marked -> plain text (\lined -> action (\record -> marked record >> lined record)))  where    markedUp :: Maybe FilePath -> (SaveEvalFunc -> IO a) -> IO a    markedUp Nothing go = go dontSaveEval    markedUp (Just file) go = do      logDebug (printf "The option '--protocol' is specified, every firing will be recorded in '%s' as XML" file)      cursor <- newIORef emptyNesting      began <- getMonotonicTime      bracket (opened file) (\protocol -> endEvalXml protocol cursor began `finally` hClose protocol) $ \protocol ->        go (saveEvalXml protocol cursor (flattened ctx))    plain :: Maybe FilePath -> (SaveEvalFunc -> IO a) -> IO a    plain Nothing go = go dontSaveEval    plain (Just file) go = do      logDebug (printf "The option '--minutes' is specified, every firing will be recorded in '%s' as text" file)      cursor <- newIORef emptyProtocol      began <- getMonotonicTime      watch <- if profile then Just <$> profiling else pure Nothing      bracket (opened file) (\protocol -> endEval protocol cursor began `finally` hClose protocol) $ \protocol ->        go (saveEval protocol cursor (flattened ctx) (salted ctx) watch)    opened :: FilePath -> IO Handle    opened file = do      createDirectoryIfMissing True (takeDirectory file)      protocol <- openFile file WriteMode      hSetEncoding protocol utf8      pure protocollambdasOf :: Maybe FilePath -> IO LambdaslambdasOf Nothing = do  logDebug "The option '--symbolic' is not specified, no λ function can be fired"  pure emptyLambdaslambdasOf (Just file) = do  logDebug (printf "The option '--symbolic' is specified, reading the λ functions from '%s'" file)  ensuredFile file >>= readLambdasstarted :: Expression -> ReduceContext -> IO ()started expr ctx = modifyIORef' ctx._minted (max (taken expr))heading :: SaveEvalFunc -> PrintContext -> Judgment -> Expression -> IO ()heading record ctx judgment locator =  record . EvRun judgment . T.pack =<< flattened ctx locatorflattened :: PrintContext -> Expression -> IO Stringflattened ctx@PrintCtx{..} expr =  pure (P.printExpressionWith shaped expr (_sugar, UNICODE, SINGLELINE, _margin))  where    shaped :: SugarType -> EXPRESSION -> EXPRESSION    shaped sugar = maybe id (abridged _abridgedData) _abridged . hidden ctx sugarsalted :: PrintContext -> Expression -> IO Stringsalted ctx = flattened ctx{_sugar = SALTY}headerOf :: [String] -> Maybe FilePath -> String -> Expression -> Maybe FilePath -> Lambdas -> IO HeaderheaderOf args file input expr table lambdas = do  now <- getZonedTime  pure    ( Header        (showVersion version)        (mfilter (/= "UNKNOWN") (Just gitRevision))        now        args        (World file (length (lines input)) (BS.length (encodeUtf8 (T.pack input))) (tops expr) (bindings expr))        ((,sized lambdas) <$> table)    )  where    tops :: Expression -> Int    tops (ExFormation bds) = length [attr | BiTau attr _ <- bds]    tops _ = 0    bindings :: Expression -> Int    bindings (ExFormation bds) = length bds + sum [bindings inner | BiTau _ inner <- bds]    bindings (ExApplication inner (ArTau _ arg)) = bindings inner + bindings arg    bindings (ExDispatch inner _) = bindings inner    bindings (ExPhiMeet _ _ inner) = bindings inner    bindings (ExPhiAgain _ _ inner) = bindings inner    bindings _ = 0aimed :: PrintContext -> Judgment -> Header -> Maybe String -> Expression -> ReduceContext -> IO (Expression, ReduceContext)aimed printCtx judgment header Nothing expr ctx = do  ctx._saveEval . EvHeader header ctx._locator =<< locatedExpression ctx._locator expr  heading ctx._saveEval printCtx judgment ctx._locator  pure (expr, ctx)aimed printCtx judgment header (Just src) expr@(ExFormation _) ctx = do  target <- parseExpressionThrows src  logDebug (printf "The option '--inside' is specified, reducing '%s' inside the given universe" (P.printExpression target))  held <- newIORef []  (universe, aiming) <- insideUniverse target expr ctx{_saveEval = \record -> modifyIORef' held (record :)}  ctx._saveEval (EvHeader header aiming._locator target)  heading ctx._saveEval printCtx judgment aiming._locator  mapM_ (ctx._saveEval . resited ctx._site aiming._locator) . reverse =<< readIORef held  pure (universe, aiming{_site = aiming._locator, _opened = Just (ctx._nesting, judgment, aiming._locator), _saveEval = ctx._saveEval})aimed _ _ _ (Just _) expr _ =  invalidCLIArguments    (printf "The option --inside requires the input expression to be a formation, but given: %s" (P.printExpression expr))readInput :: Maybe FilePath -> IO StringreadInput inputFile' =  withoutBOM <$> case inputFile' of    Just pth -> do      logDebug (printf "Reading from file: '%s'" pth)      readFile =<< ensuredFile pth    Nothing -> do      logDebug "Reading from stdin"      getContents' `catch` (\(e :: SomeException) -> throwIO (CouldNotReadFromStdin (show e)))withoutBOM :: String -> StringwithoutBOM ('\xFEFF' : rest) = restwithoutBOM input = inputparseInput :: String -> IOFormat -> Form -> IO ExpressionparseInput phi PHI form = parseExpressionThrows' form phiparseInput xmir XMIR _ = parseXMIRThrows xmir >>= xmirToPhiparseInput _ LATEX _ = invalidCLIArguments "LaTeX cannot be used as input format"parseInputWithAtoms :: String -> IOFormat -> Form -> IO (Expression, Atoms)parseInputWithAtoms xmir XMIR _ = do  doc <- parseXMIRThrows xmir  (,) <$> xmirToPhi doc <*> xmirAtoms docparseInputWithAtoms input format form = (,M.empty) <$> parseInput input format formprintRewrittens :: PrintContext -> Rewrittens' -> IO StringprintRewrittens ctx@PrintCtx{..} rewrittens@(chain, _)  | _outputFormat == LATEX && _sequence = rewrittensToLatex rewrittens (printCtxToLatexCtx ctx)  | _canonize = withHeaders <$> zipWithM (printCanonized ctx) (map fst chain) (map fst (canonize chain))  | otherwise = withHeaders <$> mapM (printAnswer ctx . fst) chain  where    withHeaders :: [String] -> String    withHeaders rendered      | _headers && _sequence = intercalate "\n" (zipWith prefixed (stepHeaders chain) rendered)      | otherwise = intercalate "\n" rendered      where        prefixed :: String -> String -> String        prefixed = printf "\n%s\n%s"printAnswer :: PrintContext -> Expression -> IO StringprintAnswer ctx@PrintCtx{..} expr  | _canonize = printCanonized ctx expr (canonizeExpr expr)  | otherwise = printFocused ctx exprprintCanonized :: PrintContext -> Expression -> Expression -> IO StringprintCanonized ctx@PrintCtx{..} expr canonized =  printFocused ctx{_xmirCtx = renameAtoms (zip (lambdaNames expr) (lambdaNames canonized)) _xmirCtx} canonizedprintFocused :: PrintContext -> Expression -> IO StringprintFocused ctx@PrintCtx{..} expr  | _focus == ExRoot = printInFormat ctx expr  | otherwise = locatedExpression _focus expr >>= printExpression ctxfilteredAt :: Expression -> Expression -> [Expression] -> [Expression] -> Expression -> IO ExpressionfilteredAt locator universe excluded included answer = do  placed <- withLocatedExpression locator answer universe  shown <- F.exclude' placed excluded >>= (`F.include'` included)  locatedExpression locator shownprintExpression :: PrintContext -> Expression -> IO StringprintExpression ctx@PrintCtx{..} ex = case _outputFormat of  PHI -> pure (printPhi ctx ex)  XMIR -> throwIO CouldNotPrintExpressionInXMIR  LATEX -> pure (expressionToLaTeX ex (printCtxToLatexCtx ctx))printInFormat :: PrintContext -> Expression -> IO StringprintInFormat ctx@PrintCtx{..} expr = case _outputFormat of  PHI -> pure (printPhi ctx expr)  XMIR -> expressionToXMIR expr _xmirCtx <&> printXMIR  LATEX -> pure (expressionToLaTeX expr (printCtxToLatexCtx ctx))printPhi :: PrintContext -> Expression -> StringprintPhi PrintCtx{..} expr = P.printExpression' (if _hideRho then rholess expr else expr) (_sugar, UNICODE, _line, _margin)  where    rholess :: Expression -> Expression    rholess (ExFormation bds) = ExFormation [binding bd | bd <- bds, not (rho bd)]    rholess (ExDispatch inner attr) = ExDispatch (rholess inner) attr    rholess (ExApplication inner (ArTau AtRho _)) = rholess inner    rholess (ExApplication inner (ArTau attr arg)) = ExApplication (rholess inner) (ArTau attr (rholess arg))    rholess (ExPhiMeet prefix idx inner) = ExPhiMeet prefix idx (rholess inner)    rholess (ExPhiAgain prefix idx inner) = ExPhiAgain prefix idx (rholess inner)    rholess other = other    binding :: Binding -> Binding    binding (BiTau attr inner) = BiTau attr (rholess inner)    binding other = other    rho :: Binding -> Bool    rho (BiTau AtRho _) = True    rho (BiVoid AtRho) = True    rho _ = Falsehidden :: PrintContext -> SugarType -> EXPRESSION -> EXPRESSIONhidden PrintCtx{..} sugar  | _hideRho = withoutRho sugar  | otherwise = idprintCtxToLatexCtx :: PrintContext -> LatexContextprintCtxToLatexCtx PrintCtx{..} =  LatexContext _sugar _line _margin _nonumber _compress _canonize _meetPopularity _meetLength _focus _expression _label _meetPrefix _headers _hideRhogetRules :: Bool -> Bool -> [FilePath] -> IO [Y.Rule]getRules normalize shuffle rules = do  ordered <- (++) <$> builtin <*> custom  if shuffle    then do      logDebug "The --shuffle option is provided, rules are used in random order"      R.shuffle ordered    else pure ordered  where    builtin :: IO [Y.Rule]    builtin      | normalize = do          logDebug (printf "The --normalize option is provided, %d built-it normalization rules are used" (length normalizationRules))          pure normalizationRules      | otherwise = pure []    custom :: IO [Y.Rule]    custom      | null rules = do          logDebug "No --rule option is provided, no user rules are used"          pure []      | otherwise = do          logDebug (printf "Using rules from files: [%s]" (intercalate ", " rules))          yamls <- mapM ensuredFile (nub rules)          mapM (loadRule >=> validateRewriteRule) yamlsloadRule :: FilePath -> IO Y.RuleloadRule file =  catch (Y.yamlRule file) reportInvalidRule  where    reportInvalidRule :: Yaml.ParseException -> IO Y.Rule    reportInvalidRule err =      invalidCLIArguments (printf "Could not load rule from '%s'\n%s" file (Y.shiftYamlError (Yaml.prettyPrintParseException err)))validateRewriteRule :: Y.Rule -> IO Y.RulevalidateRewriteRule rule =  let used = maybe [] (map Y.function) rule.where_   in case filter (\fn -> fn `notElem` (buildFunctions ++ execFunctions)) used of        (fn : _) -> invalidCLIArguments (printf "Function '%s' in rule '%s' is not supported" fn rule.name)        [] -> case filter (`elem` execFunctions) used of          [] -> case filter (\extra -> returned (Y.function extra) /= kind (Y.meta extra)) (fromMaybe [] rule.where_) of            [] -> pure rule            (extra : _) ->              invalidCLIArguments                ( printf                    "Function '%s' in rule '%s' returns %s, but its meta '%s' is %s"                    (Y.function extra)                    rule.name                    (returned (Y.function extra))                    (P.printExtraArg (Y.meta extra))                    (kind (Y.meta extra))                )          (fn : _) ->            invalidCLIArguments              (printf "Function '%s' in rule '%s' is available only for dataization and morphing, not for rewriting" fn rule.name)  where    returned :: String -> String    returned "random-tau" = "an attribute"    returned "tau" = "an attribute"    returned "join" = "bindings"    returned "dataize" = "bytes"    returned _ = "an expression"    kind :: Y.ExtraArgument -> String    kind (Y.ArgAttribute _) = "an attribute"    kind (Y.ArgBinding _) = "bindings"    kind (Y.ArgBytes _) = "bytes"    kind (Y.ArgExpression _) = "an expression"printOut :: Maybe FilePath -> String -> IO ()printOut target content = case target of  Nothing -> do    logDebug "The option '--target' is not specified, printing to console..."    putStrLn content  Just file -> do    logDebug (printf "The option '--target' is specified, printing to '%s'..." file)    overwrite file (content ++ "\n")    logDebug (printf "The command result was saved in '%s'" file)engine :: IO Engineengine = case compiled of  Nothing -> pure yaml  Just linked    | fresh linked -> logDebug "The built-in rules run compiled, as 'phino compile' wrote them" >> pure linked    | otherwise -> throwIO StaleEngine