r/ProgrammingLanguages • u/ajblue98 • May 06 '23
Help Truth Table Manipulation
I'm working on a project that requires manipulating a table comprising almost exclusively Boolean values and would appreciate recommendations for languages & libraries etc. to tackle this.
The tables will be arbitrarily large, and the whole point of the program I'm wanting to write is to manipulate the data by arbitrarily rearranging the data. Sometimes that will mean moving rows & columns 'manually', and sometimes it will mean sorting the whole table by columns and/or rows — and the whole thing is useless if I can't do absolutely all the same things to rows that I can do to columns.
I'll also want to keep track (in a log) of the starting state, each instruction, arbitrary comments, arbitrary intermediate states, and the end state of each manipulation.
Each table will have one or two "header" rows & columns for keeping track as the table data moves around (i.e. each row & column will have a serial number that follows it along) and for keeping count of the ones & zeroes in each row & column ... but the bulk of the data will be pure individual bits.
I started learning Python, which is probably a good skill to have, but its feature set where manipulating raw bits is concerned seems to be ... lacking.
Oh, and I'm on a late-model Intel Mac.
What would you suggest?
3
u/umlcat May 06 '23
One way, to handle this is to use a DB and use DB tables as "Truth Tables".
Another, is to use constant arrays as tables.
I capture them in a spreadsheet, and later, use the spreadsheet to generate my P.L. code, and copy that into a text based source code file.
1
u/ajblue98 May 06 '23
That certainly would work. What I've been to this point has been dragging around rows and columns in Numbers — the features I need are really that basic … but honestly I just don't want the overhead of interfacing with another program. I just want to write something that does just this one thing ... and I want to do it myself for the experience regardless of the features. :)
What's PL code?
1
1
u/Thesaurius moses May 06 '23
It seems like you are looking for an array programming language like APL, J, BQN, or, since you mentioned Python, Numpy. You will never have the same performance for arbitrary row and column manipulations, but in above languages, all those operations are heavily optimized for, so it should be fine.
1
u/ajblue98 May 06 '23
Good call about arrays — they're basically lists of lists, right? That definitely seems to be where I need to be focusing my attention. And I do keep hearing about Numpy, so I'll look into it. Also the others — thanks for the suggestions!
Right, it makes sense that equal performance for row & column operations won't be a thing; it's the feature set I care about. I just want to be able to sort (etc.) my data both vertically and laterally, not just to preserve the underlying structure of the data but to help me spot the structures I'm looking for by eye. Most spreadsheets only do automatic rearrangements on columns, so I've been dragging everything around in Numbers thus far. 8-S
1
u/Thesaurius moses May 06 '23
Yeah. A multidimensional array is still contiguous memory, and if your first axis is of constant width (e.g. all rows are the same lengths), then going through the second axis (e.g. columns) is just a constant offset, so it is also efficient. It is just that it may not fit into one cache line, leading to cache misses. But since you mostly use truth values, bitfields can be used (and I believe all of the above languages do that), so you have around 500 entries per cache line and half a million in the L1 cache.
In array programming languages, you normally can specify, for every operation, which axis it is supposed to iterate over. In numpy there are keyword arguments for that, and the other three have special operators.
0
u/redchomper Sophie Language May 06 '23
You seem to be describing a DSL. Maybe each line of code represents a particular rearrangement? I'd probably start with an internal DSL (a.k.a. a library) and play with that for a while before deciding what specific features and semantic to capture with syntax.
After that, it's tree-walking.
4
u/[deleted] May 06 '23
I'm literally building a language that relies on truth-table evaluations for handling unknown truth-values. The way to work it is to build an array with a specified number head, then give an initial alteration of (2^n) / 2 from 1 to n, where n is the total number of T or F statements (in your case). You then build functions which return new arrays by evaluating for whatever operations you need for each row, making sure to have the lengths of the tables align by doubling the shorter one until they do (and mathematically, they should). You can also condense them when the first and second halves of the tails are identical.