packages feed

phino-0.0.140 / src / Matcher.hs

-- 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)import Data.Text (Text)-- Meta value-- The right part of substitutiondata MetaValue  = MvAttribute Attribute -- !t  | MvIndex Int -- α𝑖  | MvBytes Bytes -- !b  | MvBindings [Binding] -- !B  | MvFunction Function -- !F  | MvExpression Expression -- !e  deriving (Eq, Show)-- The left-hand side of a substitution: a meta-variable the rule author named-- and may reference from a result, or an anonymous slot that only the pattern-- it was written in can addressdata Meta  = Named Text  | Anon Slot  deriving (Eq, Ord, Show)-- Substitution-- Shows the match of meta variable to meta valuenewtype Subst = Subst (Map Meta MetaValue)  deriving (Eq, Show)-- A way to match a pattern expression against a target expression, yielding-- the substitutions under which they agree.type MatchExpressionFunc = Expression -> Expression -> [Subst]-- Empty substitutionsubstEmpty :: SubstsubstEmpty = Subst Map.empty-- Singleton substitution with one (key -> value) pairsubstSingle :: Text -> MetaValue -> SubstsubstSingle key value = Subst (Map.singleton (Named key) value)-- Singleton substitution binding one anonymous slotsubstSlot :: Slot -> MetaValue -> SubstsubstSlot slot value = Subst (Map.singleton (Anon slot) 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) = 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 = []matchAlpha :: Alpha -> Alpha -> [Subst]matchAlpha (AlMeta meta) (Alpha idx) = [substSingle meta (MvIndex idx)]matchAlpha (AlAny slot) (Alpha idx) = [substSlot slot (MvIndex idx)]matchAlpha ptn tgt  | ptn == tgt = [substEmpty]  | otherwise = []-- A λ meta stands for any λ name at all — an ordinary one and a symbol alike,-- since a symbol is a name nothing answers and not a variable of the rule-- language (see 'FnSymbol'). Every other pair matches only itself.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 = []-- Whether a λ function is a name a program wrote rather than a meta-variable-- a rule wrote.named :: Function -> Boolnamed (Function _) = Truenamed (FnSymbol _) = Truenamed _ = FalsematchBinding :: 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 (BiTau pattr pexp) (BiTau tattr texp) = combineMany (matchAttribute pattr tattr) (matchExpression' pexp texp)matchBinding _ _ = []matchArgument :: Argument -> Argument -> [Subst]matchArgument (ArTau pattr pexp) (ArTau tattr texp) = combineMany (matchAttribute pattr tattr) (matchExpression' pexp texp)matchArgument (ArAlpha palpha pexp) (ArAlpha talpha texp) = combineMany (matchAlpha palpha talpha) (matchExpression' pexp texp)matchArgument _ _ = []-- Match bindings with orderingmatchBindings :: [Binding] -> [Binding] -> [Subst]matchBindings [] [] = [substEmpty]matchBindings [] _ = []matchBindings ((BiMeta name) : pbs) tbs = matchBindingsMeta (substSingle name) pbs tbsmatchBindings ((BiAny slot) : pbs) tbs = matchBindingsMeta (substSlot slot) pbs tbsmatchBindings (pb : pbs) (tb : tbs) = combineMany (matchBinding pb tb) (matchBindings pbs tbs)matchBindings _ _ = []-- A meta binding stands for any leading run of the target bindings, so every-- way of splitting the target into that run and the rest is tried. The rest is-- carried down one binding at a time instead of being cut out of the target-- anew at every index, which is what made a formation of N bindings cost N-- walks of itself rather than one, and the run itself is put together only-- where the pattern after it matched, so a split the pattern throws away costs-- nothing to name. A meta binding with nothing after it takes the whole rest in-- one step: the pattern is out of bindings, so the only split that matches is-- the one leaving nothing behind (#1316).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 pbs after]        ++ case after of          [] -> []          (tb : rest) -> go (tb : before) restmatchExpression' :: 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' (ExFormation pbs) (ExFormation tbs) = matchBindings pbs tbsmatchExpression' (ExDispatch pexp pattr) (ExDispatch texp tattr) = combineMany (matchAttribute pattr tattr) (matchExpression' pexp texp)matchExpression' (ExApplication pexp parg) (ExApplication texp targ) = combineMany (matchExpression' pexp texp) (matchArgument parg targ)matchExpression' (ExPhiAgain prefix idx expr) (ExPhiAgain prefix' idx' expr')  | prefix == prefix' && idx == idx' = matchExpression' expr expr'  | otherwise = []matchExpression' (ExPhiMeet prefix idx expr) (ExPhiMeet prefix' idx' expr')  | prefix == prefix' && idx == idx' = matchExpression' expr expr'  | otherwise = []matchExpression' _ _ = []-- Deep match pattern to expression inside bindingmatchBindingExpression :: Binding -> Expression -> [Subst]matchBindingExpression (BiTau _ expr) ptn = matchExpressionDeep ptn exprmatchBindingExpression _ _ = []matchArgumentExpression :: Argument -> Expression -> [Subst]matchArgumentExpression (ArTau _ expr) ptn = matchExpressionDeep ptn exprmatchArgumentExpression (ArAlpha _ expr) ptn = matchExpressionDeep ptn expr-- Match expression with deep nested expression(s) matchingmatchExpressionDeep :: MatchExpressionFuncmatchExpressionDeep ptn tgt =  let matched = matchExpression' ptn tgt      deep = case tgt of        ExFormation bds -> concatMap (`matchBindingExpression` ptn) bds        ExDispatch expr _ -> matchExpressionDeep ptn expr        ExApplication expr arg -> matchExpressionDeep ptn expr ++ matchArgumentExpression arg ptn        _ -> []   in matched ++ deepmatchExpression :: MatchExpressionFuncmatchExpression = matchExpressionDeep