-
Notifications
You must be signed in to change notification settings - Fork 452
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
Parametric modular interpretation #659
Changes from all commits
38ba7d8
14864b6
9a13a80
053b62f
14ceed9
a92be35
d1c183e
c4e379b
0826ca4
dd7319d
306239f
27b2b86
2422a57
4115251
d1d349e
3062b20
79e34ea
6118b05
4a8573e
8e0b773
bdfd7e1
e9e372d
f8aadac
3da958d
e1dade3
777357d
81c778b
6be2ff0
39e643d
db9e97d
841c179
55c349b
7696adf
68f823e
3cd8945
b5b39bd
26655c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,7 +22,6 @@ newtype EnvC m a = EnvC { runEnv :: m a } | |
instance Algebra sig m | ||
=> Algebra (Env Name :+: sig) (EnvC m) where | ||
alg hdl sig ctx = case sig of | ||
L (Alloc name) -> pure (name <$ ctx) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should never have been here. |
||
L (Bind _ _ m) -> hdl (m <$ ctx) | ||
L (Lookup name) -> pure (Just name <$ ctx) | ||
R other -> EnvC (alg (runEnv . hdl) other ctx) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
module Analysis.Carrier.Fail.WithLoc | ||
( -- * Fail carrier | ||
runFail | ||
, FailC(..) | ||
-- * Fail effect | ||
, module Control.Effect.Fail | ||
) where | ||
|
||
import Analysis.Reference | ||
import Control.Algebra | ||
import Control.Applicative | ||
import Control.Carrier.Error.Either | ||
import Control.Effect.Fail | ||
import Control.Effect.Reader | ||
import Prelude hiding (fail) | ||
|
||
-- Fail carrier | ||
|
||
runFail :: FailC m a -> m (Either (Reference, String) a) | ||
runFail = runError . runFailC | ||
|
||
newtype FailC m a = FailC { runFailC :: ErrorC (Reference, String) m a } | ||
deriving (Alternative, Applicative, Functor, Monad) | ||
|
||
instance Has (Reader Reference) sig m => MonadFail (FailC m) where | ||
fail s = do | ||
ref <- ask | ||
FailC (throwError (ref :: Reference, s)) | ||
|
||
instance Has (Reader Reference) sig m => Algebra (Fail :+: sig) (FailC m) where | ||
alg _ (L (Fail s)) _ = fail s | ||
alg hdl (R other) ctx = FailC (alg (runFailC . hdl) (R other) ctx) | ||
Comment on lines
+24
to
+37
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This used to live elsewhere in the project. I’ve moved it here, and simplified it slightly by use of the |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE MultiParamTypeClasses #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
module Analysis.Carrier.Store.Monovariant | ||
( -- * Store carrier | ||
MAddr(..) | ||
, MStore(..) | ||
, runStoreState | ||
, runStore | ||
, StoreC(..) | ||
-- * Store effect | ||
, module Analysis.Effect.Store | ||
-- * Env carrier | ||
, EnvC(..) | ||
-- * Env effect | ||
, module Analysis.Effect.Env | ||
) where | ||
|
||
import Analysis.Effect.Env | ||
import Analysis.Effect.Store | ||
import Analysis.Name | ||
import Control.Algebra | ||
import Control.Carrier.State.Church | ||
import Control.Effect.Labelled | ||
import Control.Effect.NonDet | ||
import Control.Monad.Fail as Fail | ||
import Data.Map as Map | ||
import Data.Set as Set | ||
|
||
newtype MAddr = MAddr Name | ||
deriving (Eq, Ord, Show) | ||
|
||
newtype MStore value = MStore { getMStore :: Map.Map MAddr (Set.Set value) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎩 @patrickt for the suggestion of using a hash map here; |
||
deriving (Eq, Ord, Show) | ||
|
||
instance Ord value => Semigroup (MStore value) where | ||
MStore s1 <> MStore s2 = MStore (Map.unionWith Set.union s1 s2) | ||
|
||
instance Ord value => Monoid (MStore value) where | ||
mempty = MStore Map.empty | ||
|
||
|
||
-- Store carrier | ||
|
||
runStoreState :: Applicative m => StateC (MStore value) m a -> m (MStore value, a) | ||
runStoreState = runState (curry pure) (MStore Map.empty) | ||
|
||
runStore :: Labelled Store (StoreC value) m a -> m a | ||
runStore = runStoreC . runLabelled | ||
|
||
newtype StoreC value m a = StoreC { runStoreC :: m a } | ||
deriving (Alternative, Applicative, Functor, Monad, Fail.MonadFail) | ||
|
||
instance (Has (State (MStore value)) sig m, Alternative m, Ord value) => Algebra (Store MAddr value :+: sig) (StoreC value m) where | ||
alg hdl sig ctx = StoreC $ do | ||
MStore store <- get @(MStore value) | ||
case sig of | ||
L op -> case op of | ||
Alloc name -> let addr = MAddr name in addr <$ ctx <$ put (MStore (Map.insertWith Set.union addr Set.empty store)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I decided against abstracting out a full module for the monovariant store to model insertion with the correct monoidal semantics vs. just doing the |
||
Assign addr value -> ctx <$ put (MStore (Map.insertWith Set.union addr (Set.singleton value) store)) | ||
Fetch addr -> foldMapA ((<$ put (MStore store)) . (<$ ctx)) (Map.findWithDefault Set.empty addr store) | ||
|
||
R other -> alg (runStoreC . hdl) other ctx | ||
|
||
|
||
-- Env carrier | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For convenience, I’ve placed both the |
||
|
||
newtype EnvC value m a = EnvC { runEnv :: m a } | ||
deriving (Applicative, Functor, Monad, Fail.MonadFail) | ||
|
||
instance Has (State (MStore value)) sig m | ||
=> Algebra (Env MAddr :+: sig) (EnvC value m) where | ||
alg hdl sig ctx = case sig of | ||
L op -> case op of | ||
Bind _ _ m -> hdl (m <$ ctx) | ||
Lookup n -> do | ||
MStore store <- get @(MStore value) | ||
pure (MAddr n <$ Map.lookup (MAddr n) store <$ ctx) | ||
Comment on lines
+81
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the hallmark of monovariant addressing: there’s no need for a separate environment because the keys of the store are precisely the addresses we’d track anyway. This distinct representation of stores and environments between the monovariant and precise addressing strategies stands in contrast to the old analysis suite in the |
||
|
||
R other -> EnvC (alg (runEnv . hdl) other ctx) |
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.
🔥
fused-syntax
had been used forCore
(along with a few other things), but it never worked the way I wanted and I don’t think it’s a good idea to try to continue maintaining it for the sake of this, so 🔥