{-# LANGUAGE PostfixOperators #-}
{-# LANGUAGE FlexibleContexts #-}

-- | Language-polymorphic functions that are defined by GOOL code
module Drasil.Shared.LanguageRenderer.Macros (
  ifExists, decrement1, increment, increment1, runStrategy,
  listSlice, makeSetterVal, stringListVals, stringListLists, forRange,
  notifyObservers, notifyObservers', arrayDecAsList
) where

import Drasil.Shared.CodeType (CodeType(..))
import Drasil.Shared.InterfaceCommon (Label, Body, Block, SVariable, SValue,
  bodyStatements, oneLiner, VariableElim(..), getCodeType, listOf,
  ValueSym(valueType), NumericExpression((#+), (#-), (#*), (#/)), Comparison(..),
  BooleanExpression((?&&), (?||)), at, StatementSym(..),
  AssignStatement((&+=), (&-=), (&++)), (&=), convScope)
import qualified Drasil.Shared.InterfaceCommon as IC
import Drasil.GOOL.InterfaceGOOL (($.), observerListName, OOStatement)
import Drasil.Shared.RendererClassesCommon (RenderValue(cast),
  ValueElim(valueInt))
import qualified Drasil.Shared.RendererClassesCommon as S (
  RenderStatement(stmt), RenderValue)
import qualified Drasil.Shared.RendererClassesCommon as RC (BodyElim(..),
  StatementElim(statement))
import Drasil.Shared.Helpers (toCode, onStateValue, on2StateValues)
import Drasil.Shared.State (MS, VS, MS, lensMStoVS, genVarName, genLoopIndex,
  genVarNameIf, getVarScope)
import Drasil.Shared.AST (ScopeData, TypeData, FuncData)

import Data.Maybe (fromMaybe, isNothing)
import Data.Functor ((<&>))
import Control.Lens.Zoom (zoom)
import Text.PrettyPrint.HughesPJ (Doc, vcat)

ifExists
  :: (IC.ControlStatement r smt, IC.ValueExpression r)
  => SValue r -> MS (r Body) -> MS (r Body) -> MS (r smt)
ifExists :: forall (r :: * -> *) smt.
(ControlStatement r smt, ValueExpression r) =>
SValue r -> MS (r Body) -> MS (r Body) -> MS (r smt)
ifExists SValue r
v MS (r Body)
ifBody = [(SValue r, MS (r Body))] -> MS (r Body) -> MS (r smt)
forall (r :: * -> *) smt.
ControlStatement r smt =>
[(SValue r, MS (r Body))] -> MS (r Body) -> MS (r smt)
IC.ifCond [(SValue r -> SValue r
forall (r :: * -> *). ValueExpression r => SValue r -> SValue r
IC.notNull SValue r
v, MS (r Body)
ifBody)]

decrement1
  :: (IC.AssignStatement r smt, IC.Literal r)
  => SVariable r -> MS (r smt)
decrement1 :: forall (r :: * -> *) smt.
(AssignStatement r smt, Literal r) =>
SVariable r -> MS (r smt)
decrement1 SVariable r
v = SVariable r
v SVariable r -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
AssignStatement r smt =>
SVariable r -> SValue r -> MS (r smt)
&-= Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
1

increment
  :: (IC.AssignStatement r smt, IC.NumericExpression r, IC.VariableValue r)
  => SVariable r -> SValue r -> MS (r smt)
increment :: forall (r :: * -> *) smt.
(AssignStatement r smt, NumericExpression r, VariableValue r) =>
SVariable r -> SValue r -> MS (r smt)
increment SVariable r
vr SValue r
vl = SVariable r
vr SVariable r -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
AssignStatement r smt =>
SVariable r -> SValue r -> MS (r smt)
&= SVariable r -> SValue r
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
IC.valueOf SVariable r
vr SValue r -> SValue r -> SValue r
forall (r :: * -> *).
NumericExpression r =>
SValue r -> SValue r -> SValue r
#+ SValue r
vl

increment1
  :: (IC.AssignStatement r smt, IC.Literal r)
  => SVariable r -> MS (r smt)
increment1 :: forall (r :: * -> *) smt.
(AssignStatement r smt, Literal r) =>
SVariable r -> MS (r smt)
increment1 SVariable r
vr = SVariable r
vr SVariable r -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
AssignStatement r smt =>
SVariable r -> SValue r -> MS (r smt)
&+= Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
1

strat
  :: (RC.BodyElim r, Monad r, RC.StatementElim r smt)
  => MS (r smt) -> MS (r Body) -> MS (r Doc)
strat :: forall (r :: * -> *) smt.
(BodyElim r, Monad r, StatementElim r smt) =>
MS (r smt) -> MS (r Body) -> MS (r Body)
strat = (r smt -> r Body -> r Body)
-> State MethodState (r smt)
-> State MethodState (r Body)
-> State MethodState (r Body)
forall a b c s.
(a -> b -> c) -> State s a -> State s b -> State s c
on2StateValues (\r smt
result r Body
b -> Body -> r Body
forall (r :: * -> *) a. Monad r => a -> r a
toCode (Body -> r Body) -> Body -> r Body
forall a b. (a -> b) -> a -> b
$ [Body] -> Body
vcat [r Body -> Body
forall (r :: * -> *). BodyElim r => r Body -> Body
RC.body r Body
b,
  r smt -> Body
forall (r :: * -> *) smt. StatementElim r smt => r smt -> Body
RC.statement r smt
result])

runStrategy
  :: ( IC.AssignStatement r smt
     , RC.BodyElim r
     , Monad r
     , S.RenderStatement r smt
     , RC.StatementElim r smt
     )
  => Label
  -> [(Label, MS (r Body))]
  -> Maybe (SValue r)
  -> Maybe (SVariable r)
  -> MS (r Doc)
runStrategy :: forall (r :: * -> *) smt.
(AssignStatement r smt, BodyElim r, Monad r, RenderStatement r smt,
 StatementElim r smt) =>
Label
-> [(Label, MS (r Body))]
-> Maybe (SValue r)
-> Maybe (SVariable r)
-> MS (r Body)
runStrategy Label
l [(Label, MS (r Body))]
strats Maybe (SValue r)
rv Maybe (SVariable r)
av = MS (r Body)
-> (MS (r Body) -> MS (r Body))
-> Maybe (MS (r Body))
-> MS (r Body)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe
  (Label -> Label -> MS (r Body)
forall {a}. Label -> Label -> a
strError Label
l Label
"RunStrategy called on non-existent strategy")
  (MS (r smt) -> MS (r Body) -> MS (r Body)
forall (r :: * -> *) smt.
(BodyElim r, Monad r, StatementElim r smt) =>
MS (r smt) -> MS (r Body) -> MS (r Body)
strat (MS (r smt) -> MS (r smt)
forall (r :: * -> *) smt.
RenderStatement r smt =>
MS (r smt) -> MS (r smt)
S.stmt MS (r smt)
resultState)) (Label -> [(Label, MS (r Body))] -> Maybe (MS (r Body))
forall a b. Eq a => a -> [(a, b)] -> Maybe b
lookup Label
l [(Label, MS (r Body))]
strats)
  where resultState :: MS (r smt)
resultState = MS (r smt)
-> (SVariable r -> MS (r smt)) -> Maybe (SVariable r) -> MS (r smt)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe MS (r smt)
forall (r :: * -> *) smt. StatementSym r smt => MS (r smt)
IC.emptyStmt SVariable r -> MS (r smt)
asgState Maybe (SVariable r)
av
        asgState :: SVariable r -> MS (r smt)
asgState SVariable r
v = MS (r smt)
-> (SValue r -> MS (r smt)) -> Maybe (SValue r) -> MS (r smt)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (Label -> Label -> MS (r smt)
forall {a}. Label -> Label -> a
strError Label
l
          Label
"Attempt to assign null return to a Value") (SVariable r
v &=) Maybe (SValue r)
rv
        strError :: Label -> Label -> a
strError Label
n Label
s = Label -> a
forall a. HasCallStack => Label -> a
error (Label -> a) -> Label -> a
forall a b. (a -> b) -> a -> b
$ Label
"Strategy '" Label -> Label -> Label
forall a. [a] -> [a] -> [a]
++ Label
n Label -> Label -> Label
forall a. [a] -> [a] -> [a]
++ Label
"': " Label -> Label -> Label
forall a. [a] -> [a] -> [a]
++ Label
s Label -> Label -> Label
forall a. [a] -> [a] -> [a]
++ Label
"."

listSlice
  :: (IC.SharedStatement r smt, ValueElim r, VariableElim r)
  => Maybe (SValue r)
  -> Maybe (SValue r)
  -> Maybe (SValue r)
  -> SVariable r
  -> SValue r
  -> MS (r Block)
listSlice :: forall (r :: * -> *) smt.
(SharedStatement r smt, ValueElim r, VariableElim r) =>
Maybe (SValue r)
-> Maybe (SValue r)
-> Maybe (SValue r)
-> SVariable r
-> SValue r
-> MS (r Body)
listSlice Maybe (SValue r)
beg Maybe (SValue r)
end Maybe (SValue r)
step SVariable r
vnew SValue r
vold = do

  Label
l_temp <- [Label] -> Label -> MS Label
genVarName [] Label
"temp"
  Label
l_i <- MS Label
genLoopIndex
  r Variable
vn <- LensLike'
  (Zoomed (StateT ValueState Identity) (r Variable))
  MethodState
  ValueState
-> SVariable r -> StateT MethodState Identity (r Variable)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (r Variable))
  MethodState
  ValueState
(ValueState -> Focusing Identity (r Variable) ValueState)
-> MethodState -> Focusing Identity (r Variable) MethodState
Lens' MethodState ValueState
lensMStoVS SVariable r
vnew
  ScopeData
scpData <- Label -> MS ScopeData
getVarScope (Label -> MS ScopeData) -> Label -> MS ScopeData
forall a b. (a -> b) -> a -> b
$ r Variable -> Label
forall (r :: * -> *). VariableElim r => r Variable -> Label
variableName r Variable
vn
  let scp :: r ScopeData
scp = ScopeData -> r ScopeData
forall (r :: * -> *). ScopeSym r => ScopeData -> r ScopeData
convScope ScopeData
scpData
      var_temp :: SVariable r
var_temp = Label -> VS (r TypeData) -> SVariable r
forall (r :: * -> *).
VariableSym r =>
Label -> VS (r TypeData) -> SVariable r
IC.var Label
l_temp ((r Variable -> r TypeData) -> SVariable r -> VS (r TypeData)
forall a b s. (a -> b) -> State s a -> State s b
onStateValue r Variable -> r TypeData
forall (r :: * -> *). VariableElim r => r Variable -> r TypeData
variableType SVariable r
vnew)
      v_temp :: SValue r
v_temp = SVariable r -> SValue r
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
IC.valueOf SVariable r
var_temp
      var_i :: SVariable r
var_i = Label -> VS (r TypeData) -> SVariable r
forall (r :: * -> *).
VariableSym r =>
Label -> VS (r TypeData) -> SVariable r
IC.var Label
l_i VS (r TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
IC.int
      v_i :: SValue r
v_i = SVariable r -> SValue r
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
IC.valueOf SVariable r
var_i

  let step' :: SValue r
step' = SValue r -> Maybe (SValue r) -> SValue r
forall a. a -> Maybe a -> a
fromMaybe (Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
1) Maybe (SValue r)
step
  r Value
stepV <- LensLike'
  (Zoomed (StateT ValueState Identity) (r Value))
  MethodState
  ValueState
-> SValue r -> StateT MethodState Identity (r Value)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (r Value))
  MethodState
  ValueState
(ValueState -> Focusing Identity (r Value) ValueState)
-> MethodState -> Focusing Identity (r Value) MethodState
Lens' MethodState ValueState
lensMStoVS SValue r
step'
  let mbStepV :: Maybe Integer
mbStepV = r Value -> Maybe Integer
forall (r :: * -> *). ValueElim r => r Value -> Maybe Integer
valueInt r Value
stepV

  -- Generate fresh variable names if required
  Label
begName <- Bool -> Label -> MS Label
genVarNameIf (Maybe (SValue r) -> Bool
forall a. Maybe a -> Bool
isNothing Maybe (SValue r)
beg Bool -> Bool -> Bool
&& Maybe Integer -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Integer
mbStepV) Label
"begIdx"
  Label
endName <- Bool -> Label -> MS Label
genVarNameIf (Maybe (SValue r) -> Bool
forall a. Maybe a -> Bool
isNothing Maybe (SValue r)
end Bool -> Bool -> Bool
&& Maybe Integer -> Bool
forall a. Maybe a -> Bool
isNothing Maybe Integer
mbStepV) Label
"endIdx"

  let (MS (r smt)
setBeg, SValue r
begVal) = Label
-> SValue r
-> Maybe Integer
-> Maybe (SValue r)
-> SValue r
-> SValue r
-> r ScopeData
-> (MS (r smt), SValue r)
forall (r :: * -> *) smt.
SharedStatement r smt =>
Label
-> SValue r
-> Maybe Integer
-> Maybe (SValue r)
-> SValue r
-> SValue r
-> r ScopeData
-> (MS (r smt), SValue r)
makeSetterVal Label
begName SValue r
step' Maybe Integer
mbStepV Maybe (SValue r)
beg (Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
0)    (SValue r -> SValue r
forall (r :: * -> *) smt. List r smt => SValue r -> SValue r
IC.listSize SValue r
vold SValue r -> SValue r -> SValue r
forall (r :: * -> *).
NumericExpression r =>
SValue r -> SValue r -> SValue r
#- Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
1) r ScopeData
scp
      (MS (r smt)
setEnd, SValue r
endVal) = Label
-> SValue r
-> Maybe Integer
-> Maybe (SValue r)
-> SValue r
-> SValue r
-> r ScopeData
-> (MS (r smt), SValue r)
forall (r :: * -> *) smt.
SharedStatement r smt =>
Label
-> SValue r
-> Maybe Integer
-> Maybe (SValue r)
-> SValue r
-> SValue r
-> r ScopeData
-> (MS (r smt), SValue r)
makeSetterVal Label
endName SValue r
step' Maybe Integer
mbStepV Maybe (SValue r)
end (SValue r -> SValue r
forall (r :: * -> *) smt. List r smt => SValue r -> SValue r
IC.listSize SValue r
vold) (Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt (-Integer
1)) r ScopeData
scp

  Maybe Integer
mbBegV <- case Maybe (SValue r)
beg of
        Maybe (SValue r)
Nothing -> Maybe Integer -> StateT MethodState Identity (Maybe Integer)
forall a. a -> StateT MethodState Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Integer
forall a. Maybe a
Nothing
        (Just SValue r
b) -> LensLike'
  (Zoomed (StateT ValueState Identity) (r Value))
  MethodState
  ValueState
-> SValue r -> StateT MethodState Identity (r Value)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (r Value))
  MethodState
  ValueState
(ValueState -> Focusing Identity (r Value) ValueState)
-> MethodState -> Focusing Identity (r Value) MethodState
Lens' MethodState ValueState
lensMStoVS SValue r
b StateT MethodState Identity (r Value)
-> (r Value -> Maybe Integer)
-> StateT MethodState Identity (Maybe Integer)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> r Value -> Maybe Integer
forall (r :: * -> *). ValueElim r => r Value -> Maybe Integer
valueInt
  Maybe Integer
mbEndV <- case Maybe (SValue r)
end of
        Maybe (SValue r)
Nothing -> Maybe Integer -> StateT MethodState Identity (Maybe Integer)
forall a. a -> StateT MethodState Identity a
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe Integer
forall a. Maybe a
Nothing
        (Just SValue r
e) -> LensLike'
  (Zoomed (StateT ValueState Identity) (r Value))
  MethodState
  ValueState
-> SValue r -> StateT MethodState Identity (r Value)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (r Value))
  MethodState
  ValueState
(ValueState -> Focusing Identity (r Value) ValueState)
-> MethodState -> Focusing Identity (r Value) MethodState
Lens' MethodState ValueState
lensMStoVS SValue r
e StateT MethodState Identity (r Value)
-> (r Value -> Maybe Integer)
-> StateT MethodState Identity (Maybe Integer)
forall (f :: * -> *) a b. Functor f => f a -> (a -> b) -> f b
<&> r Value -> Maybe Integer
forall (r :: * -> *). ValueElim r => r Value -> Maybe Integer
valueInt
  -- Get the condition for the for-loop
  let cond :: SValue r
cond = case Maybe Integer
mbStepV of
              -- If step is a litInt, do a one-sided check
              (Just Integer
s) -> if Integer
s Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
0 then SValue r
v_i SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?< SValue r
endVal else SValue r
v_i SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?> SValue r
endVal
              Maybe Integer
Nothing -> case (Maybe Integer
mbBegV, Maybe Integer
mbEndV) of
                -- If both bounds are litInt's, do a two-sided check.
                -- Also, make sure step is in same direction as check.
                (Just Integer
b, Just Integer
e) -> if Integer
e Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
>= Integer
b
                    then SValue r
begVal SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?<= SValue r
v_i SValue r -> SValue r -> SValue r
forall (r :: * -> *).
BooleanExpression r =>
SValue r -> SValue r -> SValue r
?&& SValue r
v_i SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?< SValue r
endVal SValue r -> SValue r -> SValue r
forall (r :: * -> *).
BooleanExpression r =>
SValue r -> SValue r -> SValue r
?&& SValue r
step' SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?> Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
0
                    else SValue r
endVal SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?< SValue r
v_i SValue r -> SValue r -> SValue r
forall (r :: * -> *).
BooleanExpression r =>
SValue r -> SValue r -> SValue r
?&& SValue r
v_i SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?<= SValue r
begVal SValue r -> SValue r -> SValue r
forall (r :: * -> *).
BooleanExpression r =>
SValue r -> SValue r -> SValue r
?&& SValue r
step' SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?< Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
0
                -- If bounds are not litInt's, do both two-sided checks
                (Maybe Integer, Maybe Integer)
_ ->  SValue r
begVal SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?<= SValue r
v_i SValue r -> SValue r -> SValue r
forall (r :: * -> *).
BooleanExpression r =>
SValue r -> SValue r -> SValue r
?&& SValue r
v_i SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?< SValue r
endVal SValue r -> SValue r -> SValue r
forall (r :: * -> *).
BooleanExpression r =>
SValue r -> SValue r -> SValue r
?&& SValue r
step' SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?> Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
0 SValue r -> SValue r -> SValue r
forall (r :: * -> *).
BooleanExpression r =>
SValue r -> SValue r -> SValue r
?||
                      SValue r
endVal SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?< SValue r
v_i SValue r -> SValue r -> SValue r
forall (r :: * -> *).
BooleanExpression r =>
SValue r -> SValue r -> SValue r
?&& SValue r
v_i SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?<= SValue r
begVal SValue r -> SValue r -> SValue r
forall (r :: * -> *).
BooleanExpression r =>
SValue r -> SValue r -> SValue r
?&& SValue r
step' SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?< Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
0

  [MS (r smt)] -> MS (r Body)
forall (r :: * -> *) smt.
BlockSym r smt =>
[MS (r smt)] -> MS (r Body)
IC.block [
    Integer -> SVariable r -> r ScopeData -> MS (r smt)
forall (r :: * -> *) smt.
DeclStatement r smt =>
Integer -> SVariable r -> r ScopeData -> MS (r smt)
IC.listDec Integer
0 SVariable r
var_temp r ScopeData
scp,
    MS (r smt)
setBeg, MS (r smt)
setEnd,
    MS (r smt) -> SValue r -> MS (r smt) -> MS (r Body) -> MS (r smt)
forall (r :: * -> *) smt.
ControlStatement r smt =>
MS (r smt) -> SValue r -> MS (r smt) -> MS (r Body) -> MS (r smt)
IC.for (SVariable r -> r ScopeData -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
DeclStatement r smt =>
SVariable r -> r ScopeData -> SValue r -> MS (r smt)
IC.varDecDef SVariable r
var_i r ScopeData
forall (r :: * -> *). ScopeSym r => r ScopeData
IC.local SValue r
begVal) SValue r
cond
      (MS (r smt)
-> (SValue r -> MS (r smt)) -> Maybe (SValue r) -> MS (r smt)
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (SVariable r
var_i &++) (SVariable r
var_i &+=) Maybe (SValue r)
step)
      (MS (r smt) -> MS (r Body)
forall (r :: * -> *) smt.
BodySym r smt =>
MS (r smt) -> MS (r Body)
oneLiner (MS (r smt) -> MS (r Body)) -> MS (r smt) -> MS (r Body)
forall a b. (a -> b) -> a -> b
$ SValue r -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
List r smt =>
SValue r -> SValue r -> MS (r smt)
IC.listAppend SValue r
v_temp (SValue r -> SValue r -> SValue r
forall (r :: * -> *) smt.
List r smt =>
SValue r -> SValue r -> SValue r
IC.listAccess SValue r
vold SValue r
v_i)),
    SVariable r
vnew SVariable r -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
AssignStatement r smt =>
SVariable r -> SValue r -> MS (r smt)
&= SValue r
v_temp]

-- Java, C#, C++, and Swift --
-- | Gets the expression and code for setting bounds in a list slice
--   Input:
--   - String: Variable name for bound (to be created if necessary),
--   - SValue: step value
--   - Maybe Integer: literal value of step, if exists
--   - Maybe SValue: given value of bound
--   - SValue: value of bound if bound not given and step is positive
--   - SValue: value of bound if bound not given and step is negative
--   Output: (SValue): (setter, value) of bound
makeSetterVal :: (IC.SharedStatement r smt) => Label -> SValue r ->
  Maybe Integer -> Maybe (SValue r) -> SValue r -> SValue r -> r ScopeData ->
  (MS (r smt), SValue r)
makeSetterVal :: forall (r :: * -> *) smt.
SharedStatement r smt =>
Label
-> SValue r
-> Maybe Integer
-> Maybe (SValue r)
-> SValue r
-> SValue r
-> r ScopeData
-> (MS (r smt), SValue r)
makeSetterVal Label
_     SValue r
_    Maybe Integer
_      (Just SValue r
v) SValue r
_  SValue r
_  r ScopeData
_   = (MS (r smt)
forall (r :: * -> *) smt. StatementSym r smt => MS (r smt)
IC.emptyStmt, SValue r
v)
makeSetterVal Label
_     SValue r
_   (Just Integer
s) Maybe (SValue r)
_       SValue r
lb SValue r
rb r ScopeData
_   = (MS (r smt)
forall (r :: * -> *) smt. StatementSym r smt => MS (r smt)
IC.emptyStmt, if Integer
s Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Integer
0 then SValue r
lb else SValue r
rb)
makeSetterVal Label
vName SValue r
step Maybe Integer
_       Maybe (SValue r)
_       SValue r
lb SValue r
rb  r ScopeData
scp =
  let theVar :: SVariable r
theVar = Label -> VS (r TypeData) -> SVariable r
forall (r :: * -> *).
VariableSym r =>
Label -> VS (r TypeData) -> SVariable r
IC.var Label
vName VS (r TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
IC.int
      theSetter :: MS (r smt)
theSetter = SVariable r -> r ScopeData -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
DeclStatement r smt =>
SVariable r -> r ScopeData -> SValue r -> MS (r smt)
IC.varDecDef SVariable r
theVar r ScopeData
scp (SValue r -> MS (r smt)) -> SValue r -> MS (r smt)
forall a b. (a -> b) -> a -> b
$ SValue r -> SValue r -> SValue r -> SValue r
forall (r :: * -> *).
ValueExpression r =>
SValue r -> SValue r -> SValue r -> SValue r
IC.inlineIf (SValue r
step SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?> Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
0) SValue r
lb SValue r
rb
  in (MS (r smt)
theSetter, SValue r -> SValue r
forall (r :: * -> *). IndexTranslator r => SValue r -> SValue r
IC.intToIndex (SValue r -> SValue r) -> SValue r -> SValue r
forall a b. (a -> b) -> a -> b
$ SVariable r -> SValue r
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
IC.valueOf SVariable r
theVar)

stringListVals
  :: ( IC.AssignStatement r smt
     , IC.List r smt
     , IC.Literal r
     , RenderValue r
     , IC.TypeElim r
     , VariableElim r
     )
  => [SVariable r] -> SValue r -> MS (r smt)
stringListVals :: forall (r :: * -> *) smt.
(AssignStatement r smt, List r smt, Literal r, RenderValue r,
 TypeElim r, VariableElim r) =>
[SVariable r] -> SValue r -> MS (r smt)
stringListVals [SVariable r]
vars SValue r
sl = LensLike'
  (Zoomed (StateT ValueState Identity) (r Value))
  MethodState
  ValueState
-> SValue r -> StateT MethodState Identity (r Value)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (r Value))
  MethodState
  ValueState
(ValueState -> Focusing Identity (r Value) ValueState)
-> MethodState -> Focusing Identity (r Value) MethodState
Lens' MethodState ValueState
lensMStoVS SValue r
sl StateT MethodState Identity (r Value)
-> (r Value -> StateT MethodState Identity (r smt))
-> StateT MethodState Identity (r smt)
forall a b.
StateT MethodState Identity a
-> (a -> StateT MethodState Identity b)
-> StateT MethodState Identity b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (\r Value
slst -> [StateT MethodState Identity (r smt)]
-> StateT MethodState Identity (r smt)
forall (r :: * -> *) smt.
StatementSym r smt =>
[MS (r smt)] -> MS (r smt)
multi ([StateT MethodState Identity (r smt)]
 -> StateT MethodState Identity (r smt))
-> [StateT MethodState Identity (r smt)]
-> StateT MethodState Identity (r smt)
forall a b. (a -> b) -> a -> b
$ CodeType -> [StateT MethodState Identity (r smt)]
checkList
  (r TypeData -> CodeType
forall (r :: * -> *). TypeElim r => r TypeData -> CodeType
getCodeType (r TypeData -> CodeType) -> r TypeData -> CodeType
forall a b. (a -> b) -> a -> b
$ r Value -> r TypeData
forall (r :: * -> *). ValueSym r => r Value -> r TypeData
valueType r Value
slst))
  where checkList :: CodeType -> [StateT MethodState Identity (r smt)]
checkList (List CodeType
String) = [SVariable r] -> Integer -> [StateT MethodState Identity (r smt)]
assignVals [SVariable r]
vars Integer
0
        checkList CodeType
_ = Label -> [StateT MethodState Identity (r smt)]
forall a. HasCallStack => Label -> a
error
          Label
"Value passed to stringListVals must be a list of strings"
        assignVals :: [SVariable r] -> Integer -> [StateT MethodState Identity (r smt)]
assignVals [] Integer
_ = []
        assignVals (SVariable r
v:[SVariable r]
vs) Integer
n = SVariable r -> SValue r -> StateT MethodState Identity (r smt)
forall (r :: * -> *) smt.
AssignStatement r smt =>
SVariable r -> SValue r -> MS (r smt)
IC.assign SVariable r
v (VS (r TypeData) -> SValue r -> SValue r
forall (r :: * -> *).
RenderValue r =>
VS (r TypeData) -> SValue r -> SValue r
cast ((r Variable -> r TypeData) -> SVariable r -> VS (r TypeData)
forall a b s. (a -> b) -> State s a -> State s b
onStateValue r Variable -> r TypeData
forall (r :: * -> *). VariableElim r => r Variable -> r TypeData
variableType SVariable r
v)
          (SValue r -> SValue r -> SValue r
forall (r :: * -> *) smt.
List r smt =>
SValue r -> SValue r -> SValue r
IC.listAccess SValue r
sl (Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
n))) StateT MethodState Identity (r smt)
-> [StateT MethodState Identity (r smt)]
-> [StateT MethodState Identity (r smt)]
forall a. a -> [a] -> [a]
: [SVariable r] -> Integer -> [StateT MethodState Identity (r smt)]
assignVals [SVariable r]
vs (Integer
nInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
1)

stringListLists
  :: (S.RenderValue r, IC.SharedStatement r smt, IC.TypeElim r, VariableElim r)
  => [SVariable r] -> SValue r -> MS (r smt)
stringListLists :: forall (r :: * -> *) smt.
(RenderValue r, SharedStatement r smt, TypeElim r,
 VariableElim r) =>
[SVariable r] -> SValue r -> MS (r smt)
stringListLists [SVariable r]
lsts SValue r
sl = do
  r Value
slst <- LensLike'
  (Zoomed (StateT ValueState Identity) (r Value))
  MethodState
  ValueState
-> SValue r -> StateT MethodState Identity (r Value)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (r Value))
  MethodState
  ValueState
(ValueState -> Focusing Identity (r Value) ValueState)
-> MethodState -> Focusing Identity (r Value) MethodState
Lens' MethodState ValueState
lensMStoVS SValue r
sl
  Label
l_i <- MS Label
genLoopIndex
  let
    checkList :: CodeType -> MS (r smt)
checkList (List CodeType
String) = (SVariable r -> StateT MethodState Identity (r Variable))
-> [SVariable r] -> StateT MethodState Identity [r Variable]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM (LensLike'
  (Zoomed (StateT ValueState Identity) (r Variable))
  MethodState
  ValueState
-> SVariable r -> StateT MethodState Identity (r Variable)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (r Variable))
  MethodState
  ValueState
(ValueState -> Focusing Identity (r Variable) ValueState)
-> MethodState -> Focusing Identity (r Variable) MethodState
Lens' MethodState ValueState
lensMStoVS) [SVariable r]
lsts StateT MethodState Identity [r Variable]
-> ([r Variable] -> MS (r smt)) -> MS (r smt)
forall a b.
StateT MethodState Identity a
-> (a -> StateT MethodState Identity b)
-> StateT MethodState Identity b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= [CodeType] -> MS (r smt)
listVals ([CodeType] -> MS (r smt))
-> ([r Variable] -> [CodeType]) -> [r Variable] -> MS (r smt)
forall b c a. (b -> c) -> (a -> b) -> a -> c
.
      (r Variable -> CodeType) -> [r Variable] -> [CodeType]
forall a b. (a -> b) -> [a] -> [b]
map (r TypeData -> CodeType
forall (r :: * -> *). TypeElim r => r TypeData -> CodeType
getCodeType (r TypeData -> CodeType)
-> (r Variable -> r TypeData) -> r Variable -> CodeType
forall b c a. (b -> c) -> (a -> b) -> a -> c
. r Variable -> r TypeData
forall (r :: * -> *). VariableElim r => r Variable -> r TypeData
variableType)
    checkList CodeType
_ = Label -> MS (r smt)
forall a. HasCallStack => Label -> a
error
      Label
"Value passed to stringListLists must be a list of strings"
    listVals :: [CodeType] -> MS (r smt)
listVals [] = MS (r smt)
loop
    listVals (List CodeType
_:[CodeType]
vs) = [CodeType] -> MS (r smt)
listVals [CodeType]
vs
    listVals [CodeType]
_ = Label -> MS (r smt)
forall a. HasCallStack => Label -> a
error
      Label
"All values passed to stringListLists must have list types"
    loop :: MS (r smt)
loop = SVariable r
-> SValue r -> SValue r -> SValue r -> MS (r Body) -> MS (r smt)
forall (r :: * -> *) smt.
ControlStatement r smt =>
SVariable r
-> SValue r -> SValue r -> SValue r -> MS (r Body) -> MS (r smt)
IC.forRange SVariable r
var_i (Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
0) (SValue r -> SValue r
forall (r :: * -> *) smt. List r smt => SValue r -> SValue r
IC.listSize SValue r
sl SValue r -> SValue r -> SValue r
forall (r :: * -> *).
NumericExpression r =>
SValue r -> SValue r -> SValue r
#/ SValue r
numLists)
      (Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
1) ([MS (r smt)] -> MS (r Body)
forall (r :: * -> *) smt.
BodySym r smt =>
[MS (r smt)] -> MS (r Body)
bodyStatements ([MS (r smt)] -> MS (r Body)) -> [MS (r smt)] -> MS (r Body)
forall a b. (a -> b) -> a -> b
$ [SValue r] -> Integer -> [MS (r smt)]
appendLists ((SVariable r -> SValue r) -> [SVariable r] -> [SValue r]
forall a b. (a -> b) -> [a] -> [b]
map SVariable r -> SValue r
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
IC.valueOf [SVariable r]
lsts) Integer
0)
    appendLists :: [SValue r] -> Integer -> [MS (r smt)]
appendLists [] Integer
_ = []
    appendLists (SValue r
v:[SValue r]
vs) Integer
n = SValue r -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
List r smt =>
SValue r -> SValue r -> MS (r smt)
IC.listAppend SValue r
v (VS (r TypeData) -> SValue r -> SValue r
forall (r :: * -> *).
RenderValue r =>
VS (r TypeData) -> SValue r -> SValue r
cast
      (VS (r TypeData) -> VS (r TypeData)
forall (r :: * -> *).
TypeSym r =>
VS (r TypeData) -> VS (r TypeData)
IC.innerType (VS (r TypeData) -> VS (r TypeData))
-> VS (r TypeData) -> VS (r TypeData)
forall a b. (a -> b) -> a -> b
$ (r Value -> r TypeData) -> SValue r -> VS (r TypeData)
forall a b s. (a -> b) -> State s a -> State s b
onStateValue r Value -> r TypeData
forall (r :: * -> *). ValueSym r => r Value -> r TypeData
valueType SValue r
v)
      (SValue r -> SValue r -> SValue r
forall (r :: * -> *) smt.
List r smt =>
SValue r -> SValue r -> SValue r
IC.listAccess SValue r
sl ((SValue r
v_i SValue r -> SValue r -> SValue r
forall (r :: * -> *).
NumericExpression r =>
SValue r -> SValue r -> SValue r
#* SValue r
numLists) SValue r -> SValue r -> SValue r
forall (r :: * -> *).
NumericExpression r =>
SValue r -> SValue r -> SValue r
#+ Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
n)))
      MS (r smt) -> [MS (r smt)] -> [MS (r smt)]
forall a. a -> [a] -> [a]
: [SValue r] -> Integer -> [MS (r smt)]
appendLists [SValue r]
vs (Integer
nInteger -> Integer -> Integer
forall a. Num a => a -> a -> a
+Integer
1)
    numLists :: SValue r
numLists = Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt (Int -> Integer
forall a. Integral a => a -> Integer
toInteger (Int -> Integer) -> Int -> Integer
forall a b. (a -> b) -> a -> b
$ [SVariable r] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [SVariable r]
lsts)
    var_i :: SVariable r
var_i = Label -> VS (r TypeData) -> SVariable r
forall (r :: * -> *).
VariableSym r =>
Label -> VS (r TypeData) -> SVariable r
IC.var Label
l_i VS (r TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
IC.int
    v_i :: SValue r
v_i = SVariable r -> SValue r
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
IC.valueOf SVariable r
var_i
  CodeType -> MS (r smt)
checkList (r TypeData -> CodeType
forall (r :: * -> *). TypeElim r => r TypeData -> CodeType
getCodeType (r TypeData -> CodeType) -> r TypeData -> CodeType
forall a b. (a -> b) -> a -> b
$ r Value -> r TypeData
forall (r :: * -> *). ValueSym r => r Value -> r TypeData
valueType r Value
slst)

forRange :: (IC.SharedStatement r smt) => SVariable r -> SValue r -> SValue r ->
  SValue r -> MS (r Body) -> MS (r smt)
forRange :: forall (r :: * -> *) smt.
SharedStatement r smt =>
SVariable r
-> SValue r -> SValue r -> SValue r -> MS (r Body) -> MS (r smt)
forRange SVariable r
i SValue r
initv SValue r
finalv SValue r
stepv = MS (r smt) -> SValue r -> MS (r smt) -> MS (r Body) -> MS (r smt)
forall (r :: * -> *) smt.
ControlStatement r smt =>
MS (r smt) -> SValue r -> MS (r smt) -> MS (r Body) -> MS (r smt)
IC.for (SVariable r -> r ScopeData -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
DeclStatement r smt =>
SVariable r -> r ScopeData -> SValue r -> MS (r smt)
IC.varDecDef SVariable r
i r ScopeData
forall (r :: * -> *). ScopeSym r => r ScopeData
IC.local SValue r
initv)
  (SVariable r -> SValue r
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
IC.valueOf SVariable r
i SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?< SValue r
finalv) (SVariable r
i SVariable r -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
AssignStatement r smt =>
SVariable r -> SValue r -> MS (r smt)
&+= SValue r
stepv)

observerIndex :: (IC.VariableSym r) => SVariable r
observerIndex :: forall (r :: * -> *). VariableSym r => SVariable r
observerIndex = Label -> VS (r TypeData) -> SVariable r
forall (r :: * -> *).
VariableSym r =>
Label -> VS (r TypeData) -> SVariable r
IC.var Label
"observerIndex" VS (r TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
IC.int

observerIdxVal :: (IC.VariableValue r) => SValue r
observerIdxVal :: forall (r :: * -> *). VariableValue r => SValue r
observerIdxVal = SVariable r -> SValue r
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
IC.valueOf SVariable r
forall (r :: * -> *). VariableSym r => SVariable r
observerIndex

obsList :: (IC.VariableValue r) => VS (r TypeData) -> SValue r
obsList :: forall (r :: * -> *).
VariableValue r =>
VS (r TypeData) -> SValue r
obsList VS (r TypeData)
t = SVariable r -> SValue r
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
IC.valueOf (SVariable r -> SValue r) -> SVariable r -> SValue r
forall a b. (a -> b) -> a -> b
$ Label -> VS (r TypeData) -> SVariable r
forall (r :: * -> *).
VariableSym r =>
Label -> VS (r TypeData) -> SVariable r
listOf Label
observerListName VS (r TypeData)
t

notify
  :: (OOStatement r smt)
  => VS (r TypeData) -> VS (r FuncData) -> MS (r Body)
notify :: forall (r :: * -> *) smt.
OOStatement r smt =>
VS (r TypeData) -> VS (r FuncData) -> MS (r Body)
notify VS (r TypeData)
t VS (r FuncData)
f = MS (r smt) -> MS (r Body)
forall (r :: * -> *) smt.
BodySym r smt =>
MS (r smt) -> MS (r Body)
oneLiner (MS (r smt) -> MS (r Body)) -> MS (r smt) -> MS (r Body)
forall a b. (a -> b) -> a -> b
$ SValue r -> MS (r smt)
forall (r :: * -> *) smt.
StatementSym r smt =>
SValue r -> MS (r smt)
IC.valStmt (SValue r -> MS (r smt)) -> SValue r -> MS (r smt)
forall a b. (a -> b) -> a -> b
$ SValue r -> SValue r -> SValue r
forall (r :: * -> *) smt.
List r smt =>
SValue r -> SValue r -> SValue r
at (VS (r TypeData) -> SValue r
forall (r :: * -> *).
VariableValue r =>
VS (r TypeData) -> SValue r
obsList VS (r TypeData)
t) SValue r
forall (r :: * -> *). VariableValue r => SValue r
observerIdxVal SValue r -> VS (r FuncData) -> SValue r
forall (r :: * -> *).
OOFunctionSym r =>
SValue r -> VS (r FuncData) -> SValue r
$. VS (r FuncData)
f

notifyObservers
  :: (OOStatement r smt)
  => VS (r FuncData) -> VS (r TypeData) -> MS (r smt)
notifyObservers :: forall (r :: * -> *) smt.
OOStatement r smt =>
VS (r FuncData) -> VS (r TypeData) -> MS (r smt)
notifyObservers VS (r FuncData)
f VS (r TypeData)
t = MS (r smt) -> SValue r -> MS (r smt) -> MS (r Body) -> MS (r smt)
forall (r :: * -> *) smt.
ControlStatement r smt =>
MS (r smt) -> SValue r -> MS (r smt) -> MS (r Body) -> MS (r smt)
IC.for MS (r smt)
initv (SValue r
forall (r :: * -> *). VariableValue r => SValue r
observerIdxVal SValue r -> SValue r -> SValue r
forall (r :: * -> *).
Comparison r =>
SValue r -> SValue r -> SValue r
?< SValue r -> SValue r
forall (r :: * -> *) smt. List r smt => SValue r -> SValue r
IC.listSize (VS (r TypeData) -> SValue r
forall (r :: * -> *).
VariableValue r =>
VS (r TypeData) -> SValue r
obsList VS (r TypeData)
t))
  (SVariable r
forall (r :: * -> *). VariableSym r => SVariable r
observerIndex &++) (VS (r TypeData) -> VS (r FuncData) -> MS (r Body)
forall (r :: * -> *) smt.
OOStatement r smt =>
VS (r TypeData) -> VS (r FuncData) -> MS (r Body)
notify VS (r TypeData)
t VS (r FuncData)
f)
  where initv :: MS (r smt)
initv = SVariable r -> r ScopeData -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
DeclStatement r smt =>
SVariable r -> r ScopeData -> SValue r -> MS (r smt)
IC.varDecDef SVariable r
forall (r :: * -> *). VariableSym r => SVariable r
observerIndex r ScopeData
forall (r :: * -> *). ScopeSym r => r ScopeData
IC.local (SValue r -> MS (r smt)) -> SValue r -> MS (r smt)
forall a b. (a -> b) -> a -> b
$ Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
0

notifyObservers'
  :: (OOStatement r smt)
  => VS (r FuncData) -> VS (r TypeData) -> MS (r smt)
notifyObservers' :: forall (r :: * -> *) smt.
OOStatement r smt =>
VS (r FuncData) -> VS (r TypeData) -> MS (r smt)
notifyObservers' VS (r FuncData)
f VS (r TypeData)
t = SVariable r
-> SValue r -> SValue r -> SValue r -> MS (r Body) -> MS (r smt)
forall (r :: * -> *) smt.
ControlStatement r smt =>
SVariable r
-> SValue r -> SValue r -> SValue r -> MS (r Body) -> MS (r smt)
IC.forRange SVariable r
forall (r :: * -> *). VariableSym r => SVariable r
observerIndex SValue r
initv (SValue r -> SValue r
forall (r :: * -> *) smt. List r smt => SValue r -> SValue r
IC.listSize (SValue r -> SValue r) -> SValue r -> SValue r
forall a b. (a -> b) -> a -> b
$ VS (r TypeData) -> SValue r
forall (r :: * -> *).
VariableValue r =>
VS (r TypeData) -> SValue r
obsList VS (r TypeData)
t )
    (Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
1) (VS (r TypeData) -> VS (r FuncData) -> MS (r Body)
forall (r :: * -> *) smt.
OOStatement r smt =>
VS (r TypeData) -> VS (r FuncData) -> MS (r Body)
notify VS (r TypeData)
t VS (r FuncData)
f)
    where initv :: SValue r
initv = Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
0

arrayDecAsList
  :: (IC.SharedStatement r smt, VariableElim r)
  => Integer -> SValue r -> SVariable r -> r ScopeData -> MS (r smt)
arrayDecAsList :: forall (r :: * -> *) smt.
(SharedStatement r smt, VariableElim r) =>
Integer -> SValue r -> SVariable r -> r ScopeData -> MS (r smt)
arrayDecAsList Integer
len SValue r
dflt SVariable r
vr r ScopeData
scp = do
  r Variable
vr' <- LensLike'
  (Zoomed (StateT ValueState Identity) (r Variable))
  MethodState
  ValueState
-> SVariable r -> StateT MethodState Identity (r Variable)
forall c.
LensLike'
  (Zoomed (StateT ValueState Identity) c) MethodState ValueState
-> StateT ValueState Identity c -> StateT MethodState Identity c
forall (m :: * -> *) (n :: * -> *) s t c.
Zoom m n s t =>
LensLike' (Zoomed m c) t s -> m c -> n c
zoom LensLike'
  (Zoomed (StateT ValueState Identity) (r Variable))
  MethodState
  ValueState
(ValueState -> Focusing Identity (r Variable) ValueState)
-> MethodState -> Focusing Identity (r Variable) MethodState
Lens' MethodState ValueState
lensMStoVS SVariable r
vr
  let innerTp :: VS (r TypeData)
innerTp = VS (r TypeData) -> VS (r TypeData)
forall (r :: * -> *).
TypeSym r =>
VS (r TypeData) -> VS (r TypeData)
IC.innerType (VS (r TypeData) -> VS (r TypeData))
-> VS (r TypeData) -> VS (r TypeData)
forall a b. (a -> b) -> a -> b
$ r TypeData -> VS (r TypeData)
forall a. a -> StateT ValueState Identity a
forall (m :: * -> *) a. Monad m => a -> m a
return (r TypeData -> VS (r TypeData)) -> r TypeData -> VS (r TypeData)
forall a b. (a -> b) -> a -> b
$ r Variable -> r TypeData
forall (r :: * -> *). VariableElim r => r Variable -> r TypeData
variableType r Variable
vr'
  Label
i <- [Label] -> Label -> MS Label
genVarName [] Label
"i"
  [MS (r smt)] -> MS (r smt)
forall (r :: * -> *) smt.
StatementSym r smt =>
[MS (r smt)] -> MS (r smt)
multi [
    SVariable r -> r ScopeData -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
DeclStatement r smt =>
SVariable r -> r ScopeData -> SValue r -> MS (r smt)
IC.varDecDef SVariable r
vr r ScopeData
scp (VS (r TypeData) -> [SValue r] -> SValue r
forall (r :: * -> *).
Literal r =>
VS (r TypeData) -> [SValue r] -> SValue r
IC.litList VS (r TypeData)
innerTp []),
    SVariable r
-> SValue r -> SValue r -> SValue r -> MS (r Body) -> MS (r smt)
forall (r :: * -> *) smt.
ControlStatement r smt =>
SVariable r
-> SValue r -> SValue r -> SValue r -> MS (r Body) -> MS (r smt)
IC.forRange (Label -> VS (r TypeData) -> SVariable r
forall (r :: * -> *).
VariableSym r =>
Label -> VS (r TypeData) -> SVariable r
IC.var Label
i VS (r TypeData)
forall (r :: * -> *). TypeSym r => VS (r TypeData)
IC.int) (Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
0) (Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
len) (Integer -> SValue r
forall (r :: * -> *). Literal r => Integer -> SValue r
IC.litInt Integer
1)
      (MS (r smt) -> MS (r Body)
forall (r :: * -> *) smt.
BodySym r smt =>
MS (r smt) -> MS (r Body)
oneLiner (MS (r smt) -> MS (r Body)) -> MS (r smt) -> MS (r Body)
forall a b. (a -> b) -> a -> b
$ SValue r -> SValue r -> MS (r smt)
forall (r :: * -> *) smt.
List r smt =>
SValue r -> SValue r -> MS (r smt)
IC.listAppend (SVariable r -> SValue r
forall (r :: * -> *). VariableValue r => SVariable r -> SValue r
IC.valueOf SVariable r
vr) SValue r
dflt)]