reprimTail ::
  FreshName n =>
  MonadFresh m =>
  a ->
  [n] ->
  Tail Core.Prim n a ->
  ExceptT (ReprimError n a) m (Term k X64.Prim n a -> Term k X64.Prim n a)
reprimTail an ns = \case
  Copy ac xs ->
    pure $
      Let an ns (Copy ac xs)

  Call ac n xs ->
    pure $
      Let an ns (Call ac n xs)

  Prim ap p0 xs -> do
    case reprimTrivial p0 of
      Just p ->
        pure $
          Let an ns (Prim ap p xs)
      Nothing ->
        reprimComplex an ns ap p0 xs
reprimTail ::
  FreshName n =>
  MonadFresh m =>
  a ->
  [n] ->
  Tail Core.Prim n a ->
  ExceptT (ReprimError n a) m (Term k X64.Prim n a -> Term k X64.Prim n a)
reprimTail an ns = \case
  Copy ac xs ->
    pure $
      Let an ns (Copy ac xs)

  Call ac n xs ->
    pure $
      Let an ns (Call ac n xs)

  Prim ap p0 xs -> do
    case reprimTrivial p0 of
      Just p ->
        pure $
          Let an ns (Prim ap p xs)
      Nothing ->
        reprimComplex an ns ap p0 xs
reprimTail an ns = \case
  Copy ac xs ->
    pure $
      Let an ns (Copy ac xs)

  Call ac n xs ->
    pure $
      Let an ns (Call ac n xs)

  Prim ap p0 xs -> do
    case reprimTrivial p0 of
      Just p ->
        pure $
          Let an ns (Prim ap p xs)
      Nothing ->
        reprimComplex an ns ap p0 xs