From a16a4a764f8ae2ee53a8b0aa83f3fe531f7707a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Madeleine=20Sydney=20=C5=9Alaga?= Date: Fri, 11 Sep 2026 17:51:24 -0600 Subject: [PATCH] --- src/Gyehoek/Scheme/Expand.hs | 51 ++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/Gyehoek/Scheme/Expand.hs b/src/Gyehoek/Scheme/Expand.hs index 500f542..963fae2 100644 --- a/src/Gyehoek/Scheme/Expand.hs +++ b/src/Gyehoek/Scheme/Expand.hs @@ -17,6 +17,7 @@ import qualified Data.List.NonEmpty as NE import Data.Monoid (Ap(Ap, getAp)) import Gyehoek.Sexp.Grammar.Base ((:-)(..)) import Control.Lens.Extras (is) +import Control.Applicative (Alternative(..)) data Scope = MkScope @@ -43,7 +44,10 @@ data Trans = MkTrans } deriving (Show, Generic) -data Rule = MkRule Pat Tem +data Rule = MkRule + { rhs :: Pat + , lhs :: Tem + } deriving (Show, Generic) data Pat @@ -196,25 +200,50 @@ instance S.DatumIso Tem where -- Just -- ... match :: Pat -> Datum -> Maybe (HashMap Name Datum) +match p = getAp . match' p + +match' :: Pat -> Datum -> Ap Maybe (HashMap Name Datum) -match PatWildcard _ = Just mempty +match' PatWildcard _ = pure mempty -match (PatVar x) e = Just $ H.singleton x e +match' (PatVar x) e = pure $ H.singleton x e -match (PatList ps Nothing Nothing) (List es) = matches ps es +match' (PatList ps Nothing Nothing) (List es) = matches ps es -match (PatList ps Nothing (Just p)) (DotList es e) = +match' (PatList ps (Just []) Nothing) (List es) = do + let (ps',p) = ps ^?! _Snoc + (r,rest) <- fold $ alignWith f ps' es + rest' <- rest + & fmap (fmap (fmap (:[])) . match' p) + & foldr (liftA2 $ H.unionWith (<>)) mempty + & fmap (fmap List) + pure $ r <> rest' + where + f (These a b) = (,[]) <$> match' a b + f (This a) = empty + f (That b) = pure (mempty,[b]) + +match' (PatList ps Nothing (Just p)) (DotList es e) = matches (p:|ps) (NE.cons e es) -match _ _ = _ +match' _ _ = _ + +matchPrefix + :: (Semialign f, Foldable f) + => f Pat -> f Datum -> Ap Maybe (HashMap Name Datum, List Datum) +matchPrefix ps es = fold $ alignWith f ps es + where + f (These a b) = (,[]) <$> match' a b + f (This a) = empty + f (That b) = pure (mempty,[b]) matches :: (Semialign f, Foldable f) - => f Pat -> f Datum -> Maybe (HashMap Name Datum) -matches ps es = getAp . foldMap Ap $ alignWith f ps es - where - f (These a b) = match a b - f _ = Nothing + => f Pat -> f Datum -> Ap Maybe (HashMap Name Datum) +matches ps es = fold $ alignWith matchThese ps es + +matchThese (These a b) = match' a b +matchThese _ = empty