-- SPDX-FileCopyrightText: Copyright (c) 2025 Objectionary.com-- SPDX-License-Identifier: MIT-- The goal of the module is to traverse given Ast and build substitutions-- from meta variables to appropriate meta valuesmodule Matcher whereimport Astimport Data.Map.Strict (Map)import qualified Data.Map.Strict as Mapimport Data.Maybe (catMaybes)-- Meta value-- The right part of substitutiondata MetaValue = MvAttribute Attribute -- !a | MvBytes String -- !b | MvBindings [Binding] -- !B | MvFunction String -- !F | MvExpression Expression -- !e | MvTail [Tail] -- !t deriving (Eq, Show)-- Tail operation after expression-- Dispatch or applicationdata Tail = TaApplication Binding -- BiTau only | TaDispatch Attribute deriving (Eq, Show)-- Substitution-- Shows the match of meta name to meta valuenewtype Subst = Subst (Map String MetaValue) deriving (Eq, Show)-- Empty substitutionsubstEmpty :: SubstsubstEmpty = Subst Map.empty-- Singleton substitution with one (key -> value) pairsubstSingle :: String -> MetaValue -> SubstsubstSingle key value = Subst (Map.singleton key value)-- Combine two substitutions into a single one-- Fails if values by the same keys are not equalcombine :: Subst -> Subst -> Maybe Substcombine (Subst a) (Subst b) = combine' (Map.toList b) a where combine' :: [(String, MetaValue)] -> Map String MetaValue -> Maybe Subst combine' [] acc = Just (Subst acc) combine' ((key, value) : rest) acc = case Map.lookup key acc of Just found | found == value -> combine' rest acc | otherwise -> Nothing Nothing -> combine' 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 ptn tgt | ptn == tgt = [substEmpty] | otherwise = []matchBinding :: Binding -> Binding -> [Subst]matchBinding (BiVoid pattr) (BiVoid tattr) = matchAttribute pattr tattrmatchBinding (BiDelta pbts) (BiDelta tbts) | pbts == tbts = [substEmpty] | otherwise = []matchBinding (BiMetaDelta meta) (BiDelta tBts) = [substSingle meta (MvBytes tBts)]matchBinding (BiLambda pFunc) (BiLambda tFunc) | pFunc == tFunc = [substEmpty] | otherwise = []matchBinding (BiMetaLambda meta) (BiLambda tFunc) = [substSingle meta (MvFunction tFunc)]matchBinding (BiTau pattr pexp) (BiTau tattr texp) = combineMany (matchAttribute pattr tattr) (matchExpression pexp texp)matchBinding _ _ = []-- Match bindings with orderingmatchBindings :: [Binding] -> [Binding] -> [Subst]matchBindings [] [] = [substEmpty]matchBindings [] _ = []matchBindings ((BiMeta name) : pbs) tbs = do let splits = [splitAt idx tbs | idx <- [0 .. length tbs]] catMaybes [ combine (substSingle name (MvBindings before)) subst | (before, after) <- splits, subst <- matchBindings pbs after ]matchBindings (pb : pbs) (tb : tbs) = combineMany (matchBinding pb tb) (matchBindings pbs tbs)matchBindings _ _ = []-- Recursively go through given target expression and try to find-- the head expression which matches to given pattern.-- If there's one - build the list of all the tail operations after head expression.-- The tail operations may be only dispatches or applicationstailExpressions :: Expression -> Expression -> ([Subst], [Tail])tailExpressions ptn tgt = do let (substs, tails) = tailExpressionsReversed ptn tgt (substs, reverse tails) where tailExpressionsReversed :: Expression -> Expression -> ([Subst], [Tail]) tailExpressionsReversed ptn' tgt' = case matchExpression ptn' tgt' of [] -> case tgt' of ExDispatch expr attr -> do let (substs, tails) = tailExpressionsReversed ptn' expr (substs, TaDispatch attr : tails) ExApplication expr tau -> do let (substs, tails) = tailExpressionsReversed ptn' expr (substs, TaApplication tau : tails) _ -> ([], []) substs -> (substs, [])matchExpression :: Expression -> Expression -> [Subst]matchExpression (ExMeta meta) tgt = [substSingle meta (MvExpression tgt)]matchExpression ExThis ExThis = [substEmpty]matchExpression ExGlobal ExGlobal = [substEmpty]matchExpression ExTermination ExTermination = [substEmpty]matchExpression (ExFormation pbs) (ExFormation tbs) = matchBindings pbs tbsmatchExpression (ExDispatch pexp pattr) (ExDispatch texp tattr) = combineMany (matchAttribute pattr tattr) (matchExpression pexp texp)matchExpression (ExApplication pexp pbd) (ExApplication texp tbd) = combineMany (matchExpression pexp texp) (matchBinding pbd tbd)matchExpression (ExMetaTail exp meta) tgt = case tailExpressions exp tgt of ([], _) -> [] (substs, tails) -> combineMany substs [substSingle meta (MvTail tails)]matchExpression _ _ = []-- Deep match pattern to expression inside bindingmatchBindingExpression :: Binding -> Expression -> [Subst]matchBindingExpression (BiTau _ texp) ptn = matchExpressionDeep ptn texpmatchBindingExpression _ _ = []-- Match expression with deep nested expression(s) matchingmatchExpressionDeep :: Expression -> Expression -> [Subst]matchExpressionDeep ptn tgt = do let matched = matchExpression ptn tgt deep = case tgt of ExFormation bds -> concatMap (`matchBindingExpression` ptn) bds ExDispatch exp _ -> matchExpressionDeep ptn exp ExApplication exp tau -> matchExpressionDeep ptn exp ++ matchBindingExpression tau ptn _ -> [] matched ++ deepmatchProgram :: Expression -> Program -> [Subst]matchProgram ptn (Program exp) = matchExpressionDeep ptn exp