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

Program execution hangs on recursive functions which should terminate #199

Open
jazullo opened this issue Mar 8, 2023 · 1 comment
Open

Comments

@jazullo
Copy link
Collaborator

jazullo commented Mar 8, 2023

Consider this interpreter for logical expressions:

data Val
  = Zero
  | One

data Ast
  = Val Val
  | Not Ast
  | Or Ast Ast
  | And Ast Ast

eval :: Ast -> Val
eval x = case x of
  Val b -> b
  Not e -> 
    case eval e of
      One -> Zero
      Zero -> One
  Or e1 e2 -> 
    case eval e1 of
      One -> One
      Zero -> eval e2
  And e1 e2 -> 
    case eval e1 of
      One -> eval e2
      Zero -> Zero

gibbon_main =
  let
    _ = printPacked (eval (Val Zero))  -- works
    _ = printPacked (eval (Val One))  -- works
    _ = printPacked (eval (Not (Val One)))  -- hangs
    _ = printPacked (eval (And (Val One) (Val One)))  -- hangs
    _ = printPacked (eval (Or (Val One) (Val One)))  -- hangs
  in ()

All expressions which contain a node with an operator will cause the evaluator to hang. Tested on issue_191.

@jazullo
Copy link
Collaborator Author

jazullo commented Mar 8, 2023

The interpreter works when Bool is used over Val

data Ast
  = Val Bool
  | Not Ast
  | Or Ast Ast
  | And Ast Ast

eval :: Ast -> Bool
eval x = case x of
  Val b -> b
  Not e -> if eval e then False else True
  Or e1 e2 -> if eval e1 then True else eval e2
  And e1 e2 -> if eval e1 then eval e2 else False

simplify :: Ast -> Ast
simplify x = Val (eval x)

gibbon_main =
  let
    _ = printPacked (simplify (Val False))
    _ = printPacked (simplify (Val True))
    _ = printPacked (simplify (Not (Val True)))
    _ = printPacked (simplify (And (Val True) (Val True)))
    _ = printPacked (simplify (Or (Val True) (Val True)))
  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

1 participant