|
| 1 | +{-# LANGUAGE LambdaCase #-} |
| 2 | + |
| 3 | +module LambdaBuffers.Compiler.TypeClass.Solve (solveM, solve, Overlap (..)) where |
| 4 | + |
| 5 | +import LambdaBuffers.Compiler.TypeClass.Pat ( |
| 6 | + Pat (AppP, DecP, ProdP, RecP, RefP, SumP, VarP, (:*), (:=)), |
| 7 | + matches, |
| 8 | + ) |
| 9 | +import LambdaBuffers.Compiler.TypeClass.Rules ( |
| 10 | + Class (csupers), |
| 11 | + Constraint (C), |
| 12 | + Rule ((:<=)), |
| 13 | + mapPat, |
| 14 | + ruleHeadClass, |
| 15 | + ruleHeadPat, |
| 16 | + ) |
| 17 | + |
| 18 | +import Control.Monad.Except (throwError) |
| 19 | +import Control.Monad.Reader (ReaderT, runReaderT) |
| 20 | +import Control.Monad.Reader.Class (MonadReader (ask)) |
| 21 | +import Control.Monad.Writer.Class (MonadWriter (tell)) |
| 22 | +import Control.Monad.Writer.Strict (WriterT, execWriterT) |
| 23 | +import Data.Foldable (traverse_) |
| 24 | +import Data.List (foldl') |
| 25 | +import Data.Set qualified as S |
| 26 | +import Data.Text (Text) |
| 27 | + |
| 28 | +{- Pattern/Template/Unification variable substitution. |
| 29 | + Given a string that represents a variable name, |
| 30 | + and a type to instantiate variables with that name to, |
| 31 | + performs the instantiation |
| 32 | +-} |
| 33 | +subV :: Text -> Pat -> Pat -> Pat |
| 34 | +subV varNm t = \case |
| 35 | + var@(VarP v) -> if v == varNm then t else var |
| 36 | + x :* xs -> subV varNm t x :* subV varNm t xs |
| 37 | + l := x -> subV varNm t l := subV varNm t x |
| 38 | + ProdP xs -> ProdP (subV varNm t xs) |
| 39 | + RecP xs -> RecP (subV varNm t xs) |
| 40 | + SumP xs -> SumP (subV varNm t xs) |
| 41 | + AppP t1 t2 -> AppP (subV varNm t t1) (subV varNm t t2) |
| 42 | + RefP n x -> RefP (subV varNm t n) (subV varNm t x) |
| 43 | + DecP a b c -> DecP (subV varNm t a) (subV varNm t b) (subV varNm t c) |
| 44 | + other -> other |
| 45 | + |
| 46 | +{- Performs substitution on an entire instance (the first argument) given the |
| 47 | + concrete types from a Pat (the second argument). |
| 48 | + Note that ONLY PatVars which occur in the Instance *HEAD* are replaced, though they |
| 49 | + are replaced in the instance superclasses as well (if they occur there). |
| 50 | +-} |
| 51 | +subst :: Rule -> Pat -> Rule |
| 52 | +subst cst@(C _ t :<= _) ty = mapPat (go (getSubs t ty)) cst |
| 53 | + where |
| 54 | + go :: [(Text, Pat)] -> Pat -> Pat |
| 55 | + go subs tty = |
| 56 | + let noflip p1 p2 = uncurry subV p2 p1 |
| 57 | + in foldl' noflip tty subs |
| 58 | + |
| 59 | +{- Given two patterns (which are hopefully structurally similar), gather a list of all substitutions |
| 60 | + from the PatVars in the first argument to the concrete types (hopefully!) in the second argument |
| 61 | +-} |
| 62 | +getSubs :: Pat -> Pat -> [(Text, Pat)] -- should be a set, whatever |
| 63 | +getSubs (VarP s) t = [(s, t)] |
| 64 | +getSubs (x :* xs) (x' :* xs') = getSubs x x' <> getSubs xs xs' |
| 65 | +getSubs (l := t) (l' := t') = getSubs l l' <> getSubs t t' |
| 66 | +getSubs (ProdP xs) (ProdP xs') = getSubs xs xs' |
| 67 | +getSubs (RecP xs) (RecP xs') = getSubs xs xs' |
| 68 | +getSubs (SumP xs) (SumP xs') = getSubs xs xs' |
| 69 | +getSubs (AppP t1 t2) (AppP t1' t2') = getSubs t1 t1' <> getSubs t2 t2' |
| 70 | +getSubs (RefP n t) (RefP n' t') = getSubs n n' <> getSubs t t' |
| 71 | +getSubs (DecP a b c) (DecP a' b' c') = getSubs a a' <> getSubs b b' <> getSubs c c' |
| 72 | +getSubs _ _ = [] |
| 73 | + |
| 74 | +-- NoMatch isn't fatal but OverlappingMatches is (i.e. we need to stop when we encounter it) |
| 75 | +data MatchError |
| 76 | + = NoMatch |
| 77 | + | OverlappingMatches [Rule] |
| 78 | + |
| 79 | +-- for SolveM, since we catch NoMatch |
| 80 | +data Overlap = Overlap Constraint [Rule] |
| 81 | + deriving stock (Show, Eq) |
| 82 | + |
| 83 | +selectMatchingInstance :: Pat -> Class -> [Rule] -> Either MatchError Rule |
| 84 | +selectMatchingInstance p c rs = case filter matchPatAndClass rs of |
| 85 | + [] -> Left NoMatch |
| 86 | + [r] -> Right r |
| 87 | + overlaps -> Left $ OverlappingMatches overlaps |
| 88 | + where |
| 89 | + matchPatAndClass :: Rule -> Bool |
| 90 | + matchPatAndClass r = |
| 91 | + ruleHeadClass r == c |
| 92 | + && ruleHeadPat r |
| 93 | + `matches` p |
| 94 | + |
| 95 | +type SolveM = ReaderT [Rule] (WriterT (S.Set Constraint) (Either Overlap)) |
| 96 | + |
| 97 | +{- Given a list of instances (the initial scope), determines whether we can derive |
| 98 | + an instance of the Class argument for the Pat argument. A result of [] indicates that there are |
| 99 | + no remaining subgoals and that the constraint has been solved. |
| 100 | + NOTE: At the moment this handles superclasses differently than you might expect - |
| 101 | + instead of assuming that the superclasses for all in-scope classes are defined, |
| 102 | + we check that those constraints can be solved before affirmatively judging that the |
| 103 | + target constraint has been solved. I *think* that makes sense in this context (whereas in Haskell |
| 104 | + it doesn't b/c it's *impossible* to have `instance Foo X` if the definition of Foo is |
| 105 | + `class Bar y => Foo y` without an `instance Bar X`) |
| 106 | +-} |
| 107 | +solveM :: Constraint -> SolveM () |
| 108 | +solveM cst@(C c pat) = |
| 109 | + ask >>= \inScope -> |
| 110 | + -- First, we look for the most specific instance... |
| 111 | + case selectMatchingInstance pat c inScope of |
| 112 | + Left e -> case e of |
| 113 | + NoMatch -> tell $ S.singleton cst |
| 114 | + OverlappingMatches olps -> throwError $ Overlap cst olps |
| 115 | + -- If there is, we substitute the argument of the constraint to be solved into the matching rules |
| 116 | + Right rule -> case subst rule pat of |
| 117 | + -- If there are no additional constraints on the rule, we try to solve the superclasses |
| 118 | + C _ p :<= [] -> solveClassesFor p (csupers c) |
| 119 | + -- If there are additional constraints on the rule, we try to solve them |
| 120 | + C _ _ :<= is -> do |
| 121 | + traverse_ solveM is |
| 122 | + solveClassesFor pat (csupers c) |
| 123 | + where |
| 124 | + -- NOTE(@bladyjoker): The version w/ flip is more performant... |
| 125 | + -- Given a Pat and a list of Classes, attempt to solve the constraints |
| 126 | + -- constructed from the Pat and each Class |
| 127 | + solveClassesFor :: Pat -> [Class] -> SolveM () |
| 128 | + solveClassesFor p = traverse_ (\cls -> solveM (C cls p)) |
| 129 | + |
| 130 | +solve :: [Rule] -> Constraint -> Either Overlap [Constraint] |
| 131 | +solve rules c = fmap S.toList $ execWriterT $ runReaderT (solveM c) rules |
0 commit comments