>Because you stop wasting time writing boilerplate loops and spent more time focusing on the actual problem solving
APL and derivatives do this way better.
>Because prolog is awkward as all hell
That is only the result of poor docs and syntax that could definitely be better but some things like lexers are easy af declaratively while traditionally you would need loops with hundreds of lines of logic
I did advent of code using prolog. Performance sucks. In particular, there was the Van Eck Sequence, which was awfully slow to compute with prolog. Switching to python and doing the exact same computation was about 100 times faster.
I ported the code. Literally copy&paste and made the necessary changes regarding syntax. Turned tail recursion into a loop, and turned prologs rbtree into a python dict. That's it.
That's what I mean by mindset. Your code doesn't actually mean the same thing between the two languages. Good fast code in Prolog doesn't look like the equivalent Python code, because Prolog runs your code through a logic solver. It's like using a saw vs using a hammer to drive in a nail. You need to use different constructs/code structures in Prolog to accomplish the same task quickly.
Yeah. It's possible that the hash table you used was slow. This is fast on my machine.
%% hash table
:- dynamic hash_table/2.
hash_put(K, V) :-
( hash_table(K, V)
-> true
;
% delete the old entry if any
(hash_table(K, W)
->
retract(hash_table(K, W))
;
true
),
% insert
asserta(hash_table(K, V))
).
hash_get(K, V) :-
hash_table(K, V).
% compute the number spoken on PTurn + 1
speak(PTurn, PNum, NextNumber) :-
(hash_get(PNum, OldTurn)
->
NextNumber is PTurn - OldTurn
;
NextNumber = 0
).
% clear the hash table, and add
% the starting numbers.
setup(S1, S2, S3) :-
retractall(hash_table(_, _)),
hash_put(S1, 1),
hash_put(S2, 2),
hash_put(prev, S3).
% recursive procedure to compute the number spoken each turn
compute_iter(Turn, TargetTurn, Result) :-
% compute number spoken on this turn
hash_get(prev, PNum),
PTurn is Turn - 1,
speak(PTurn, PNum, NextNumber),
write('Turn: '),
write(Turn),
write(', Number: '),
write(NextNumber),
write('n'),
% save the previous number
% such that its "last seen value"
% is the most recent one.
hash_put(PNum, PTurn),
% mark this turn's number as the new "previous"
hash_put(prev, NextNumber),
( Turn = TargetTurn
->
Result = NextNumber
;
NextTurn is Turn + 1,
compute_iter(NextTurn, TargetTurn, Result)
).
:- compute(0, 3, 6, 2020, Result),
write('On turn 2020, number is '),
write(Result),
write('n').
3 weeks ago
Anonymous
teach me senpai!
3 weeks ago
Anonymous
kek, okay what do you need to know?
>the hash table
Prolog is a database of facts, and you can modify this database at runtime.
assert() introduces a fact, and retract() deletes it.
Maybe you'll like this explanation:
I set this program up so that I can create or delete facts that look like "hash_table(K, V)". You can think of this as saying "at key K, the hash table has stored the value V".
This is the 'dictionary'.
The goal hash_put(K, V) uses assert() to insert the fact "hash_table(K, V)" into the dictionary. For example hash_put(hello, world) will create the fact "hash_table(hello, world)".
hash_get(K, V) can be used to "get" things from the hash table. Continuing the example above, I could write "hash_get(hello, X)" and Prolog will search for a fact that matches the pattern and make X equal to "world". But I can also do hash_get(X, world) to find a key associated with "world". So it's not a normal dictionary, it's more flexible because of Prolog's "unification" feature.
>van eck sequence
In the hash table I store the numbers that have been spoken so far, together with the turn number on which the number was most recently spoken. For example if number 3 was most recently spoken on turn 5, then the database should contain the fact hash_table(3, 5). And so on for all the other numbers spoken. If I encounter the number 3 on a later turn, say, 8, then I delete the old fact with retract() before I create a new fact hash_table(3, 8). This is what hash_put does.
I use the goal compute_iter, which uses tail recursion to compute the numbers to add to the hash table each turn. It begins at turn 4 and just counts up to 2020.
I also use the hash table to track another variable I called "prev", which is the number spoken on the previous turn. Only after I have calculated the number for the current turn (NextNumber) will I insert the previous one into the hash table as normal.
3 weeks ago
Anonymous
>Given your starting numbers, what will be the 30000000th number spoken?
Is it still fast on your machine for part 2 of the puzzle? The 30000000th number spoken? Because on my machine it has been running for quite a while and is still not finished.
Meanwhile, the translated python code which I mentioned earlier is done within seconds on my machine. (It looks weird because it's ported from prolog without cleaning up.)
[CODE]
def take_turn(Dict, LastTurn, LastNumber):
while LastTurn < 30000000:
Age = 0
if LastNumber in Dict:
PreviousTurn = Dict[LastNumber]
Age = LastTurn - PreviousTurn
Dict[LastNumber] = LastTurn
CurrentTurn = LastTurn + 1
LastTurn = CurrentTurn
LastNumber = Age
return LastNumber
def initialize_dict(List):
Turn = 1
Dict = dict()
for E in List:
Dict[E] = Turn
Turn = Turn + 1
return Dict
https://www.cs.cmu.edu/Groups/AI/html/faqs/lang/prolog/prg/part1/faq-doc-9.html >The Prolog 1000 is a database of real Prolog applications being assembled in conjunction with the Association for Logic Programming (ALP) and PVG. The aim is to demonstrate how Prolog is being used in the real world and it already contains over 500 programs with a total of well over 2 million lines of code.
So linux kernel alone is 15M loc.
Those 2M is probably mostly projects by some overly-ardent graduate students and actually does nothing or could be rewritten in another language just as easily. The question here is: what are the idiomatic use-cases for prolog, and not "hey, we can do it here too, just 10 times slower".
I mean, I tried to read some prolog books, but lost interest, and they started either trivial or with how the engine or whatever works. What I gathered is that it all it is good for is a database of facts, where some facts are automatically inferred. The solver is of a general nature, meaning it's slow as fuck, but it's also dumb as fuck, because logical inference based on facts goes only so far.
anons, extract the archive. that's the main point of my post, I just copy pasted a few lines just so that you know wtf the random file is about, but when you look at the text files it's fucking impressive
I mean, I tried to read some prolog books, but lost interest, and they started either trivial or with how the engine or whatever works. What I gathered is that it all it is good for is a database of facts, where some facts are automatically inferred. The solver is of a general nature, meaning it's slow as fuck, but it's also dumb as fuck, because logical inference based on facts goes only so far.
So linux kernel alone is 15M loc.
Those 2M is probably mostly projects by some overly-ardent graduate students and actually does nothing or could be rewritten in another language just as easily. The question here is: what are the idiomatic use-cases for prolog, and not "hey, we can do it here too, just 10 times slower".
I mean, I tried to read some prolog books, but lost interest, and they started either trivial or with how the engine or whatever works. What I gathered is that it all it is good for is a database of facts, where some facts are automatically inferred. The solver is of a general nature, meaning it's slow as fuck, but it's also dumb as fuck, because logical inference based on facts goes only so far.
> logical inference based on facts goes only so far...
...which is to say -- not very far. So I don't see how prolog can compete with normal languages. You basically have a general-purpose algorithm under the hood and you are trying to cajole it into solving the actual problem for you. That sounds not just anti-straitforward, but retarded as fuck.
So, prolog is basically a slow retarded database mainly used for jerking off in academia. Anything I missed?
>So, prolog is basically a slow retarded database mainly used for jerking off in academia. Anything I missed?
It was created around the time when symbolic AI was more popular than deep learning. And it's still used in industry at some places. Prolog is also great for expressing problems that require search or dynamic programming, like computing a schedule for N people that has to satisfy various constraints. Sure, you can also do anything in other languages because of Turing-completeness, but Prolog makes its tasks more convenient.
(me)
I forgot to mention planning problems, pathfinding, etc.
There are also computations that take so much time that Prolog being somewhat slower doesn't make a serious difference. Then what's more important is how convenient the language is.
>So, prolog is basically a slow retarded database mainly used for jerking off in academia. Anything I missed?
I think the best use for Prolog is "expert systems", not specific and intensive solvers. Programs that are not used to make relatively simple data structures transformations but rather programs containing hundreds of conditionals, all of them relating to the real logic of the program/applications, not conditionals inside loops checing for termaintions or for doing filtering.
You can see it from the list of companies and applications here
Is it though?
https://www.cs.cmu.edu/Groups/AI/html/faqs/lang/prolog/prg/part1/faq-doc-9.html >The Prolog 1000 is a database of real Prolog applications being assembled in conjunction with the Association for Logic Programming (ALP) and PVG. The aim is to demonstrate how Prolog is being used in the real world and it already contains over 500 programs with a total of well over 2 million lines of code.
https://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/prolog/doc/pl_1000/pl1000v1.gz. Each of this application is used to "help people think or act", they do not really transform
In this list there are least 1 or 2 prolog system for doing Law type kind of stuff. You can easily imagine that the kind of program requied for helping lawyers is not one that will transform linked list. It's more like it will contains thousands of conditionals to discrimimate situations or something. All that matters is the "rules".
>they do not really transform
*data structures. Like any regular program you use, from text editor to virtual machine to browser engine to game engine to ffmpeg, etc.... All of those transform data, their use case is not to help people think.
Buy an ad.
>t. Filtered by prolog
Keep seething retard
you saw the youtube seething last week? no one wants ads. they'd rather reply to based threads bringing up based technology.
what is prolog and why would I use it?
Declarative programming language. Because you stop wasting time writing boilerplate loops and spent more time focusing on the actual problem solving
Ah, you're the tranny in the OCaml thread having a melt down over there.
>declarative
>has cut operator
what did xe mean by this?
what does a cut operator?
Stops execution of a computational branch.
>So, anon, what problems have you been solving with Prolog lately?
>Declarative programming language
Ah, so it's a DSL.
What domain is it specialized for?
constraint satisfaction
implementing type inference in it is very easy for example
>Because you stop wasting time writing boilerplate loops and spent more time focusing on the actual problem solving
APL and derivatives do this way better.
Because prolog is awkward as all hell. For 99% of use cases it sucks. For the last 1% there's usually better choices.
>Because prolog is awkward as all hell
That is only the result of poor docs and syntax that could definitely be better but some things like lexers are easy af declaratively while traditionally you would need loops with hundreds of lines of logic
NTA but do you know of good docs?
Lexers are easy af in any language. Not difficult to make. But how do you optimize prolog lexers when it's slow and taking up your time?
I did advent of code using prolog. Performance sucks. In particular, there was the Van Eck Sequence, which was awfully slow to compute with prolog. Switching to python and doing the exact same computation was about 100 times faster.
They are not the exact same computations if the speed was that different. Prolog just requires a different mindset and time investment.
I ported the code. Literally copy&paste and made the necessary changes regarding syntax. Turned tail recursion into a loop, and turned prologs rbtree into a python dict. That's it.
That's what I mean by mindset. Your code doesn't actually mean the same thing between the two languages. Good fast code in Prolog doesn't look like the equivalent Python code, because Prolog runs your code through a logic solver. It's like using a saw vs using a hammer to drive in a nail. You need to use different constructs/code structures in Prolog to accomplish the same task quickly.
Alright then, can you show me a performant solution using prolog for the van eck sequence (advent of code 2020 day 15 part 2)?
Yeah. It's possible that the hash table you used was slow. This is fast on my machine.
%% hash table
:- dynamic hash_table/2.
hash_put(K, V) :-
( hash_table(K, V)
-> true
;
% delete the old entry if any
(hash_table(K, W)
->
retract(hash_table(K, W))
;
true
),
% insert
asserta(hash_table(K, V))
).
hash_get(K, V) :-
hash_table(K, V).
% compute the number spoken on PTurn + 1
speak(PTurn, PNum, NextNumber) :-
(hash_get(PNum, OldTurn)
->
NextNumber is PTurn - OldTurn
;
NextNumber = 0
).
% clear the hash table, and add
% the starting numbers.
setup(S1, S2, S3) :-
retractall(hash_table(_, _)),
hash_put(S1, 1),
hash_put(S2, 2),
hash_put(prev, S3).
% recursive procedure to compute the number spoken each turn
compute_iter(Turn, TargetTurn, Result) :-
% compute number spoken on this turn
hash_get(prev, PNum),
PTurn is Turn - 1,
speak(PTurn, PNum, NextNumber),
write('Turn: '),
write(Turn),
write(', Number: '),
write(NextNumber),
write('n'),
% save the previous number
% such that its "last seen value"
% is the most recent one.
hash_put(PNum, PTurn),
% mark this turn's number as the new "previous"
hash_put(prev, NextNumber),
( Turn = TargetTurn
->
Result = NextNumber
;
NextTurn is Turn + 1,
compute_iter(NextTurn, TargetTurn, Result)
).
compute(S1, S2, S3, TargetTurn, Result) :-
TargetTurn > 3,
setup(S1, S2, S3),
compute_iter(4, TargetTurn, Result).
:- compute(0, 3, 6, 2020, Result),
write('On turn 2020, number is '),
write(Result),
write('n').
teach me senpai!
kek, okay what do you need to know?
>the hash table
Prolog is a database of facts, and you can modify this database at runtime.
assert() introduces a fact, and retract() deletes it.
Maybe you'll like this explanation:
I set this program up so that I can create or delete facts that look like "hash_table(K, V)". You can think of this as saying "at key K, the hash table has stored the value V".
This is the 'dictionary'.
The goal hash_put(K, V) uses assert() to insert the fact "hash_table(K, V)" into the dictionary. For example hash_put(hello, world) will create the fact "hash_table(hello, world)".
hash_get(K, V) can be used to "get" things from the hash table. Continuing the example above, I could write "hash_get(hello, X)" and Prolog will search for a fact that matches the pattern and make X equal to "world". But I can also do hash_get(X, world) to find a key associated with "world". So it's not a normal dictionary, it's more flexible because of Prolog's "unification" feature.
>van eck sequence
In the hash table I store the numbers that have been spoken so far, together with the turn number on which the number was most recently spoken. For example if number 3 was most recently spoken on turn 5, then the database should contain the fact hash_table(3, 5). And so on for all the other numbers spoken. If I encounter the number 3 on a later turn, say, 8, then I delete the old fact with retract() before I create a new fact hash_table(3, 8). This is what hash_put does.
I use the goal compute_iter, which uses tail recursion to compute the numbers to add to the hash table each turn. It begins at turn 4 and just counts up to 2020.
I also use the hash table to track another variable I called "prev", which is the number spoken on the previous turn. Only after I have calculated the number for the current turn (NextNumber) will I insert the previous one into the hash table as normal.
>Given your starting numbers, what will be the 30000000th number spoken?
Is it still fast on your machine for part 2 of the puzzle? The 30000000th number spoken? Because on my machine it has been running for quite a while and is still not finished.
Meanwhile, the translated python code which I mentioned earlier is done within seconds on my machine. (It looks weird because it's ported from prolog without cleaning up.)
[CODE]
def take_turn(Dict, LastTurn, LastNumber):
while LastTurn < 30000000:
Age = 0
if LastNumber in Dict:
PreviousTurn = Dict[LastNumber]
Age = LastTurn - PreviousTurn
Dict[LastNumber] = LastTurn
CurrentTurn = LastTurn + 1
LastTurn = CurrentTurn
LastNumber = Age
return LastNumber
def initialize_dict(List):
Turn = 1
Dict = dict()
for E in List:
Dict[E] = Turn
Turn = Turn + 1
return Dict
def main():
FirstInputNumbers = [0,3]
LastInputNumber = 6
LastInputTurn = 3
Dict = initialize_dict(FirstInputNumbers)
Result = take_turn(Dict, LastInputTurn, LastInputNumber)
print(Result)
main()
[/CODE]
Let's try using the code tags again, I guess....
def take_turn(Dict, LastTurn, LastNumber):
while LastTurn < 30000000:
Age = 0
if LastNumber in Dict:
PreviousTurn = Dict[LastNumber]
Age = LastTurn - PreviousTurn
Dict[LastNumber] = LastTurn
CurrentTurn = LastTurn + 1
LastTurn = CurrentTurn
LastNumber = Age
return LastNumber
def initialize_dict(List):
Turn = 1
Dict = dict()
for E in List:
Dict[E] = Turn
Turn = Turn + 1
return Dict
def main():
FirstInputNumbers = [0,3]
LastInputNumber = 6
LastInputTurn = 3
Dict = initialize_dict(FirstInputNumbers)
Result = take_turn(Dict, LastInputTurn, LastInputNumber)
print(Result)
main()
you can drop [/CODE] (in lowercase) if there is nothing after
I see, thanks!
Everybody's read through the prolog and now we're waiting for the main text
>Anon, were you trying to lie to me about your Prolog experience?
I did prolog back in 2005 at college. It was a meme then and is now.
Is it though?
https://www.cs.cmu.edu/Groups/AI/html/faqs/lang/prolog/prg/part1/faq-doc-9.html
>The Prolog 1000 is a database of real Prolog applications being assembled in conjunction with the Association for Logic Programming (ALP) and PVG. The aim is to demonstrate how Prolog is being used in the real world and it already contains over 500 programs with a total of well over 2 million lines of code.
https://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/prolog/doc/pl_1000/pl1000v1.gz
So linux kernel alone is 15M loc.
Those 2M is probably mostly projects by some overly-ardent graduate students and actually does nothing or could be rewritten in another language just as easily. The question here is: what are the idiomatic use-cases for prolog, and not "hey, we can do it here too, just 10 times slower".
anons, extract the archive. that's the main point of my post, I just copy pasted a few lines just so that you know wtf the random file is about, but when you look at the text files it's fucking impressive
I mean, I tried to read some prolog books, but lost interest, and they started either trivial or with how the engine or whatever works. What I gathered is that it all it is good for is a database of facts, where some facts are automatically inferred. The solver is of a general nature, meaning it's slow as fuck, but it's also dumb as fuck, because logical inference based on facts goes only so far.
> logical inference based on facts goes only so far...
...which is to say -- not very far. So I don't see how prolog can compete with normal languages. You basically have a general-purpose algorithm under the hood and you are trying to cajole it into solving the actual problem for you. That sounds not just anti-straitforward, but retarded as fuck.
So, prolog is basically a slow retarded database mainly used for jerking off in academia. Anything I missed?
>So, prolog is basically a slow retarded database mainly used for jerking off in academia. Anything I missed?
It was created around the time when symbolic AI was more popular than deep learning. And it's still used in industry at some places. Prolog is also great for expressing problems that require search or dynamic programming, like computing a schedule for N people that has to satisfy various constraints. Sure, you can also do anything in other languages because of Turing-completeness, but Prolog makes its tasks more convenient.
(me)
I forgot to mention planning problems, pathfinding, etc.
There are also computations that take so much time that Prolog being somewhat slower doesn't make a serious difference. Then what's more important is how convenient the language is.
>So, prolog is basically a slow retarded database mainly used for jerking off in academia. Anything I missed?
I think the best use for Prolog is "expert systems", not specific and intensive solvers. Programs that are not used to make relatively simple data structures transformations but rather programs containing hundreds of conditionals, all of them relating to the real logic of the program/applications, not conditionals inside loops checing for termaintions or for doing filtering.
You can see it from the list of companies and applications here
https://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/lang/prolog/doc/pl_1000/pl1000v1.gz. Each of this application is used to "help people think or act", they do not really transform
In this list there are least 1 or 2 prolog system for doing Law type kind of stuff. You can easily imagine that the kind of program requied for helping lawyers is not one that will transform linked list. It's more like it will contains thousands of conditionals to discrimimate situations or something. All that matters is the "rules".
>they do not really transform
*data structures. Like any regular program you use, from text editor to virtual machine to browser engine to game engine to ffmpeg, etc.... All of those transform data, their use case is not to help people think.
prolog is basically Lisp except with weird(er) syntax and you can run functions bidirectionally
Kinda reminds me of the Duolingo owl. Are they related?
>prolog
>future
kek
who knew the future was 30 years ago
>peak high tech was the 1990s
Yes.