It seems to me that the whole construct of functio...
# thinking-together
n
It seems to me that the whole construct of functions as primitives that take arguments and produce results leads to boilerplate and duplication because it needlessly privileges arguments as "independent variables". I'll use an example to elucidate: Take a rectangle of base
b
and height
h
. It's common to think of these as "independent" variables and define quantities like the following as "dependent": •
perimeter(b, h) => 2*(b+h)
area(b, h) => b*h
diagonal(b, h) => sqrt(b^2+h^2)
But if I were to ask, what are the base and height of a rectangle whose area is
a
and diagonal is
d
, our programming languages have no tooling to do this (except the symbolic manipulation libraries for computer algebra). All the required information is there but we have privileged
b
and
h
over
area
and
diagonal
and thus, we now need to figure out the formula for side lengths and program it as the function
sides(a, d)
I should be able to describe a structure, and auto-generate all possibilities functions (including for currying and partial applications, and while we are at it, all the partial derivatives with respect to each other) so that i can just declare what is known and what I want to calculate. A rectangle can then be represented with any set of variables that make everything else determinable. I should get access to all possible constructors like
new Rectangle(area: a, diagonal: d)
. And I want to see this be available for all programming tasks, not just algebra/math. For creating a graph, is the constructor
new Graph(Node[], Edges[])
really the privileged one? Why not build languages in a way that I automatically get
new Graph(AdjacencyMatrix)
and
graph.getNodes()
and
graph.getEdges()
.
l
Yes! Another example:
range{start.inclusive, end.exclusive, domain is int{0..30}, end=start+length, midpoint?, length?}
; By using constraints to define relations, and invertable/full on algebraic operations; an expression could be automatically reworked to isolate different set of variables; and provide ability to change them. Eg, here, changing the length will lead to an ambiguous case (>1 free variable), but with a structural editor, it may show all possible interpretations for you to select (eg. keep end or start fixed, or scale track around certain point, or add custom interpretation logic (eg. using non-linear scaling)). Integrating this system deeply with the UI would allow pretty much auto-generation of it just from the expression/range concept, auto supporting all interaction (drag track, endpoints, scale around scalepoint, etc), which would otherwise have been pages of code in eg. current web development...
(Instead of generating function... why not just keep everything declarative at its core? :)) )
g
See, also, PROLOG, miniKanren, constraints, unification. My quickie tutorial

https://www.youtube.com/watch?v=QOYAHoLiyg0

includes converting a query result to JSON.
j
You might enjoy https://dl.acm.org/doi/10.1145/3397537.3397546 But I’ll also just mention term rewriting in case you aren’t familiar. It’s how Mathematica is able to do the sorts of things you listed. It’s a shame there is no mainstream, non-proprietary language based on it.
d
seems to me exactly what oo systems do, having different constructors, and providing methods on the objects?
a
An OO system won't just casually reconstruct a side of a rectangle from the other side and the diagonal, no. TBH I'm leery of that sort of thing from a performance standpoint. I don't want to have to think too hard to know when my code is doing sqrt's. Logic programming is cool, but I definitely don't want it all the time.
g
aside: If you need to steal/get-ideas-from code: the yawningly easiest-to-understand implementation of PROLOG is Nils Holm’s http://www.t3x.org/bits/prolog6.html (it’s so easy, that I ported it to 2 other languages - JavaScript and CL). If you want to dig in and make an optimized PROLOG, look at WAM (Warren Abstract Machine). Ait Kaci’s tutorial is helpful https://en.wikipedia.org/wiki/Warren_Abstract_Machine (I had found a $0 download, but can’t find it at this moment). Gprolog is implemented with WAM and can be forced to show what WAM it generates. MiniKanren is relational programming done without backtracking http://minikanren.org.
k
This issue is quite serious in scientific computing. A mathematical equation is a lot more than a piece of code that implements a particular evaluation based on it. And yet, we see equations disappear, to be replaced by less valuable code. My attempt to fix this is to move from programming to specification languages. You'd write equations defining the relations for a rectangle in the specification languages, and then either read them into your program, or derive your program from them. Details: http://blog.khinsen.net/posts/2020/12/10/the-structure-and-interpretation-of-scientific-models/
w
Relatedly, most if not all validation concerns arise from data dependencies. Take, for instance, an
Event
with a
start_date
and an
end_date
. Naturally, we should have
start_date ≤ end_date
in the end, but for a UI, it can be nice to choose them in either order or to have an
InconsistentEvent
that then gets fixed by the time you use it. But some operations over regular events still sense for inconsistent events. Now I
Also thinking about scheduling dependencies from assembling LEGO with my son and his friend yesterday. Friend's mind was blown to see son and I concurrently working on different steps. Earlier this year, concurrency concerns lead me down a path to radically improving the expressivity of my software's model layer because being explicit about data-dependencies allowed for context-based resolution of references.
t
Something similar to this approach is Clojure
You write your code in the form of “resolvers” which establish relationships between attributes, then you simply give it some input data (in the form of an immutable map) and make a query (in the form of EQL) and the Pathom system determines the resolvers that need to run in order to fulfil your query.
This video by the creator is good but a bit more focused on using it to combine APIs

https://www.youtube.com/watch?v=YaHiff2vZ_o

n
@Tom Hutchinson This was really interesting and highly relevant. Thanks for sharing!
r
There's also something like this in Modelica