Skip to content

Commit

Permalink
Control ZipMatch for ground types with ZipMatchK for Type
Browse files Browse the repository at this point in the history
  • Loading branch information
fizruk committed Oct 22, 2024
1 parent 677bd1c commit 85f7d16
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions haskell/free-foil/src/Control/Monad/Free/Foil/Generic.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,19 @@ class ZipMatchK (f :: k) where
=> Mappings as bs cs -> f :@@: as -> f :@@: bs -> Maybe (f :@@: cs)
zipMatchWithK = genericZipMatchWithK @f @as @bs @cs

zipMatchViaEq :: Eq a => Mappings as bs cs -> a -> a -> Maybe a
zipMatchViaEq _ x y
| x == y = Just x
| otherwise = Nothing

zipMatchViaChooseLeft :: Mappings as bs cs -> a -> a -> Maybe a
zipMatchViaChooseLeft _ x _ = Just x

-- instance ZipMatchK (,) -- missing GenericK instance upstream
instance ZipMatchK []
instance ZipMatchK Maybe
instance ZipMatchK Either
instance ZipMatchK (Either a)
instance ZipMatchK a => ZipMatchK (Either a)

type ReqsZipMatch f as bs = ReqsZipMatchWith f as bs (ZipLoT as bs)
class GZipMatch (f :: LoT k -> Type) where
Expand Down Expand Up @@ -137,9 +145,9 @@ instance ApplyMappings v => ZipMatchFields (Var v) where
type ReqsZipMatchFieldsWith (Var v) as bs cs = () -- InterpretVar v cs ~ (InterpretVar v as, InterpretVar v bs))
zipMatchFieldsWith g (Field x) (Field y) = Field <$> applyMappings @_ @v g x y

instance ZipMatchFields (Kon k) where
instance ZipMatchK k => ZipMatchFields (Kon k) where
type ReqsZipMatchFieldsWith (Kon k) as bs cs = ()
zipMatchFieldsWith _ (Field l) _ = Just (Field l)
zipMatchFieldsWith _ (Field l) (Field r) = Field <$> zipMatchWithK @_ @k M0 l r

instance (ZipMatchFields t, ZipMatchK k) => ZipMatchFields (Kon k :@: t) where
type ReqsZipMatchFieldsWith (Kon k :@: t) as bs cs = ReqsZipMatchFieldsWith t as bs cs
Expand Down

0 comments on commit 85f7d16

Please sign in to comment.