Want to use core.logic from the REPL

Hi,

I want to have a REPL with core.logic working in it.

I do the thing on the website: (use 'clojure.core.logic) in my VS Code REPL.

I get:

; Execution error (FileNotFoundException) at user/eval7643 (REPL:50).
; Could not locate clojure/core/logic__init.class, clojure/core/logic.clj or clojure/core/logic.cljc on classpath.

That seems like a bug to me.

It seems I need to start a project and add the dependency there, but… it would really be cool if it was possible not to do that.

I found this: How to use a dependency from clojure repl without starting a lein project? - #4 by seancorfield

But I don’t understand anything that’s written there. I typed some stuff but that just gave more error messages.

1 Like

Hi @alper, glad to have you reach out to the community :slight_smile: Hope you didn’t have too harsh a landing in your attempt to get started with Clojure! And do hope you enjoy your time here :smiley:

Just quick comments to point you on track - currently you are trying to use an external library that is org.clojure/core.logic. This requires you to pull in the dependency (the core.logic jar).

There are 2 recommended ways to do this using different tools - Leiningen and deps.edn. While deps.edn might be simpler overall and recommended by others, I would want to direct you to use Leiningen at the moment, as there happens to be a good guide that should help you more quickly overcome this hurdle/frustration from being stuck :slight_smile:

What you need to do is:

  1. Download leiningen by following the instructions here - https://leiningen.org/
  2. If things are working well, you should be able to find the lein binary and run lein repl. This will start a repl in your command line. You can try typing (+ 1 1) to get 2. You can then type exit to exit
  3. Leiningen has a quick way to create a new project. lein new app <<NAME>> where <<NAME>> is the name of the project. You may use the name number-puzzles to follow the guide below
  4. Follow the guide below and add [org.clojure/core.logic "1.0.1"] to your project.clj file in the :dependencies section
  5. Start the repl in your project folder (the folder that lein new app number-puzzles created) with Calva: Start a Project REPL and Connect (aka Jack-In)

https://mattsenior.com/2014/02/using-clojures-core-logic-to-solve-simple-number-puzzles

Should you require more help, I would direct you to the Clojurian Slack http://clojurians.net/. Clojureverse is good for longer form discussions but Slack especially the #beginner channel would be ideal for more responsive back and forth assistance from the community :).

Additionally, it might be wise, if you have yet to getting started with Clojure through a book. Personally I would recommend Getting Clojure by Russ Olsen but you may alternative go to Clojure for the Brave and True which is free Learn to Program the World's Most Bodacious Language with Clojure for the Brave and True.

It would also be ideal perhaps just have a quick call to sync up with a Clojurian, just so that they would be able to quickly help you to overcome the initial hurdles and show how things should look like in terms of workflow etc. That being said, just reaching out in the Clojurian Slack #beginner channel would probably be good enough and if need be, someone can separately connect with you from there :slight_smile:

1 Like

Thanks for the thorough reply.

I think I can manage setting up a project if I google around a bit. I’ve done this a ton of times back in the day. I was just hoping it could be done without the overhead of a project.

My mental model is python where I would do:

python -m pip install core.logic
python
>> import core.logic

So no way to install packages permanently and globally in Clojure like pip does or npm -g?

Got there but stuff like this never ceases to put me off.

My post that you link to is very old and specific to Boot – an alternative to Leiningen at the time which has since dropped in popularity. The official Clojure CLI – Clojure - Deps and CLI Guide – is what we switched to at work in 2018, from Boot (we’d switched from Leiningen to Boot in 2015).

If you just want to quickly experiment with a library (without setting up a project), you can do this:

seanc@Sean-win-11-laptop:~/clojure$ clj -Sdeps '{:deps {org.clojure/core.logic {:mvn/version "RELEASE"}}}'
Downloading: org/clojure/core.logic/maven-metadata.xml from central
Downloading: org/clojure/core.logic/maven-metadata.xml from sonatype
Clojure 1.11.1
user=> (use 'clojure.core.logic)
WARNING: == already refers to: #'clojure.core/== in namespace: user, being replaced by: #'clojure.core.logic/==
nil
user=>

You can use "RELEASE" to get the latest version of a library without having to look it up which is fine for experimentation but if you’re building something repeatable, use a specific version.

All libraries have specific coordinates which don’t always match their namespaces. The README should always specify both the coordinates and the namespaces – you need to know both to be able to use the library. The coordinates are how you get the library on your classpath. The namespaces are how you use the code in your REPL (or editor).

And to answer your question about “global installation”: there’s no concept of “installation” at all with JVM-based languages really – it’s all about the classpath you create for each specific JVM process you start up. The artifacts (JAR files) are cached “globally” for your account – that’s handled behind the scenes by lein, boot, clj – but each run of a REPL has a distinct classpath which lists which code to use.

2 Likes

Hi! Quick addition if you didn’t use Calva when first learning back in the day like you said. Calva jack-in will give you all the options it deems relevant, as seen here. I notice that in different flavors of clj projects it will hide or show different REPL env possibilities. When in a lein project directory, i.e there’s a project.clj file, I get all these lein options, of which there’s a lot.

Then for instance if I’m working with the CLI with deps.edn I’ll get the counterpart options like deps.edn + Figwheel Main. Or, I’m working with shadow-cljs on its own, Calva doesn’t see a project.clj or deps.edn file, and the only jack-in option it gives me is shadow-cljs.

To my knowledge, I’ll go through the ones in your screencap for you or anyone else.

  1. Babashka Start essentially a generic nRepl REPL using a different tool that isn’t lein or CLI, but a clojure scripting interpreter. Amazing tool but for Calva it’s “ignore it til you need it”
  2. nbb Node.js, CLJS, and babashka. Ignore unless that sounds like you
  3. joyride Hack and modify VS Code itself. Needs the VS Code extension installed. Pretty advanced. Ignore for day-to-day usage
  4. Leiningen The correct option for most every standard clj app/lib/etc created by leiningen!
  5. Leiningen + Figwheel Main For web development, hotloading et al. Ignore if not doing webdev
  6. Leiningen + ClojureScript build-in for browser Ignore if not doing webdev
  7. Leiningen + ClojureScript built-in for node For developing node.js apps, ignore if not doing that
  8. Leiningen + Legacy Figwheel Ignore
  9. Leiningen + lein-shadow Plugin to maintain project.clj but execute in shadow-cljs. Ignore unless doing webdev AND need something really custom, maybe from migrating existing projects.

So unfortunately, it’s the trade off between having robust tools at your fingertips at the cost of not shielding newcomers from the dreaded complexity of power users’ niche use cases.

I feel your pain, I think the feeling for me is like this. All I want to do is get up and running with something new. From the very first steps of working with projects I’m confronted by a fork in the road where I need to make a major decision that will impact the whole subsequent experience. For things I haven’t dove into deeply yet, I have to trawl around for explanations, which pretty often seem above my level, fragmented, sometimes outdated. So…I’ll take a stab and make a decision without necessarily understanding comprehensively the What OR the Why (I don’t want to study the fine grain between 9 cryptic options, I want to code!), and almost instantly I’m rewarded with the next fork in the road with even more options. :face_with_spiral_eyes:

Some healthy masochism helps .. But that’s not everyone :yum: :melting_face:

4 Likes

It’s frowned upon, because it’s a bad practice for real work that need reproducibility and isolation between things.

That said, you can do it, here is how:
— Taken from a reddit comment of mine about the same:

In Clojure those are called libraries or lib(s) for short. You don’t install them; you simply declare a dependency on them. This is normally done by editing the dependency configuration for your project.

If you’re using lein for your Clojure development, then you want to edit the project.clj file, which is where libraries you depend on are configured.

If you’re using Clojure CLI clj or clojure command for your Clojure development, then you want to edit the deps.edn file, which is where libraries you depend on are configured.

Say you’re using lein, and you want to use conch from inside your project, modify the project.clj file like so:

(defproject org.example/sample "1.0.0-SNAPSHOT"
  :dependencies [[org.clojure/clojure "1.11.1"]
                 [me.raynes/conch "0.8.0"]] ;;<-- We added the library here
  ...)

If you’re using Clojure CLI, and you want to use conch from inside your project, modify the deps.edn file like so:

{:paths ["src" "target/classes"]
 :deps {org.clojure/clojure {:mvn/version "1.11.1"}
        me.raynes/conch {:mvn/version "0.8.0"}}
 ...}

If you don’t have a project, say you’re just working in a script without any directory structure and without a project-level project.clj or deps.edn file, you need to instead modify the global profiles.clj for lein or deps.edn for Clojure CLI file in the same way I showed above. Those define the default global library dependencies and are the closest you can get to a global install.

For lein you find the file at: ~/.lein/profiles.clj and this time it looks a bit different:

{:user {:dependencies [[me.raynes/conch "0.8.0"]]}}

You add the dependency in a similar way, but it goes inside the :user profile under :dependencies.

For Clojure CLI, you find the file at: ~/.clojure/deps.edn and for this one its very similar to what you did before:

{:deps {me.raynes/conch {:mvn/version "0.8.0"}}}

Just add it to the :deps key.

Now if you start a REPL or run a script anywhere those libraries will always be available.

Note that defining global dependencies like that is considered a bad practice in general, but when you’re a total beginner, it can be easier until you learn more and understand how to manage projects and local dependencies.

With deps.edn, you can also create global aliases, those would already be a bit cleaner, in that you can tack them on to bring in some dependencies ad-hoc, but it might be too advanced for now, but once you learn about aliases in tools.deps, just know you can define some global ones as well in your ~/.clojure/deps.edn config file, and they can then be used from anywhere as well.

1 Like