Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

caseCon introduces free variables because of evaluator changes #2623

Closed
leonschoorl opened this issue Dec 12, 2023 · 0 comments · Fixed by #2624
Closed

caseCon introduces free variables because of evaluator changes #2623

leonschoorl opened this issue Dec 12, 2023 · 0 comments · Fixed by #2624

Comments

@leonschoorl
Copy link
Member

topEntity = foo @System

foo :: forall dom. Signal dom (Vec 1 (Signed 2)) -> Signal dom Bool
foo = \input ->
    let
      scs :: Signal dom (Vec 1 Bool)
      scs = bundle $ map f $ unbundle input
    in
      fmap bar scs


bar :: (KnownNat n) => Vec (n+1) Bool -> Bool
bar = fold (&&) . map (id)

f :: Signal dom a -> Signal dom Bool
f = const $ pure $ True

When compiled with -fclash-debug-invariants (DebugSilent or higher) this crashes with:

    Clash error call:
    Clash.Rewrite.Util(226): Error when applying rewrite caseCon to:
    case Clash.Sized.Vector.map @GHC.Types.Bool[3674937295934324744]
           @GHC.Types.Bool[3674937295934324744]
           @(GHC.TypeNats.+[3674937295934325540] 0 1)
           (GHC.Base.id[8214565720323788222][GlobalId]
              @GHC.Types.Bool[3674937295934324744])
           x[6989586621679027447][LocalId] of
      Clash.Sized.Vector.Cons[8214565720323790062] m[12]
        (_co_[7] :: GHC.Prim.~#[3674937295934324842]
                      GHC.Num.Natural.Natural[3674937295934324786]
                      GHC.Num.Natural.Natural[3674937295934324786]
                      1
                      (GHC.TypeNats.+[3674937295934325540] 0 1))
          (el[6] :: GHC.Types.Bool[3674937295934324744])
          (rest[14] :: Clash.Sized.Vector.Vec[8214565720323790059]
                         0
                         GHC.Types.Bool[3674937295934324744]) ->
        el[6][LocalId]
    Result:
    let
      el[18] :: GHC.Types.Bool[3674937295934324744]
      = (λ(x[8214565720323804840] :: GHC.Types.Bool[3674937295934324744]) ->
          x[8214565720323804840][LocalId])
          ((λ(ds[6341068275337658375] :: Clash.Signal.Internal.Signal[8214565720323790017]
                                           "System"
                                           (Clash.Sized.Internal.Signed.Signed[8214565720323790051] 2)) ->
             Main.f_x[8214565720323803592][GlobalId] @"System")
             (Clash.Sized.Vector.head @0
                @(Clash.Sized.Internal.Signed.Signed[8214565720323790051] 2)
                ds[6341068275337658369][LocalId]))
    in el[18][LocalId]
    It introduces free variables.
    Before: x[6989586621679027447] :: Clash.Sized.Vector.Vec[8214565720323790059]
                                (GHC.TypeNats.+[3674937295934325540] 0 1)
                                GHC.Types.Bool[3674937295934324744]
    After: ds[6341068275337658369] :: Clash.Signal.Internal.Signal[8214565720323790017]
                                 "System"
                                 (Clash.Sized.Vector.Vec[8214565720323790059]
                                    1
                                    (Clash.Sized.Internal.Signed.Signed[8214565720323790051] 2))
    CallStack (from HasCallStack):
      error, called at src/Clash/Rewrite/Util.hs:226:15 in clash-lib-1.9.0-inplace:Clash.Rewrite.Util

The problem is that since Give evaluator acces to inscope let-bindings #2571 the evaluator has access to the let bindings, and they may reference new free variables.

In this case it looks like:

\ds[6341068275337658369] ->
  let x[6989586621679027447] = ... ds[6341068275337658369] ...
  in .................. case map id x[6989586621679027447] of ...
                    --  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^  term being rewritten

The evaluator transforms this into some transformed form of x which contains ds, and then checkDebug complains there is now a new free variable in the expression: ds

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant