packages feed

phino-0.0.170 / test / Fixtures.hs

{-# LANGUAGE OverloadedStrings #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Fixtures  ( defaultReduceContext  , explainPack  , fixtureLambdas  , lambdasFile  , linked  , loopingLambdas  , overdue  , primitives  , readProtocol  , readUtf8  , recorded  , recorded'  , recordedWhole  , recordedXml  , recording  , withLambdas  , withLambdasOf  , withTemp  )whereimport AST (Expression (ExRoot))import CLI.Helpers (withEvalFunc)import CLI.Types (IOFormat (PHI), PrintContext (PrintCtx))import Compiled (compiled)import Control.Exception (bracket, evaluate)import Data.Aeson (FromJSON (parseJSON), withObject, (.:))import Data.ByteString qualified as BSimport Data.Char (toLower)import Data.IORef (newIORef)import Data.List (isPrefixOf, stripPrefix)import Data.Map.Strict qualified as Mapimport Data.Maybe (fromMaybe)import Data.Text qualified as Timport Data.Text.Encoding (encodeUtf8)import Data.Yaml qualified as Yamlimport Dataize (reduction)import Deps (Duration (..), Judgment (..), SaveEvalFunc, dontSaveEval, dontSaveStep)import Engine (Engine, building, yaml)import Evaluate (evaluation)import GHC.Clock (getMonotonicTime)import Lambdas (Lambdas, emptyLambdas, readLambdas)import Lining (LineFormat (MULTILINE))import Morph (Deadline (..), ReduceContext (..), Steps (..))import Sides (sides)import Sugar (SugarType (SWEET))import System.Directory (getTemporaryDirectory, removePathForcibly)import System.FilePath (takeExtension)import System.IO (Handle, IOMode (ReadMode), hClose, hGetContents, hSetEncoding, openBinaryTempFile, utf8, withFile)import XMIR (defaultXmirContext)defaultReduceContext :: Expression -> IO ReduceContextdefaultReduceContext loc = do  minted <- newIORef 0  named <- sides  pure (ReduceContext loc loc Nothing 25 25 Nothing Nothing (Steps 250 0) Nothing minted named Nothing Nothing 1 Nothing False True False Nothing 0 Morphing [] Map.empty Nothing emptyLambdas [] False Nothing (building linked) reduction evaluation dontSaveStep dontSaveEval linked)linked :: Enginelinked = fromMaybe yaml compiledwithLambdas :: Lambdas -> ReduceContext -> ReduceContextwithLambdas lambdas ctx = ctx{_symbolic = lambdas}lambdasFile :: FilePathlambdasFile = "test-resources/atoms.yaml"fixtureLambdas :: IO LambdasfixtureLambdas = readLambdas lambdasFileloopingLambdas :: (FilePath -> IO a) -> IO aloopingLambdas = withLambdasOf "- λ: L_loop\n  𝑛: ⟦ λ ⤍ L_loop ⟧\n"overdue :: Int -> IO Deadlineoverdue cap = Deadline (Duration (show cap) (fromIntegral cap)) . subtract 1 <$> getMonotonicTimewithLambdasOf :: T.Text -> (FilePath -> IO a) -> IO awithLambdasOf lambdas = withTemp "phino-symbolic-.yaml" (encodeUtf8 lambdas)primitives :: String -> Stringprimitives src =  unlines    [ "[["    , "  bytes -> [["    , "    @ -> ?,"    , "    not -> [[ ^ -> ?, L> L_bytes_not ]],"    , "    eq -> [[ ^ -> ?, b -> ?, L> L_bytes_eq ]]"    , "  ]],"    , "  bool -> [["    , "    @ -> ?,"    , "    if -> [[ ^ -> ?, then -> ?, else -> ?, L> L_fork ]]"    , "  ]],"    , "  number -> [["    , "    @ -> ?,"    , "    as-bytes -> $.@,"    , "    plus -> [[ ^ -> ?, x -> ?, L> L_number_plus ]],"    , "    times -> [[ ^ -> ?, x -> ?, L> L_number_times ]],"    , "    div -> [[ ^ -> ?, x -> ?, L> L_number_div ]],"    , "    gt -> [[ ^ -> ?, x -> ?, L> L_number_gt ]],"    , "    eq -> [[ ^ -> ?, x -> ?, @ -> $.^.as-bytes.eq( b -> x.as-bytes ) ]],"    , "    nope -> [[ ^ -> ?, L> L_number_nope ]]"    , "  ]],"    , "  @ -> " ++ src    , "]]"    ]recorded :: (SaveEvalFunc -> IO a) -> IO (a, String)recorded = recorded' Falserecorded' :: Bool -> (SaveEvalFunc -> IO a) -> IO (a, String)recorded' hidden action =  withTemp "phino-protocol-.txt" BS.empty $ \path -> do    answer <- withEvalFunc Nothing (Just path) False (printing hidden) action    written <- withoutTotals <$> readUtf8 path    pure (answer, written)recordedXml :: (SaveEvalFunc -> IO a) -> IO (a, String)recordedXml action =  withTemp "phino-protocol-.xml" BS.empty $ \path -> do    answer <- withEvalFunc (Just path) Nothing False (printing False) action    written <- readProtocol path    pure (answer, written)recordedWhole :: (SaveEvalFunc -> IO a) -> IO (a, String)recordedWhole action =  withTemp "phino-protocol-.xml" BS.empty $ \path -> do    answer <- withEvalFunc (Just path) Nothing False (printing False) action    written <- readUtf8 path    pure (answer, written)printing :: Bool -> PrintContextprinting hidden =  PrintCtx    SWEET    hidden    Nothing    False    MULTILINE    2    defaultXmirContext    False    False    False    False    False    1    1    ExRoot    Nothing    Nothing    Nothing    PHInewtype ExplainPack = ExplainPack Stringinstance FromJSON ExplainPack where  parseJSON = withObject "ExplainPack" (\pack -> ExplainPack <$> pack .: "latex")explainPack :: FilePath -> IO StringexplainPack path = do  ExplainPack latex <- Yaml.decodeFileThrow path  pure latexreadUtf8 :: FilePath -> IO StringreadUtf8 path =  withFile path ReadMode $ \stream -> do    hSetEncoding stream utf8    content <- hGetContents stream    _ <- evaluate (length content)    pure contentrecording :: FilePath -> Stringrecording path  | map toLower (takeExtension path) == ".xml" = "--protocol=" ++ path  | otherwise = "--minutes=" ++ pathreadProtocol :: FilePath -> IO StringreadProtocol path = sansTotals <$> readUtf8 path  where    sansTotals :: String -> String    sansTotals text      | "<?xml" `isPrefixOf` text = withoutWrapper text      | otherwise = withoutTotals (withoutHeader text)withoutHeader :: String -> StringwithoutHeader text = case span ("# " `isPrefixOf`) (lines text) of  (_ : _, "" : rest) -> unlines rest  _ -> textwithoutTotals :: String -> StringwithoutTotals text  | [msec, firings, fps] <- drop (length ls - 3) ls  , "msec " `isPrefixOf` msec  , "firings " `isPrefixOf` firings  , "fps " `isPrefixOf` fps =      unlines (take (length ls - 3) ls)  | otherwise = text  where    ls = lines textwithoutWrapper :: String -> StringwithoutWrapper text = case lines text of  (decl : "<protocol>" : rest)    | Just kept <- withoutRunTotals (dropWhile told rest) -> unlines (decl : map dedented kept)  _ -> text  where    told :: String -> Bool    told line = any (`isPrefixOf` line) ["  <written ", "  <command>", "  <world ", "  <symbolic ", "  <locator>"]    withoutRunTotals :: [String] -> Maybe [String]    withoutRunTotals rest = case reverse rest of      (closing : fps : firings : msec : kept)        | closing == "</protocol>"        , "<fps>" `isPrefixOf` dropWhile (== ' ') fps        , "<firings>" `isPrefixOf` dropWhile (== ' ') firings        , "<msec>" `isPrefixOf` dropWhile (== ' ') msec ->            Just (reverse kept)      _ -> Nothing    dedented :: String -> String    dedented line = fromMaybe line (stripPrefix "  " line)withTemp :: String -> BS.ByteString -> (FilePath -> IO a) -> IO awithTemp template content action = do  dir <- getTemporaryDirectory  bracket (openBinaryTempFile dir template) discarded $ \(path, handle) -> do    BS.hPut handle content    hClose handle    action path  where    discarded :: (FilePath, Handle) -> IO ()    discarded (path, handle) = hClose handle >> removePathForcibly path