{"id":23328004,"url":"https://github.com/mcsoto/cosmos","last_synced_at":"2025-08-22T22:32:42.877Z","repository":{"id":18927282,"uuid":"22146555","full_name":"mcsoto/cosmos","owner":"mcsoto","description":"A new logic programming language.","archived":false,"fork":false,"pushed_at":"2023-07-30T15:52:14.000Z","size":1570,"stargazers_count":176,"open_issues_count":2,"forks_count":8,"subscribers_count":17,"default_branch":"master","last_synced_at":"2023-11-07T15:27:07.426Z","etag":null,"topics":["composite-types","cosmos","cosmos-language","functors","interpreter","language","logic-programming","prolog"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mcsoto.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-07-23T12:37:05.000Z","updated_at":"2023-10-20T01:27:18.000Z","dependencies_parsed_at":"2022-07-25T06:47:04.380Z","dependency_job_id":null,"html_url":"https://github.com/mcsoto/cosmos","commit_stats":null,"previous_names":[],"tags_count":0,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcsoto%2Fcosmos","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcsoto%2Fcosmos/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcsoto%2Fcosmos/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mcsoto%2Fcosmos/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mcsoto","download_url":"https://codeload.github.com/mcsoto/cosmos/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230645345,"owners_count":18258530,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["composite-types","cosmos","cosmos-language","functors","interpreter","language","logic-programming","prolog"],"created_at":"2024-12-20T20:54:09.346Z","updated_at":"2024-12-20T20:54:09.968Z","avatar_url":"https://github.com/mcsoto.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"\nCosmos\n====\n\n\u003e 1. (noun) the world or universe regarded as an orderly, harmonious system.\n\nCosmos is a modern logic programming language.\n\n_Cosmos 0.12 VM - Alpha version released._\n\nStatus\n---\n\nThe language is in testing stage.\n\nThe development has moved to [here](https://cosmos-lang.github.io/).\n\n* This repo has Cosmos(c). It's the repo for the standalone C compiler.\n* At some point if we or any contributor wants to work on the standalone C compiler, they may use this repo.\n* Currently, the most up-to-date version is Cosmos(pl).\n* Cosmos(pl) is found on the link.\n\nDownload\n---\n\n_Coming soon._\n\nDocumentation\n---\n\n_Wait for it._\n\nSee test samples.\n\nBuilding\n---\n\nRequirements:\n- gcc\n\nUse `make cosmos` or `make` (this will install additional utilities)\n\nLinux will require `sudo make`\n\nTested using mingw/cygwin and gcc (7.4.0).\n\nSee readme.txt for dev info.\n\nQueries\n----\n\nYou can try out the language by opening the interpreter and making _queries_ to the language.\n\n```\n$ cosmos -i\n\u003e x=1\n| x = 1\n\u003e x=1 or 2=x\n| x = 1\n| x = 2\n```\n\nThis will compile and run a file `test1.co`.\n\n```\n$ cosmos -l test1\n```\n\nOverview\n====\n\n```javascript\nrel main()\n    x = io.read()\n    io.writeln(\"hello, \"+x+'!')\n```\n\nThe syntax is close to that of a typical scripting language (Python/JavaScript/Lua). The difference is that whereas traditional scripting languages tend to focus on imperative programming, Cosmos focuses on declarative (logic and functional) programming.\n\n\nA Neutral Language\n----\n\nAlthough it is a logic programming language, code in this language can look and behave very conventionally: code might seem imperative or functional.\n\nHowever, it's a declarative language. As such, variables are immutable. Instead of modifying a value we create a new one.\n\n```javascript\nlist.push(l, 55, l2) //instead of modifying l, we create a new variable l2\nio.writeln(l)  //[1, 2, 3]\nio.writeln(l2) //[1, 2, 3, 55]\n```\n\nCosmos adopts principles from (procedural) scripting languages, logic programming and functional programming. You may write using an almost imperative style, this will however compile to logic code.\n\n\nRelations\n----\n\nInstead of functions, Cosmos has relations.\n\nWhereas functions have one output, relations may have zero, one or more outputs. You can check this by making queries at the interpreter.\n\n```$\ncosmos -i\n\u003e x=1 or x=2 //this query has two answers (outputs)\n| x = 1\n| x = 2\n```\n\nIf the system picks one answer and it turns out to be invalid, the system will backtrack and pick the other.\n\n```javascript\nrel p(x)\n    x=1 or x=2\n    \nrel main()\n\tp(x)\n\tx!=1\n\tio.writeln(x) //2\n```\n\n\nWhen a relation is nested, the last parameter is hidden.\n\n```javascript\n//note that the there is no 'return' in the definition\n//instead, the parameter y is explicit\n//this is typically the 'output' parameter\nrel double(x, y)\n    y = x*2\n\ndouble(4,x) //x is 8\n```\n\nRelations may adopt function syntax. When nesting, for example,\n\n```javascript\nprint(double(3)) //this will print 6\n```\n\nLogic-wise, `double(4,x)` is read as a statement: \"the double of 4 is x\".\n\n`double(4)` reads as a function; \"the double of 4\".\n\nFunctors\n----\n\nFunctors are composite data.\n```\nfunctor(F, Functor) //declares an object for creating functors\nx = F(1, 2) //x is assigned to a functor F with the values 1 and 2\nx = F(1, a) //uses pattern matching to match F(1, 2) against F(1, a)\nprint(a) //2\n```\n\nLists are syntax sugar for the functor Cons. Here are two ways to define a list:\n\n```\nl = [1, 2]\nl = Cons(1, Cons(2, Cons))\n```\n\nRelations such as _first_, _map_ and _filter_ can be used to manipulate lists.\n\n```\nrequire('io', io)\nrequire('math', math)\nrequire('list', list)\n\nrel main()\n    l = [1,2,3]\n    list.first(l, head) //head is 1\n    list.rest(l, tail) //tail is [2, 3]\n    list.map(l, math.inc, l2) //l2 is [2, 3, 4]\n    list.filter(l, rel(x) x!=3;, l4) //l4 is [1, 2]\n```\n\nCosmos adopts many principles and features that are common in functional programming languages (although the principles apply to *relations* rather than *functions*).\n\nTables\n----\n\nTables (also known as maps, dictionaries, etc.) are structures that map keys to values.\n\n```javascript\nTable t = {x=1 and y=2}\ntable.set(t, 'a', 1, t2)\n\nprint(t) //{'x': 1, 'y': 2}\nprint(t2) //{'x': 1, 'y': 2, 'a': 1}\n```\n\nBooleans\n----\n\nThere is no boolean type. Instead, relations themselves are \"booleans\".\n\nCode such as\n\n```\nif(s = 'a')\n\tx = 0\nelseif(s = 'b')\n\tx = 1\nelse\n\tx = 2\n```\n\nis simply sugar for\n\n```\n(s = 'a' and x = 0) or (s = 'b' and x = 1) or x = 2\n```\n\nFor a less imperative-looking code,\n\n```\ncase\n\ts = 'a'\n\tx = 0\ncase\n\ts = 'b'\n\tx = 1\ncase\n\tx = 2\n```\n\n\nWhitespace\n----\n\nThe language is whitespace sensitive.\n```\nrel p(x)\n    x!=1\n    x\u003c5\n```\nThis could be a single line.\n```\nrel p(x) x!=1 and x\u003c5;\n```\nIt's possible to drop the whitespace semantics by writing the unnecessary characters, although this is not generally advisable.\n\nNote that relations from different lines are separated by _ands_ (semicolons are only used to end the indendation).\n\nDetails\n----\n\n**0.12.1**\n\n- Added trace for debugging. Simply add this to your file,\n```\ndebug.sethook(debug.trace)\n```\n\n**0.12**\n\nWith the advent of the Cosmos 0.12 VM,\n- It's still in alpha.\n- It does not rely on other languages to build.\n- C++ VM released. Parser is still built in Cosmos(!) itself.\n- The VM is based on the WAM, as it provides a set of bytecode instructions for Prolog. Temporary variables are not used. See original WAM paper.\n- The parser makes use of an intermediary language, \"CWAM\". `Put(Var(0), 1)` translates to `put_variable 0,1`.\n- This has missing features, however, it's already more than (raw) Prolog. Again, this was uploaded only to give the language a basis.\n- It has its own license.\n- Does not rely on other Prolog implementations.\n- For language samples, see test examples.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcsoto%2Fcosmos","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmcsoto%2Fcosmos","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmcsoto%2Fcosmos/lists"}