6 ms·
The Mercury functional programming language
- ggm 6y agoTargets gnu C extensions if available. Are there many which are both useful, type safe, memory safe and not in LLVM? because I suspect reasoning about back end compiled state is a thing for a strongly typed deterministic language and gnu extensions are funny. The thing about how its syntactic sugar over pure-ish prolog was fascinating. (Talks about how its self hosted, bootstrapped up from prolog because they could)
- quicklime 6y agoMaybe not, but Mercury predates LLVM by almost a decade, so I can see why they targeted GCC rather than LLVM.
- pabs3 6y agoThis is apparently what PrinceXML (recently discussed on HN) is written in, except for the font shaping engine, which is written in Rust. https://en.wikipedia.org/wiki/Prince_(software) https://en.wikipedia.org/wiki/Prince_(software) https://yeslogic.com/blog/allsorts-rust-font-shaping-engine/ https://yeslogic.com/blog/allsorts-rust-font-shaping-engine/
- alfiedotwtf 6y ago> apparently Can confirm... we use a mix of Mercury and Rust :)
- vram22 6y agoYes, Prince XML is written in Mercury. I had mentioned this on HN a few times earlier: https://hn.algolia.com/?dateRange=all&page=0&prefix=true&query=Prince%20xml%20mercury%20vram22&sort=byDate&type=all https://hn.algolia.com/?dateRange=all&page=0&prefix=true&que... Apropos of the hn.algolia.com search used in the above link, it seems pretty good and accurate. I could put my HN user name into the text box along with words like prince and xml, and it worked right. Only returned comments by me. I did not have to put my name in a separate user name field. I'm guessing this a full-text search feature.
- aidenn0 6y agoThe email client I use, bower, is written in this.
- deleted 6y ago[deleted]
- quelltext 6y agoCan someone explain why it is called a "functional" language? Wikipedia says it's essentially a pure subset of Prolog, but Prolog is not classified as a functional language, so I'm curious what makes this one called a functional language. I assume there must be some significant language features that e.g. functions.
- riffraff 6y agoI think the difference is that in Prolog you have imperative state management while Mercury only has pure functions, and manages state through linear types. EDIT: the "about" page may explain this a tad better https://mercurylang.org/about.html https://mercurylang.org/about.html
- jfmc 6y agoAnother difference between Prolog and Mercury is that Mercury lacks 'real' unification. Predicates must be moded (they must be compiled for some particular variable instantiation states). The compiler reorders literals in a way that free variables do not need to be represented in memory. Almost all the 'logical' side of the language is discarded statically at compile-time. The first time that I tried it (+ 20 years ago!) executables were blazingly fast! The bad side (at the time) was poor flexibility and very slow compilation time (compared with Prolog).
- lars512 6y agoOf course, for any prolog program of significant size, you want nearly all of it to be moded and deterministic anyway, and not leave choice points all over the shop.
- toolslive 6y agoIndeed. Mercury has a really powerful static type system, while prolog hasn't.
- dragonwriter 6y ago> Wikipedia says it's essentially a pure subset of Prolog, but Prolog is not classified as a functional language That statement used “pure” in the sense of “functionally pure” (I'm not sure about “subset”; it certainly lacks some prolog features that are impure, but seems to have features that Prolog lacks; it seems more like it contains a pure subset of Prolog than that it is one.) > I assume there must be some significant language features that e.g. functions. I think you missed something I. that sentence, but, yes, Mercury has first-class and higher-order functions.
- jfmc 6y agoThe title should be corrected to "The Mercury functional logic programming language".
- kyberias 6y agoTried to spend 2 minutes getting the basics. The "crash course" is impenetrable.
- sabellito 6y agoIt seems that the crash course is more suitable for people who know haskell. Perhaps you'd have a better time with the book instead: https://www.mercurylang.org/documentation/papers/book.pdf https://www.mercurylang.org/documentation/papers/book.pdf
- jfmc 6y agoThings gets better if you try a couple of hours rather than minutes. If you've never been exposed to the logic programming paradigm this is completely understandable. Isn't it?
- FridgeSeal 6y agoThis seems pretty cool, but I don’t really understand the function-mode concept, does anyone have a good explanation of what it is or why it’s a feature?
- padator 6y agoI think it's more related to Prolog than functions. In prolog you can use a predicate in many ways. For example append(L1, L2, L3) declared that the concatenation of the list L1 and L2 result in L3, but you can use it in a "reverse" way too (thx to Prolog awesomeness) like append(L1, L2, [1,2,3]) and Prolog will enumerate all possibilities for L1 and L2. This can be slow though, and I think 'mode' is a way to declare all this different way to use prediate (in, int, out), (out, out, in), etc. and maybe compile things efficiently. See https://www.mercurylang.org/information/doc-latest/mercury_ref/Determinism-categories.html https://www.mercurylang.org/information/doc-latest/mercury_r...
- Avshalom 6y agoMode declarations aren't really a feature so much as they are additional information provided by the programmer to the compiler. In Prolog if you have a predicate foo(X,Z) :- bar(X,Y), baz(Y,Z). Traditionally if you call foo(1,Z) it will try to find an answer for bar(1,Y) first then feed that Y value into baz, if you call foo(X,3) it will still try to find values for bar(X,Y) first. Sometimes that's okay because the engine will be like well yeah there's something that'll work so we'll say bar is true and move on to baz but some times it tell you to go suck eggs. You could write two different versions of foo %foo(in,out) foo(X,Z) :- ground(X), bar(X,Y), baz(Y,Z). %foo(out,in) foo(X,Z) :- ground(Z), baz(Y,Z), bar(X,Y). but having to do that sucks. First it's forcing you to think about Prolog's implementation rather than the logical relationship between X and Z; second it's repetitive and forces you to keep both versions in sync with each other which is gonna get rough for more complex predicates. One of the things Mercury uses that mode info for is to re-arrange the body of foo(X,Y). So if you tell it that you will use both foo(in,out) and foo(out,in) it will compile two different versions of foo and dispatch appropriately.
- YeGoblynQueenne 6y ago
- lars512 6y agoProlog feels like imperative programming, but with a special kind of depth first search through options built-in to the language. Mercury feels like a functional language, but with the same depth-first search built-in. I like some of the semi-determinism or non-determinism, it’s sometimes a really natural way of specifying part of a problem. But it’s only magic sauce part of the time.
- cryptonector 6y agoYou should look at Icon [0][1], which is a lot like... Pascal, or any Algol family language, but which has pervasive depth-first search and backtracking built-in. Icon is and feels very procedural. Or jq, which also has pervasive depth-first search and backtracking built-in. jq is and feels very functional. [0] https://www2.cs.arizona.edu/icon/ [1] https://en.wikipedia.org/wiki/Icon_%28programming_language%29
- nabla9 6y agoOld (late 90s) joke about Mercury programming: Q: How many Mercury programmers you need to change a light bulb? A: Three. One to change the light bulb and two to distract the nurses. ---- Maybe some Mercury programmer can explain.
- nerdponx 6y agoI always wanted to try logic programming, but I felt like I never understood how to use it effectively. For my own personal coding projects, 99% of the time I am either writing a CLI tool or a daemon/server of some kind. I've attempted to learn Prolog in the past, but I never got to the point where I felt like I could actually write a complete program that interacts with the world in a meaningful way. So I pretty much gave up before ever getting as far as checking out something like Mercury... and I couldn't even figure out how to use miniKanren at all. I can certainly see the utility of logic programming as an embedded DSL, but I have no idea how to write a "complete" program in a logic language. Am I missing something? Is there a specific book or guide I should follow? Are there examples of open source programs that use logic programming I should look at?