Skip to content

Commit

Permalink
Add new functions copyVar and copyVarList
Browse files Browse the repository at this point in the history
  • Loading branch information
ijaketak committed Oct 8, 2023
1 parent 4ec752e commit 4a2763f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### 0.3 (still not released)

* Add new functions `copyVar` and `copyVarList`.
* `newVarList`: Refactoring.

### 0.2.1 -- 2023-10-09
Expand Down
17 changes: 15 additions & 2 deletions src/Data/Binder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ module Data.Binder
, nameOf
, boxVar
, newVar
, copyVar
, isClosed
, occur
-- ** Box
Expand Down Expand Up @@ -69,6 +70,7 @@ module Data.Binder
, namesOf
, boxVarList
, newVarList
, copyVarList
-- * Binder for list
, BinderList
, binderList'Names
Expand Down Expand Up @@ -164,16 +166,24 @@ boxVar :: Var m a -> Box m a
boxVar x = x ^. var'Box

-- | Create a new variable with given name.
newVar :: forall m a. MonadNumbering m => Text -> (Var m a -> m a) -> m (Var m a)
newVar :: MonadNumbering m => Text -> (Var m a -> m a) -> m (Var m a)
newVar name mkFree = do
i <- numbering
buildVar i name mkFree

copyVar :: MonadNumbering m => Var m b -> (Var m a -> m a) -> m (Var m a)
copyVar x mkFree = buildVar (x ^. var'Key) (x ^. var'Name) mkFree

buildVar :: MonadNumbering m => Numbering m -> Text -> (Var m a -> m a)
-> m (Var m a)
buildVar i name mkFree =
let x = let b = Box'Env
(M.singleton i $ AnyVar x)
(Closure $ \env ->
let f (AnyOne y) = pure $ unsafeCoerce y
in f $ fromJust $ M.lookup i env)
in Var i $ VarBody name mkFree b
return x
in return x


-- | 'Box' is closed if it exposes no free variables.
Expand Down Expand Up @@ -319,6 +329,9 @@ boxVarList = fmap $ view var'Box
newVarList :: MonadNumbering m => [Text] -> (Var m a -> m a) -> m (VarList m a)
newVarList names mkFree = flip traverse names $ flip newVar mkFree

copyVarList :: MonadNumbering m => VarList m b -> (Var m a -> m a) -> m (VarList m a)
copyVarList xs mkFree = flip traverse xs $ flip copyVar mkFree


-- | Essentially, @BinderList a m b@ means @[a] -> m b@.
data BinderList a m b = BinderList
Expand Down

0 comments on commit 4a2763f

Please sign in to comment.