1
1
mirror of https://github.com/github/semantic.git synced 2025-01-03 04:51:57 +03:00

Clean up a bunch of algebras.

This commit is contained in:
Rob Rix 2018-10-22 20:30:07 -04:00
parent 314aff5d56
commit 21f17ff56f
13 changed files with 24 additions and 24 deletions

View File

@ -99,7 +99,7 @@ instance ( Member (Reader (ModuleTable (NonEmpty (Module (ModuleResult address))
) )
=> Carrier (Modules address :+: sig) (ModulesC address m) where => Carrier (Modules address :+: sig) (ModulesC address m) where
ret = ModulesC . const . ret ret = ModulesC . const . ret
eff op = ModulesC (\ paths -> (alg paths \/ (eff . handlePure (flip runModulesC paths))) op) eff op = ModulesC (\ paths -> (alg paths \/ eff . handleReader paths runModulesC) op)
where alg paths (Load name k) = askModuleTable >>= maybeM (throwLoadError (ModuleNotFoundError name)) . fmap (runMerging . foldMap1 (Merging . moduleBody)) . ModuleTable.lookup name >>= flip runModulesC paths . k where alg paths (Load name k) = askModuleTable >>= maybeM (throwLoadError (ModuleNotFoundError name)) . fmap (runMerging . foldMap1 (Merging . moduleBody)) . ModuleTable.lookup name >>= flip runModulesC paths . k
alg paths (Lookup path k) = askModuleTable >>= flip runModulesC paths . k . fmap (runMerging . foldMap1 (Merging . moduleBody)) . ModuleTable.lookup path alg paths (Lookup path k) = askModuleTable >>= flip runModulesC paths . k . fmap (runMerging . foldMap1 (Merging . moduleBody)) . ModuleTable.lookup path
alg paths (Resolve names k) = runModulesC (k (find (`Set.member` paths) names)) paths alg paths (Resolve names k) = runModulesC (k (find (`Set.member` paths) names)) paths

View File

@ -40,13 +40,13 @@ instance ( Carrier (Allocator address :+: sig) (AllocatorC address m)
) )
=> Carrier (Allocator (Hole context address) :+: sig) (AllocatorC (Hole context address) m) where => Carrier (Allocator (Hole context address) :+: sig) (AllocatorC (Hole context address) m) where
ret = demoteA . ret ret = demoteA . ret
eff = alg \/ AllocatorC . eff . handlePure runAllocatorC eff = alg \/ AllocatorC . eff . handleCoercible
where alg (Alloc name k) = Total <$> promoteA (eff (L (Alloc name ret))) >>= k where alg (Alloc name k) = Total <$> promoteA (eff (L (Alloc name ret))) >>= k
instance (Carrier (Deref value :+: sig) (DerefC address value m), Carrier sig m, Monad m) instance (Carrier (Deref value :+: sig) (DerefC address value m), Carrier sig m, Monad m)
=> Carrier (Deref value :+: sig) (DerefC (Hole context address) value m) where => Carrier (Deref value :+: sig) (DerefC (Hole context address) value m) where
ret = demoteD . ret ret = demoteD . ret
eff = alg \/ DerefC . eff . handlePure runDerefC eff = alg \/ DerefC . eff . handleCoercible
where alg (DerefCell cell k) = promoteD (eff (L (DerefCell cell ret))) >>= k where alg (DerefCell cell k) = promoteD (eff (L (DerefCell cell ret))) >>= k
alg (AssignCell value cell k) = promoteD (eff (L (AssignCell value cell ret))) >>= k alg (AssignCell value cell k) = promoteD (eff (L (AssignCell value cell ret))) >>= k

View File

@ -43,13 +43,13 @@ instance ( Carrier (Allocator address :+: sig) (AllocatorC address m)
) )
=> Carrier (Allocator (Located address) :+: sig) (AllocatorC (Located address) m) where => Carrier (Allocator (Located address) :+: sig) (AllocatorC (Located address) m) where
ret = demoteA . ret ret = demoteA . ret
eff = alg \/ AllocatorC . eff . handlePure runAllocatorC eff = alg \/ AllocatorC . eff . handleCoercible
where alg (Alloc name k) = Located <$> promoteA (eff (L (Alloc name ret))) <*> currentPackage <*> currentModule <*> pure name <*> ask >>= k where alg (Alloc name k) = Located <$> promoteA (eff (L (Alloc name ret))) <*> currentPackage <*> currentModule <*> pure name <*> ask >>= k
instance (Carrier (Deref value :+: sig) (DerefC address value m), Carrier sig m, Monad m) instance (Carrier (Deref value :+: sig) (DerefC address value m), Carrier sig m, Monad m)
=> Carrier (Deref value :+: sig) (DerefC (Located address) value m) where => Carrier (Deref value :+: sig) (DerefC (Located address) value m) where
ret = demoteD . ret ret = demoteD . ret
eff = alg \/ DerefC . eff . handlePure runDerefC eff = alg \/ DerefC . eff . handleCoercible
where alg (DerefCell cell k) = promoteD (eff (L (DerefCell cell ret))) >>= k where alg (DerefCell cell k) = promoteD (eff (L (DerefCell cell ret))) >>= k
alg (AssignCell value cell k) = promoteD (eff (L (AssignCell value cell ret))) >>= k alg (AssignCell value cell k) = promoteD (eff (L (AssignCell value cell ret))) >>= k

View File

@ -20,12 +20,12 @@ instance Show Monovariant where
instance Carrier sig m => Carrier (Allocator Monovariant :+: sig) (AllocatorC Monovariant m) where instance Carrier sig m => Carrier (Allocator Monovariant :+: sig) (AllocatorC Monovariant m) where
ret = AllocatorC . ret ret = AllocatorC . ret
eff = AllocatorC . (alg \/ (eff . handlePure runAllocatorC)) eff = AllocatorC . (alg \/ eff . handleCoercible)
where alg (Alloc name k) = runAllocatorC (k (Monovariant name)) where alg (Alloc name k) = runAllocatorC (k (Monovariant name))
instance (Ord value, Carrier sig m, Alternative m, Monad m) => Carrier (Deref value :+: sig) (DerefC Monovariant value m) where instance (Ord value, Carrier sig m, Alternative m, Monad m) => Carrier (Deref value :+: sig) (DerefC Monovariant value m) where
ret = DerefC . ret ret = DerefC . ret
eff = DerefC . (alg \/ (eff . handlePure runDerefC)) eff = DerefC . (alg \/ eff . handleCoercible)
where alg (DerefCell cell k) = traverse (foldMapA pure) (nonEmpty (toList cell)) >>= runDerefC . k where alg (DerefCell cell k) = traverse (foldMapA pure) (nonEmpty (toList cell)) >>= runDerefC . k
alg (AssignCell value cell k) = runDerefC (k (Set.insert value cell)) alg (AssignCell value cell k) = runDerefC (k (Set.insert value cell))

View File

@ -19,12 +19,12 @@ instance Show Precise where
instance (Member Fresh sig, Carrier sig m, Monad m) => Carrier (Allocator Precise :+: sig) (AllocatorC Precise m) where instance (Member Fresh sig, Carrier sig m, Monad m) => Carrier (Allocator Precise :+: sig) (AllocatorC Precise m) where
ret = AllocatorC . ret ret = AllocatorC . ret
eff = AllocatorC . (alg \/ (eff . handlePure runAllocatorC)) eff = AllocatorC . (alg \/ eff . handleCoercible)
where alg (Alloc _ k) = Precise <$> fresh >>= runAllocatorC . k where alg (Alloc _ k) = Precise <$> fresh >>= runAllocatorC . k
instance Carrier sig m => Carrier (Deref value :+: sig) (DerefC Precise value m) where instance Carrier sig m => Carrier (Deref value :+: sig) (DerefC Precise value m) where
ret = DerefC . ret ret = DerefC . ret
eff = DerefC . (alg \/ (eff . handlePure runDerefC)) eff = DerefC . (alg \/ eff . handleCoercible)
where alg (DerefCell cell k) = runDerefC (k (fst <$> Set.minView cell)) where alg (DerefCell cell k) = runDerefC (k (fst <$> Set.minView cell))
alg (AssignCell value _ k) = runDerefC (k (Set.singleton value)) alg (AssignCell value _ k) = runDerefC (k (Set.singleton value))

View File

@ -31,7 +31,7 @@ instance ( Member (Allocator address) sig
) )
=> Carrier (Abstract.Function term address Abstract :+: sig) (FunctionC term address Abstract (Evaluator term address Abstract m)) where => Carrier (Abstract.Function term address Abstract :+: sig) (FunctionC term address Abstract (Evaluator term address Abstract m)) where
ret = FunctionC . const . ret ret = FunctionC . const . ret
eff op = FunctionC (\ eval -> (alg eval \/ (eff . handlePure (flip runFunctionC eval))) op) eff op = FunctionC (\ eval -> (alg eval \/ eff . handleReader eval runFunctionC) op)
where alg eval = \case where alg eval = \case
Function _ params body k -> do Function _ params body k -> do
env <- foldr (\ name rest -> do env <- foldr (\ name rest -> do
@ -48,7 +48,7 @@ instance ( Member (Allocator address) sig
instance (Carrier sig m, Alternative m, Monad m) => Carrier (Boolean Abstract :+: sig) (BooleanC Abstract m) where instance (Carrier sig m, Alternative m, Monad m) => Carrier (Boolean Abstract :+: sig) (BooleanC Abstract m) where
ret = BooleanC . ret ret = BooleanC . ret
eff = BooleanC . (alg \/ eff . handlePure runBooleanC) eff = BooleanC . (alg \/ eff . handleCoercible)
where alg (Boolean _ k) = runBooleanC (k Abstract) where alg (Boolean _ k) = runBooleanC (k Abstract)
alg (AsBool _ k) = runBooleanC (k True) <|> runBooleanC (k False) alg (AsBool _ k) = runBooleanC (k True) <|> runBooleanC (k False)
alg (Disjunction a b k) = (runBooleanC a <|> runBooleanC b) >>= runBooleanC . k alg (Disjunction a b k) = (runBooleanC a <|> runBooleanC b) >>= runBooleanC . k
@ -61,7 +61,7 @@ instance ( Member (Abstract.Boolean Abstract) sig
) )
=> Carrier (While Abstract :+: sig) (WhileC Abstract m) where => Carrier (While Abstract :+: sig) (WhileC Abstract m) where
ret = WhileC . ret ret = WhileC . ret
eff = WhileC . (alg \/ eff . handlePure runWhileC) eff = WhileC . (alg \/ eff . handleCoercible)
where alg (Abstract.While cond body k) = do where alg (Abstract.While cond body k) = do
cond' <- runWhileC cond cond' <- runWhileC cond
ifthenelse cond' (runWhileC body *> empty) (runWhileC (k unit)) ifthenelse cond' (runWhileC body *> empty) (runWhileC (k unit))

View File

@ -74,7 +74,7 @@ instance ( FreeVariables term
) )
=> Carrier (Abstract.Function term address (Value term address) :+: sig) (Abstract.FunctionC term address (Value term address) (Evaluator term address (Value term address) m)) where => Carrier (Abstract.Function term address (Value term address) :+: sig) (Abstract.FunctionC term address (Value term address) (Evaluator term address (Value term address) m)) where
ret = FunctionC . const . ret ret = FunctionC . const . ret
eff op = FunctionC (\ eval -> (alg eval \/ (eff . handlePure (flip runFunctionC eval))) op) eff op = FunctionC (\ eval -> (alg eval \/ eff . handleReader eval runFunctionC) op)
where alg eval = \case where alg eval = \case
Abstract.Function name params body k -> do Abstract.Function name params body k -> do
packageInfo <- currentPackage packageInfo <- currentPackage
@ -107,7 +107,7 @@ instance ( Member (Reader ModuleInfo) sig
) )
=> Carrier (Abstract.Boolean (Value term address) :+: sig) (BooleanC (Value term address) m) where => Carrier (Abstract.Boolean (Value term address) :+: sig) (BooleanC (Value term address) m) where
ret = BooleanC . ret ret = BooleanC . ret
eff = BooleanC . (alg \/ (eff . handlePure runBooleanC)) eff = BooleanC . (alg \/ eff . handleCoercible)
where alg :: Abstract.Boolean (Value term address) (BooleanC (Value term address) m) (BooleanC (Value term address) m a) -> m a where alg :: Abstract.Boolean (Value term address) (BooleanC (Value term address) m) (BooleanC (Value term address) m a) -> m a
alg = \case alg = \case
Abstract.Boolean b k -> runBooleanC . k $! Boolean b Abstract.Boolean b k -> runBooleanC . k $! Boolean b
@ -140,7 +140,7 @@ instance ( Member (Reader ModuleInfo) sig
-- ) -- )
-- => Carrier (Abstract.While (Value term address) :+: sig) (WhileC (Value term address) (Eff (InterposeC (Resumable (BaseError (UnspecializedError (Value term address)))) m))) where -- => Carrier (Abstract.While (Value term address) :+: sig) (WhileC (Value term address) (Eff (InterposeC (Resumable (BaseError (UnspecializedError (Value term address)))) m))) where
-- ret = WhileC . ret -- ret = WhileC . ret
-- eff = WhileC . (alg \/ (eff . handlePure runWhileC)) -- eff = WhileC . (alg \/ eff . handleCoercible)
-- where alg = \case -- where alg = \case
-- Abstract.While cond body k -> interpose @(Resumable (BaseError (UnspecializedError (Value term address)))) -- Abstract.While cond body k -> interpose @(Resumable (BaseError (UnspecializedError (Value term address))))
-- (\(Resumable (BaseError _ _ (UnspecializedError _)) k) -> throwAbort) (runEvaluator (loop (\continue -> do -- (\(Resumable (BaseError _ _ (UnspecializedError _)) k) -> throwAbort) (runEvaluator (loop (\continue -> do

View File

@ -253,7 +253,7 @@ instance ( Member (Allocator address) sig
) )
=> Carrier (Abstract.Function term address Type :+: sig) (FunctionC term address Type (Evaluator term address Type m)) where => Carrier (Abstract.Function term address Type :+: sig) (FunctionC term address Type (Evaluator term address Type m)) where
ret = FunctionC . const . ret ret = FunctionC . const . ret
eff op = FunctionC (\ eval -> (alg eval \/ (eff . handlePure (flip runFunctionC eval))) op) eff op = FunctionC (\ eval -> (alg eval \/ eff . handleReader eval runFunctionC) op)
where alg eval = \case where alg eval = \case
Abstract.Function _ params body k -> do Abstract.Function _ params body k -> do
(env, tvars) <- foldr (\ name rest -> do (env, tvars) <- foldr (\ name rest -> do
@ -285,7 +285,7 @@ instance ( Member (Reader ModuleInfo) sig
) )
=> Carrier (Abstract.Boolean Type :+: sig) (BooleanC Type m) where => Carrier (Abstract.Boolean Type :+: sig) (BooleanC Type m) where
ret = BooleanC . ret ret = BooleanC . ret
eff = BooleanC . (alg \/ (eff . handlePure runBooleanC)) eff = BooleanC . (alg \/ eff . handleCoercible)
where alg (Abstract.Boolean _ k) = runBooleanC (k Bool) where alg (Abstract.Boolean _ k) = runBooleanC (k Bool)
alg (Abstract.AsBool t k) = unify t Bool *> (runBooleanC (k True) <|> runBooleanC (k False)) alg (Abstract.AsBool t k) = unify t Bool *> (runBooleanC (k True) <|> runBooleanC (k False))
alg (Abstract.Disjunction t1 t2 k) = ((runBooleanC t1 >>= unify Bool) <|> (runBooleanC t2 >>= unify Bool) >>= runBooleanC . k) alg (Abstract.Disjunction t1 t2 k) = ((runBooleanC t1 >>= unify Bool) <|> (runBooleanC t2 >>= unify Bool) >>= runBooleanC . k)
@ -298,7 +298,7 @@ instance ( Member (Abstract.Boolean Type) sig
) )
=> Carrier (Abstract.While Type :+: sig) (WhileC Type m) where => Carrier (Abstract.While Type :+: sig) (WhileC Type m) where
ret = WhileC . ret ret = WhileC . ret
eff = WhileC . (alg \/ (eff . handlePure runWhileC)) eff = WhileC . (alg \/ eff . handleCoercible)
where alg (Abstract.While cond body k) = do where alg (Abstract.While cond body k) = do
cond' <- runWhileC cond cond' <- runWhileC cond
ifthenelse cond' (runWhileC body *> empty) (runWhileC (k unit)) ifthenelse cond' (runWhileC body *> empty) (runWhileC (k unit))

View File

@ -56,4 +56,4 @@ newtype DistributeC m a = DistributeC { runDistributeC :: m a }
instance Carrier (Distribute :+: Lift IO) (DistributeC (Eff (LiftC IO))) where instance Carrier (Distribute :+: Lift IO) (DistributeC (Eff (LiftC IO))) where
ret = DistributeC . ret ret = DistributeC . ret
eff = DistributeC . ((\ (Distribute task k) -> liftIO (Async.runConcurrently (Async.Concurrently (runM (runDistributeC task)))) >>= runDistributeC . k) \/ (eff . handlePure runDistributeC)) eff = DistributeC . ((\ (Distribute task k) -> liftIO (Async.runConcurrently (Async.Concurrently (runM (runDistributeC task)))) >>= runDistributeC . k) \/ eff . handleCoercible)

View File

@ -243,7 +243,7 @@ newtype FilesC m a = FilesC { runFilesC :: m a }
instance (Member (Error SomeException) sig, MonadIO m, Carrier sig m) => Carrier (Files :+: sig) (FilesC m) where instance (Member (Error SomeException) sig, MonadIO m, Carrier sig m) => Carrier (Files :+: sig) (FilesC m) where
ret = FilesC . ret ret = FilesC . ret
eff = FilesC . (alg \/ (eff . handlePure runFilesC)) eff = FilesC . (alg \/ eff . handleCoercible)
where alg = \case where alg = \case
Read (FromPath path) k -> (readBlobFromPath path `catchIO` (throwError . toException @SomeException)) >>= runFilesC . k Read (FromPath path) k -> (readBlobFromPath path `catchIO` (throwError . toException @SomeException)) >>= runFilesC . k
Read (FromHandle handle) k -> (readBlobsFromHandle handle `catchIO` (throwError . toException @SomeException)) >>= runFilesC . k Read (FromHandle handle) k -> (readBlobsFromHandle handle `catchIO` (throwError . toException @SomeException)) >>= runFilesC . k

View File

@ -63,6 +63,6 @@ newtype ResolutionC m a = ResolutionC { runResolutionC :: m a }
instance (Member Files sig, Carrier sig m, Monad m) => Carrier (Resolution :+: sig) (ResolutionC m) where instance (Member Files sig, Carrier sig m, Monad m) => Carrier (Resolution :+: sig) (ResolutionC m) where
ret = ResolutionC . ret ret = ResolutionC . ret
eff = ResolutionC . (alg \/ (eff . handlePure runResolutionC)) eff = ResolutionC . (alg \/ eff . handleCoercible)
where alg (NodeJSResolution dir prop excludeDirs k) = nodeJSResolutionMap dir prop excludeDirs >>= runResolutionC . k where alg (NodeJSResolution dir prop excludeDirs k) = nodeJSResolutionMap dir prop excludeDirs >>= runResolutionC . k
alg (NoResolution k) = runResolutionC (k Map.empty) alg (NoResolution k) = runResolutionC (k Map.empty)

View File

@ -205,7 +205,7 @@ newtype TraceInTelemetryC m a = TraceInTelemetryC { runTraceInTelemetryC :: m a
instance (Member Telemetry sig, Carrier sig m, Monad m) => Carrier (Trace :+: sig) (TraceInTelemetryC m) where instance (Member Telemetry sig, Carrier sig m, Monad m) => Carrier (Trace :+: sig) (TraceInTelemetryC m) where
ret = TraceInTelemetryC . ret ret = TraceInTelemetryC . ret
eff = TraceInTelemetryC . ((\ (Trace str k) -> writeLog Debug str [] >> runTraceInTelemetryC k) \/ (eff . handlePure runTraceInTelemetryC)) eff = TraceInTelemetryC . ((\ (Trace str k) -> writeLog Debug str [] >> runTraceInTelemetryC k) \/ eff . handleCoercible)
-- | An effect describing high-level tasks to be performed. -- | An effect describing high-level tasks to be performed.
@ -249,7 +249,7 @@ newtype TaskC m a = TaskC { runTaskC :: m a }
instance (Member (Error SomeException) sig, Member (Lift IO) sig, Member (Reader Config) sig, Member Resource sig, Member Telemetry sig, Member Timeout sig, Member Trace sig, Carrier sig m, MonadIO m) => Carrier (Task :+: sig) (TaskC m) where instance (Member (Error SomeException) sig, Member (Lift IO) sig, Member (Reader Config) sig, Member Resource sig, Member Telemetry sig, Member Timeout sig, Member Trace sig, Carrier sig m, MonadIO m) => Carrier (Task :+: sig) (TaskC m) where
ret = TaskC . ret ret = TaskC . ret
eff = TaskC . (alg \/ (eff . handlePure runTaskC)) eff = TaskC . (alg \/ eff . handleCoercible)
where alg = \case where alg = \case
Parse parser blob k -> runParser blob parser >>= runTaskC . k Parse parser blob k -> runParser blob parser >>= runTaskC . k
Analyze interpret analysis k -> runTaskC (k (interpret analysis)) Analyze interpret analysis k -> runTaskC (k (interpret analysis))

View File

@ -158,7 +158,7 @@ newtype TelemetryC m a = TelemetryC { runTelemetryC :: (LogQueue, StatQueue) ->
instance (Carrier sig m, MonadIO m) => Carrier (Telemetry :+: sig) (TelemetryC m) where instance (Carrier sig m, MonadIO m) => Carrier (Telemetry :+: sig) (TelemetryC m) where
ret = TelemetryC . const . ret ret = TelemetryC . const . ret
eff op = TelemetryC (\ queues -> (alg queues \/ (eff . handlePure (flip runTelemetryC queues))) op) eff op = TelemetryC (\ queues -> (alg queues \/ eff . handleReader queues runTelemetryC) op)
where alg queues (WriteStat stat k) = queueStat (snd queues) stat *> runTelemetryC k queues where alg queues (WriteStat stat k) = queueStat (snd queues) stat *> runTelemetryC k queues
alg queues (WriteLog level message pairs k) = queueLogMessage (fst queues) level message pairs *> runTelemetryC k queues alg queues (WriteLog level message pairs k) = queueLogMessage (fst queues) level message pairs *> runTelemetryC k queues