module DDC.Source.Tetra.Compounds
( module DDC.Type.Compounds
, takeAnnotOfExp
, xLAMs
, xLams
, makeXLamFlags
, takeXLAMs
, takeXLams
, takeXLamFlags
, xApps
, makeXAppsWithAnnots
, takeXApps
, takeXApps1
, takeXAppsAsList
, takeXAppsWithAnnots
, takeXConApps
, takeXPrimApps
, xBox
, xRun
, dcUnit
, takeNameOfDaCon
, takeTypeOfDaCon
, bindsOfPat
, pTrue
, pFalse
, wApp
, wApps
, takeXWitness
, takeWAppsAsList
, takePrimWiConApps
, xErrorDefault)
where
import DDC.Source.Tetra.Exp
import DDC.Source.Tetra.Prim
import DDC.Type.Compounds
import Data.Text (Text)
import DDC.Core.Exp.Annot.Compounds
( dcUnit
, takeNameOfDaCon
, takeTypeOfDaCon
, bindsOfPat
, wApp
, wApps
, takeXWitness
, takeWAppsAsList
, takePrimWiConApps)
takeAnnotOfExp :: GExp l -> Maybe (GAnnot l)
takeAnnotOfExp xx
= case xx of
XVar a _ -> Just a
XPrim a _ -> Just a
XCon a _ -> Just a
XLAM a _ _ -> Just a
XLam a _ _ -> Just a
XApp a _ _ -> Just a
XLet a _ _ -> Just a
XCase a _ _ -> Just a
XCast a _ _ -> Just a
XType{} -> Nothing
XWitness{} -> Nothing
XDefix a _ -> Just a
XInfixOp a _ -> Just a
XInfixVar a _ -> Just a
xLAMs :: GAnnot l -> [GBind l] -> GExp l -> GExp l
xLAMs a bs x
= foldr (XLAM a) x bs
xLams :: GAnnot l -> [GBind l] -> GExp l -> GExp l
xLams a bs x
= foldr (XLam a) x bs
takeXLAMs :: GExp l -> Maybe ([GBind l], GExp l)
takeXLAMs xx
= let go bs (XLAM _ b x) = go (b:bs) x
go bs x = (reverse bs, x)
in case go [] xx of
([], _) -> Nothing
(bs, body) -> Just (bs, body)
takeXLams :: GExp l -> Maybe ([GBind l], GExp l)
takeXLams xx
= let go bs (XLam _ b x) = go (b:bs) x
go bs x = (reverse bs, x)
in case go [] xx of
([], _) -> Nothing
(bs, body) -> Just (bs, body)
makeXLamFlags :: GAnnot l -> [(Bool, GBind l)] -> GExp l -> GExp l
makeXLamFlags a fbs x
= foldr (\(f, b) x'
-> if f then XLAM a b x'
else XLam a b x')
x fbs
takeXLamFlags :: GExp l -> Maybe ([(Bool, GBind l)], GExp l)
takeXLamFlags xx
= let go bs (XLAM _ b x) = go ((True, b):bs) x
go bs (XLam _ b x) = go ((False, b):bs) x
go bs x = (reverse bs, x)
in case go [] xx of
([], _) -> Nothing
(bs, body) -> Just (bs, body)
xApps :: GAnnot l -> GExp l -> [GExp l] -> GExp l
xApps a t1 ts = foldl (XApp a) t1 ts
makeXAppsWithAnnots :: GExp l -> [(GExp l, GAnnot l)] -> GExp l
makeXAppsWithAnnots f xas
= case xas of
[] -> f
(arg,a ) : as -> makeXAppsWithAnnots (XApp a f arg) as
takeXApps :: GExp l -> Maybe (GExp l, [GExp l])
takeXApps xx
= case takeXAppsAsList xx of
(x1 : xsArgs) -> Just (x1, xsArgs)
_ -> Nothing
takeXApps1 :: GExp l -> GExp l -> (GExp l, [GExp l])
takeXApps1 x1 x2
= case takeXApps x1 of
Nothing -> (x1, [x2])
Just (x11, x12s) -> (x11, x12s ++ [x2])
takeXAppsAsList :: GExp l -> [GExp l]
takeXAppsAsList xx
= case xx of
XApp _ x1 x2 -> takeXAppsAsList x1 ++ [x2]
_ -> [xx]
takeXAppsWithAnnots :: GExp l -> (GExp l, [(GExp l, GAnnot l)])
takeXAppsWithAnnots xx
= case xx of
XApp a f arg
-> let (f', args') = takeXAppsWithAnnots f
in (f', args' ++ [(arg,a)])
_ -> (xx, [])
takeXPrimApps :: GExp l -> Maybe (GPrim l, [GExp l])
takeXPrimApps xx
= case takeXAppsAsList xx of
XPrim _ p : xs -> Just (p, xs)
_ -> Nothing
takeXConApps :: GExp l -> Maybe (DaCon (GName l), [GExp l])
takeXConApps xx
= case takeXAppsAsList xx of
XCon _ dc : xs -> Just (dc, xs)
_ -> Nothing
xBox a x = XCast a CastBox x
xRun a x = XCast a CastRun x
xErrorDefault :: (GPrim l ~ PrimVal, GName l ~ Name)
=> GAnnot l -> Text -> Integer -> GExp l
xErrorDefault a name n
= xApps a
(XPrim a (PrimValError OpErrorDefault))
[ XCon a (DaConPrim (NameLitTextLit name) (tBot kData))
, XCon a (DaConPrim (NameLitNat n) (tBot kData))]
pTrue = PData (DaConPrim (NameLitBool True) tBool) []
pFalse = PData (DaConPrim (NameLitBool False) tBool) []