module DDC.Core.Flow.Prim.OpControl
( readOpControl
, typeOpControl
, xLoopN
, xGuard
, xSegment
, xSplit)
where
import DDC.Core.Flow.Prim.KiConFlow
import DDC.Core.Flow.Prim.TyConPrim
import DDC.Core.Flow.Prim.TyConFlow
import DDC.Core.Flow.Prim.Base
import DDC.Core.Compounds.Simple
import DDC.Core.Exp.Simple
import DDC.Base.Pretty
import Control.DeepSeq
import Data.Char
import Data.List
instance NFData OpControl
instance Pretty OpControl where
ppr fo
= case fo of
OpControlLoop -> text "loop#"
OpControlLoopN -> text "loopn#"
OpControlGuard -> text "guard#"
OpControlSegment -> text "segment#"
OpControlSplit n -> text "split$" <> int n <> text "#"
readOpControl :: String -> Maybe OpControl
readOpControl str
| Just rest <- stripPrefix "split$" str
, (ds, "#") <- span isDigit rest
, not $ null ds
, arity <- read ds
= Just $ OpControlSplit arity
| otherwise
= case str of
"loop#" -> Just $ OpControlLoop
"loopn#" -> Just $ OpControlLoopN
"guard#" -> Just $ OpControlGuard
"segment#" -> Just $ OpControlSegment
_ -> Nothing
typeOpControl :: OpControl -> Type Name
typeOpControl op
= case op of
OpControlLoop
-> tForall kRate
$ \_ -> (tNat `tFun` tUnit) `tFun` tUnit
OpControlLoopN
-> tForall kRate
$ \kR -> tRateNat kR `tFun` (tNat `tFun` tUnit) `tFun` tUnit
OpControlGuard
-> tRef tNat
`tFun` tBool
`tFun` (tNat `tFun` tUnit)
`tFun` tUnit
OpControlSegment
-> tRef tNat
`tFun` tNat
`tFun` (tNat `tFun` tNat `tFun` tUnit)
`tFun` tUnit
OpControlSplit n
-> tForall kRate
$ \tK -> tRateNat tK
`tFun` (tRateNat (tDown n tK) `tFun` tUnit)
`tFun` (tRateNat (tTail n tK) `tFun` tUnit)
`tFun` tUnit
type TypeF = Type Name
type ExpF = Exp () Name
xLoopN :: TypeF -> ExpF -> ExpF -> ExpF
xLoopN tR xRN xF
= xApps (xVarOpControl OpControlLoopN)
[XType tR, xRN, xF]
xGuard :: ExpF -> ExpF -> ExpF -> ExpF
xGuard xCount xFlag xFun
= xApps (xVarOpControl OpControlGuard)
[xCount, xFlag, xFun]
xSegment :: ExpF -> ExpF -> ExpF -> ExpF
xSegment xCount xIters xFun
= xApps (xVarOpControl OpControlSegment)
[xCount, xIters, xFun]
xSplit :: Int -> TypeF -> ExpF
-> ExpF -> ExpF -> ExpF
xSplit n tK xRN xDownFn xTailFn
= xApps (xVarOpControl $ OpControlSplit n)
[ XType tK, xRN, xDownFn, xTailFn ]
xVarOpControl :: OpControl -> ExpF
xVarOpControl op
= XVar (UPrim (NameOpControl op) (typeOpControl op))