1
1
mirror of https://github.com/github/semantic.git synced 2024-11-28 10:15:55 +03:00

🔥 the Analysis env fields.

This commit is contained in:
Rob Rix 2019-11-04 12:12:59 -05:00
parent 17da79fe24
commit 961bf622ca
No known key found for this signature in database
GPG Key ID: F188A01508EA1CF7
5 changed files with 20 additions and 32 deletions

View File

@ -11,10 +11,7 @@ import GHC.Generics (Generic1)
-- --
-- This is intended to be replaced with a selection of algebraic effects providing these interfaces and carriers providing reusable implementations. -- This is intended to be replaced with a selection of algebraic effects providing these interfaces and carriers providing reusable implementations.
data Analysis term name address value m = Analysis data Analysis term name address value m = Analysis
{ alloc :: name -> m address { deref :: address -> m (Maybe value)
, bind :: forall a . name -> address -> m a -> m a
, lookupEnv :: name -> m (Maybe address)
, deref :: address -> m (Maybe value)
, assign :: address -> value -> m () , assign :: address -> value -> m ()
, abstract :: (term name -> m value) -> name -> term name -> m value , abstract :: (term name -> m value) -> name -> term name -> m value
, apply :: (term name -> m value) -> value -> value -> m value , apply :: (term name -> m value) -> value -> value -> m value

View File

@ -130,10 +130,7 @@ concreteAnalysis :: ( Carrier sig m
) )
=> Analysis term name Precise (Concrete term name) m => Analysis term name Precise (Concrete term name) m
concreteAnalysis = Analysis{..} concreteAnalysis = Analysis{..}
where alloc = A.alloc where deref = gets . IntMap.lookup
bind = A.bind
lookupEnv = A.lookupEnv
deref = gets . IntMap.lookup
assign addr value = modify (IntMap.insert addr value) assign addr value = modify (IntMap.insert addr value)
abstract _ name body = do abstract _ name body = do
path <- ask path <- ask
@ -142,7 +139,7 @@ concreteAnalysis = Analysis{..}
pure (Closure path span name body env) pure (Closure path span name body env)
apply eval (Closure path span name body env) a = do apply eval (Closure path span name body env) a = do
local (const path) . local (const span) $ do local (const path) . local (const span) $ do
addr <- alloc name addr <- A.alloc name
assign addr a assign addr a
local (const (Map.insert name addr env)) (eval body) local (const (Map.insert name addr env)) (eval body)
apply _ f _ = fail $ "Cannot coerce " <> show f <> " to function" apply _ f _ = fail $ "Cannot coerce " <> show f <> " to function"
@ -155,7 +152,7 @@ concreteAnalysis = Analysis{..}
asString v = fail $ "Cannot coerce " <> show v <> " to String" asString v = fail $ "Cannot coerce " <> show v <> " to String"
record fields = do record fields = do
fields' <- for fields $ \ (name, value) -> do fields' <- for fields $ \ (name, value) -> do
addr <- alloc name addr <- A.alloc name
assign addr value assign addr value
pure (name, addr) pure (name, addr)
pure (Record (Map.fromList fields')) pure (Record (Map.fromList fields'))

View File

@ -96,7 +96,8 @@ runFile eval file = traverse run file
-- FIXME: decompose into a product domain and two atomic domains -- FIXME: decompose into a product domain and two atomic domains
importGraphAnalysis importGraphAnalysis
:: ( Alternative m :: forall term name m sig
. ( Alternative m
, Carrier sig m , Carrier sig m
, Member (Env name name) sig , Member (Env name name) sig
, Member (Reader Path.AbsRelFile) sig , Member (Reader Path.AbsRelFile) sig
@ -110,19 +111,16 @@ importGraphAnalysis
) )
=> Analysis term name name (Value term name) m => Analysis term name name (Value term name) m
importGraphAnalysis = Analysis{..} importGraphAnalysis = Analysis{..}
where alloc = A.alloc where deref addr = gets (Map.lookup addr >=> nonEmpty . Set.toList) >>= maybe (pure Nothing) (foldMapA (pure . Just))
bind = A.bind
lookupEnv = A.lookupEnv
deref addr = gets (Map.lookup addr >=> nonEmpty . Set.toList) >>= maybe (pure Nothing) (foldMapA (pure . Just))
assign addr v = modify (Map.insertWith (<>) addr (Set.singleton v)) assign addr v = modify (Map.insertWith (<>) addr (Set.singleton v))
abstract _ name body = do abstract _ name body = do
path <- ask path <- ask
span <- ask span <- ask
pure (Value (Closure path span name body) mempty) pure (Value (Closure path span name body) mempty)
apply eval (Value (Closure path span name body) _) a = local (const path) . local (const span) $ do apply eval (Value (Closure path span name body) _) a = local (const path) . local (const span) $ do
addr <- alloc name addr <- alloc @name @name name
assign addr a assign addr a
bind name addr (eval body) A.bind name addr (eval body)
apply _ f _ = fail $ "Cannot coerce " <> show f <> " to function" apply _ f _ = fail $ "Cannot coerce " <> show f <> " to function"
unit = pure mempty unit = pure mempty
bool _ = pure mempty bool _ = pure mempty
@ -132,7 +130,7 @@ importGraphAnalysis = Analysis{..}
asString _ = pure mempty asString _ = pure mempty
record fields = do record fields = do
for_ fields $ \ (k, v) -> do for_ fields $ \ (k, v) -> do
addr <- alloc k addr <- A.alloc @name @name k
assign addr v assign addr v
pure (Value Abstract (foldMap (valueGraph . snd) fields)) pure (Value Abstract (foldMap (valueGraph . snd) fields))
_ ... m = pure (Just m) _ ... m = pure (Just m)

View File

@ -94,7 +94,8 @@ runFile eval file = traverse run file
. convergeTerm (Proxy @name) (fix (cacheTerm . eval scopeGraphAnalysis)) . convergeTerm (Proxy @name) (fix (cacheTerm . eval scopeGraphAnalysis))
scopeGraphAnalysis scopeGraphAnalysis
:: ( Alternative m :: forall term name m sig
. ( Alternative m
, Carrier sig m , Carrier sig m
, Member (Reader Path.AbsRelFile) sig , Member (Reader Path.AbsRelFile) sig
, Member (Env name name) sig , Member (Env name name) sig
@ -105,10 +106,7 @@ scopeGraphAnalysis
) )
=> Analysis term name name (ScopeGraph name) m => Analysis term name name (ScopeGraph name) m
scopeGraphAnalysis = Analysis{..} scopeGraphAnalysis = Analysis{..}
where alloc = A.alloc where deref addr = do
bind = A.bind
lookupEnv = A.lookupEnv
deref addr = do
ref <- askRef ref <- askRef
bindRef <- asks (Map.lookup addr) bindRef <- asks (Map.lookup addr)
cell <- gets (Map.lookup addr >=> nonEmpty . Set.toList) cell <- gets (Map.lookup addr >=> nonEmpty . Set.toList)
@ -119,9 +117,9 @@ scopeGraphAnalysis = Analysis{..}
bindRef <- asks (Map.lookup addr) bindRef <- asks (Map.lookup addr)
modify (Map.insertWith (<>) addr (Set.singleton (extendBinding addr ref bindRef <> v))) modify (Map.insertWith (<>) addr (Set.singleton (extendBinding addr ref bindRef <> v)))
abstract eval name body = do abstract eval name body = do
addr <- alloc name addr <- A.alloc @name @name name
assign name mempty assign name mempty
bind name addr (eval body) A.bind name addr (eval body)
apply _ f a = pure (f <> a) apply _ f a = pure (f <> a)
unit = pure mempty unit = pure mempty
bool _ = pure mempty bool _ = pure mempty

View File

@ -152,7 +152,8 @@ runFile eval file = traverse run file
. convergeTerm (Proxy @name) (fix (cacheTerm . eval typecheckingAnalysis)) . convergeTerm (Proxy @name) (fix (cacheTerm . eval typecheckingAnalysis))
typecheckingAnalysis typecheckingAnalysis
:: ( Alternative m :: forall term name m sig
. ( Alternative m
, Carrier sig m , Carrier sig m
, Member (Env name name) sig , Member (Env name name) sig
, Member Fresh sig , Member Fresh sig
@ -162,14 +163,11 @@ typecheckingAnalysis
) )
=> Analysis term name name (Type name) m => Analysis term name name (Type name) m
typecheckingAnalysis = Analysis{..} typecheckingAnalysis = Analysis{..}
where alloc = A.alloc where deref addr = gets (Map.lookup addr >=> nonEmpty . Set.toList) >>= maybe (pure Nothing) (foldMapA (pure . Just))
bind = A.bind
lookupEnv = A.lookupEnv
deref addr = gets (Map.lookup addr >=> nonEmpty . Set.toList) >>= maybe (pure Nothing) (foldMapA (pure . Just))
assign addr ty = modify (Map.insertWith (<>) addr (Set.singleton ty)) assign addr ty = modify (Map.insertWith (<>) addr (Set.singleton ty))
abstract eval name body = do abstract eval name body = do
-- FIXME: construct the associated scope -- FIXME: construct the associated scope
addr <- alloc name addr <- A.alloc @name @name name
arg <- meta arg <- meta
assign addr arg assign addr arg
ty <- eval body ty <- eval body
@ -187,7 +185,7 @@ typecheckingAnalysis = Analysis{..}
asString s = unify (Alg String) s $> mempty asString s = unify (Alg String) s $> mempty
record fields = do record fields = do
fields' <- for fields $ \ (k, v) -> do fields' <- for fields $ \ (k, v) -> do
addr <- alloc k addr <- A.alloc @name @name k
(k, v) <$ assign addr v (k, v) <$ assign addr v
-- FIXME: should records reference types by address instead? -- FIXME: should records reference types by address instead?
pure (Alg (Record (Map.fromList fields'))) pure (Alg (Record (Map.fromList fields')))