@@ -616,10 +616,10 @@ esCodegenTcoMutualLoopBinding mode env tcoIdent bindings = case NonEmptyArray.to
616616 esCodegenTcoLoopBinding mode (noPure env) ident tco
617617 bindings' -> do
618618 let maxArgs = fromMaybe 0 $ maximum $ NonEmptyArray.length <<< _.arguments <<< snd <$> bindings
619− let argIdents = esTcoArgIdent tcoIdent <$> NonEmptyArray.range 0 maxArgs
619+ let argIdents = esTcoArgIdent tcoIdent <$> NonEmptyArray.range 0 (maxArgs - 1)
620620 let branchIdent = esTcoBranchIdent tcoIdent
621621 esSepStatements $
622− [ esBinding tcoIdent $ esTcoFn tcoIdent (NonEmptyArray.cons branchIdent argIdents) $ esBranches
622+ [ esBinding tcoIdent $ esMutualTcoFn tcoIdent (NonEmptyArray.cons branchIdent argIdents) $ esBranches
623623 ( mapWithIndex
624624 ( \ix (Tuple _ tco) -> do
625625 let { value: argNames, accum: env' } = freshNames RefStrict env tco.arguments
@@ -634,8 +634,11 @@ esCodegenTcoMutualLoopBinding mode env tcoIdent bindings = case NonEmptyArray.to
634634 ]
635635 <>
636636 mapWithIndex
637− ( \ix (Tuple ident _) ->
638− esBinding ident $ esPure $ esApp (esIdent tcoIdent) [ esInt ix ]
637+ ( \ix (Tuple ident { arguments: args }) -> do
638+ let { value: idents } = freshNames RefStrict env args
639+ esBinding ident $ esCurriedFn idents
640+ [ Return $ esApp (esIdent tcoIdent) $ Array.cons (esInt ix) (esIdent <$> NonEmptyArray.toArray idents)
641+ ]
639642 )
640643 bindings'
641644
@@ -903,7 +906,7 @@ esForeignModulePath (ModuleName _) = "./foreign.js"
903906esTcoMutualIdent :: NonEmptyArray Ident -> Ident
904907esTcoMutualIdent idents = case NonEmptyArray.toArray idents of
905908 [ ident ] -> ident
906− _ -> Ident $ foldMap (String.take 5 <<< unwrap) idents
909+ _ -> Ident $ "$" <> foldMap (String.take 5 <<< unwrap) idents
907910
908911esTcoLoopIdent :: Ident -> Ident
909912esTcoLoopIdent (Ident tcoIdent) = Ident (tcoIdent <> "$c")
@@ -921,7 +924,13 @@ esTcoCopyIdent :: Ident -> Ident
921924esTcoCopyIdent (Ident tcoIdent) = Ident (tcoIdent <> "$copy")
922925
923926esTcoFn :: forall a. Ident -> NonEmptyArray Ident -> Dodo.Doc a -> Dodo.Doc a
924−esTcoFn tcoIdent args body = esCurriedFn (esTcoCopyIdent <$> args)
927+esTcoFn tcoIdent args body = esCurriedFn (esTcoCopyIdent <$> args) (esTcoFnBody tcoIdent args body)
928+
929+esMutualTcoFn :: forall a. Ident -> NonEmptyArray Ident -> Dodo.Doc a -> Dodo.Doc a
930+esMutualTcoFn tcoIdent args body = esFn (esTcoCopyIdent <$> NonEmptyArray.toArray args) (esTcoFnBody tcoIdent args body)
931+
932+esTcoFnBody :: forall a. Ident -> NonEmptyArray Ident -> Dodo.Doc a -> Array (EsStatement (Dodo.Doc a))
933+esTcoFnBody tcoIdent args body =
925934 [ Statement $ esLetBindings $ NonEmptyArray.appendArray bindings
926935 [ Tuple (esTcoLoopIdent tcoIdent) (Just (esBoolean true))
927936 , Tuple (esTcoReturnIdent tcoIdent) Nothing