packages feed

ychr-0.1.0.0: test/golden/function_pattern_dispatch/function_pattern_dispatch.chr

:- module(function_pattern_dispatch, [t/3, type(tags/0), type(pair_t/0)]).
:- use_module(prelude).
:- chr_constraint t/3.
:- chr_type tags ---> fn ; c ; d.
:- chr_type pair_t ---> p(any, any).

:- function f/1, classify/1, deep/1.

% Literal patterns first; variable as fallback. Top-to-bottom matters.
f(0)  -> quote(zero).
f(1)  -> quote(one).
f(42) -> quote(answer).
f(_)  -> quote(other).

% Compound-term patterns, exercised top-to-bottom.
classify([])         -> quote(empty).
classify([_])        -> quote(one_elem).
classify([_, _])     -> quote(two_elems).
classify([_, _, _])  -> quote(three_elems).
classify(_)          -> quote(many).

% Nested patterns: order matters when several would match.
deep(p(0, _))      -> quote(zero_first).
deep(p(_, 0))      -> quote(zero_second).
deep(p(_, _))      -> quote(pair).
deep(_)            -> quote(other).

t(fn, X, R)       <=> R is f(X).
t(c, X, R)        <=> R is classify(X).
t(d, X, R)        <=> R is deep(X).