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

Support for mutually-recursive operators in the type checker #2978

Open
konnov opened this issue Sep 2, 2024 · 0 comments
Open

Support for mutually-recursive operators in the type checker #2978

konnov opened this issue Sep 2, 2024 · 0 comments
Assignees
Labels
feature A new feature or functionality FTC-Snowcat Feature: Fully-functional type checker Snowcat

Comments

@konnov
Copy link
Collaborator

konnov commented Sep 2, 2024

This is probably a very rare feature request: freespek/ssf-mc#14

Here is the test to reproduce:

---- MODULE RecMutual1 ----
(*
 * An example of mutual recursion. Even though the model checker of Apalache does not support recursion
 * any longer, asking the users to use FoldSeq or FoldSet instead, the type checker should still accept it.
 *
 * We simply use the example of mutual recursion from Wikipedia:
 * https://en.wikipedia.org/wiki/Mutual_recursion
 *
 * Obviously, it is an extremely suboptimal way to check if a number is even or odd.
 * We can simply use (i % 2) == 0 in TLA+.
 *
 * Igor Konnov, September 2024
 *)
EXTENDS Integers

VARIABLES
    \* @type: Int;
    n,
    \* @type: Str;
    oddity

RECURSIVE is_even(_)
RECURSIVE is_odd(_)

is_even(i) ==
  IF i = 0
  THEN TRUE
  ELSE is_odd(i - 1)

is_odd(i) ==
  IF i = 0
  THEN FALSE
  ELSE is_even(i - 1)

get_oddity(i) ==
  IF is_even(i) = 0
  THEN "even"
  ELSE "odd"

Init ==
    /\ n = 0
    /\ oddity = get_oddity(n)

Next ==
    /\ n' = n + 1
    /\ oddity' = get_oddity(n')

====

When we call apalache-mc typecheck, it fails in the parser:

$ apalache-mc typecheck test/tla/RecMutual1.tla
Type checking test/tla/RecMutual1.tla                             I@18:44:32.916
PASS #0: SanyParser                                               I@18:44:32.986
Parsing file /Users/igor/devl/apalache/test/tla/RecMutual1.tla
Parsing file /private/var/folders/93/s11lf4f16c77bpq91r90njbm0000gn/T/Integers.tla
Parsing file /private/var/folders/93/s11lf4f16c77bpq91r90njbm0000gn/T/Naturals.tla
Check the dependency graph in dependencies.dot. You can view it with graphviz. E@18:44:33.097
Parsing error: Found a cyclic dependency among operators: get_oddity, Init, is_even, is_odd, Next

To fix this, we would have to properly identify mutually recursive operators, or extract this from SANY.

@konnov konnov added the feature A new feature or functionality label Sep 2, 2024
@konnov konnov self-assigned this Sep 2, 2024
@konnov konnov added the FTC-Snowcat Feature: Fully-functional type checker Snowcat label Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A new feature or functionality FTC-Snowcat Feature: Fully-functional type checker Snowcat
Projects
None yet
Development

No branches or pull requests

1 participant