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

Temporary "exists" variables in suchthat #185

Open
julianhyde opened this issue Oct 5, 2022 · 2 comments
Open

Temporary "exists" variables in suchthat #185

julianhyde opened this issue Oct 5, 2022 · 2 comments

Comments

@julianhyde
Copy link
Collaborator

julianhyde commented Oct 5, 2022

In suchthat expressions you often need to declare a variable in an "exists". But it can be a little verbose. Suppose you have a list of edges and a function:

val edges = [(1, 2), (2, 3), (1, 4), (4, 2), (4, 3)];

fun isEdge (x, y) = (x, y) elem edges;

To compute a list of nodes that are two steps apart, you could write:

from (x, y) suchthat exists (
  from z suchthat isEdge(x, z) andalso isEdge(z, y));

Using the in paradigm you generate two temporary variables z and z2 that need to be eliminated using group x, y:

from (x, z) in edges,
    (z2, y) in edges
  where z = z2
  group x, y;

Leveraging #184 this becomes slightly more concise but you still need a group to eliminate z:

from (x, z) in edges,
    (same z, y) in edges
  group x, y;

We propose a variant of exists:

from (x, y) exists z suchthat isEdge(x, z) andalso isEdge(z, y);

In Datalog, any variable that does not appear on the left-side of the rule becomes an "exists variable", for example Z in the following:

is_step2 (X, Y) :- is_edge(X, Z), is_edge(Z, Y).

Morel cannot compete with Datalog's terseness. Morel is a typed language, and that means that its variables need to be declared, in a pattern, at exactly one point, so that their type can be specified. (Usually the type is inferred, but that point needs to exist.) The exists variable suchthat sugar seems to be about as terse as we can get.

@julianhyde
Copy link
Collaborator Author

julianhyde commented Oct 5, 2022

The follow is more concise. Is it better?

from (x, y, exists z) suchthat isEdge(x, z) andalso isEdge(z, y);

@julianhyde
Copy link
Collaborator Author

Following #202, the syntax would be

from x, y, z
  where isEdge (x, y) andalso isEdge (z, y)

That seems better. No exists keyword necessary.

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