lainchan / λ

Declarative Programming

Solomon · 2019-10-02 15:02
Do you think there are any declarative programming languages at the moment that are developed enough to be useful? Declarative programming is a really fascinating subject, I just wish there was more research/development there. Prolog has been the number one in the field forever, but things like Sentient Lang and Mercury are able to do similar things while being a much newer and more featureful language. That being said, with a proper declarative language, there's no such thing as a feature ;)
typedef · 2019-10-02 16:25
SQL, man, can't be more declarative than that.
Nemissa · 2019-10-02 20:50
>>15459 SQL obviously. Arguably the most successful fourth generation language yet.
Karl Koch · 2020-01-12 13:15
>>15464 in numbers, yes. (But then js is the most successful overall, and I am not willing to admit that.) Prolog is the most fascinating one I know of, but I can't say I undstand it well. I need to learn it right now, just like >>16405
Neo · 2020-01-14 03:03
Don't forget that Erlang, Haskell, and Lisp are all declarative languages (although Lisp has picked up a lot of imperative elements)
Karl Koch · 2020-01-14 13:48
wait, Erlang is declarative? Is Elixir then too? I fuarrrking love Erlang / Elixir, more than is reasonable. Had too much trouble programming parallel code before.
Kay · 2020-01-14 17:38
SML is the most usable mainstream PL. I'd consider Haskell more declarative than SML since the actual declarations of types and data are static. Prolog would be more declarative than SML. But all this is meaningless, what is a declarative language? I doubt any exist, since they're full of side effects. A recursive algorithm which can be implemented in a fully declarative subset of Haskell doesn't provide any benefit just from the fact that it's declarative.
null · 2020-01-16 18:59
>>16545 Think about it like this: Let's say you want your friend to pick you up An imperative language would be like telling him step by step where to go. He doesn't really know where he's going and if he runs into any unexpected issues on the way he's kind of boned, but you're confident enough in your instructions that it doesn't worry you. In a declarative language, you tell your friend where you are and that you need to be picked up, and it's up to him to figure out how to get there. You have no idea what route he'll take or what he'll encounter on the way, but that doesn't matter because you trust him to find the most efficient route to you. Also note that the order in which you say "I'm at (place)" and "I need to be picked up" don't matter. Your friend shoud naturally reason that he needs to go to your location to pick you up so the order will arise naturally. The difference between the two approaches is less of a technical difference and more of a philosophical one. Declarative programming places the importance on "what", while imperative programming places the importance on "how".
Fred Nurk · 2020-02-06 14:56
>>16537 To varying degrees. I think that Lisp dialects are the most interesting in that direction because while they are not necessarily declarative themselves, macros let you implement DSL's which are definitely declarative. Haskell is also pretty good at making DSL's, but in a different way. I suppose that being functional with lazy evaluation makes many pieces of code more declarative. I haven't worked enough with Erlang to talk in detail about it, apart from at the top level with its actor model that sets it apart and the fact that it's generally kind of functional. Elixir borrows macros from Lisp though.
hash table · 2020-03-06 11:22
renames · 2020-03-08 20:39
>>17254 >The number one most important defining aspect of a declarative language is the reduction and elimination of state. Immutability provides this for data, but not for logic. But you can't have imperative logic if there's only immutable data. >The structured control flow statements used in most of the popular languages are inherently stateful because they make functions dependent on the functions and commands executed before them. If I rearranged the code in a Rust program it would output completely different results than kt dud before. Sure, but I can reorder the expressions of a Haskell program and get different results. I can express an imperative program or a declarative program with denotational semantics. Reordering expressions in one is just as meaningful as reordering expressions in another.
Chani · 2020-03-08 22:30
>>17254 a purely declarative language can't and doesn't exist. in the end a command is interpreted and does something for the user. you can think of any program as a series of pure transforms over time. the difference is the granularity of those transforms. SQL and Haskell having larger granules than C and asm.
Anonymous · 2020-03-08 22:31
>>17270 my general point is the granules are culturally getting largers as we adopt more functional style in our code (collectively).
Recursion without an exit · 2020-07-02 05:07
Have someone actually tried Mercury? How was it? Does it have any type system?
word · 2020-07-02 07:00
>>16537 I believe the functional and declarative paradigms are different. Erlang, Haskell, and Lisp are functional but can they be considered declarative?
Recursion without an exit · 2020-07-03 02:43
>>19461 To answer that one need a formal criteria.
cdr · 2020-07-04 01:19
>>19459 >Have someone actually tried Mercury? How was it? B&D Prolog spinoff >Does it have any type system? RTFM https://mercurylang.org/information/doc-release/mercury_ref/Types.html
let · 2020-10-19 05:15
Take the prolog pill fib(1,1). fib(2,1). fib(N,FN) :- N>2, N1 is N-1, N2 is N-2, fib(N1,FN1), fib(N2,FN2), FN is FN1+FN2.
nibble · 2020-10-27 15:57
I've been learning prolog at school. It's hard, but I find it very interesting and I love breaking my mind over dumb prolog problems
Unknown · 2021-02-19 14:11
Markov Algorithms are neat. They repeatedly rewrite their input based on certain rules. aA Aa a#a A# a# #B B a A C C a # This implements the GCD algorithm, where the integers are however many a letters each, seperated by #, but I've not checked it thoroughly yet beyond a few test cases.
Cowboy · 2021-02-19 18:00
>>24543 While it's on my mind, here's a ruleset which determines if a string is suitable for this ruleset, with validity signalled by an empty string and being invalid otherwise: aa a a#a
Chani · 2021-02-21 22:19
PineScript in TradingView feels declarative at times. A lot of its values are not single values but a series of values, so it's like you're writing a map function that's applied to a stock chart. https://www.pinecoders.com/
Dozer · 2021-04-03 10:22
Since this is the de-facto logic language thread, I'll ask here: what's the best logic language for handling state mutation? Or otherwise how can I efficiently handle state that can't be represented as a dictionary? Context is that I've found programming in Dialog (https://linusakesson.net/dialog/) to be really fun, but I'm running up against the constraints of the language trying to implement procedurally generated rooms. In Dialog, state mutation is restricted to the equivalent of global variables represented as un-ary facts, and dictionaries represented as bin-ary facts. These facts do not backtrack/multiple-return/are deterministic. Since these are the only available ways to store and modify state (without using a monad), there's no way to directly represent facts such as (instance $Obj in $Room contains $Child) . Of course, I can use a combination of ($Obj is in $RoomList) , ($Obj has children $NestedList) and for loops, but at that point I'm programming imperatively. It also seems like it would be slow, since it's essentially a poor implementation of a logic language on top of Dialog. In contrast, the standard library handles things by assuming most objects have only one instance, and those that should not are merely moved between rooms as the player does (which breaks for containers). It's implemented as ($Obj has parent $Parent) , which naturally forms a tree. Looking elsewhere, I've found that Prolog handles state mutation via the more general assert/retract, but this allows for duplicate facts or rules, since retract only removes the topmost instance. Other languages all seem to be too unpolished or cumbersome. Flix is almost exactly what I want, but it currently does not have the ability to return all solutions to a query as anything other than debug output. There's also differential datalog, but since it's bottom-up, I fear that the database will explode in size as it calculates all the possible paths in the game. Would Mercury be a good fit here? I just want to make game.
John Rain · 2021-08-30 07:03
>>27347 Prolog is also a very good declarative programming language which you can embed in lisp. You declare facts and it basically does a graph traversal to figure out the answer. It is not always efficient but this language is really good for a lot of things, especially this, and can be optimized by being careful. Example prolog program with rules for car, cdr, cons, and member. % Car is the head of a list. car([X|Y],X). cdr([X|Y],Y). cons(X,R,[X|R]). ?- car([1, 2], X). X = 1. % x is a member if it's at the head of the list, or if it's in the rest of the list. member(X,[X|R]). member(X,[Y|R]) :- member(X,R). ?- member(1, [1, 2, 3]). true. ?- member(X,[23,45,67,12,222,19,9,6]), Y is X*X, Y < 100. X = 9 Y = 81 ; X = 6 Y = 36 ;
Agent Smith · 2021-08-30 07:09
>>15460 >>15464 sql is fuarrrking trash, one of the worst things that has ever been created seriously
t · 2021-08-31 06:19
I enjoy prolog but I have never found it useful. it was a waste of time to learn it. Although i said it was enjoyable, it wasnt to the point of being recreationally enjoyable
Crazy Jake · 2022-02-11 13:52
If we're counting SQL, there's also Datomic's flavor of Datalog.
par22 · 2022-08-23 19:29
I am thinking of making some kind of "expert system" for self-education purposes. Is Prolog unusually good for that kind of thing?
drop · 2022-08-26 10:50
>https://lpn.swi-prolog.org/lpnpage.php?pageid=online > >>32506 >Is Prolog unusually good for that kind of thing? I would use Prolog if I ever wanted to make an expert system. Prolog and Lisp used to be the languages for making AI programs.
Hopper · 2022-08-27 11:14
>>15459 Very interesting thread. I've heard of prolog and verilog before and I have intentions of learning them. Any good book resources for learning such paradigm?
SHODAN · 2022-08-27 12:00
Copilot looks really cool. It's used to generate code for hard real time applications. It's a declarative language that's a subset of Haskell. https://copilot-language.github.io/
Koichiro · 2022-08-27 18:26
>>25237 The most battle-tested language for state mutation is Javascript, the world's most popular and widely distributed programming language.
par22 · 2022-08-27 23:31
>>32554 If you are familiar with Lisp and want a short introduction to Prolog-style logic programming: * SICP chapter 4 has an introduction to logic programming, and shows an implementation of a logic programming language in Scheme. * Paradigms of Artificial Intelligence Programming (chapters 11 and 12) has a similar introduction but with more detail, and uses Common Lisp instead of Scheme. Refer to the books in >>17243 for learning Prolog using Prolog.
aliased · 2022-08-28 10:44
>>32557 Thanks will take a look
Jens Hansen · 2022-09-04 06:03
Wow, it's already been over a year? God I'm useless. >>32556 Javascript is not a logic language, unfortunately. While in principle I could write any game I wanted to in an imperative language, declarative languages map especially well to interactive fiction. This is because the majority of an interactive fiction game is data: text to emit, facts about the world, and relations between facts. What's more, relationship definitions (i.e. code) tend to be very nonlocal, which is usually the opposite of best practice in imperative programming. Logic languages dominate interactive fiction for good reason.
par22 · 2022-09-04 07:18
>>32646 >Logic languages dominate interactive fiction for good reason. Are you referring to rule-based programming languages such as Inform? https://en.wikipedia.org/wiki/Inform Rule-Based Programming in Interactive Fiction: https://www.eblong.com/zarf/essays/rule-based-if/index.html
null · 2022-09-04 09:23
>>32647 Yes, your second link even states that rule-based programming is related to logical languages. From what research I did, it didn't seem like there's much practical distinction besides the circumstances of their development e.g.: logical in academia and rule-based in business, for the most part. This said, I can't remember why exactly I rejected Inform 7 as a candidate language. Researching it now, it does appear to have tables and an extension supporting dynamic object creation. I must have missed the latter during my initial research, since the official advice is to pre-instantiate a large enough number of objects. Thanks for jogging me out of my stupidity - hopefully there isn't some hidden gotcha that I've forgotten.
dictionary · 2023-12-05 01:02
For those who don't know much about them, this presentation covers some of the basic features that make truly declarative systems so interesting: https://www.youtube.com/watch?v=mRwHZTNGdoY There's some real neat stuff in there. I know when I first got linked to it, I spent weeks digging through these "outdated" languages afterwards. Higher-gen languages are the best candidates for giving even advanced early languages like Lisp a run for their money. Anyway, just thought I'd share. It occurred to me that I didn't post it before, and anons that hadn't at least seen a presentation like that probably didn't get anywhere near as much out of this thread as I did, so here it is.
par22 · 2024-03-02 16:04
The real practitioners of declarative programming are the bosses who get to dictate the requirements of software that will be implemented by programmers. The code monkey programmers are collectively a compiler for the language of the boss. This is the unfortunate reality of declarative programming in a world dominated by imperative programming.
Pulse · 2024-03-07 07:20
>>37822 Datalog mentioned! https://news.ycombinator.com/item?id=39606885
Apoc · 2024-03-08 00:27
>>21490 Sir, that code is exponential. Do not use recursion to calculate fibonaccis. Consider the following piece of corecursion shamelessly stolen from rosettacode instead: fib(C, [P,S], C, N) :- N is P + S. fib(C, [P,S], Cv, V) :- succ(C, Cn), N is P + S, !, fib(Cn, [S,N], Cv, V). fib(0, 0). fib(1, 1). fib(C, N) :- fib(2, [0,1], C, N).
Mario Rossi · 2025-02-12 23:00
Good news! Prolog has just entered the top of the TIOBE index. This is most likely due to the rise of A.I.-based technology research; although this is just my opinion. Let's see if it holds up or if it was just a fleeting moment. If anyone is interested in getting started in the logical and declarative paradigm, I personally think that doing some research on Symbolic Logic (a branch of Discrete Mathematics) is not a bad idea.
Backus · 2025-02-13 03:32
>>41162 Almost certainly because of its use in reasoning systems and logical inference, so you're right
Nguoi La · 2025-02-13 05:26
>>41162 the tiobe index has never beeb an indicator of real world usage of programming language and anyone that cares about that isn't trying to push a narrative should use different (and multiple) sources how many times a language gets googled means nothing
John Titor · 2025-02-13 12:03
it really depends on what you mean by declarative, the term is a bit vague. it could be argued that all languages that have LINQ-esque support for functional programming are declarative, there is hardly any modern programming language that wouldnt be included in the category in this sense.