packages feed

phino-0.0.159 / src / Morph.hs

{-# LANGUAGE DeriveAnyClass #-}{-# LANGUAGE DerivingStrategies #-}{-# LANGUAGE DuplicateRecordFields #-}{-# LANGUAGE OverloadedRecordDot #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE RecordWildCards #-}{-# OPTIONS_GHC -Wno-name-shadowing #-}{-# OPTIONS_GHC -Wno-unused-record-wildcards #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Morph (Answer (..), Deadline (..), Firing (..), Kept (..), ReduceContext (..), ReduceException (..), EvaluationFunc, Memo (..), ReductionFunc, Morphed, Steps (..), Tally (..), admitting, boxed, charged, counted, deeper, emptyState, ended, enter, entering, execBuildTerm, expired, inferred, insideUniverse, isLambda, lambda, leadsTo, memoized, morph, morph', morphing, normalized, onward, opening, parking, recalled, refused, remember, remembered, retained, settled, starved, tallied, timed, universed, unparked) whereimport ASTimport Builder (buildExpressionThrows, nameIn)import Control.Applicative ((<|>))import Control.Exception (Exception, catch, evaluate, throwIO, try)import Control.Monad (when)import Data.IORef (IORef, modifyIORef', newIORef, readIORef, writeIORef)import Data.List (find, partition)import Data.List.NonEmpty (NonEmpty (..))import qualified Data.List.NonEmpty as NEimport qualified Data.Map.Strict as Mapimport Data.Maybe (isJust, listToMaybe)import qualified Data.Text as Timport Deps (Acyclic (..), BuildTermFunc, BuildTermMethod, Evaluation (..), Judgment (..), SaveEvalFunc, SaveStepFunc, State (..), Term (..), dontSaveStep)import Engine (Engine (..))import GHC.Clock (getMonotonicTime)import qualified Inference as Inimport Lambdas (Lambdas)import Locator (locatedExpression, withLocatedExpression)import Matcher (substEmpty)import Must (Must (..))import Printer (printExpression)import Random (shuffle)import Rewriter (RewriteContext (RewriteContext), Rewritten, Seen, rewrite, seenInsert)import Rule (RuleContext (RuleContext))import System.Timeout (timeout)import Text.Printf (printf)import Yaml (ExtraArgument (..))type Morphed = (Expression, NonEmpty Rewritten)type ReductionFunc = Expression -> ReduceContext -> Expression -> State -> IO (Maybe Bytes, State)type EvaluationFunc = ReduceContext -> State -> Expression -> Expression -> IO (Expression, State)emptyState :: StateemptyState = State Nothing Nothingdata Steps = Steps  { _limit :: Int  , _spent :: Int  }data Tally = Tally  { _ceiling :: Int  , _count :: IORef Int  }data Deadline = Deadline  { _seconds :: Int  , _until :: Double  }data Memo = Memo (IORef (Store (Int, Kept))) (IORef (Map.Map Firing Answer)) (IORef Int) (IORef Int)data Answer  = Answer Expression Expression  | Joined (Expression, Expression) Expressionended :: Answer -> Expressionended (Answer _ normal) = normalended (Joined _ normal) = normaldata Firing = Firing T.Text [Either Int Bytes] [Expression]  deriving (Eq, Ord)data Kept  = Answered Answer  | Looped Expression  | Stalled T.Text Inttype Store answer = Map.Map Int [(Expression, answer)]data ReduceContext = ReduceContext  { _locator :: Expression  , _site :: Expression  , _universe :: Maybe Expression  , _maxDepth :: Int  , _maxCycles :: Int  , _steps :: Steps  , _tally :: Maybe Tally  , _minted :: IORef Int  , _deadline :: Maybe Deadline  , _memo :: Maybe Memo  , _nesting :: Int  , _opened :: Maybe (Int, Judgment, Expression)  , _depthSensitive :: Bool  , _shuffle :: Bool  , _partial :: Bool  , _acyclic :: Maybe Acyclic  , _maxLoops :: Int  , _judgment :: Judgment  , _parked :: [T.Text]  , _entered :: Seen  , _symbolic :: Lambdas  , _buildTerm :: BuildTermFunc  , _reduce :: ReductionFunc  , _evaluate :: EvaluationFunc  , _saveStep :: SaveStepFunc  , _saveEval :: SaveEvalFunc  , _engine :: Engine  }data Budget  = Depth Judgment Int  | Firings Int  | Cycles Intdata ReduceException  = OutOfSteps Budget  | OutOfTime Int  | Stuck T.Text  | StuckAt T.Text (NonEmpty Rewritten) State  | OutOfStepsAt Budget (NonEmpty Rewritten) State  | Diverged T.Text  | Looping Expression  | LoopingAt Expression (NonEmpty Rewritten) State  | Undataizable Expression (NonEmpty Rewritten) State  | Unmorphable Expression  deriving anyclass (Exception)instance Show ReduceException where  show (OutOfSteps (Depth judgment limit)) =    printf "%s did not finish before reaching the limit of steps: --max-steps=%d" (show judgment) limit  show (OutOfSteps (Firings limit)) =    printf "Evaluation did not finish before reaching the limit of firings: --max-firings=%d" limit  show (OutOfSteps (Cycles limit)) =    printf "Normalization did not finish before reaching the limit of cycles: --max-cycles=%d" limit  show (OutOfStepsAt budget _ _) = show (OutOfSteps budget)  show (OutOfTime limit) =    printf "Evaluation did not finish before reaching the limit of seconds: --max-seconds=%d" limit  show (Stuck func) = printf "No entry of --symbolic answers the λ function '%s'" (T.unpack func)  show (StuckAt func _ _) = show (Stuck func)  show (Diverged func) = printf "The λ function '%s' could not join its two sides" (T.unpack func)  show (Looping term) = printf "Reduction entered a formation it is already inside: %s" (printExpression term)  show (LoopingAt term _ _) = show (Looping term)  show (Undataizable ExTermination _ _) = "dataization reached the terminator ⊥, which signals an error and cannot be dataized"  show (Undataizable term _ _) = printf "no dataization rule matched, while dataization expects a normal form: %s" (printExpression term)  show (Unmorphable term) = printf "Morphing expects a normal form, but no morphing rule matches: %s" (printExpression term)deeper :: ReduceContext -> IO ReduceContextdeeper ctx@ReduceContext{_steps = Steps limit spent} = do  clocked ctx  when (spent >= limit) $ do    starve ctx._memo    ctx._saveEval (EvStarved ctx._nesting limit ctx._judgment ctx._site)    throwIO (OutOfSteps (Depth ctx._judgment limit))  pure ctx{_steps = Steps limit (spent + 1)}  where    starve :: Maybe Memo -> IO ()    starve Nothing = pure ()    starve (Just (Memo _ _ _ exhausted)) = modifyIORef' exhausted (+ 1)tallied :: Maybe Int -> IO (Maybe Tally)tallied = traverse (\cap -> Tally cap <$> newIORef 0)timed :: Maybe Int -> IO (Maybe Deadline)timed = traverse (\cap -> Deadline cap . (+ fromIntegral cap) <$> getMonotonicTime)charged :: ReduceContext -> IO ()charged ctx = do  clocked ctx  mapM_ billed ctx._tally  where    billed :: Tally -> IO ()    billed (Tally cap count) = do      fired <- readIORef count      when (fired >= cap) $ do        ctx._saveEval (EvSpent ctx._nesting cap ctx._judgment ctx._site)        throwIO (OutOfSteps (Firings cap))      writeIORef count (fired + 1)clocked :: ReduceContext -> IO ()clocked ctx = mapM_ clock ctx._deadline  where    clock :: Deadline -> IO ()    clock (Deadline cap due) = do      now <- getMonotonicTime      when (now >= due) (expired ctx cap)expired :: ReduceContext -> Int -> IO aexpired ctx cap = do  ctx._saveEval (EvTimeout ctx._nesting cap ctx._judgment ctx._site)  throwIO (OutOfTime cap)memoized :: Maybe Acyclic -> IO (Maybe Memo)memoized (Just Plausible) = Just <$> (Memo <$> newIORef Map.empty <*> newIORef Map.empty <*> newIORef 0 <*> newIORef 0)memoized _ = pure Nothingrecalled :: Maybe Memo -> Expression -> Int -> IO (Maybe Kept)recalled Nothing _ _ = pure Nothingrecalled (Just (Memo store _ answers _)) form spent = do  kept <- readIORef store  count <- readIORef answers  let live = [known | (term, (stamp, known)) <- Map.findWithDefault [] (hashExpression form) kept, term == form, current count stamp known]  pure (find answered live <|> listToMaybe live)  where    current :: Int -> Int -> Kept -> Bool    current count stamp (Stalled _ least) = stamp == count && spent >= least    current _ _ _ = True    answered :: Kept -> Bool    answered (Answered _) = True    answered _ = Falsecounted :: Maybe Memo -> IO Intcounted Nothing = pure 0counted (Just (Memo _ _ answers _)) = readIORef answersstarved :: Maybe Memo -> IO Intstarved Nothing = pure 0starved (Just (Memo _ _ _ exhausted)) = readIORef exhaustedretained :: Maybe Memo -> Expression -> Int -> Kept -> IO ()retained Nothing _ _ _ = pure ()retained (Just (Memo store _ _ _)) form stamp kept =  modifyIORef' store (Map.insertWith (++) (hashExpression form) [(form, (stamp, kept))])remembered :: Maybe Memo -> Firing -> IO (Maybe Answer)remembered Nothing _ = pure Nothingremembered (Just (Memo _ firings _ _)) firing = Map.lookup firing <$> readIORef firingsremember :: Maybe Memo -> Firing -> Answer -> IO ()remember Nothing _ _ = pure ()remember (Just (Memo _ firings answers _)) firing answer = do  modifyIORef' firings (Map.insert firing answer)  modifyIORef' answers (+ 1)parking :: NonEmpty Rewritten -> State -> IO a -> IO aparking seq state action = action `catch` rethrow  where    rethrow :: ReduceException -> IO a    rethrow (Stuck func) = throwIO (StuckAt func seq state)    rethrow (OutOfSteps budget) = throwIO (OutOfStepsAt budget seq state)    rethrow (Looping term) = throwIO (LoopingAt term seq state)    rethrow failure = throwIO failureunparked :: IO a -> IO aunparked action = action `catch` rethrow  where    rethrow :: ReduceException -> IO a    rethrow (StuckAt func _ _) = throwIO (Stuck func)    rethrow (OutOfStepsAt budget _ _) = throwIO (OutOfSteps budget)    rethrow (LoopingAt term _ _) = throwIO (Looping term)    rethrow failure = throwIO failureentering :: Expression -> ReduceContext -> IO ReduceContextentering term ctx = maybe (pure ctx) (`enter` ctx) (entrance ctx._judgment term)admitting :: Expression -> ReduceContext -> IO (Either (Acyclic, Expression) ReduceContext)admitting term ctx = maybe (pure (Right ctx)) (`admitted` ctx) (entrance ctx._judgment term)enter :: Expression -> ReduceContext -> IO ReduceContextenter form ctx = admitted form ctx >>= either (refused form ctx) purerefused :: Expression -> ReduceContext -> (Acyclic, Expression) -> IO arefused form ctx (mode, before) = do  ctx._saveEval (EvLooped ctx._nesting ctx._judgment mode before ctx._site)  throwIO (Looping form)admitted :: Expression -> ReduceContext -> IO (Either (Acyclic, Expression) ReduceContext)admitted form ctx = maybe (pure (Right ctx)) remembered ctx._acyclic  where    remembered :: Acyclic -> IO (Either (Acyclic, Expression) ReduceContext)    remembered mode =      maybe (Right ctx{_entered = seenInsert (digest mode form) form ctx._entered}) (\before -> Left (mode, before))        <$> awaited (listToMaybe (drop ctx._maxLoops (filter (repeated mode form) (Map.findWithDefault [] (digest mode form) ctx._entered))))    awaited :: Maybe Expression -> IO (Maybe Expression)    awaited found = case ctx._deadline of      Nothing -> pure found      Just (Deadline cap due) -> do        now <- getMonotonicTime        maybe (expired ctx cap) pure =<< timeout (ceiling (max 0 (due - now) * 1000000)) (evaluate found)    digest :: Acyclic -> Expression -> Int    digest Proven = hashShape    digest Plausible = hashSkeleton    repeated :: Acyclic -> Expression -> Expression -> Bool    repeated Proven form before = alike form before    repeated Plausible form before = within before formentrance :: Judgment -> Expression -> Maybe Expressionentrance Dataization term@(ExFormation bds)  | boxed bds || isJust (lambda bds) = Just termentrance Morphing (ExDispatch form@(ExFormation bds) _)  | isJust (lambda bds) = Just formentrance Evaluation term@(ExFormation bds)  | isJust (lambda bds) = Just termentrance _ _ = Nothingboxed :: [Binding] -> Boolboxed bds = any phi bds && not (any isLambda bds) && not (any delta bds)  where    phi :: Binding -> Bool    phi (BiTau AtPhi _) = True    phi _ = False    delta :: Binding -> Bool    delta (BiDelta _) = True    delta _ = Falselambda :: [Binding] -> Maybe (T.Text, Expression)lambda bds = case partition isLambda bds of  ([BiLambda (Function func)], rest) -> Just (func, ExFormation rest)  _ -> NothingisLambda :: Binding -> BoolisLambda (BiLambda _) = TrueisLambda _ = Falsemorph' :: Morphed -> Expression -> State -> ReduceContext -> IO (Morphed, State)morph' = morph'' Truemorph'' :: Bool -> Morphed -> Expression -> State -> ReduceContext -> IO (Morphed, State)morph'' owner start univ state entry = go start univ state entry  where    go :: Morphed -> Expression -> State -> ReduceContext -> IO (Morphed, State)    go (expr, seq) univ state caller = do      ctx <- opening Morphing =<< deeper =<< entering expr =<< parking seq state (universed univ caller{_judgment = Morphing})      parking seq state $ do        reached <- inferred expr univ state ctx ctx._engine._morphing        case reached of          Just (In.Answered step built, state') -> do            when owner (ctx._saveEval (EvMorphed ctx._nesting built))            seq' <- leadsTo seq step built ctx            pure ((built, seq'), state')          Just (In.Onward way built world, state') -> do            (morphed, state'') <- onward seq state' way built ctx            go morphed world state'' ctx          Nothing -> throwIO (Unmorphable expr)morph :: Expression -> State -> ReduceContext -> IO (Expression, [Rewritten], State)morph universe state caller@ReduceContext{..} = do  expr <- locatedExpression _locator universe  prepared <- try (universed universe caller)  case prepared of    Left (OutOfSteps _) | _partial -> pure (expr, [(universe, Nothing)], state)    Left failure -> throwIO (failure :: ReduceException)    Right univCtx -> reduced expr =<< opening Morphing univCtx  where    reduced :: Expression -> ReduceContext -> IO (Expression, [Rewritten], State)    reduced expr ctx = do      result <- try (morph' (expr, (universe, Nothing) :| []) universe state ctx)      case result of        Right ((morphed, seq), state') -> pure (morphed, reverse (NE.toList seq), state')        Left (StuckAt func seq parked) | _partial -> residue seq parked{_stuck = Just func}        Left (OutOfStepsAt _ seq parked) | _partial -> residue seq parked        Left (LoopingAt _ seq parked) -> residue seq parked        Left failure -> throwIO (failure :: ReduceException)    residue :: NonEmpty Rewritten -> State -> IO (Expression, [Rewritten], State)    residue seq parked = do      expr <- locatedExpression _locator (fst (NE.head seq))      pure (expr, reverse (NE.toList seq), parked)inferred :: Expression -> Expression -> State -> ReduceContext -> [In.Inference value] -> IO (Maybe (In.Conclusion value, State))inferred expr univ state ctx rules = do  ordered <- if ctx._shuffle then shuffle rules else pure rules  matched <- go ordered  traverse (premised state) matched  where    go :: [In.Inference value] -> IO (Maybe (In.Premises value))    go [] = pure Nothing    go (rule : rest) = rule (RuleContext (execBuildTerm univ ctx) (Just univ) ctx._engine._normal) expr univ >>= maybe (go rest) (pure . Just)    premised :: State -> In.Premises value -> IO (In.Conclusion value, State)    premised state' (In.Concludes conclusion) = pure (conclusion, state')    premised state' (In.Morphs term world next) = do      (morphed, state'') <- detached term world state' ctx      next morphed >>= premised state''    premised state' (In.Evaluates form world next) = do      (answer, state'') <- ctx._evaluate ctx state' form world      next answer >>= premised state''    premised state' (In.Contextualizes term context next) = ctx._engine._contextualize term context >>= next >>= premised state'onward :: NonEmpty Rewritten -> State -> In.Way -> Expression -> ReduceContext -> IO (Morphed, State)onward seq state (In.Taken step) expr ctx = do  seq' <- leadsTo seq step expr ctx  pure ((expr, seq'), state)onward seq state (In.Normalized step) expr ctx = do  labelled <- leadsTo seq step expr ctx  normal <- normalized expr labelled ctx  pure (normal, state)onward seq state (In.Named step) expr ctx = case ctx._universe of  Just world -> onward seq state (In.Taken step) world ctx  Nothing -> onward seq state (In.Normalized step) expr ctxonward seq state (In.Staged stage) expr ctx = morph' (expr, seq) stage state ctxleadsTo :: NonEmpty Rewritten -> (Judgment, String) -> Expression -> ReduceContext -> IO (NonEmpty Rewritten)leadsTo ((current, _) :| rest) rule expr ReduceContext{..} = do  updated <- withLocatedExpression _locator expr current  pure ((updated, Nothing) :| (current, Just rule) : rest)normalized :: Expression -> NonEmpty Rewritten -> ReduceContext -> IO (Expression, NonEmpty Rewritten)normalized expr seq ctx@ReduceContext{..} = do  whole <- withLocatedExpression _locator expr (fst (NE.head seq))  (rewrittens, exceeded) <- rewrite whole _engine._normalization (rewriteContext ctx)  when exceeded (throwIO (OutOfSteps (Cycles _maxCycles)))  let (rw :| rws) = NE.reverse rewrittens      seq' = rw :| rws <> NE.tail seq  expr' <- locatedExpression _locator (fst rw)  pure (expr', seq')  where    rewriteContext :: ReduceContext -> RewriteContext    rewriteContext ReduceContext{..} =      RewriteContext _locator _maxDepth _maxCycles _depthSensitive _universe _buildTerm _engine._normal _engine._matching MtDisabled Nothing _saveStep (\redex object -> _saveEval (EvApplied _nesting _judgment (called _universe redex) object _site))    called :: Maybe Expression -> Expression -> Expression    called universe (ExApplication head' arg) = ExApplication (nameIn universe head') arg    called _ redex = redexuniversed :: Expression -> ReduceContext -> IO ReduceContextuniversed _ ctx@ReduceContext{_universe = Just _} = pure ctxuniversed univ ctx = do  (normal, _) <- normalized univ ((univ, Nothing) :| []) ctx{_locator = ExRoot, _saveStep = dontSaveStep}  ctx._saveEval (EvWorld normal)  pure ctx{_universe = Just normal}insideUniverse :: Expression -> Expression -> ReduceContext -> IO (Expression, ReduceContext)insideUniverse expr univ ctx@ReduceContext{_buildTerm = buildTerm} = case univ of  ExFormation bds -> do    (TeAttribute attr) <- buildTerm "random-tau" [] substEmpty    let aiming = ctx{_locator = ExDispatch ExRoot attr}        synthetic = ExFormation (BiTau attr expr : bds)    (normal, _) <- normalized expr ((synthetic, Nothing) :| []) aiming    pure (ExFormation (BiTau attr normal : bds), aiming{_universe = extended attr normal})  _ -> throwIO (userError "Can't reduce an expression inside a universe which is not a formation")  where    extended :: Attribute -> Expression -> Maybe Expression    extended attr normal = case ctx._universe of      Just (ExFormation bds) -> Just (ExFormation (BiTau attr normal : bds))      _ -> Nothingopening :: Judgment -> ReduceContext -> IO ReduceContextopening judgment ctx  | ctx._opened == Just (ctx._nesting, judgment, ctx._site) = pure ctx{_judgment = judgment}  | otherwise = do      ctx._saveEval (EvStarted ctx._nesting judgment ctx._site)      pure ctx{_judgment = judgment, _nesting = ctx._nesting + 1, _opened = Just (ctx._nesting + 1, judgment, ctx._site)}settled :: Expression -> Expression -> State -> ReduceContext -> IO (Expression, State)settled term univ state ctx = do  (normal, _) <- normalized term ((univ, Nothing) :| []) ctx  ((morphed, _), state') <- morph' (normal, (univ, Nothing) :| []) univ state ctx  pure (morphed, state')morphing :: Expression -> ReduceContext -> Expression -> State -> IO (Expression, State)morphing univ ctx expr state = do  (universe, aiming) <- insideUniverse expr univ ctx  normal <- locatedExpression aiming._locator universe  unparked $ do    ((morphed, _), state') <- morph' (normal, (universe, Nothing) :| []) universe state =<< opening Morphing =<< universed universe aiming    pure (morphed, state')execBuildTerm :: Expression -> ReduceContext -> BuildTermFuncexecBuildTerm _ ctx "evaluate" = evaluated ctxexecBuildTerm univ ctx "morph" = _morph univ ctxexecBuildTerm _ ctx func = _buildTerm ctx funcevaluated :: ReduceContext -> BuildTermMethodevaluated ctx [ArgExpression expr, ArgExpression universe] subst = do  form <- buildExpressionThrows expr subst  world <- buildExpressionThrows universe subst  TeExpression . fst <$> ctx._evaluate ctx emptyState form worldevaluated _ _ _ = throwIO (userError "Function evaluate() requires exactly 2 expression arguments")_morph :: Expression -> ReduceContext -> BuildTermMethod_morph univ ctx [ArgExpression expr] subst = do  built <- buildExpressionThrows expr subst  TeExpression . fst <$> detached built univ emptyState ctx_morph _ _ _ _ = throwIO (userError "Function morph() requires exactly 1 expression argument")detached :: Expression -> Expression -> State -> ReduceContext -> IO (Expression, State)detached expr univ state ctx = unparked $ do  ((morphed, _), state') <- morph'' (ctx._judgment /= Morphing) (expr, (univ, Nothing) :| []) univ state ctx  pure (morphed, state')