{-# LANGUAGE LambdaCase #-}{-# LANGUAGE ScopedTypeVariables #-}{-# LANGUAGE TupleSections #-}-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MITmodule Matcher whereimport ASTimport Data.Map.Strict (Map)import qualified Data.Map.Strict as Mapimport Data.Maybe (catMaybes)import Data.Text (Text)data MetaValue = MvAttribute Attribute | MvBytes Bytes | MvBindings [Binding] | MvFunction Function | MvExpression Expression deriving (Show)instance Eq MetaValue where MvAttribute left == MvAttribute right = left == right MvBytes left == MvBytes right = left == right MvBindings left == MvBindings right = ExFormation left == ExFormation right MvFunction left == MvFunction right = left == right MvExpression left == MvExpression right = left == right _ == _ = Falsedata Order = Unordered | Ordered deriving (Eq, Show)data Meta = Named Text | Anon Slot deriving (Eq, Ord, Show)newtype Subst = Subst (Map Meta MetaValue) deriving (Eq, Show)type MatchExpressionFunc = Expression -> Expression -> [Subst]substEmpty :: SubstsubstEmpty = Subst Map.emptysubstSingle :: Text -> MetaValue -> SubstsubstSingle key value = Subst (Map.singleton (Named key) value)substSlot :: Slot -> MetaValue -> SubstsubstSlot slot value = Subst (Map.singleton (Anon slot) value)combine :: Subst -> Subst -> Maybe Substcombine (Subst a) (Subst b) = go (Map.toList b) a where go :: [(Meta, MetaValue)] -> Map Meta MetaValue -> Maybe Subst go [] acc = Just (Subst acc) go ((key, value) : rest) acc = case Map.lookup key acc of Just found | found == value -> go rest acc | otherwise -> Nothing Nothing -> go rest (Map.insert key value acc)combineMany :: [Subst] -> [Subst] -> [Subst]combineMany xs xy = catMaybes [combine x y | x <- xs, y <- xy]matchAttribute :: Attribute -> Attribute -> [Subst]matchAttribute (AtMeta meta) tgt = [substSingle meta (MvAttribute tgt)]matchAttribute (AtAny slot) tgt = [substSlot slot (MvAttribute tgt)]matchAttribute ptn tgt | ptn == tgt = [substEmpty] | otherwise = []matchFunction :: Function -> Function -> [Subst]matchFunction (FnMeta meta) tgt | named tgt = [substSingle meta (MvFunction tgt)]matchFunction (FnAny slot) tgt | named tgt = [substSlot slot (MvFunction tgt)]matchFunction ptn tgt | ptn == tgt = [substEmpty] | otherwise = []named :: Function -> Boolnamed (Function _) = Truenamed (FnSymbol _) = Truenamed _ = FalsematchBinding :: Order -> Binding -> Binding -> [Subst]matchBinding _ (BiVoid pattr) (BiVoid tattr) = matchAttribute pattr tattrmatchBinding _ (BiDelta (BtMeta meta)) (BiDelta tdata) = [substSingle meta (MvBytes tdata)]matchBinding _ (BiDelta (BtAny slot)) (BiDelta tdata) = [substSlot slot (MvBytes tdata)]matchBinding _ (BiDelta pdata) (BiDelta tdata) | pdata == tdata = [substEmpty] | otherwise = []matchBinding _ (BiLambda pFunc) (BiLambda tFunc) = matchFunction pFunc tFuncmatchBinding order (BiTau pattr pexp) (BiTau tattr texp) = combineMany (matchAttribute pattr tattr) (matchExpression'' order pexp texp)matchBinding _ _ _ = []matchArgument :: Order -> Argument -> Argument -> [Subst]matchArgument order (ArTau pattr pexp) (ArTau tattr texp) = combineMany (matchAttribute pattr tattr) (matchExpression'' order pexp texp)matchBindings :: Order -> [Binding] -> [Binding] -> [Subst]matchBindings Unordered pbs tbs = go [pb | pb <- pbs, not (loose pb)] tbs where go :: [Binding] -> [Binding] -> [Subst] go [] rest = case filter loose pbs of [] | null rest -> [substEmpty] [BiMeta name] -> [substSingle name (MvBindings rest)] [BiAny slot] -> [substSlot slot (MvBindings rest)] _ -> [] go (pb : others) pool = concat [combineMany (matchBinding Unordered pb tb) (go others rest) | (tb, rest) <- picks pool]matchBindings Ordered [] [] = [substEmpty]matchBindings Ordered [] _ = []matchBindings Ordered ((BiMeta name) : pbs) tbs = matchBindingsMeta (substSingle name) pbs tbsmatchBindings Ordered ((BiAny slot) : pbs) tbs = matchBindingsMeta (substSlot slot) pbs tbsmatchBindings Ordered (pb : pbs) (tb : tbs) = combineMany (matchBinding Ordered pb tb) (matchBindings Ordered pbs tbs)matchBindings Ordered _ _ = []matchBindingsMeta :: (MetaValue -> Subst) -> [Binding] -> [Binding] -> [Subst]matchBindingsMeta bind [] tbs = [bind (MvBindings tbs)]matchBindingsMeta bind pbs tbs = go [] tbs where go :: [Binding] -> [Binding] -> [Subst] go before after = catMaybes [combine (bind (MvBindings (reverse before))) subst | subst <- matchBindings Ordered pbs after] ++ case after of [] -> [] (tb : rest) -> go (tb : before) restloose :: Binding -> Boolloose (BiMeta _) = Trueloose (BiAny _) = Trueloose _ = Falsecrowded :: Expression -> Boolcrowded = \case ExFormation bds -> length (filter loose bds) > 1 || any goBinding bds ExDispatch expr _ -> crowded expr ExApplication expr (ArTau _ arg) -> crowded expr || crowded arg ExPhiMeet _ _ expr -> crowded expr ExPhiAgain _ _ expr -> crowded expr _ -> False where goBinding :: Binding -> Bool goBinding (BiTau _ expr) = crowded expr goBinding _ = FalsematchExpression' :: MatchExpressionFuncmatchExpression' = matchExpression'' UnorderedmatchExpression'' :: Order -> MatchExpressionFuncmatchExpression'' _ (ExMeta meta) tgt = [substSingle meta (MvExpression tgt)]matchExpression'' _ (ExAny slot) tgt = [substSlot slot (MvExpression tgt)]matchExpression'' _ ExXi ExXi = [substEmpty]matchExpression'' _ ExRoot ExRoot = [substEmpty]matchExpression'' _ ExTermination ExTermination = [substEmpty]matchExpression'' order (ExFormation pbs) (ExFormation tbs) = matchBindings order pbs tbsmatchExpression'' order (ExDispatch pexp pattr) (ExDispatch texp tattr) = combineMany (matchAttribute pattr tattr) (matchExpression'' order (pinned pattr tattr pexp) texp)matchExpression'' order (ExApplication pexp parg@(ArTau pattr _)) (ExApplication texp targ@(ArTau tattr _)) = combineMany (matchExpression'' order (pinned pattr tattr pexp) texp) (matchArgument order parg targ)matchExpression'' order (ExPhiAgain prefix idx expr) (ExPhiAgain prefix' idx' expr') | prefix == prefix' && idx == idx' = matchExpression'' order expr expr' | otherwise = []matchExpression'' order (ExPhiMeet prefix idx expr) (ExPhiMeet prefix' idx' expr') | prefix == prefix' && idx == idx' = matchExpression'' order expr expr' | otherwise = []matchExpression'' _ _ _ = []pinned :: Attribute -> Attribute -> Expression -> Expressionpinned (AtMeta meta) tattr = goExpr where goExpr :: Expression -> Expression goExpr (ExFormation bds) = ExFormation (map goBinding bds) goExpr (ExDispatch expr attr) = ExDispatch (goExpr expr) (goAttribute attr) goExpr (ExApplication expr (ArTau attr arg)) = ExApplication (goExpr expr) (ArTau (goAttribute attr) (goExpr arg)) goExpr expr = expr goBinding :: Binding -> Binding goBinding (BiTau attr expr) = BiTau (goAttribute attr) (goExpr expr) goBinding (BiVoid attr) = BiVoid (goAttribute attr) goBinding bd = bd goAttribute :: Attribute -> Attribute goAttribute (AtMeta meta') | meta' == meta = tattr goAttribute attr = attrpinned _ _ = idmatchExpressionDeep :: MatchExpressionFuncmatchExpressionDeep = matchExpressionDeep' FalsematchExpressionDeep' :: Bool -> MatchExpressionFuncmatchExpressionDeep' = matchExpressionDeep'' UnorderedmatchExpressionDeep'' :: Order -> Bool -> MatchExpressionFuncmatchExpressionDeep'' order redex ptn tgt = go tgt [] where go :: Expression -> [Subst] -> [Subst] go expr rest | redex && inert expr = rest | fitting ptn expr = matchExpression'' order ptn expr ++ below expr rest | otherwise = below expr rest below :: Expression -> [Subst] -> [Subst] below (ExFormation bds) rest = foldr inside rest bds below (ExDispatch expr _) rest = go expr rest below (ExApplication expr (ArTau _ arg)) rest = go expr (go arg rest) below _ rest = rest inside :: Binding -> [Subst] -> [Subst] inside (BiTau _ expr) rest = go expr rest inside _ rest = restmatchExpression :: MatchExpressionFuncmatchExpression = matchExpressionDeepreachable :: Expression -> Expression -> Boolreachable = reachable' Falsereachable' :: Bool -> Expression -> Expression -> Boolreachable' redex ptn = go where go :: Expression -> Bool go tgt | redex && inert tgt = False | otherwise = fitting ptn tgt || case tgt of ExFormation bds -> any inside bds ExDispatch expr _ -> go expr ExApplication expr (ArTau _ arg) -> go expr || go arg _ -> False inside :: Binding -> Bool inside (BiTau _ expr) = go expr inside _ = Falsefitting :: Expression -> Expression -> Boolfitting = go where go :: Expression -> Expression -> Bool go (ExMeta _) _ = True go (ExAny _) _ = True go ExXi ExXi = True go ExRoot ExRoot = True go ExTermination ExTermination = True go (ExFormation pbs) (ExFormation tbs) = all (\pbd -> loose pbd || any (kin pbd) tbs) pbs go (ExDispatch pexp pattr) (ExDispatch texp tattr) = same pattr tattr && go pexp texp go (ExApplication pexp (ArTau pattr _)) (ExApplication texp (ArTau tattr _)) = same pattr tattr && go pexp texp go (ExPhiAgain{}) (ExPhiAgain{}) = True go (ExPhiMeet{}) (ExPhiMeet{}) = True go _ _ = False kin :: Binding -> Binding -> Bool kin (BiTau pattr _) (BiTau tattr _) = same pattr tattr kin (BiVoid pattr) (BiVoid tattr) = same pattr tattr kin (BiLambda _) (BiLambda _) = True kin (BiDelta _) (BiDelta _) = True kin _ _ = False same :: Attribute -> Attribute -> Bool same (AtMeta _) _ = True same (AtAny _) _ = True same pattr tattr = pattr == tattrsites :: forall a. Bool -> (Expression -> [a]) -> Expression -> [(Expression, a)]sites redex rule tgt = go tgt [] where go :: Expression -> [(Expression, a)] -> [(Expression, a)] go expr rest | redex && inert expr = rest | otherwise = map (expr,) (rule expr) ++ below expr rest below :: Expression -> [(Expression, a)] -> [(Expression, a)] below (ExFormation bds) rest = foldr inside rest bds below (ExDispatch expr _) rest = go expr rest below (ExApplication expr (ArTau _ arg)) rest = go expr (go arg rest) below _ rest = rest inside :: Binding -> [(Expression, a)] -> [(Expression, a)] inside (BiTau _ expr) rest = go expr rest inside _ rest = resthits :: forall a. [(Int, Bool, Maybe Expression -> Expression -> [a])] -> Maybe Expression -> Expression -> [Int]hits rules universe tgt = go [tgt] rules where go :: [Expression] -> [(Int, Bool, Maybe Expression -> Expression -> [a])] -> [Int] go [] _ = [] go _ [] = [] go (expr : rest) pending | inert expr && and [redex | (_, redex, _) <- pending] = go rest pending | otherwise = case [idx | (idx, redex, rule) <- pending, not (redex && inert expr), not (null (rule universe expr))] of [] -> go (below expr rest) pending met -> met ++ go (below expr rest) [rule | rule@(idx, _, _) <- pending, idx `notElem` met] below :: Expression -> [Expression] -> [Expression] below (ExFormation bds) rest = foldr inside rest bds below (ExDispatch expr _) rest = expr : rest below (ExApplication expr (ArTau _ arg)) rest = expr : arg : rest below _ rest = rest inside :: Binding -> [Expression] -> [Expression] inside (BiTau _ expr) rest = expr : rest inside _ rest = restpicks :: [Binding] -> [(Binding, [Binding])]picks = go [] where go :: [Binding] -> [Binding] -> [(Binding, [Binding])] go _ [] = [] go before (bd : after) = (bd, reverse before ++ after) : go (bd : before) after