Last active
October 22, 2018 17:25
-
-
Save tuzz/74f88d484389980faf2a72f041875316 to your computer and use it in GitHub Desktop.
A proof-of-concept Sentient program for a set membership problem.
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
# A proof-of-concept Sentient program for a set membership problem. | |
# $ sentient subset.snt --assign '{ target: [1, 1, 0, 1], max_selections: 2 }' | |
# | |
# Outputs: {"members":[false,false,false,false,true,false,false,false,true,false,false,false]} | |
# Corresponding to: uk - scotland | |
# Given: a set of all the countries in the world, and a set of possible | |
# groupings of those countries (e.g. “Scandinavia” or “Central America”, or | |
# “All”), given a set of arbitrary countries, I want to compute the most concise | |
# description of that set, e.g. “All countries except UK and USA” or “All | |
# Scandinavian Countries, plus Canada”, or “Scandinavia and North America and | |
# Spain”. When I say “the most concise” I don’t really particularly care about | |
# character counts; using “number of entities involved in description” is | |
# probably fine | |
england = [1, 0, 0, 0]; | |
wales = [0, 1, 0, 0]; | |
scotland = [0, 0, 1, 0]; | |
ireland = [0, 0, 0, 1]; | |
uk = [1, 1, 1, 1]; | |
britain = [1, 1, 1, 0]; | |
except_england = [-1, 0, 0, 0]; | |
except_wales = [0, -1, 0, 0]; | |
except_scotland = [0, 0, -1, 0]; | |
except_ireland = [0, 0, 0, -1]; | |
except_uk = [-1, -1, -1, -1]; | |
except_britain = [-1, -1, -1, 0]; | |
sets = [ | |
england, wales, scotland, ireland, uk, britain, | |
except_england, except_wales, except_scotland, | |
except_ireland, except_uk, except_britain | |
]; | |
array12<bool> members; | |
array4<int> target; | |
int max_selections; | |
result = sets.reduce([0, 0, 0, 0], function^ (accumulator, set, index) { | |
summed = [accumulator, set].transpose.map(*sum); | |
return members[index] ? summed : accumulator; | |
}); | |
selections = members.countBy(*self); | |
invariant result == target; | |
invariant selections <= max_selections; | |
expose members, target, max_selections; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment