@@ -15,15 +15,20 @@ import Control.Monad.Freer.Internal (handleRelayS)
15import Control.Monad.Freer.Reader
15import Control.Monad.Freer.Reader
16import Control.Monad.Freer.State
16import Control.Monad.Freer.State
17import Control.Monad.Freer.Writer
17import Control.Monad.Freer.Writer
18import Data.Fix
19import Data.Fix
19import Data.Foldable
20import Data.Foldable
20import Data.Functor
21import Data.Functor
21import Data.Functor.Classes
22import Data.Functor.Classes
22import Data.List.NonEmpty (NonEmpty (..))
24import Data.List.NonEmpty (NonEmpty (..))
23import qualified Data.List.NonEmpty as NE
25import qualified Data.List.NonEmpty as NE
26+import Data.Map.Shifted.Strict (ShiftedMap (..))
27+import qualified Data.Map.Shifted.Strict as SM
24import Data.Map.Strict (Map)
28import Data.Map.Strict (Map)
25import qualified Data.Map.Strict as M
29import qualified Data.Map.Strict as M
26import Data.MonoTraversable
30import Data.MonoTraversable
27import Data.Range (Range (..))
32import Data.Range (Range (..))
28import qualified Data.Range as R
33import qualified Data.Range as R
29import Data.Sequence (Seq (..))
34import Data.Sequence (Seq (..))
@@ -174,16 +179,22 @@ isClosed (x :-> y) = isClosed x && isClosed y
174isClosed (List x) = isClosed x
179isClosed (List x) = isClosed x
175isClosed (NAttrSet xs) = all isClosed . M.elems $ xs
180isClosed (NAttrSet xs) = all isClosed . M.elems $ xs
182+newtype DeBrujinContext = DeBrujinContext (Sum Int)
183+ deriving newtype (Eq, Ord, Show, Semigroup, Monoid, Group, Num)
185+instance Act DeBrujinContext DeBrujin where
186+ act (DeBrujinContext (Sum n)) (DeBrujin i j) = DeBrujin (i + n) j
177close :: Range TypeVariable -> NType -> NType
188close :: Range TypeVariable -> NType -> NType
178close range =
189close range =
180− . evalState @(Map TypeVariable DeBrujin) M.empty
191+ . evalState @(ShiftedMap DeBrujinContext TypeVariable DeBrujin) mempty
181 . runReader @Int 0
192 . runReader @Int 0
182 . evalState @Int 0
193 . evalState @Int 0
185 -- State is the sequential number, Reader is the binding context number
196 -- State is the sequential number, Reader is the binding context number
186− close' :: NType -> Eff '[State Int, Reader Int, State (Map TypeVariable DeBrujin)] NType
197+ close' :: NType -> Eff '[State Int, Reader Int, State (ShiftedMap DeBrujinContext TypeVariable DeBrujin)] NType
187 close' x@(NAtomic _) = return x
198 close' x@(NAtomic _) = return x
188 close' x@(NBrujin _) = return x
199 close' x@(NBrujin _) = return x
189 close' (NTypeVariable tv) | tv `R.member` range = NBrujin <$> newbrujin tv
200 close' (NTypeVariable tv) | tv `R.member` range = NBrujin <$> newbrujin tv
@@ -192,18 +203,18 @@ close range =
192 close' (NAttrSet x) = bndCtx $ NAttrSet <$> traverse close' x
203 close' (NAttrSet x) = bndCtx $ NAttrSet <$> traverse close' x
194 bndCtx m = do
205 bndCtx m = do
195− modify @(Map TypeVariable DeBrujin) (fmap (\(DeBrujin i j) -> DeBrujin (i + 1) j))
206+ modify @(ShiftedMap DeBrujinContext TypeVariable DeBrujin) (SM.shift 1)
196 x <- local @Int (+ 1) m
207 x <- local @Int (+ 1) m
197− modify @(Map TypeVariable DeBrujin) (fmap (\(DeBrujin i j) -> DeBrujin (i - 1) j))
208+ modify @(ShiftedMap DeBrujinContext TypeVariable DeBrujin) (SM.shift (-1))
199 newbrujin t = do
210 newbrujin t = do
200− get @(Map TypeVariable DeBrujin) <&> M.lookup t >>= \case
211+ get @(ShiftedMap DeBrujinContext TypeVariable DeBrujin) <&> SM.lookup t >>= \case
201 Nothing -> do
212 Nothing -> do
204 modify @Int (+ 1)
215 modify @Int (+ 1)
205 let x = DeBrujin i j
216 let x = DeBrujin i j
206− modify @(Map TypeVariable DeBrujin) (M.insert t x)
217+ modify @(ShiftedMap DeBrujinContext TypeVariable DeBrujin) (SM.insert t x)
208 Just x -> return x
219 Just x -> return x