mirror of
https://github.com/github/semantic.git
synced 2025-01-01 19:55:34 +03:00
While operates on values, not ValueRefs.
This commit is contained in:
parent
36a7c31166
commit
ed073ba245
@ -140,17 +140,17 @@ newtype BooleanC value m a = BooleanC { runBooleanC :: m a }
|
||||
|
||||
|
||||
-- | The fundamental looping primitive, built on top of 'ifthenelse'.
|
||||
while :: (Member (While address value) sig, Carrier sig m)
|
||||
while :: (Member (While value) sig, Carrier sig m)
|
||||
=> Evaluator term address value m value -- ^ Condition
|
||||
-> Evaluator term address value m value -- ^ Body
|
||||
-> Evaluator term address value m (ValueRef address value)
|
||||
-> Evaluator term address value m value
|
||||
while cond body = send (While cond body ret)
|
||||
|
||||
-- | Do-while loop, built on top of while.
|
||||
doWhile :: (Member (While address value) sig, Carrier sig m)
|
||||
doWhile :: (Member (While value) sig, Carrier sig m)
|
||||
=> Evaluator term address value m value -- ^ Body
|
||||
-> Evaluator term address value m value -- ^ Condition
|
||||
-> Evaluator term address value m (ValueRef address value)
|
||||
-> Evaluator term address value m value
|
||||
doWhile body cond = body *> while cond body
|
||||
|
||||
-- | C-style for loops.
|
||||
@ -163,7 +163,7 @@ forLoop :: ( Carrier sig m
|
||||
, Member (State (ScopeGraph address)) sig
|
||||
, Member (Reader (CurrentFrame address)) sig
|
||||
, Member (Reader (CurrentScope address)) sig
|
||||
, Member (While address value) sig
|
||||
, Member (While value) sig
|
||||
, Member Fresh sig
|
||||
, Ord address
|
||||
)
|
||||
@ -171,23 +171,23 @@ forLoop :: ( Carrier sig m
|
||||
-> Evaluator term address value m value -- ^ Condition
|
||||
-> Evaluator term address value m value -- ^ Increment/stepper
|
||||
-> Evaluator term address value m value -- ^ Body
|
||||
-> Evaluator term address value m (ValueRef address value)
|
||||
-> Evaluator term address value m value
|
||||
forLoop initial cond step body = initial *> while cond (withLexicalScopeAndFrame body *> step)
|
||||
|
||||
data While address value m k
|
||||
= While (m value) (m value) (ValueRef address value -> k)
|
||||
data While value m k
|
||||
= While (m value) (m value) (value -> k)
|
||||
deriving (Functor)
|
||||
|
||||
instance HFunctor (While address value) where
|
||||
instance HFunctor (While value) where
|
||||
hmap f (While cond body k) = While (f cond) (f body) k
|
||||
|
||||
|
||||
runWhile :: Carrier (While address value :+: sig) (WhileC address value (Eff m))
|
||||
=> Evaluator term address value (WhileC address value (Eff m)) a
|
||||
runWhile :: Carrier (While value :+: sig) (WhileC value (Eff m))
|
||||
=> Evaluator term address value (WhileC value (Eff m)) a
|
||||
-> Evaluator term address value m a
|
||||
runWhile = raiseHandler $ runWhileC . interpret
|
||||
|
||||
newtype WhileC address value m a = WhileC { runWhileC :: m a }
|
||||
newtype WhileC value m a = WhileC { runWhileC :: m a }
|
||||
|
||||
|
||||
class Show value => AbstractIntro value where
|
||||
|
@ -45,7 +45,7 @@ class (Show1 constr, Foldable constr) => Evaluatable constr where
|
||||
, FreeVariables term
|
||||
, Member (Allocator address) sig
|
||||
, Member (Boolean value) sig
|
||||
, Member (While address value) sig
|
||||
, Member (While value) sig
|
||||
, Member (Deref value) sig
|
||||
, Member (State (ScopeGraph address)) sig
|
||||
, Member (Error (LoopControl value)) sig
|
||||
|
@ -70,13 +70,13 @@ instance ( Member (Abstract.Boolean Abstract) sig
|
||||
, Alternative m
|
||||
, Monad m
|
||||
)
|
||||
=> Carrier (While address Abstract :+: sig) (WhileC address Abstract m) where
|
||||
=> Carrier (While Abstract :+: sig) (WhileC Abstract m) where
|
||||
ret = WhileC . ret
|
||||
eff = WhileC . handleSum
|
||||
(eff . handleCoercible)
|
||||
(\ (Abstract.While cond body k) -> do
|
||||
cond' <- runWhileC cond
|
||||
ifthenelse cond' (runWhileC body *> empty) (runWhileC (k $ Rval unit)))
|
||||
ifthenelse cond' (runWhileC body *> empty) (runWhileC (k unit)))
|
||||
|
||||
|
||||
instance Ord address => ValueRoots address Abstract where
|
||||
|
@ -134,10 +134,10 @@ instance forall sig m term address. ( Carrier sig m
|
||||
, Show address
|
||||
, Show term
|
||||
)
|
||||
=> Carrier (Abstract.While address (Value term address) :+: sig) (WhileC address (Value term address) (Eff m)) where
|
||||
=> Carrier (Abstract.While (Value term address) :+: sig) (WhileC (Value term address) (Eff m)) where
|
||||
ret = WhileC . ret
|
||||
eff = WhileC . handleSum (eff . handleCoercible) (\case
|
||||
Abstract.While cond body k -> interpose @(Resumable (BaseError (UnspecializedError (Value term address)))) (runEvaluator (rvalBox =<< loop (\continue -> do
|
||||
Abstract.While cond body k -> interpose @(Resumable (BaseError (UnspecializedError (Value term address)))) (runEvaluator (loop (\continue -> do
|
||||
cond' <- Evaluator (runWhileC cond)
|
||||
|
||||
-- `interpose` is used to handle 'UnspecializedError's and abort out of the
|
||||
|
@ -311,13 +311,13 @@ instance ( Member (Abstract.Boolean Type) sig
|
||||
, Alternative m
|
||||
, Monad m
|
||||
)
|
||||
=> Carrier (Abstract.While address Type :+: sig) (WhileC address Type m) where
|
||||
=> Carrier (Abstract.While Type :+: sig) (WhileC Type m) where
|
||||
ret = WhileC . ret
|
||||
eff = WhileC . handleSum
|
||||
(eff . handleCoercible)
|
||||
(\ (Abstract.While cond body k) -> do
|
||||
cond' <- runWhileC cond
|
||||
ifthenelse cond' (runWhileC body *> empty) (runWhileC (k (Rval unit))))
|
||||
ifthenelse cond' (runWhileC body *> empty) (runWhileC (k unit)))
|
||||
|
||||
|
||||
instance AbstractHole Type where
|
||||
|
@ -333,7 +333,7 @@ instance Ord1 For where liftCompare = genericLiftCompare
|
||||
instance Show1 For where liftShowsPrec = genericLiftShowsPrec
|
||||
|
||||
instance Evaluatable For where
|
||||
eval eval (fmap (eval >=> Abstract.value) -> For before cond step body) = forLoop before cond step body
|
||||
eval eval (fmap (eval >=> Abstract.value) -> For before cond step body) = Rval <$> forLoop before cond step body
|
||||
|
||||
|
||||
data ForEach a = ForEach { forEachBinding :: !a, forEachSubject :: !a, forEachBody :: !a }
|
||||
@ -362,7 +362,7 @@ instance Ord1 While where liftCompare = genericLiftCompare
|
||||
instance Show1 While where liftShowsPrec = genericLiftShowsPrec
|
||||
|
||||
instance Evaluatable While where
|
||||
eval eval While{..} = while (eval whileCondition >>= Abstract.value) (eval whileBody >>= Abstract.value)
|
||||
eval eval While{..} = Rval <$> while (eval whileCondition >>= Abstract.value) (eval whileBody >>= Abstract.value)
|
||||
|
||||
instance Tokenize While where
|
||||
tokenize While{..} = within' Scope.Loop $ do
|
||||
@ -378,7 +378,7 @@ instance Ord1 DoWhile where liftCompare = genericLiftCompare
|
||||
instance Show1 DoWhile where liftShowsPrec = genericLiftShowsPrec
|
||||
|
||||
instance Evaluatable DoWhile where
|
||||
eval eval DoWhile{..} = doWhile (eval doWhileBody >>= Abstract.value) (eval doWhileCondition >>= Abstract.value)
|
||||
eval eval DoWhile{..} = Rval <$> doWhile (eval doWhileBody >>= Abstract.value) (eval doWhileCondition >>= Abstract.value)
|
||||
|
||||
-- Exception handling
|
||||
|
||||
|
@ -26,7 +26,7 @@ type ModuleC address value m
|
||||
|
||||
type ValueC term address value m
|
||||
= FunctionC term address value (Eff
|
||||
( WhileC address value (Eff
|
||||
( WhileC value (Eff
|
||||
( BooleanC value (Eff
|
||||
( InterposeC (Resumable (BaseError (UnspecializedError value))) (Eff
|
||||
m)))))))
|
||||
@ -44,8 +44,8 @@ evaluate :: ( AbstractValue term address value (ValueC term address value inner)
|
||||
, booleanC ~ BooleanC value (Eff (InterposeC (Resumable (BaseError (UnspecializedError value))) (Eff inner)))
|
||||
, booleanSig ~ (Boolean value :+: Interpose (Resumable (BaseError (UnspecializedError value))) :+: innerSig)
|
||||
, Carrier booleanSig booleanC
|
||||
, whileC ~ WhileC address value (Eff booleanC)
|
||||
, whileSig ~ (While address value :+: booleanSig)
|
||||
, whileC ~ WhileC value (Eff booleanC)
|
||||
, whileSig ~ (While value :+: booleanSig)
|
||||
, Carrier whileSig whileC
|
||||
, functionC ~ FunctionC term address value (Eff whileC)
|
||||
, functionSig ~ (Function term address value :+: whileSig)
|
||||
@ -142,7 +142,7 @@ evalTerm :: ( Carrier sig m
|
||||
, Member (Reader (CurrentFrame address)) sig
|
||||
, Member (Reader (CurrentScope address)) sig
|
||||
, Member (State Span) sig
|
||||
, Member (While address value) sig
|
||||
, Member (While value) sig
|
||||
, Member Fresh sig
|
||||
, Member Trace sig
|
||||
, Ord address
|
||||
|
Loading…
Reference in New Issue
Block a user