6 ms·
Minikanren – An embedded DSL for logic programming
- deleted 8y ago[deleted]
- simongray 8y agoI've been looking at the Clojure implementation, core.logic, but can never figure out what to use this paradigm for. Can anybody name the cases where logic programming really shines?
- CJefferson 8y agoIt is useful for problems where the solution is difficult to find -- any kind of problem with many constraints which conflict, and maybe an optimisation condition. Such problems are very difficult from most programming, when it tends to be way to find what you want (although of course you can always optimise it further).
- weberc2 8y agoThis sounds like a layout engine for a GUI toolkit. If you have a bunch of widgets with optional min and max widths and other constraints and you put them all in the same row, the layout engine needs to compute the real size of each widget (also constrained by the width of the parent widget, which is also dynamic and resolved by the layout engine save for the top level window widget) so it can be rendered.
- typon 8y agoYou can use standard constraint solvers for these kinds of problems. Although, you can use minikanren in such an application, you are missing out on a lot of its power if you do that. An example where such systems shine is program generation, theorem proving, knowledge databases, program verification and other applications where the search space is huge but the rules are relatively simple.
- simongray 8y agoOk, thanks for answering. I guess what I'm really looking for are some practical use cases that can serve as an example, otherwise it will never cross my mind to use logic programming when I need to decide on technologies.
- Blackthorn 8y agoBasically -- anything that you'd use SQL for. SQL is probably the most widely-used logic programming language.
- typon 8y agoSQL does way less things than a full-blown logic programming system
- Blackthorn 8y agoWith common table expressions and windowing, SQL is Turing complete.
- typon 8y agoSo is LaTeX...but people aren't implementing logic programs in it.
- Blackthorn 8y agoThe argument is that minikanren does more than SQL. I strongly disagree.
- haolez 8y agoCare to elaborate? Genuinely curious.
- yorwba 8y agoLanguages of all kinds of paradigms are Turing complete. But we still use different languages for different purposes, because some programs are more convenient to write in a certain paradigm that maps well to their problem domain. In Prolog for example, you can easily define concatenation of cons-lists as concatenate(nil, Ys, Ys). concatenate(cons(X, Xs), Ys, cons(X, Zs)) :- concatenate(Xs, Ys, Zs). and then generate all 4 possible solutions for the query concatenate(X, Y, cons(1, cons(2, cons(3, nil)))). Of course Prolog has built-in lists that'd allow simply writing append(X, Y, [1, 2, 3]), but the ease of defining certain simple algorithms and then running them backwards is what makes logic programming so useful in my opinion. If SQL can express an equivalent program equally succinctly, I'd very much like to see how.
- kimi 8y agoSolving Sudoku :-)
- agumonkey 8y agoIMO it sits at quite a higher level of programming. You use logic programming to explore ~infinite program states; all that in extremely concise (near fully declarative of course) ways. In Bratko's prolog book you quickly get to do graph processing, some algorithms are 4 clauses and almost word for word what you have in mind. It's mind boggling to me. You also have bidirectional reasoning, you don't have to write a function A => B and then another one for B => A. It's even more telling with multivariate functions. I knew a guy who needed something similar for a large application. He needed a way to reason backward with incomplete parameters.
- otabdeveloper1 8y ago"Logic programming" is a misnomer. It's really a kind of parallel reality database query language. It's not used much because relational databases and SQL won in the end. (I guess back in the day people imagined computers as knowledge graphs and logical predicates on them, not tabular data and joins.)
- convolvatron 8y agowhy a misnomer? its exactly what programming would look like if you wanted to try to implement computing as predicate calculus.
- a-saleh 8y agoI would look at uKanren, the smallest mini-kanren cousin. I like Bodil Stokke's [1] introduction, but if you are not afraid of lisp, you can read/re-implement the original(?) [2] paper :) It is basically bunch of combinators for depth-first search? So if you think "I kinda want to brute-force solution for these hard-coded constraints", you might instead want to create a simple logic-programming environment first :-) Last time I played with these, I used it for implementing a toy cryptograhic protocol verification system for a term project. It have barely worked :D A.f.a.i.k. type-checkers often use some sort of logic programming at their core. [1] https://paperswelove.org/2015/video/bodil-stokke-%CE%BCkanren/ https://paperswelove.org/2015/video/bodil-stokke-%CE%BCkanre... [2] http://webyrd.net/scheme-2013/papers/HemannMuKanren2013.pdf http://webyrd.net/scheme-2013/papers/HemannMuKanren2013.pdf
- KirinDave 8y agoSure, I can name one I've prototyped (but never gotten to deploy in prod) in industry. I proposed an embedded μKanren in an Authorization framework for API endpoints. It relied on a system like JWT (or, preferably something less broken like Macaroons) to provide a series of "auth grants" on the request, auth grants being parameterized lines like: (= :user-id 16) (bu.admin-grant (group 10)) (is-super-admin) A few more like this. Then, you could use Python annotations on API methods to specify this kind of setup on API endpoints: (= :group_id request.group-id) (bu.admin_grant (group :group-id)) The system would then perform a kind of logical unification with the request, the cryptographically validated request token, and the API policy. When a request comes in, the request token is merged a "all is forbidden" default set (which is not a literal hash table, it's a policy annotation for the map that contains them). Thus, it's very easy to create arbitrarily complex security policies and so long as they're logically consistent, the interpreter can tell you so (and if it fails, it can tell you why the request doesn't have access). What's also interesting was that you could unify the API endpoint annotations with an empty set of grants, and then it'd produce a series of example permissions that would be valid (or you could merge it with a user's invalid grants, and it could tell you what access they need to go through). This worked well for internal documentation and I had hoped that it'd be applicable to internal tooling to explain to an operator of a tool what access they needed to go request to use the tool (and even where to do it). If you're curious, for most requests the time complexity and memory costs were very modest (approaching but not passing 1ms in the worst case I saw, which for API endpoints written in Python is usually dwarfed by garbage like JSON serialization).
- tom_mellior 8y agoHere is mediKanren (from the same authors): https://github.com/webyrd/mediKanren https://github.com/webyrd/mediKanren It's used for automated search through medical databases for drug discovery: https://www.uab.edu/mix/stories/a-high-speed-dr-house-for-medical-breakthroughs https://www.uab.edu/mix/stories/a-high-speed-dr-house-for-me... Here is the Gerrit code review system: https://www.gerritcodereview.com/ https://www.gerritcodereview.com/ It allows you to specify complex rules for workflows, who may sign off on what patches etc. Those rules are written in Prolog, but I guess they might as well be written in some other logic language. Here is (or rather, will be) Gigahorse, an Ethereum smart contract decompiler: https://2019.icse-conferences.org/event/icse-2019-technical-papers-gigahorse-thorough-declarative-decompilation-of-smart-contracts https://2019.icse-conferences.org/event/icse-2019-technical-... The abstract doesn't say what "declarative, logic-based specification" is used in the implementation, but I know that two of the authors do a lot of work in Datalog, so it's Datalog.
- avhon1 8y agoI took a class with Daniel Friedman, the creator of Minikanren and author of The [Little|Seasoned|Reasoned] Schemer. One of the homework assignments was to write a typechecker for the simply-typed lambda calculus in minikanren. (Most of the semester, we were writing interpreters for the lambda calculus, and doing things like rewriting the interpreter in continuation-passing-style (so that every recursive call was a tail call).)
- nightcracker 8y agoHow does this relate to Prolog and Datalog?
- tokai 8y agoIt's right at the end of the first paragraph. http://minikanren.org/minikanren-and-prolog.html http://minikanren.org/minikanren-and-prolog.html
- shioyama 8y agoSlightly more approachable intros to this topic: Hello, declarative world: https://codon.com/hello-declarative-world https://codon.com/hello-declarative-world Reasoned PHP: https://igor.io/2014/08/06/reasoned-php.html https://igor.io/2014/08/06/reasoned-php.html
- mdiep 8y agoI made a Swift library, Logician (https://github.com/mdiep/Logician https://github.com/mdiep/Logician), based on microKanren with the help of these links. I also wrote about how it works with Swift: https://matt.diephouse.com/2016/12/logic-programming-in-swift/ https://matt.diephouse.com/2016/12/logic-programming-in-swif... I haven't had the chance to build anything that uses them. But having become familiar, I think there are lots of potential applications. I always thought that Prolog was strange. Logic programming makes a lot more sense as a library IMO.
- nils-m-holm 8y agoOr, if you prefer a book: http://t3x.org/amk/index.html http://t3x.org/amk/index.html
- majewsky 8y agoSo this appears to be a language to specify logic systems. This makes it especially odd how unstructured the website appears. If the language is so simple and uses only 3 or 4 operators, why not just start with a short definition of those operators, and maybe a list of short examples? That would go a long way towards explaining to me as a casual visitor why I should care about this.
- lambda 8y agoThere is a link to exactly that at the end of the second paragraph: http://io.livecode.ch/learn/webyrd/webmk http://io.livecode.ch/learn/webyrd/webmk
- KirinDave 8y agoSo, this project is a web side of a famous (arguably THE most famous) book on relational programming. It's a book that introduces a dialogue for learning the language, how it's implemented, and its principles. The principles of relational programming are the important part. It's not strictly "Incorrect" to say "MiniKanren specifies logical systems" but it doesn't really capture the spirit of relational programming. The book is linked from the page, and the book itself is well reviewed on average (and gets an excellent rating from me, personally). The first 3 paragraphs explain this and link to a video with more details. MiniKanren's had some extremely interesting results presented in it, most famously a relational bidirectional interpreter (which can suggest programs that transform input in a specified way). William Byrd (one of the primary authors of the work) still gives an excellent talk on the subject because it's not well known. [0] I can't help but notice the link to the book, the exhaustive list of papers and talks, links to videos and an invitation schedule. It seems like there was an awful lot of information you just ignored. As a friendly bit of advice, PLT, PLR, and in general the deeper and more modern end of algorithmic work is not going to present a punch, javascript-framework-esque 60 second animation for you. So if you are going to pursue more on MiniKanren (and I really recommend it, it's amazing stuff) then I strongly recommend you gird yourself for rifling through references and books as a first resort rather than a last.
- ufo 8y ago
- lopatin 8y agoMinikanren (and core.logic) is very fun to explore. It's such a brain bender and it seems well suited for some PL things. E.g. say you have some custom interpreter. If you implement that interpreter in Minikanren, you can run it backwards and answer the question "what program, if any, evaluates to this result". To generate a quine, you make both the input and the output of the interpreter the same logic var, and have Minikanren solve for it. On the other hand, I was unable to find a use for it any real world problems (in my case: scheduling and portfolio optimization). I always use a linear constraint solver instead.
- deleted 8y ago[deleted]
- Jeff_Brown 8y agoBe forewarned: MiniKanren has no "not" operator. (I invested a lot of time teaching myself MiniKanren, working through most of "the book" (The Reasoned Schemer), before I found that out.)
- appleflaxen 8y agointeresting detail; is there a reason why it doesn't?
- avhon1 8y agoSome implementations, including Will Byrd's [0], have "=/=" and "absento". This is basically "not". [0] https://github.com/webyrd/faster-miniKanren https://github.com/webyrd/faster-miniKanren
- xitrium 8y agoGentle plug for my literate Haskell implementation that aims to teach the concepts as they are defined: https://github.com/seantalts/hasktrip/blob/master/doc/MicroKanren.md https://github.com/seantalts/hasktrip/blob/master/doc/MicroK...
- gilmi 8y agoHey thanks!
- YeGoblynQueenne 8y agoFrom the list of implementtaions, microkanren in Prolog: https://github.com/mndrix/microkanren-prolog https://github.com/mndrix/microkanren-prolog (By the indefatigable Michael Hendrix)
- dang 8y agoFrom 2014: https://news.ycombinator.com/item?id=8395079 https://news.ycombinator.com/item?id=8395079