{-# LANGUAGE DeriveGeneric #-}{-# LANGUAGE LambdaCase #-}{-# LANGUAGE OverloadedStrings #-}{-# LANGUAGE PatternSynonyms #-}{-# LANGUAGE ViewPatterns #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MIT-- This module represents AST tree for parsed phi-calculus expressionmodule AST whereimport Data.Bits (xor)import Data.List (foldl')import Data.Maybe (listToMaybe)import Data.Text (Text)import qualified Data.Text as Timport GHC.Generics (Generic)-- An anonymous meta-variable, written bare β π, π΅, π, π, π, πΏ, π, π or π-- with no index after it. It matches whatever term stands in its place and no rule-- can name it afterwards, so it is known only by the kind it was written as-- ('t', 'B', 'e', 'n', 'k', 'd', 'F', 'S', 'i') and by the offset it was written-- at, which tells it apart from every other anonymous meta of the same term.data Slot = Slot Text Int deriving (Eq, Ord, Show)data Expression = ExFormation [Binding] | ExXi | ExRoot | ExTermination | ExApplication Expression Argument | ExDispatch Expression Attribute | ExMeta Text | ExAny Slot | ExPhiMeet (Maybe String) Int Expression | ExPhiAgain (Maybe String) Int Expression | {- | Bare data πΏ β the raw bytes extracted by the 'delta' dataization rule. It is not a phi-calculus term but a rendering-only chain node, so a '--sequence' derivation can terminate at the data itself rather than at the data object it was pulled out of (see #980). It never flows into the matcher, builder or the dataization relation. -} ExBytes Bytes deriving (Eq, Ord, Show, Generic)data Argument = ArTau Attribute Expression | ArAlpha Alpha Expression deriving (Eq, Ord, Show, Generic)data Alpha = Alpha Int | AlMeta Text | AlAny Slot deriving (Eq, Ord, Generic)data Binding = BiTau Attribute Expression | BiVoid Attribute | BiDelta Bytes | BiLambda Function | BiMeta Text | BiAny Slot deriving (Eq, Ord, Show, Generic)data Bytes = BtEmpty | BtOne String | BtMany [String] | BtMeta Text | BtAny Slot deriving (Eq, Ord, Show, Generic)data Attribute = AtLabel Text | AtPhi | AtRho | AtLambda | AtDelta | AtMeta Text | AtAny Slot deriving (Eq, Generic, Ord)data Function = Function Text | FnMeta Text | FnAny Slot | {- | A symbol π1 β a Ξ» function nothing answers, which is what makes the value the term it stands in carries unknown. It is a name and not a meta-variable: no substitution ever binds it and the matcher never reads it, while 'FnMeta' π1 stands for any Ξ» name at all, a symbol included. -} FnSymbol Int | {- | A symbol written bare, π, which is an answer asking for a fresh one. The slot it was written at tells two of them apart inside one answer, so each is minted its own name (see 'minted' in 'Lambdas'). -} FnFresh Slot deriving (Eq, Generic, Show, Ord)instance Show Attribute where show (AtLabel label) = T.unpack label show AtRho = "Ο" show AtPhi = "Ο" show AtDelta = "Ξ" show AtLambda = "Ξ»" show (AtMeta meta) = '!' : T.unpack meta show (AtAny (Slot kind _)) = '!' : T.unpack kindinstance Show Alpha where show (Alpha idx) = 'Ξ±' : show idx show (AlMeta meta) = 'Ξ±' : '!' : T.unpack meta show (AlAny (Slot kind _)) = 'Ξ±' : '!' : T.unpack kind-- A cheap, fixed-size digest of an expression, used for fast (dirty) equality-- checks during loop detection. Equal expressions always produce the same-- digest, but distinct expressions may collide, so a positive digest match-- must always be confirmed with a full structural (==) comparison.hashExpression :: Expression -> InthashExpression = goExpr fnvOffset where fnvPrime, fnvOffset :: Int fnvPrime = 1099511628211 fnvOffset = 14695981039 -- FNV-1a style mixing step (Int multiplication wraps silently). step :: Int -> Int -> Int step h x = (h `xor` x) * fnvPrime hashText :: Int -> Text -> Int hashText = T.foldl' (\h c -> step h (fromEnum c)) hashString :: Int -> String -> Int hashString = foldl' (\h c -> step h (fromEnum c)) goSlot :: Int -> Slot -> Int goSlot h (Slot kind idx) = step (hashText h kind) idx hashMaybeString :: Int -> Maybe String -> Int hashMaybeString h Nothing = step h 0 hashMaybeString h (Just s) = hashString (step h 1) s goExpr :: Int -> Expression -> Int goExpr h = \case ExFormation bds -> foldl' goBinding (step h 1) bds ExXi -> step h 2 ExRoot -> step h 3 ExTermination -> step h 4 ExApplication ex arg -> goArgument (goExpr (step h 5) ex) arg ExDispatch ex at -> goAttribute (goExpr (step h 6) ex) at ExMeta t -> hashText (step h 7) t ExAny slot -> goSlot (step h 32) slot ExPhiMeet ms i ex -> goExpr (hashMaybeString (step (step h 9) i) ms) ex ExPhiAgain ms i ex -> goExpr (hashMaybeString (step (step h 10) i) ms) ex ExBytes bts -> goBytes (step h 8) bts goBinding :: Int -> Binding -> Int goBinding h = \case BiTau at ex -> goExpr (goAttribute (step h 11) at) ex BiDelta bts -> goBytes (step h 12) bts BiVoid at -> goAttribute (step h 13) at BiLambda fn -> goFunction (step h 14) fn BiMeta t -> hashText (step h 15) t BiAny slot -> goSlot (step h 33) slot goBytes :: Int -> Bytes -> Int goBytes h = \case BtEmpty -> step h 17 BtOne s -> hashString (step h 18) s BtMany ss -> foldl' hashString (step h 19) ss BtMeta t -> hashText (step h 20) t BtAny slot -> goSlot (step h 34) slot goAttribute :: Int -> Attribute -> Int goAttribute h = \case AtLabel t -> hashText (step h 21) t AtPhi -> step h 23 AtRho -> step h 24 AtLambda -> step h 25 AtDelta -> step h 26 AtMeta t -> hashText (step h 27) t AtAny slot -> goSlot (step h 35) slot goArgument :: Int -> Argument -> Int goArgument h = \case ArTau at ex -> goExpr (goAttribute (step h 22) at) ex ArAlpha al ex -> goExpr (goAlpha (step h 30) al) ex goAlpha :: Int -> Alpha -> Int goAlpha h = \case Alpha idx -> step (step h 31) idx AlMeta t -> hashText (step h 28) t AlAny slot -> goSlot (step h 36) slot goFunction :: Int -> Function -> Int goFunction h = \case Function t -> hashText (step h 16) t FnMeta t -> hashText (step h 29) t FnAny slot -> goSlot (step h 37) slot FnSymbol idx -> step (step h 38) idx FnFresh slot -> goSlot (step h 39) slot-- Every symbol a term carries, in the order it was written. A symbol is what-- makes the value a term stands for unknown, and the run reads the-- dependencies between its firings off them: a term carrying π4 is the term-- the firing that minted π4 answered with, whatever it has been rewritten-- into since.symbols :: Expression -> [Int]symbols = goExpr where goExpr :: Expression -> [Int] goExpr (ExFormation bds) = concatMap goBinding bds goExpr (ExApplication expr arg) = goExpr expr ++ goArgument arg goExpr (ExDispatch expr _) = goExpr expr goExpr (ExPhiMeet _ _ expr) = goExpr expr goExpr (ExPhiAgain _ _ expr) = goExpr expr goExpr _ = [] goBinding :: Binding -> [Int] goBinding (BiTau _ expr) = goExpr expr goBinding (BiLambda (FnSymbol idx)) = [idx] goBinding _ = [] goArgument :: Argument -> [Int] goArgument (ArTau _ expr) = goExpr expr goArgument (ArAlpha _ expr) = goExpr expr-- The symbol a term stands for, if its value is one at all. A term carries its-- value where the Ο chain ends, so that is the only place a symbol names this-- term: one sitting under Ο, or inside an operand, belongs to the term it was-- minted for and says nothing about this one. This is how a firing is read as-- the answer of an earlier firing.denoted :: Expression -> Maybe Intdenoted = goExpr where goExpr :: Expression -> Maybe Int goExpr (ExFormation bds) = listToMaybe (concatMap goBinding bds) goExpr (ExApplication _ (ArTau AtPhi expr)) = goExpr expr goExpr (ExApplication expr _) = goExpr expr goExpr (ExPhiMeet _ _ expr) = goExpr expr goExpr (ExPhiAgain _ _ expr) = goExpr expr goExpr _ = Nothing goBinding :: Binding -> [Int] goBinding (BiLambda (FnSymbol idx)) = [idx] goBinding (BiTau AtPhi expr) = maybe [] pure (goExpr expr) goBinding _ = []countNodes :: Expression -> IntcountNodes (ExFormation bds) = 1 + sum (map nodesInBinding bds) + length bds where nodesInBinding :: Binding -> Int nodesInBinding (BiTau _ expr) = countNodes expr + 2 nodesInBinding (BiMeta _) = 1 nodesInBinding (BiAny _) = 1 nodesInBinding _ = 3countNodes (ExApplication expr (ArTau _ expr')) = 4 + countNodes expr + countNodes expr'countNodes (ExApplication expr (ArAlpha _ expr')) = 4 + countNodes expr + countNodes expr'countNodes (ExDispatch expr' _) = 2 + countNodes expr'countNodes (ExPhiMeet _ _ expr) = countNodes exprcountNodes (ExPhiAgain _ _ expr) = countNodes exprcountNodes _ = 1matchBaseObject :: Expression -> Maybe T.TextmatchBaseObject (ExDispatch ExRoot (AtLabel label)) = Just labelmatchBaseObject _ = Nothingpattern BaseObject :: T.Text -> Expressionpattern BaseObject label <- (matchBaseObject -> Just label) where BaseObject label = ExDispatch ExRoot (AtLabel label)-- Minimal matcher function (required for view pattern)---- Both bindings of a literal are named Ο, the only void that the real-- 'number', 'string' and 'bytes' declare ([@] > number, see #1155). The-- legacy 'as-bytes', 'data' and positional Ξ±0 forms are still recognized-- so XMIR produced by older jeo versions keeps sugaring back.matchDataObject :: Expression -> Maybe (T.Text, Bytes)matchDataObject (ExApplication outer arg) | Just inner <- asBytesArg arg = case (matchOuter outer, matchInner inner) of (Just label, Just bts) -> Just (label, bts) _ -> Nothing where asBytesArg :: Argument -> Maybe Expression asBytesArg (ArTau AtPhi inner) = Just inner asBytesArg (ArTau (AtLabel "as-bytes") inner) = Just inner asBytesArg (ArAlpha (Alpha 0) inner) = Just inner asBytesArg _ = Nothing dataArg :: Argument -> Maybe Expression dataArg (ArTau AtPhi formation) = Just formation dataArg (ArTau (AtLabel "data") formation) = Just formation dataArg (ArAlpha (Alpha 0) formation) = Just formation dataArg _ = Nothing matchOuter :: Expression -> Maybe T.Text matchOuter (BaseObject label) = Just label matchOuter (ExPhiAgain _ _ (BaseObject label)) = Just label matchOuter _ = Nothing matchInner :: Expression -> Maybe Bytes matchInner (ExPhiAgain _ _ inner') = matchInner inner' matchInner inner' = matchInner' inner' matchInner' :: Expression -> Maybe Bytes matchInner' (ExApplication bytes arg') | Just formation <- dataArg arg' = case (matchesBytes bytes, matchFormation formation) of (True, Just bts) -> Just bts _ -> Nothing matchInner' _ = Nothing matchesBytes :: Expression -> Bool matchesBytes (BaseObject "bytes") = True matchesBytes (ExPhiAgain _ _ (BaseObject "bytes")) = True matchesBytes _ = False matchFormation :: Expression -> Maybe Bytes matchFormation (ExFormation [BiDelta bts]) = Just bts matchFormation (ExPhiAgain _ _ (ExFormation [BiDelta bts])) = Just bts matchFormation _ = NothingmatchDataObject _ = Nothingpattern DataString :: Bytes -> Expressionpattern DataString bts = DataObject "string" btspattern DataNumber :: Bytes -> Expressionpattern DataNumber bts = DataObject "number" btspattern DataObject :: T.Text -> Bytes -> Expressionpattern DataObject label bts <- (matchDataObject -> Just (label, bts)) where DataObject label bts = ExApplication (BaseObject label) (ArTau AtPhi (dataBytes bts))-- The bytes object Ξ¦.bytes(Ο β¦ β¦ Ξ β€ β¦ β§) β what a 'bytes' atom-- yields and what a 'DataObject' carries under its Ο argument.-- The payload is bound to 'Ο', the void that the real 'bytes' object-- declares ([@] > bytes), so that every dispatch on the literal can bind-- (see #1142)dataBytes :: Bytes -> ExpressiondataBytes bts = ExApplication (BaseObject "bytes") (ArTau AtPhi (ExFormation [BiDelta bts]))