-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MonadUnify #47
Open
kozross
wants to merge
6
commits into
main
Choose a base branch
from
koz/monad-unify
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
MonadUnify #47
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
packages: ./covenant.cabal | ||
|
||
tests: true | ||
|
||
test-show-details: direct | ||
|
||
package covenant | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
{-# LANGUAGE FunctionalDependencies #-} | ||
|
||
-- | Module: Control.Monad.Unify | ||
-- | ||
-- Unification, in the style of MicroKanren. In this system, /variables/ and | ||
-- /definitions/ (represented by type variables @var@ and @def@ throughout) are | ||
-- allowed to be different types for clarity. Furthermore, we use a CPS-like | ||
-- approach (similar to how 'local' works) to allow it to better integrate with | ||
-- Covenant's other systems. | ||
-- | ||
-- = See also | ||
-- | ||
-- * [MicroKanren | ||
-- paper](http://webyrd.net/scheme-2013/papers/HemannMuKanren2013.pdf) | ||
module Control.Monad.Unify | ||
( UnifyStatus (..), | ||
UnifyT, | ||
runUnifyT, | ||
MonadUnify (..), | ||
) | ||
where | ||
|
||
import Control.Monad (unless) | ||
import Control.Monad.State.Strict (StateT, gets, modify, runStateT) | ||
import Control.Monad.Trans (lift) | ||
import Control.Monad.Trans.Maybe (MaybeT, hoistMaybe, runMaybeT) | ||
import Data.EnumMap.Strict (EnumMap) | ||
import Data.EnumMap.Strict qualified as EnumMap | ||
import Data.Kind (Type) | ||
import Data.Set.NonEmpty (NESet) | ||
import Data.Set.NonEmpty qualified as NESet | ||
|
||
-- | @since 1.0.0 | ||
data UnifyStatus (var :: Type) (def :: Type) | ||
= Fresh | ||
| Equiv (NESet var) | ||
| Defined def | ||
deriving stock | ||
( -- | @since 1.0.0 | ||
Eq, | ||
-- | @since 1.0.0 | ||
Show | ||
) | ||
|
||
-- | @since 1.0.0 | ||
newtype UnifyT (var :: Type) (def :: Type) (m :: Type -> Type) (a :: Type) | ||
= UnifyT (StateT (EnumMap var (Either (NESet var) def)) (MaybeT m) a) | ||
deriving | ||
( -- | @since 1.0.0 | ||
Functor, | ||
-- | @since 1.0.0 | ||
Applicative, | ||
-- | @since 1.0.0 | ||
Monad | ||
) | ||
via StateT (EnumMap var (Either (NESet var) def)) (MaybeT m) | ||
|
||
-- | Execute all unifications. | ||
-- | ||
-- @since 1.0.0 | ||
runUnifyT :: | ||
forall (var :: Type) (def :: Type) (m :: Type -> Type) (a :: Type). | ||
(Monad m) => | ||
UnifyT var def m a -> | ||
m (Maybe (a, EnumMap var (Either (NESet var) def))) | ||
runUnifyT (UnifyT comp) = runMaybeT . runStateT comp $ EnumMap.empty | ||
|
||
-- | = Laws | ||
-- | ||
-- In the description below, @f@ is as follows: | ||
-- | ||
-- @ | ||
-- f v = case v of | ||
-- 'Fresh' -> 'pure' () | ||
-- 'Equiv' vs -> 'unify' v ('Left' ('NESet.findMin' vs)) | ||
-- 'Defined' x -> 'unify' v ('Right' x) | ||
-- @ | ||
-- | ||
-- 1. @'status' v '>>' 'status' v@ @=@ @'status' v@ | ||
-- 2. @'unify' v x '>>' 'unify' v x@ @=@ @'unify' v x@ | ||
-- 3. @'unify' v1 x '>>' 'unify' v2 y@ @=@ | ||
-- @'unify' v2 y '>>' 'unify' v1 x@ | ||
-- 3. @'unify' v1 ('Left' v2)@ @=@ @'unify' v2 ('Left' v1)@ | ||
-- 4. @'status' v '>>=' f@ @=@ @'pure' ()@ | ||
-- | ||
-- @since 1.0.0 | ||
class (Monad m) => MonadUnify var def m | m -> var def where | ||
-- | @since 1.0.0 | ||
status :: var -> m (UnifyStatus var def) | ||
|
||
-- | @since 1.0.0 | ||
unify :: var -> Either var def -> m () | ||
|
||
-- | @since 1.0.0 | ||
instance (Monad m, Enum var, Eq def, Ord var) => MonadUnify var def (UnifyT var def m) where | ||
{-# INLINEABLE status #-} | ||
status v = | ||
UnifyT $ do | ||
lookedUp <- gets (EnumMap.lookup v) | ||
pure $ case lookedUp of | ||
Nothing -> Fresh | ||
Just (Left vars) -> Equiv vars | ||
Just (Right x) -> Defined x | ||
{-# INLINEABLE unify #-} | ||
unify v x = UnifyT $ do | ||
lookedUp <- gets (EnumMap.lookup v) | ||
case lookedUp of | ||
Nothing -> case x of | ||
Left v' -> do | ||
lookedUp' <- gets (EnumMap.lookup v') | ||
modify $ case lookedUp' of | ||
Nothing -> | ||
EnumMap.insert v (Left . NESet.singleton $ v') . EnumMap.insert v' (Left . NESet.singleton $ v) | ||
Just (Left vars) -> \acc -> | ||
let newEC = NESet.insert v vars | ||
in NESet.foldl' (go newEC) acc newEC | ||
Just (Right def) -> EnumMap.insert v (Right def) | ||
Right x' -> modify (EnumMap.insert v (Right x')) | ||
Just (Left vars) -> case x of | ||
Left v' -> | ||
let newEC = NESet.insert v' vars | ||
in modify (\acc -> NESet.foldl' (go newEC) acc newEC) | ||
Right x' -> modify (\acc -> NESet.foldl' (go2 x') acc vars) | ||
Just (Right def) -> case x of | ||
Left v' -> do | ||
lookedUp' <- gets (EnumMap.lookup v') | ||
case lookedUp' of | ||
Nothing -> modify (EnumMap.insert v' (Right def)) | ||
Just (Left vars) -> | ||
let newEC = NESet.insert v' vars | ||
in modify (\acc -> NESet.foldl' (go newEC) acc newEC) | ||
Just (Right def') -> unless (def == def') (lift . hoistMaybe $ Nothing) | ||
Right x' -> unless (def == x') (lift . hoistMaybe $ Nothing) | ||
where | ||
go :: | ||
NESet var -> | ||
EnumMap var (Either (NESet var) def) -> | ||
var -> | ||
EnumMap var (Either (NESet var) def) | ||
go newEC acc v' = EnumMap.insert v' (Left newEC) acc | ||
go2 :: def -> EnumMap var (Either (NESet var) def) -> var -> EnumMap var (Either (NESet var) def) | ||
go2 x' acc v' = EnumMap.insert v' (Right x') acc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module Main (main) where | ||
|
||
import Control.Monad.Unify | ||
( UnifyStatus (Defined, Fresh), | ||
UnifyT, | ||
runUnifyT, | ||
status, | ||
unify, | ||
) | ||
import Data.Functor.Identity (Identity, runIdentity) | ||
import Data.Kind (Type) | ||
import Test.QuickCheck | ||
( Arbitrary (arbitrary, shrink), | ||
Property, | ||
forAllShrinkShow, | ||
(===), | ||
) | ||
import Test.QuickCheck.Poly (B) | ||
import Test.Tasty (adjustOption, defaultMain, testGroup) | ||
import Test.Tasty.QuickCheck (QuickCheckTests, testProperty) | ||
|
||
main :: IO () | ||
main = | ||
defaultMain . adjustOption moreTests . testGroup "MonadUnify" $ | ||
[ testProperty "Unify fresh with definition" propFreshDef, | ||
testProperty "Fresh variables' status agrees" propFreshStatus | ||
] | ||
where | ||
-- Note (Koz, 18/02/2025): By default, QuickCheck runs very few tests for | ||
-- any given property (100). This ensures that we run a sensible number of | ||
-- tests, while not blocking us from asking for more via the CLI. | ||
moreTests :: QuickCheckTests -> QuickCheckTests | ||
moreTests = max 10_000 | ||
|
||
-- Properties | ||
|
||
propFreshDef :: Property | ||
propFreshDef = forAllShrinkShow arbitrary shrink show $ \(v :: EnumA, def :: B) -> | ||
let result = runTestM (unify v (Right def) >> status v) | ||
in result === Just (Defined def) | ||
|
||
propFreshStatus :: Property | ||
propFreshStatus = forAllShrinkShow arbitrary shrink show $ \(v :: EnumA) -> | ||
let result = runTestM (status v) | ||
in result === Just Fresh | ||
|
||
-- Helpers | ||
|
||
newtype EnumA = EnumA Word | ||
deriving (Eq, Ord, Enum, Arbitrary) via Word | ||
deriving stock (Show) | ||
|
||
runTestM :: forall (a :: Type). UnifyT EnumA B Identity a -> Maybe a | ||
runTestM = fmap fst . runIdentity . runUnifyT |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why dropping the QuickCheck constraints?