- 53
- 428 537
The Power of Prolog
Приєднався 17 лис 2011
Join this channel to learn more about modern Prolog programming!
Describing a Knight's Tour with Prolog
A knight's tour is a sequence of knight moves on a chessboard such that every square is visited exactly once. With Prolog, we can easily express different models of this task, yielding different amounts of pruning power. Powerful global constraints such as circuit/1 reduce the search space significantly, and allow us to find solutions with a short and general program.
The code is available from: www.metalevel.at/knight/
The code is available from: www.metalevel.at/knight/
Переглядів: 1 148
Відео
Faster labeling for N-Queens
Переглядів 1,2 тис.10 місяців тому
In accordance with the famous equation "Algorithm = Logic Control", we can separate an algorithm into a logic component and a control component. Applying this approach to the N-queens puzzle, we can easily try different control strategies while keeping the logic the same. Varying the control component can significantly improve efficiency. The code is available from: www.metalevel.at/queens/ Rec...
Datalog
Переглядів 6 тис.Рік тому
Datalog is the functor-free subset of pure Prolog. Yes, of Prolog. Datalog resides in the intersection of several interesting topics such as logic, database theory, complexity theory and universal algebra. Datalog is a very convenient database and query language, and can express practically relevant queries that relational algebra cannot express. Datalog is also a programming language: Its prog...
The Prolog Toplevel
Переглядів 3,2 тис.2 роки тому
The toplevel is how we interact with a Prolog system. We post queries, and Prolog responds with answers. Various features can make a toplevel convenient and fun to use. Modern Prolog systems implement several innovative toplevel features that are worth emulating in all Prolog systems, and further improvements are still possible.
Definite Clause Grammars (DCGs)
Переглядів 9 тис.2 роки тому
Prolog has a built-in grammar mechanism, Definite Clause Grammars (DCGs). A DCG describes a sequence, and allows us to conveniently reason about sequences manifested as lists. DCGs are a very versatile mechanism, and one of Prolog's greatest attractions. We use them for parsing, describing output, passing arguments implicitly, reading from files, and several other applications. More information...
Formatting Output with Prolog
Переглядів 2,2 тис.2 роки тому
The nonterminal format_//2 lets us conveniently describe formatted strings and tables with Prolog. It is provided by library(format) in several modern Prolog systems such as Scryer, Tau and Trealla Prolog. The library is in the public domain, and you can freely include it in your Prolog systems and applications. It is best used together with library(pio) and its predicates for pure output, comb...
A Tour of Prolog
Переглядів 26 тис.2 роки тому
Let's take a look at Prolog's greatest attractions and most unique features! In addition to being an excellent database and query language, Prolog is uniquely suited for processing rules, reasoning about strings, and solving combinatorial problems. Rewriting code at compilation time, a feature called macros in other languages, is easily possible in Prolog. Prolog's implicit mechanisms allow exc...
Describing a Dragon Curve with Prolog
Переглядів 2,4 тис.2 роки тому
The Heighway Dragon Curve can be constructed iteratively as a sequence of folds. Prolog is ideally suited for reasoning about strings, and we can use its built-in grammar mechanism (DCGs) to conveniently describe sequences of characters that represent these folds, obtaining a short and general executable definition of this famous fractal.
Cryptography with Prolog
Переглядів 2,7 тис.3 роки тому
Prolog is very well suited for cryptographic applications: Scryer Prolog's built-in support for large integers, facilities for convenient and efficient text processing, and various predicates from library(crypto) allow us to easily build Prolog applications that guarantee secure communication and storage of data. More information: www.metalevel.at/prolog/cryptography
Creating a photo gallery with Prolog
Переглядів 3,5 тис.3 роки тому
Prolog is also a great scripting language. Let's create a photo gallery with Scryer Prolog, using library(pio) to declaratively describe HTML pages! Code: www.metalevel.at/prolog/showcases/gallery.pl
Prolog Antipatterns: How not to do it
Переглядів 4,1 тис.3 роки тому
The power of Prolog is rooted in logical properties we guarantee. If we violate these properties, then we can no longer reason logically about our code, and we cannot run it with different execution strategies.
Pondering Prolog
Переглядів 2,8 тис.3 роки тому
Let's address a few common questions about Prolog! Does Prolog need a static type system? Can I get a job with my Prolog skills? Why is Prolog so hard to learn? etc.
Solving the Social Golfer Problem with Prolog
Переглядів 3,2 тис.3 роки тому
The social golfer problem is a sports scheduling task. Prolog is extremely well suited for solving such combinatorial decision and optimization problems: We benefit from strong constraint propagation that efficiently prunes the search space, and we can also conveniently try other approaches such as SAT encodings and heuristic methods. More information: www.metalevel.at/sgp/
Prolog Meta-interpreters
Переглядів 7 тис.3 роки тому
Interpretation is pervasive in computer science both from a theoretical and a practical perspective. Prolog is exceptionally well suited for writing interpreters and meta-interpreters, and this is the source of its unique power. Meta-interpreters allow us to change and extend all features of Prolog, and incorporate new logic dynamically in our applications. More information: www.metalevel.at/ac...
Memory usage of Prolog programs
Переглядів 3,1 тис.3 роки тому
The global stack and the local stack are the most important memory areas in Prolog systems that are based on the WAM and related abstract machines. Common techniques to reduce memory usage of Prolog programs include tail call optimization, sharing of terms, and using a compact internal representation for lists and partial lists of characters. These techniques are system-specific, and there are ...
Clean vs. Defaulty Representations in Prolog
Переглядів 3,9 тис.4 роки тому
Clean vs. Defaulty Representations in Prolog
Sparrows on Eagles: Delegate your work to Prolog!
Переглядів 8 тис.4 роки тому
Sparrows on Eagles: Delegate your work to Prolog!
Thank you for this video. It has been very helpful in understanding Prolog.
Very useful, thanks!
Prolog is not definitely your average programming language... and I like it.
The intro aged very well.
Thank you a lot for your kind words, I greatly appreciate your compliment! Enjoy, and Happy New Year 2025!
Hello Markus, could you create a video about CHR? You're the best resource for learning Prolog. Thanks!
Great video as ever. I don't have `volatile` in my scryer-prolog operator table. Is that something specific to your environment?
Thank you a lot for your kind words, and for your interest! Regarding your question: `volatile` is not a standard predefined operator, and therefore Scryer Prolog does not define it in its default execution mode. However, we have: 5.5.2 Predefined operators A processor may support one or more additional predefined operators (table 7) as an implementation specific feature. So, additional operators may be available in a Prolog system as an implementation specific feature, as long as the system also has a strictly conforming mode in which the feature is turned off. Scryer Prolog is strictly conforming to the standard in its default execution mode, and therefore can be reliably used to detect syntactically valid and portable Prolog code, a major attraction of the system especially when learning the language.
This was the best prolog intro I had! Also this emacs config is great, gonna try both of 'em soon, damm as a nvim guy you kinda stole my heart there.
How are representing the AST on the sides of iur videos? Btw... Thanks alot for this whole channel, this is awesome and I thank you for that. People like you (teahcers) are the people that contribute the most with the world.
Thank you so much for your kind words, I greatly appreciate them! I draw the graph with showterm.el which is available in the "tools" directory of Scryer Prolog, please have a look, and I hope it helps: github.com/mthom/scryer-prolog/tree/master/tools
what is the software you use for these videos? You seem to evaluate prolog code and then print a tree is it emacs?
Enlightening as always. I’m inspired to rewrite a bunch of code tomorrow.
A says: Either ... or. To me this is a xor, not an or. Then you also have the solution A=0, B=1. goal: sat((~A # B) =:= A), labeling([A,B]).
Why not "is" instead of #=
(is)/2 works only if the right-hand side is ground. For example, we get: ?- X is Y + 3. error(instantiation_error,(is)/2). Whereas (#=)/2 can be used in all situations, and is therefore a lot more general and easier to use. For example: ?- #X #= #Y + 3. clpz:(#Y+3#=#X).
@@ThePowerOfProlog yeah right, but isn't there also other syntax for numeric evaluation equality? Never seen #= in university course, as they forbidden usage of any modules anyways.
The problem is for some boards, like 5x5, there is no circuit, but there is a complete path.
If you create the new state "out of the board", you must start and end in out of border. The first move from out of board can go to any position and the last move can go from any position to out of board, the condition is: if all squares are not empty you go to out of board. It will, at least I hope, create all possible paths. In graph representation, is a new node that is connected to all nodes, and it is the start point of the circuit. What do you think?
That's the same idea I came up with. I think it works fine. By forcing it to be the start point, you also (via `circuit`'s enforcement that nodes only appear once) forbid it from ending up anywhere in the middle of the path, cheaply. But by making it the neighbor of everything we say that the path can end anywhere. And the length constraint will make sure that the tour is complete.
6:00 - using clpz disunction/conjunction constraints 7:00 - foldl/3 explanation 14:13 - most general query example 15:08 - predicate overgeneralization example, explanation, and tradeoff explanation 16:50 - pairs library, pairs_key_value/3 predicate, label/1 predicate 18:50 - use of lambda to project away bindings 21:29 - core relations and conventions in constraint programming 21:35 - labeling/2 guarantees 26:42 - first example of thrashing with visualization 27:40 - example of ff options for labeling/2 27:47 - second example of thrashing with visualization 29:10 - discussion of weak propagation properties of clpz conjunctions and disjunctions 32:06 - base8 representation of chess board 33:15 - second example of weak propagation 35:27 - introduction of tuples_in/2 and all_different/1 37:46 - practical example of tuples_in/2 and all_different/1 used in a relation 38:29 - bounds consistency 39:02 - domain consistency 41:07 - third example of thrashing 43:51 - defining model as in "modeling a task" 45:05 - discussion of mental model of declarative programming 48:53 - overview of clpz combinatorial constraints 49:00 - tuples_in/2 49:07 - global_cardinality/3 49:20 - circuit/1 50:00 - circuit/1 explanation and demonstration 51:40 - representing knight's tour as a graph 52:37 - base N coordinate n_x_y_x/4 relation development and intuition 53:47 - knight_move/3 state transition / successor relation rewrite 56:08 - n_tour/2 development and rewrite 56:11 - knight_graph/4 predicate development and intuition 1:00:05 - practical usage of lambda with foldl/3 and clever inline toplevel query for iterative development 1:01:49 - discussion of operational constraings using circuit/1 1:04:22 - "Ready? Go". Successful knight tour. 1:05:10 - observations 1:08:01 - developing a readable output 1:08:59 - another example of foldl/3 usage 1:09:40 - example of nth1/3 usage 1:11:45 - usage DCGs in to develop format string 1:18:14 - recap of modeling and discussion of mental model for Prolog and conclusion *Note* for curious non-prolog developers: The name/number syntax indicates to other Prolog programmers predicate/arrity. If you have never worked with predicates before, it is easiest to categorize them as "Prolog functions" (but you will also soon see that the differences between them are great enough that the phrase "Prolog function" would make most Prolog developers instantly cringe -- I'm just mentioning this to help you orient yourself to this comment if the syntax is unfamiliar) *Note1:* DCGs stands for Definite Clause Grammars, Markus has another excellent video on them. He also has a **great video just on string formatting in Prolog.
Awesome. Thank you.
I think that most of the arguments about readability (for foldl or the final definition of knight_move) are not really convincing (people not used to functional programming have no idea what foldl does for instance). I also think that saying that there is no disjunction in an equality constraint with an absolute value… is only true if one knows how the solver is programmed ;) For the rest, didn't watch everything, but it seemed pretty nice !!! 👍🏼
Any hamiltonian path can be extended into a cycle by adding an additional vertex that connects the start and end of the path, so I imagine a way to model all knights tours is to add a (N*N + 1)th vertex that connects to all of the squares and consider the vertices next to it on the path as the ends of the tour.
I'll be following a course on Prolog at uni next semester, and I can already tell this'll be a very useful channel. Thank you for the content you put out!
Finally! I'm always waiting for these.
Let's go
norayr.am/papers/Dijkstra%20-%20Efficient%20String.pdf
Rust and eveything written in it is garbage.
Welcome ... Linux filesystem maintainers?
Can I close the clause?
Is it you Klaus?
I'm starting your online book, and I'm starting with just your videos (one after another). I've read some Prolog books before, but I want to learn to read and write Prolog like a Prolog programmer would read and write Prolog, so stuff like mother_child(Mother, Child) instead of isMother(X, Y).
this is like existential philosophy 303
This was so helpful! Thanks
Amazing video. Very fun topic!
Nice to see the progress since this video was made. When trying the example at 3:45 in this video, there is no longer a redundant choice point and the query returns only one result.
Yes indeed, and well spotted! This improvement is indeed a very recent development, it was implemented not even a year ago with github.com/mthom/scryer-prolog/commit/d520046a4f4d75de2cb76d18f46568e5fde3d614, providing lookahead indexing in Scryer Prolog!
Very well illustrated.
All these years on the web and one day, *boom*, someone else recommending Dvorak! 😬
I have been working for some time with the Prolog programming language and have recently installed Scryer Prolog 0.9.3. I am currently working my way through the book "Prolog programming in depth" by M. Covington. The best source for learning Prolog that I have found so far. Your wonderful video series on Prolog supports the whole thing. So far I have been able to use Scryer Prolog to solve all the problems I have encountered. There's just one thing I've noticed. The predicate 'listing/0, listing/1' does not seem to work. With Gnu P. and SWI P. the predicates work. Unfortunately not for Scryer. My question therefore relates to the current status of the predicate 'listing', or what am I doing wrong here.
Thank you a lot for your kind words and for your interest! In Scryer Prolog, listing/1 is available in library(format). The predicate for which a listing is generated must be public. One way to do this is to declare the predicate dynamic, using the dynamic/1 directive, as in :- dynamic(your_predicate/3). If you add such a directive for your predicate, then you can generate a listing with ?- listing(your_predicate/3). Please try it out, and file an issue in the Scryer Prolog repository if anything does not work as expected. Thank you a lot!
@@ThePowerOfProlog Thank you for your answer. It works now, using the "dynamic" directive.
The year of the Prolog programming language!.. next year will surely be the one... I'll stick with C.
Well, you should use the adequate tool for your particular problem. Give Rust a try.
Interesting that the redundant choice-point is no longer there in up-to-date scryer-prolog.
Yes indeed, well spotted! This is made possible by the following improvement in the engine from a few months ago: github.com/mthom/scryer-prolog/pull/1879/commits/749619319a40eb82e0649b23ad919cb32bcb781b With this engine improvement, and member/2 defined as in github.com/mthom/scryer-prolog/pull/1879/commits/c51ff4c03daad697f2ea77ca03d2b3462439b4d2, a redundant choicepoint is no longer created! Another important class of use-cases that benefit from this engine feature is shown in github.com/mthom/scryer-prolog/issues/1028. Such engine improvements have the potential to make many programs easier to read and write and retain very good efficiency.
Phenomenal series of videos. Perpetual Prolog beginner here. I get the arguments against the cut, but how would you then handle this very simple example? We want to get a thing. Getting it is failure-prone and expensive; we'll retry a couple of times and then give up (fail) if we can't get it, as we conclude that it's somehow ungettable. We really don't want to get it more than once. I would write without hesitation: get_thing(NumAttempts, Thing) :- NumAttempts > 0, go_get_it_and_pay_high_price(Thing), !. get_thing(NumAttempts, Thing) :- NumAttempts > 0, OneFewerAttempts is NumAttempts - 1, get_thing(OneFewerAttempts, Thing). I understand this eliminates logical possibilities -- which might be an issue if, say, thing changes over time in relevant ways. But in practice, let's say we just want the first success, not all successes, and we don't want a problem later in the program to trigger a backtrack over our get_thing/2. How would you solve this?
Supplement to former comment: Same n_factorial program executes in GNU Prolog 1.5.0.
man, I would like to learn plot the tree like in minute 1:14, did you have a vídeo of configuration of make trees?
Thank you a lot for your interest! Please see the following descriptions in the "tools" directory of Scryer Prolog: github.com/mthom/scryer-prolog/tree/master/tools I hope this helps!
Hi guys , Do anyone have idea how to integrate prolog in networking?
I have a question about the general structure of the N-Queens problem. I've implemented code similar to that presented in your previous N-Queens video. The heuristic I use is a very simple first-fail. Typically, my solution converges within 10,000 recursive function calls when iterating through board sizes. However, beyond a board size of 80, it loses this ability. Why is this? Additionally, is there a more suitable heuristic for this problem, and are there specific claims that can be made about such a heuristic? Also, what is the upper limit on the twice-reordered list heuristic combined with first fail? In the video, you demonstrate quickly solving for 120, but what about 150 or 200? Thank you.
Never mind, I did some more searching and found the wiki for Min-conflicts algorithm, apparently N-Queens can be solved in about 50 iterations regardless of board size.
Excellent Work!
Nice video! However I think you are (unconsciously) performing a magic trick while posing the problem and writing safe_queens/1. By searching for a 1D list, instead of a 2D structure, you are doing optimization while defining the problem to be solved. The idea to choose a 1D list as a representation is of course relatively obvious. But it is arbitrary. The underlying observation also holds for each row. We could further minimize the search space, by searching for a permutation of the numbers 1..N. This (in my opinion) works against the point you are making in the first half of the video. Sure, one can try different labeling strategies, while not modifying the representation. But tinkering with the *logic* part is equally effective.
Video starts as a trivial N-Queens problem, but then went into search optimization and introduced good way of thinking about performance of Prolog programs. Nice!
The prolog I know comes from watching your videos and scrolling trough "the power of prolog" website. Thank you sharing all :)
Amazing video. Gotta get myself more seriously into prolog or something like it :)
Fascinating, as always! Elucidated with expertise.
Another awesome video, thank you for your wonderful work.
A highly awaited sequel!!
Finally!!! Thanks Markus! Please don't make us wait so long between videos :)
Hello i had a question i can not solve yet: What happens if aº(bºc) = (aºb)ºc != aºbºc, what kind of mathematical structure is this, is it still a group?
If º (circle) is a binary operation, then what specifically do you mean with aºbºc ? There are binary operations that, by convention, associate to the left (example: subtraction), and binary operations that associate to the right (example: exponentiation). For instance, 5-3-1 is by convention interpreted as (5-3)-1, instead of 5-(3-1). But it is either one or the other, otherwise it is not a binary operation but a ternary operation, which takes all three arguments into account "at once".
Forget to the link to the paper d1.awsstatic.com/whitepapers/Security/Reachability_Analysis_for_AWS-based_Networks.pdf
AWS uses the optimized datalog solver soufflé to statically analysis network connectivity. One use is to prove security invariants like the DMZ network cannot access the management interfaces of any device.