@@ -14,7 +14,7 @@ import Data.Lazy (Lazy, defer, force)
1414import Data.List as List
1515import Data.Map (Map)
1616import Data.Map as Map
17−import Data.Maybe (Maybe(..), fromMaybe)
17+import Data.Maybe (Maybe(..))
1818import Data.Monoid (power)
1919import Data.Newtype (class Newtype, unwrap)
2020import Data.Set as Set
@@ -44,7 +44,7 @@ data BackendSemantics
4444 | SemEffectBind (Maybe Ident) BackendSemantics (BackendSemantics -> BackendSemantics)
4545 | SemEffectPure BackendSemantics
4646 | SemEffectDefer BackendSemantics
47− | SemBranch (NonEmptyArray (Lazy (SemConditional BackendSemantics))) (Maybe (Lazy BackendSemantics))
47+ | SemBranch (NonEmptyArray (SemConditional BackendSemantics)) (Lazy BackendSemantics)
4848 | NeutLocal (Maybe Ident) Level
4949 | NeutVar (Qualified Ident)
5050 | NeutStop (Qualified Ident)
@@ -55,21 +55,17 @@ data BackendSemantics
5555 | NeutUpdate BackendSemantics (Array (Prop BackendSemantics))
5656 | NeutLit (Literal BackendSemantics)
5757 | NeutFail String
58− | NeutBacktrack
5958 | NeutUncurriedApp BackendSemantics (Array BackendSemantics)
6059 | NeutUncurriedEffectApp BackendSemantics (Array BackendSemantics)
6160 | NeutPrimOp (BackendOperator BackendSemantics)
6261 | NeutPrimEffect (BackendEffect BackendSemantics)
6362 | NeutPrimUndefined
6463
65−data SemConditional a = SemConditional a (Maybe (SemTry a) -> a)
66−
67−type SemTry a = Tuple (Array (Lazy (SemConditional a))) (Maybe (Lazy a))
64+data SemConditional a = SemConditional (Lazy a) (Lazy a)
6865
6966data BackendExpr
7067 = ExprSyntax BackendAnalysis (BackendSyntax BackendExpr)
7168 | ExprRewrite BackendAnalysis BackendRewrite
72− | ExprBacktrack
7369
7470type LetBindingAssoc a =
7571 { ident :: Maybe Ident
@@ -126,7 +122,6 @@ instance HasAnalysis BackendExpr where
126122 analysisOf = case _ of
127123 ExprSyntax s _ -> s
128124 ExprRewrite s _ -> s
129− ExprBacktrack -> mempty
130125
131126instance HasSyntax BackendExpr where
132127 syntaxOf = case _ of
@@ -169,7 +164,6 @@ newtype Env = Env
169164 , evalExtern :: Env -> Qualified Ident -> Array ExternSpine -> Maybe BackendSemantics
170165 , locals :: Array (LocalBinding BackendSemantics)
171166 , directives :: InlineDirectiveMap
172− , branchTry :: Maybe (SemTry BackendSemantics)
173167 }
174168
175169derive instance Newtype Env _
@@ -201,9 +195,6 @@ addStop (Env env) ref acc = Env env
201195 env.directives
202196 }
203197
204−withBranchTry :: Env -> Maybe (SemTry BackendSemantics) -> Env
205−withBranchTry (Env env) branchTry = Env env { branchTry = branchTry }
206−
207198class Eval f where
208199 eval :: Env -> f -> BackendSemantics
209200
@@ -265,17 +256,8 @@ instance Eval f => Eval (BackendSyntax f) where
265256 evalAccessor env (eval env lhs) accessor
266257 Update lhs updates ->
267258 evalUpdate env (eval env lhs) (map (eval env) <$> updates)
268− Branch branches def -> do
269− let conds1 = evalPair env <$> branches
270− case def of
271− Just def' ->
272− evalBranches env conds1 (Just (defer \_ -> eval env def'))
273− Nothing ->
274− case (unwrap env).branchTry of
275− Nothing ->
276− evalBranches env conds1 Nothing
277− Just (Tuple conds2 def') ->
278− evalBranches env (NonEmptyArray.appendArray conds1 conds2) def'
259+ Branch branches def ->
260+ evalBranches env (evalPair env <$> branches) (defer \_ -> eval env def)
279261 PrimOp op ->
280262 evalPrimOp env (eval env <$> op)
281263 PrimEffect eff ->
@@ -358,9 +340,9 @@ instance Eval BackendExpr where
358340 []
359341 RewriteDistBranchesLet _ _ branches def body ->
360342 rewriteBranches (flip eval body <<< bindLocal env <<< One)
361− $ evalBranches env (evalPair env <$> branches) (Just (defer \_ -> eval env def))
343+ $ evalBranches env (evalPair env <$> branches) (defer \_ -> eval env def)
362344 RewriteDistBranchesOp branches def op ->
363− rewriteBranches dist $ evalBranches env (evalPair env <$> branches) (Just (defer \_ -> eval env def))
345+ rewriteBranches dist $ evalBranches env (evalPair env <$> branches) (defer \_ -> eval env def)
364346 where
365347 dist = case op of
366348 DistApp spine ->
@@ -377,8 +359,6 @@ instance Eval BackendExpr where
377359 evalPrimOp env <<< Op2 op2 (eval env lhs)
378360 ExprSyntax _ expr ->
379361 eval env expr
380− ExprBacktrack ->
381− NeutBacktrack
382362
383363instance Eval NeutralExpr where
384364 eval env (NeutralExpr a) = eval env a
@@ -536,31 +516,26 @@ evalUpdate initEnv initLhs props =
536516 _ ->
537517 NeutUpdate lhs props
538518
539−evalBranches :: Env -> NonEmptyArray (Lazy (SemConditional BackendSemantics)) -> Maybe (Lazy BackendSemantics) -> BackendSemantics
519+evalBranches :: Env -> NonEmptyArray (SemConditional BackendSemantics) -> Lazy BackendSemantics -> BackendSemantics
540520evalBranches _ initConds initDef = go [] (NonEmptyArray.toArray initConds) initDef
541521 where
542522 go acc conds def = case Array.uncons conds of
543523 Just { head, tail } ->
544− case force head of
545− SemConditional (NeutLit (LitBoolean didMatch)) k ->
546− if didMatch then
547− go acc [] (Just (defer \_ -> k (Just (Tuple tail def))))
548− else
549− go acc tail def
550− SemConditional (NeutFail err) _ ->
551− go acc [] (Just (defer \_ -> NeutFail err))
552− _ ->
553− go (Array.snoc acc head) tail def
524+ case head of
525+ SemConditional c t -> case force c of
526+ NeutLit (LitBoolean didMatch)
527+ | didMatch -> go acc [] t
528+ | otherwise -> go acc tail def
529+ NeutFail err ->
530+ go acc [] (defer \_ -> NeutFail err)
531+ _ ->
532+ go (Array.snoc acc head) tail def
554533 Nothing ->
555534 case NonEmptyArray.fromArray acc of
556535 Just bs ->
557536 SemBranch bs def
558537 Nothing ->
559− case def of
560− Just sem ->
561− force sem
562− Nothing ->
563− NeutBacktrack
538+ force def
564539
565540rewriteBranches :: (BackendSemantics -> BackendSemantics) -> BackendSemantics -> BackendSemantics
566541rewriteBranches k = go
@@ -571,12 +546,12 @@ rewriteBranches k = go
571546 SemLetRec a b ->
572547 SemLetRec a (go <$> b)
573548 SemBranch bs def ->
574− SemBranch (map (\(SemConditional a b) -> SemConditional a (go <$> b)) <$> bs) (map go <$> def)
549+ SemBranch ((\(SemConditional a b) -> SemConditional a (go <$> b)) <$> bs) (go <$> def)
575550 sem ->
576551 k sem
577552
578−evalPair :: forall f. Eval f => Env -> Pair f -> Lazy (SemConditional BackendSemantics)
579−evalPair env (Pair a b) = defer \_ -> SemConditional (eval env a) (flip eval b <<< withBranchTry env)
553+evalPair :: forall f. Eval f => Env -> Pair f -> SemConditional BackendSemantics
554+evalPair env (Pair a b) = SemConditional (defer \_ -> eval env a) (defer \_ -> eval env b)
580555
581556evalAssocLet :: Env -> BackendSemantics -> (Env -> BackendSemantics -> BackendSemantics) -> BackendSemantics
582557evalAssocLet env sem go = case sem of
@@ -1076,9 +1051,9 @@ quote = go
10761051 build ctx $ EffectDefer (quote (ctx { effect = true }) sem)
10771052 SemBranch branches def -> do
10781053 let ctx' = ctx { effect = false }
1079− let quoteCond (SemConditional a k) = buildPair ctx' (quote ctx' a) (quote ctx (k Nothing))
1080− let branches' = quoteCond <<< force <$> branches
1081− fromMaybe ExprBacktrack $ foldr (buildBranchCond ctx) (quote ctx <<< force <$> def) branches'
1054+ let quoteCond (SemConditional a b) = Pair (quote ctx' $ force a) (quote ctx $ force b)
1055+ let branches' = quoteCond <$> branches
1056+ foldr (buildBranchCond ctx) (quote ctx <<< force $ def) branches'
10821057
10831058 -- Non-block constructors
10841059 SemExtern _ _ sem ->
@@ -1146,8 +1121,6 @@ quote = go
11461121 build ctx PrimUndefined
11471122 NeutFail err ->
11481123 build ctx $ Fail err
1149− NeutBacktrack ->
1150− ExprBacktrack
11511124
11521125build :: Ctx -> BackendSyntax BackendExpr -> BackendExpr
11531126build ctx = case _ of
@@ -1186,22 +1159,22 @@ build ctx = case _ of
11861159 Let _ level binding body
11871160 | Just expr' <- shouldEtaReduce level binding body ->
11881161 expr'
1189− App (ExprSyntax analysis (Branch bs (Just def))) tl
1162+ App (ExprSyntax analysis (Branch bs def)) tl
11901163 | Just expr' <- shouldDistributeBranchApps analysis bs def tl ->
11911164 expr'
1192− UncurriedApp (ExprSyntax analysis (Branch bs (Just def))) tl
1165+ UncurriedApp (ExprSyntax analysis (Branch bs def)) tl
11931166 | Just expr' <- shouldDistributeBranchUncurriedApps analysis bs def tl ->
11941167 expr'
1195− Accessor (ExprSyntax analysis (Branch bs (Just def))) acc
1168+ Accessor (ExprSyntax analysis (Branch bs def)) acc
11961169 | Just expr' <- shouldDistributeBranchAccessor analysis bs def acc ->
11971170 expr'
1198− PrimOp (Op1 op1 (ExprSyntax analysis (Branch bs (Just def))))
1171+ PrimOp (Op1 op1 (ExprSyntax analysis (Branch bs def)))
11991172 | Just expr' <- shouldDistributeBranchPrimOp1 analysis bs def op1 ->
12001173 expr'
1201− PrimOp (Op2 op2 (ExprSyntax analysis (Branch bs (Just def))) rhs)
1174+ PrimOp (Op2 op2 (ExprSyntax analysis (Branch bs def)) rhs)
12021175 | Just expr' <- shouldDistributeBranchPrimOp2L analysis bs def op2 rhs ->
12031176 expr'
1204− PrimOp (Op2 op2 lhs (ExprSyntax analysis (Branch bs (Just def))))
1177+ PrimOp (Op2 op2 lhs (ExprSyntax analysis (Branch bs def)))
12051178 | Just expr' <- shouldDistributeBranchPrimOp2R analysis bs def lhs op2 ->
12061179 expr'
12071180 expr@(EffectBind ident1 level1 (ExprSyntax _ (EffectBind ident2 level2 binding2 body2)) body1) ->
@@ -1237,41 +1210,31 @@ build ctx = case _ of
12371210 binding
12381211 EffectDefer expr@(ExprSyntax _ (EffectDefer _)) ->
12391212 expr
1240− Branch pairs (Just def) | Just expr <- simplifyBranches ctx pairs def ->
1213+ Branch pairs def | Just expr <- simplifyBranches ctx pairs def ->
12411214 expr
12421215 PrimOp (Op1 OpBooleanNot (ExprSyntax _ (PrimOp (Op1 OpBooleanNot expr)))) ->
12431216 expr
12441217 expr ->
12451218 buildDefault ctx expr
12461219
1247−buildPair :: Ctx -> BackendExpr -> BackendExpr -> Pair BackendExpr
1248−buildPair ctx p1 = case _ of
1249− ExprSyntax _ (Branch bs Nothing)
1250− | [ Pair p2 b ] <- NonEmptyArray.toArray bs ->
1251− Pair (build ctx $ PrimOp (Op2 OpBooleanAnd p1 p2)) b
1252− p2 ->
1253− Pair p1 p2
1254−
1255−buildBranchCond :: Ctx -> Pair BackendExpr -> Maybe BackendExpr -> Maybe BackendExpr
1220+buildBranchCond :: Ctx -> Pair BackendExpr -> BackendExpr -> BackendExpr
12561221buildBranchCond ctx (Pair a b) c = case b of
12571222 ExprSyntax _ (Lit (LitBoolean true))
1258− | Just (ExprSyntax _ (Lit (LitBoolean true))) <- c ->
1223+ | ExprSyntax _ (Lit (LitBoolean true)) <- c ->
12591224 c
1260− | Just (ExprSyntax _ (Lit (LitBoolean false))) <- c ->
1261− Just a
1262− | Just x@(ExprSyntax _ x') <- c, isBooleanTail x' ->
1263− Just $ build ctx (PrimOp (Op2 OpBooleanOr a x))
1225+ | ExprSyntax _ (Lit (LitBoolean false)) <- c ->
1226+ a
1227+ | ExprSyntax _ x <- c, isBooleanTail x ->
1228+ build ctx (PrimOp (Op2 OpBooleanOr a c))
12641229 ExprSyntax _ (Lit (LitBoolean false))
1265− | Just (ExprSyntax _ (Lit (LitBoolean false))) <- c ->
1230+ | ExprSyntax _ (Lit (LitBoolean false)) <- c ->
12661231 c
12671232 | ExprSyntax _ (PrimOp (Op1 (OpIsTag _) (ExprSyntax _ x1))) <- a
1268− , Just (ExprSyntax _ (PrimOp (Op1 (OpIsTag _) (ExprSyntax _ x2)))) <- c
1233+ , ExprSyntax _ (PrimOp (Op1 (OpIsTag _) (ExprSyntax _ x2))) <- c
12691234 , isSameVariable x1 x2 ->
12701235 c
1271− ExprBacktrack ->
1272− c
12731236 _ ->
1274− Just $ build ctx (Branch (NonEmptyArray.singleton (Pair a b)) c)
1237+ build ctx (Branch (NonEmptyArray.singleton (Pair a b)) c)
12751238 where
12761239 isSameVariable = case _, _ of
12771240 Local _ l, Local _ r -> l == r
@@ -1300,7 +1263,7 @@ simplifyBranches ctx pairs def = case NonEmptyArray.toArray pairs of
13001263 , Pair (ExprSyntax _ (PrimOp (Op1 OpBooleanNot (ExprSyntax _ (Local _ lvl2))))) body2 <- b
13011264 , ExprSyntax _ (Fail _) <- def
13021265 , lvl1 == lvl2 ->
1303− Just $ build ctx $ Branch (NonEmptyArray.singleton (Pair expr1 body1)) (Just body2)
1266+ Just $ build ctx $ Branch (NonEmptyArray.singleton (Pair expr1 body1)) body2
13041267 _
13051268 | ExprSyntax _ (Branch pairs2 def2) <- def ->
13061269 Just $ build ctx (Branch (pairs <> pairs2) def2)
@@ -1391,7 +1354,7 @@ shouldDistributeBranches :: Maybe Ident -> Level -> BackendExpr -> BackendExpr -
13911354shouldDistributeBranches ident level a body = do
13921355 let BackendAnalysis s2 = analysisOf body
13931356 case a of
1394− ExprSyntax (BackendAnalysis s1) (Branch branches (Just def))
1357+ ExprSyntax (BackendAnalysis s1) (Branch branches def)
13951358 | s2.size <= 128
13961359 , s1.result == KnownNeutral
13971360 , Just (Usage us) <- Map.lookup level s2.usages
@@ -1608,9 +1571,9 @@ freeze init = Tuple (analysisOf init) (go init)
16081571 UnpackData qual ct ty tag values ->
16091572 NeutralExpr $ Let ident level (NeutralExpr (CtorSaturated qual ct ty tag (map go <$> values))) (go body)
16101573 RewriteDistBranchesLet ident level branches def body ->
1611− NeutralExpr $ Let ident level (NeutralExpr (Branch (map go <$> branches) (Just (go def)))) (go body)
1574+ NeutralExpr $ Let ident level (NeutralExpr (Branch (map go <$> branches) (go def))) (go body)
16121575 RewriteDistBranchesOp branches def op -> do
1613− let branches' = NeutralExpr $ Branch (map go <$> branches) (Just (go def))
1576+ let branches' = NeutralExpr $ Branch (map go <$> branches) (go def)
16141577 case op of
16151578 DistApp spine ->
16161579 NeutralExpr $ App branches' (go <$> spine)
@@ -1624,8 +1587,6 @@ freeze init = Tuple (analysisOf init) (go init)
16241587 NeutralExpr $ PrimOp $ Op2 op2 branches' (go rhs)
16251588 DistPrimOp2R lhs op2 ->
16261589 NeutralExpr $ PrimOp $ Op2 op2 (go lhs) branches'
1627− ExprBacktrack ->
1628− NeutralExpr $ Fail "Failed pattern match"
16291590
16301591evalMkFn :: Env -> Int -> BackendSemantics -> MkFn BackendSemantics
16311592evalMkFn env n sem