Skip to content

Add CLASSIFY base-class AS: .. ELSE: construct to declare disjoint predicate unions#2452

Open
timor wants to merge 2 commits intofactor:masterfrom
timor:predicate-classification
Open

Add CLASSIFY base-class AS: .. ELSE: construct to declare disjoint predicate unions#2452
timor wants to merge 2 commits intofactor:masterfrom
timor:predicate-classification

Conversation

@timor
Copy link
Copy Markdown
Contributor

@timor timor commented Apr 8, 2021

This allows imposing mutually-exclusive predicate classes, which helps with dispatch on ambiguous predicate classes. The definitions are checked in order. The programmer is responsible for specifying these in the correct order, as there is no way to determine implications between predicate class membership automatically.

Example:

USING: classes.predicate ;
IN: scratchpad

CLASSIFY number
AS: b-range 50 < ;
AS: a-range 100 < ;
ELSE: full-range

GENERIC: smallest-range ( number -- limit )
M: a-range smallest-range drop 100 ;
M: b-range smallest-range drop 50 ;
M: full-range smallest-range drop 1/0. ;
40 smallest-range .
50

This is directly inspired by

Ernst, Michael, Craig Kaplan, and Craig Chambers. "Predicate dispatching: A unified theory of dispatch." European Conference on Object-Oriented Programming. Springer, Berlin, Heidelberg, 1998.

(Section 2.5)

To do that, add a "disjoint" word property to predicate classes, which is an inheritable set of classes this predicate class is explicitly declared disjoint from. A DISJOINT: class-a class-b word is provided which can be used to declare disjoint relation between existing classes.

@timor timor force-pushed the predicate-classification branch 2 times, most recently from 3b66040 to 12f705b Compare April 8, 2021 13:48
@timor timor marked this pull request as ready for review April 8, 2021 13:49
@timor timor marked this pull request as draft April 8, 2021 14:41
@timor timor force-pushed the predicate-classification branch from 12f705b to 2d42787 Compare April 8, 2021 17:15
@timor timor marked this pull request as ready for review April 8, 2021 17:20
@timor timor marked this pull request as draft April 13, 2021 15:44
@timor timor force-pushed the predicate-classification branch from 2d42787 to b2ffc8d Compare May 6, 2021 11:36
timor added 2 commits May 6, 2021 13:42
Predicate classes can be declared disjoint from one-another.  This is inherited,
so predicate classes that specialize on a predicate class with an existing
disjoint specification will inherit the set of declared-disjoint classes.

Adds the `DISJOINT:` syntax word for that purpose.
@timor timor force-pushed the predicate-classification branch from b2ffc8d to db1c73a Compare May 6, 2021 11:42
@timor timor marked this pull request as ready for review May 6, 2021 11:46
@timor
Copy link
Copy Markdown
Contributor Author

timor commented May 6, 2021

I fixed an issue with inheritance and also simplified the syntax.

@timor
Copy link
Copy Markdown
Contributor Author

timor commented May 6, 2021

Also, the predicate class definitions now explicitly exclude the preceding predicate inside the CLASSIFY... construct. However, there is no ordering right now, so dispatch will probably be naive right now. It should be possible to take this into account in sort-classes or even class<=, so that the optimum dispatch order is chosen when building the decision tree.

@timor timor changed the title classes.predicate: Implement <CLASSIFY ... CLASSIFY> Add CLASSIFY base-class AS: .. ELSE: construct to declare disjoint predicate unions May 6, 2021
@mrjbq7
Copy link
Copy Markdown
Member

mrjbq7 commented Oct 14, 2025

Can we move this to extra and then merge and see how we like it?

Could call it classes.disjoint or classes.classify or something.

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 this pull request may close these issues.

2 participants