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

Variable lost when printing leftist heap representation #198

Open
jazullo opened this issue Mar 4, 2023 · 2 comments
Open

Variable lost when printing leftist heap representation #198

jazullo opened this issue Mar 4, 2023 · 2 comments
Assignees

Comments

@jazullo
Copy link
Collaborator

jazullo commented Mar 4, 2023

Here is a leftist heap implementation in gibbon:

data LHeap = E | H Int LHeap LHeap

rank :: LHeap -> Int
rank x = case x of
  E -> 0
  H y z r -> rank r + 1

merge :: LHeap -> LHeap -> LHeap
merge g h = 
  case g of
    E -> h
    H a l r -> case h of
      E -> g
      H b z0 z1 -> 
        let
          s = merge r h
        in
        if a > b then merge h g
        else if rank s > rank l then H a s l
        else H a l s

add :: Int -> LHeap -> LHeap
add x h = merge h (H x E E)

pop :: LHeap -> (Int, LHeap)
pop z = case z of
  H x l r -> (x, merge l r)
  E -> (-1, E)

gibbon_main = 
  let
    _ = printPacked (add 0 E)
  in ()

When gibbon is invoked this gives the following error:

cabal v2-exec gibbon -- --packed --no-gc --to-exe ../lheap/lheap.hs
gibbon: Var l_44_118_170_238_248 not found. Checking:
: VarE "l_44_118_170_238_248"
CallStack (from HasCallStack):
  error, called at src/Gibbon/L1/Typecheck.hs:746:21 in gibbon-0.2-inplace:Gibbon.L1.Typecheck

To the right of _ = printPacked, replacing (add 0 E) with just E is able to compile and produce the correct representation:

./lheap.exe
(E )'#()

This error was produced in branch issue_191.

@vidsinghal vidsinghal self-assigned this Mar 4, 2023
@vidsinghal
Copy link
Collaborator

This Hack seems to work

data LHeap = E | H Int LHeap LHeap

rank :: LHeap -> Int
rank x = case x of
  E -> 0
  H y z r -> rank r + 1

merge :: LHeap -> LHeap -> LHeap
merge g h = 
  case g of
    E -> h
    H a l r -> case h of
      E -> g
      H b z0 z1 -> 
        let
          s = merge r h
        in mergeIf a b l s h g

mergeIf :: Int -> Int -> LHeap -> LHeap -> LHeap -> LHeap -> LHeap 
mergeIf a b l s h g = if (a > b) then merge h g
                      else if (rank s > rank l) then mergeIfElse a b s l h g
                      else H a l s 

mergeIfElse :: Int -> Int -> LHeap -> LHeap -> LHeap -> LHeap -> LHeap 
mergeIfElse a b s l h g = H a s l

add :: Int -> LHeap -> LHeap
add x h = merge h (H x E E)

pop :: LHeap -> (Int, LHeap)
pop z = case z of
  H x l r -> (x, merge l r)
  E -> (-1, E)

gibbon_main = 
  let
    _ = printPacked (add 0 E)
  in ()

@jazullo
Copy link
Collaborator Author

jazullo commented Mar 9, 2023

Here's a simpler example that I believe is broken by the same bug.

data I = I Int
data A = A I I

swap :: A -> A
swap x = case x of
  A i1 i2 -> A i2 i1

gibbon_main = 
  let
    _ = printPacked (swap (A (I 0) (I 1)))
  in ()
gibbon: Var i1_13_57_104_141 not found. Checking: 
: VarE "i1_13_57_104_141"
CallStack (from HasCallStack):
  error, called at src/Gibbon/L1/Typecheck.hs:746:21 in gibbon-0.2-inplace:Gibbon.L1.Typecheck

Note the following works:

data A = A Int Int

swap :: A -> A
swap x = case x of
  A i1 i2 -> A i2 i1

gibbon_main = 
  let
    _ = printPacked (swap (A 0 1))
  in ()

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

No branches or pull requests

2 participants