From 741a3b25d603dfc1814a8003f0c1284e1521a1f3 Mon Sep 17 00:00:00 2001 From: Timothy Clem Date: Thu, 20 Sep 2018 09:42:55 -0700 Subject: [PATCH] Implement doWhile in terms of the new while effect --- src/Control/Abstract/Value.hs | 44 +++++++++-------------------------- 1 file changed, 11 insertions(+), 33 deletions(-) diff --git a/src/Control/Abstract/Value.hs b/src/Control/Abstract/Value.hs index b26de11a4..9018a8370 100644 --- a/src/Control/Abstract/Value.hs +++ b/src/Control/Abstract/Value.hs @@ -111,44 +111,22 @@ while :: Member (While value) effects while (Evaluator cond) (Evaluator body) = send (While cond body) -- | Do-while loop, built on top of while. -doWhile :: (AbstractValue address value effects, Member (Boolean value) effects) - => Evaluator address value effects value - -> Evaluator address value effects value - -> Evaluator address value effects value -doWhile body cond = undefined +doWhile :: Member (While value) effects + => Evaluator address value effects value + -> Evaluator address value effects value + -> Evaluator address value effects value +doWhile body cond = body *> while cond body -- | C-style for loops. -forLoop :: ( AbstractValue address value effects - , Member (Boolean value) effects - , Member (While value) effects - , Member (Env address) effects - ) - => Evaluator address value effects value -- ^ Initial statement - -> Evaluator address value effects value -- ^ Condition - -> Evaluator address value effects value -- ^ Increment/stepper - -> Evaluator address value effects value -- ^ Body - -> Evaluator address value effects value +forLoop :: (Member (While value) effects, Member (Env address) effects) + => Evaluator address value effects value -- ^ Initial statement + -> Evaluator address value effects value -- ^ Condition + -> Evaluator address value effects value -- ^ Increment/stepper + -> Evaluator address value effects value -- ^ Body + -> Evaluator address value effects value forLoop initial cond step body = locally (initial *> while cond (body *> step)) --- -- | The fundamental looping primitive, built on top of 'ifthenelse'. --- while :: (AbstractValue address value effects, Member (Boolean value) effects) --- => Evaluator address value effects value --- -> Evaluator address value effects value --- -> Evaluator address value effects value --- while cond body = loop $ \ continue -> do --- this <- cond --- ifthenelse this (body *> continue) (pure unit) --- --- -- | Do-while loop, built on top of while. --- doWhile :: (AbstractValue address value effects, Member (Boolean value) effects) --- => Evaluator address value effects value --- -> Evaluator address value effects value --- -> Evaluator address value effects value --- doWhile body cond = loop $ \ continue -> body *> do --- this <- cond --- ifthenelse this continue (pure unit) - data While value m result where While :: m value -> m value -> While value m value