1-----------------------------------------------------------------------------
3-- Module : Control.Monad.Unify
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
14-----------------------------------------------------------------------------
16{-# LANGUAGE GeneralizedNewtypeDeriving #-}
17{-# LANGUAGE FlexibleInstances #-}
18{-# LANGUAGE FlexibleContexts #-}
19{-# LANGUAGE DeriveDataTypeable #-}
20{-# LANGUAGE MultiParamTypeClasses #-}
21{-# LANGUAGE FunctionalDependencies #-}
22{-# LANGUAGE UndecidableInstances #-}
24module Control.Monad.Unify where
28import Control.Applicative
29import Control.Monad.State
30import Control.Monad.Error.Class (MonadError(..))
32import Data.HashMap.Strict as M
35-- Untyped unification variables
40-- A type which can contain unification variables
43 unknown :: Unknown -> t
44 isUnknown :: t -> Maybe Unknown
45 unknowns :: t -> [Unknown]
46 ($?) :: Substitution t -> t -> t
49-- Identifies types which support unification
51class (Partial t) => Unifiable m t | t -> m where
52 (=?=) :: t -> t -> UnifyT t m ()
55-- A substitution maintains a mapping from unification variables to their values
57data Substitution t = Substitution { runSubstitution :: M.HashMap Int t }
59instance (Partial t) => Monoid (Substitution t) where
60 mempty = Substitution M.empty
61 s1 `mappend` s2 = Substitution $
62 M.map (s2 $?) (runSubstitution s1) `M.union`
63 M.map (s1 $?) (runSubstitution s2)
66-- State required for type checking
68data UnifyState t = UnifyState {
70 -- The next fresh unification variable
74 -- The current substitution
76 , unifyCurrentSubstitution :: Substitution t
80-- An empty @UnifyState@
82defaultUnifyState :: (Partial t) => UnifyState t
83defaultUnifyState = UnifyState 0 mempty
86-- A class for errors which support unification errors
88class UnificationError t e where
89 occursCheckFailed :: t -> e
92-- The type checking monad, which provides the state of the type checker, and error reporting capabilities
94newtype UnifyT t m a = UnifyT { unUnify :: StateT (UnifyState t) m a }
95 deriving (Functor, Monad, Applicative, Alternative, MonadPlus)
97instance (MonadState s m) => MonadState s (UnifyT t m) where
98 get = UnifyT . lift $ get
99 put = UnifyT . lift . put
101instance (MonadError e m) => MonadError e (UnifyT t m) where
102 throwError = UnifyT . throwError
103 catchError e f = UnifyT $ catchError (unUnify e) (unUnify . f)
106-- Run a computation in the Unify monad, failing with an error, or succeeding with a return value and the new next unification variable
108runUnify :: UnifyState t -> UnifyT t m a -> m (a, UnifyState t)
109runUnify s = flip runStateT s . unUnify
112-- Substitute a single unification variable
114substituteOne :: (Partial t) => Unknown -> t -> Substitution t
115substituteOne u t = Substitution $ M.singleton u t
118-- Replace a unification variable with the specified value in the current substitution
120(=:=) :: (UnificationError t e, Monad m, MonadError e m, Unifiable m t) => Unknown -> t -> UnifyT t m ()
123 let sub = unifyCurrentSubstitution st
126 let current = sub $? unknown u
127 case isUnknown current of
128 Just u1 | u1 == u -> return ()
130 UnifyT $ modify $ \s -> s { unifyCurrentSubstitution = substituteOne u t <> unifyCurrentSubstitution s }
133-- Perform the occurs check, to make sure a unification variable does not occur inside a value
135occursCheck :: (UnificationError t e, Monad m, MonadError e m, Partial t) => Unknown -> t -> UnifyT t m ()
138 Nothing -> when (u `elem` unknowns t) $ UnifyT . lift . throwError $ occursCheckFailed t
142-- Generate a fresh untyped unification variable
144fresh' :: (Monad m) => UnifyT t m Unknown
147 UnifyT $ modify $ \s -> s { unifyNextVar = succ (unifyNextVar s) }
148 return $ unifyNextVar st
151-- Generate a fresh unification variable at a specific type
153fresh :: (Monad m, Partial t) => UnifyT t m t
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list