Re:Vine: A programming language based on Interaction Nets
by ngruhn on Sunday February 23, 2025 @11:39AM
(#43150670)
Prolog also has constraints! You need to import a library called CLP(FD) / CLP(Z) (depending on the Prolog system) and instead of =, <, =<, etc. you need to use #=, #<, #=<, etc.
:- use_module(library(clpfd)).
min(Min, Y, Min) :- Min #=< Y, !.
min(X, Min, Min) :- Min #=< X.
find_min_and_subtract_([], [], Min, Min).
find_min_and_subtract_([H|T], [R|Rs], TempMin, Min) :-
min(TempMin, H, NewTempMin),
R #= H - Min,
find_min_and_subtract_(T, Rs, NewTempMin, Min).
find_min_and_subtract([H|Hs], Rs, Min) :-
find_min_and_subtract_([H|Hs], Rs, H, Min).
With that (and an additional variable for the temporary minimum) it works for me: ?- find_min_and_subtract([9,2,4,12], Rs, Min).
Min = 2,
Rs = [7, 0, 2, 10]
https://swish.swi-prolog.org/p/qgchfMfB.pl
Re:Vine: A programming language based on Interaction Nets
by anonzzzies on Sunday February 23, 2025 @01:24PM
(#43151659)
OW! Thanks, I did not know about that. Last I user Prolog was mid 90s, but I really liked it, just did not have any practical use for it after graduation. Thanks for that!
But this doesn't work for all cases anyway (just the ones where the last value is also the minimum). I feel it can be fully expressed in one pass with miniKanren instead but no time to figure that out now; it can defer the arithmetic by setting up constraints.
Always liked the idea of 'using variables in the past' (or creating them in the future) and the first amazement moment was with Prolog (there were quite a lot of wow! moments in Prolog for the young me who only knew basic, pascal & c, like when you have some 'write multiply in prolog', when you deliver f(x,y)=x*y, you can not only do f(2,3) and get 6, but also f(2,y)=6 and get y=3), but, so it made me think of it.