Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.

Commit 09f2362

Browse files
committed
Abstract closures to their graphs.
1 parent cb5b0fb commit 09f2362

1 file changed

Lines changed: 14 additions & 36 deletions

File tree

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{-# LANGUAGE FlexibleContexts, OverloadedStrings, RecordWildCards, TypeApplications #-}
1+
{-# LANGUAGE FlexibleContexts, GeneralizedNewtypeDeriving, OverloadedStrings, RecordWildCards, TypeApplications #-}
22
module Analysis.ScopeGraph
33
( ScopeGraph
44
, Entry(..)
@@ -10,7 +10,6 @@ import Analysis.Eval
1010
import Analysis.FlowInsensitive
1111
import Control.Applicative (Alternative (..))
1212
import Control.Effect.Carrier
13-
import Control.Effect.Fail
1413
import Control.Effect.Fresh
1514
import Control.Effect.Reader
1615
import Control.Effect.State
@@ -33,28 +32,13 @@ data Entry = Entry
3332
}
3433
deriving (Eq, Ord, Show)
3534

36-
type ScopeGraph = Map.Map Entry (Set.Set Entry)
35+
newtype ScopeGraph = ScopeGraph { unScopeGraph :: Map.Map Entry (Set.Set Entry) }
36+
deriving (Eq, Monoid, Ord, Show)
3737

38+
instance Semigroup ScopeGraph where
39+
ScopeGraph a <> ScopeGraph b = ScopeGraph (Map.unionWith (<>) a b)
3840

39-
data Value = Value
40-
{ valueSemi :: Semi
41-
, valueGraph :: ScopeGraph
42-
}
43-
deriving (Eq, Ord, Show)
44-
45-
instance Semigroup Value where
46-
Value _ g1 <> Value _ g2 = Value Abstract (Map.unionWith (<>) g1 g2)
47-
48-
instance Monoid Value where
49-
mempty = Value Abstract mempty
50-
51-
data Semi
52-
= Closure Loc User (Term Core.Core User)
53-
| Abstract
54-
deriving (Eq, Ord, Show)
55-
56-
57-
scopeGraph :: [File (Term Core.Core User)] -> (Heap User Value, [File (Either (Loc, String) Value)])
41+
scopeGraph :: [File (Term Core.Core User)] -> (Heap User ScopeGraph, [File (Either (Loc, String) ScopeGraph)])
5842
scopeGraph
5943
= run
6044
. runFresh
@@ -65,10 +49,10 @@ runFile
6549
:: ( Carrier sig m
6650
, Effect sig
6751
, Member Fresh sig
68-
, Member (State (Heap User Value)) sig
52+
, Member (State (Heap User ScopeGraph)) sig
6953
)
7054
=> File (Term Core.Core User)
71-
-> m (File (Either (Loc, String) Value))
55+
-> m (File (Either (Loc, String) ScopeGraph))
7256
runFile file = traverse run file
7357
where run = runReader (fileLoc file)
7458
. runFailWithLoc
@@ -79,29 +63,23 @@ runFile file = traverse run file
7963
scopeGraphAnalysis
8064
:: ( Alternative m
8165
, Carrier sig m
82-
, Member (Reader Loc) sig
83-
, Member (State (Heap User Value)) sig
84-
, MonadFail m
66+
, Member (State (Heap User ScopeGraph)) sig
8567
)
86-
=> Analysis User Value m
68+
=> Analysis User ScopeGraph m
8769
scopeGraphAnalysis = Analysis{..}
8870
where alloc = pure
8971
bind _ _ m = m
9072
lookupEnv = pure . Just
91-
deref addr = gets (Map.lookup addr) >>= maybe (pure Nothing) (foldMapA (pure . Just)) . nonEmpty . maybe [] (Set.toList @Value)
73+
deref addr = gets (Map.lookup addr) >>= maybe (pure Nothing) (foldMapA (pure . Just)) . nonEmpty . maybe [] (Set.toList @ScopeGraph)
9274
assign addr ty = modify (Map.insertWith (<>) addr (Set.singleton ty))
93-
abstract _ name body = do
94-
loc <- ask
95-
pure (Value (Closure loc name body) mempty)
96-
apply eval (Value (Closure loc name body) _) a = local (const loc) $ do
75+
abstract eval name body = do
9776
addr <- alloc name
98-
assign addr a
9977
bind name addr (eval body)
100-
apply _ f _ = fail $ "Cannot coerce " <> show f <> " to function"
78+
apply _ f a = pure (f <> a)
10179
unit = pure mempty
10280
bool _ = pure mempty
10381
asBool _ = pure True <|> pure False
10482
string _ = pure mempty
10583
asString _ = pure mempty
106-
record fields = pure (Value Abstract (foldMap (valueGraph . snd) fields))
84+
record fields = pure (foldMap snd fields)
10785
_ ... m = pure (Just m)

0 commit comments

Comments
 (0)