Created
September 11, 2022 00:57
-
-
Save theoknock/033a32c22d0d47fbb2787018890443df to your computer and use it in GitHub Desktop.
Predicate branching: https://en.wikipedia.org/wiki/Predication_(computer_architecture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PredicateFunctions.h | |
// PredicateFunctions | |
// | |
// Created by Xcode Developer on 9/10/22. | |
// | |
#ifndef PredicateFunctions_h | |
#define PredicateFunctions_h | |
@import Foundation; | |
typedef typeof(unsigned long) predicate; | |
typedef typeof(const predicate(^ _Nonnull )(predicate)) predicate_function; | |
typedef typeof(const predicate(^ _Nonnull const *)(predicate)) predicate_function_t; | |
predicate_function(^(^synchronous_predicate_functions)(predicate_function))(predicate_function) = ^ (predicate(^init_func)(predicate)) { | |
return ^ (predicate(^inter_func)(predicate)) { | |
return ^ predicate (predicate pred) { | |
return inter_func(init_func(pred)); // invokes and returns two functions in serial order | |
}; | |
}; | |
}; | |
predicate_function(^(^asynchronous_predicate_functions)(predicate_function))(predicate_function) = ^ (predicate(^init_func)(predicate)) { | |
return ^ (predicate(^inter_func)(predicate)) { | |
return ^ predicate (predicate pred) { | |
return inter_func(pred) & init_func(pred); // invokes and returns two functions independently | |
}; | |
}; | |
}; | |
predicate_function (^(^(^combine_predicate_functions)(predicate_function))(predicate_function))(predicate_function(^)(predicate_function_t, predicate_function_t, predicate)) = ^ (predicate_function pred_f) { | |
return ^ (predicate_function p_func) { | |
return ^ (predicate_function(^evaluation)(predicate_function_t, predicate_function_t, predicate)) { | |
return ^ predicate (predicate p) { | |
return evaluation(&pred_f, &p_func, p)(p); | |
}; | |
}; | |
}; | |
}; | |
static void (^wxyz)(void) = ^{ | |
predicate_function pf1 = ^ predicate (predicate p) { printf("p == %lu\n", p); return p; }; | |
predicate_function pf2 = ^ predicate (predicate p) { printf("p == %lu\n", (p = p + 1)); return p; }; | |
predicate_function pfx = combine_predicate_functions(pf1)(pf2)(^ (predicate_function_t init_func_t, predicate_function_t inter_func_t, predicate pred) { | |
return ^ predicate (predicate pred) { | |
return (*inter_func_t)((*init_func_t)(pred)); | |
}; | |
// wxyz() will output p = 55 and p = 56 to the console | |
pfx(55); | |
}; | |
#endif /* PredicateFunctions_h */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment