<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged core.logic</title>
<link>https://ask.clojure.org/index.php/tag/core.logic</link>
<description></description>
<item>
<title>clojure.core.logic/project returning LVar instead of bound value</title>
<link>https://ask.clojure.org/index.php/10565/clojure-core-logic-project-returning-lvar-instead-bound-value</link>
<description>&lt;p&gt;I have a packing problem somewhat adjacent to the &quot;make change&quot; problem described and solved in &lt;a rel=&quot;nofollow&quot; href=&quot;https://blog.taylorwood.io/2018/05/10/clojure-logic.html&quot;&gt;this blog post by Taylor Wood&lt;/a&gt;.  Here is the code, transcribed with 1 addition:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns blah
  (:require [clojure.core.logic :refer :all :as l]
            [clojure.core.logic.fd :as fd]))

(defn productsumo [vars dens sum]
  (fresh [vhead vtail dhead dtail product run-sum]
    (conde
     [(emptyo vars) (== sum 0)]
     [(conso vhead vtail vars)
      (conso dhead dtail dens)
      (project [vhead]
        (do (println vhead) succeed))  ;;&amp;lt;--- Just a lame goal that should print out the head..
      (fd/* vhead dhead product)
      (fd/+ product run-sum sum)
      (productsumo vtail dtail run-sum)])))

(defn change [amount denoms]
  (let [dens (sort &amp;gt; denoms)
        vars (repeatedly (count dens) lvar)]
    (run* [q]
      ;; we want a map from denominations to their quantities
      (== q (zipmap dens vars))
      ;; prune problem space...
      ;; every var/quantity must be 0 &amp;lt;= n &amp;lt;= amount
      (everyg #(fd/in % (fd/interval 0 amount)) vars)
      ;; the real work
      (productsumo vars dens amount))))

user&amp;gt; (change 14 #{1 5 10})
&amp;lt;lvar:81678&amp;gt;
&amp;lt;lvar:81679&amp;gt;
&amp;lt;lvar:81680&amp;gt;
({10 0, 5 0, 1 14} {10 1, 5 0, 1 4} {10 0, 5 1, 1 9} {10 0, 5 2, 1 4})
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When run, instead of getting the projected or current value of the LVar &lt;code&gt;vhead&lt;/code&gt;, as I expect and have normally seen when I use &lt;code&gt;project&lt;/code&gt;, I get the Lvar.  Are there any ideas why this is the case?  &lt;/p&gt;
&lt;p&gt;My specific use case is a modification of productsumo, where I have a map of key-&amp;gt;weight that I need to use in liue of the coll &lt;code&gt;dens&lt;/code&gt; that is here.  My initial approach - which seems reasonable - is to use &lt;code&gt;project&lt;/code&gt; as above to unify a cost LVar based off the current value of &lt;code&gt;vhead&lt;/code&gt;, and then use that in the product computation.  Currently I keep getting back LVars instead of values.&lt;/p&gt;
&lt;p&gt;Any ideas?&lt;/p&gt;
</description>
<category>core.logic</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10565/clojure-core-logic-project-returning-lvar-instead-bound-value</guid>
<pubDate>Wed, 05 May 2021 07:18:08 +0000</pubDate>
</item>
<item>
<title>Is there a way to save a core.logic logic db to a file (non-datomic)?</title>
<link>https://ask.clojure.org/index.php/10220/is-there-a-way-to-save-a-core-logic-logic-db-to-file-non-datomic</link>
<description>&lt;p&gt;I looked around and didn't see anything obvious.  But, I am relatively new to Clojure and core.logic.   Just want see if there is some simple way to store a logic db to a file.   I want to store/retract facts in a logic db and save the entire db off occasionally for recovery purposes.  Then, later, possibly restore that db to memory.&lt;/p&gt;
&lt;p&gt;Thanks in advance for any help.&lt;/p&gt;
</description>
<category>Libs</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/10220/is-there-a-way-to-save-a-core-logic-logic-db-to-file-non-datomic</guid>
<pubDate>Sat, 20 Feb 2021 18:20:31 +0000</pubDate>
</item>
<item>
<title>Memoization in core.logic</title>
<link>https://ask.clojure.org/index.php/9905/memoization-in-core-logic</link>
<description>&lt;p&gt;I'm solving a sub-part of my problem using &lt;code&gt;core.logic&lt;/code&gt;. I'm anticipating that the declarative nature of describing the problem would lead to poor performance on large inputs.&lt;/p&gt;
&lt;p&gt;But the setup I have is that the inputs to core.logic rarely change, but I query them a lot. So naturally I think that memoization would speed up the performance.&lt;/p&gt;
&lt;p&gt;I could only memoize the results from top-level &lt;code&gt;(l/run*&lt;/code&gt; form, but again that's less efficient than memoizing at every &lt;code&gt;l/defne&lt;/code&gt; or at every call to &lt;code&gt;conde&lt;/code&gt;, because the latter can give the same speedups as dynamic-programming does.&lt;/p&gt;
&lt;p&gt;But I do not understand what kind of values (like &lt;code&gt;LVar&lt;/code&gt;s introduced by fresh) pass between the logical clauses. They are different from regular clojure values, and I have never even called one of the clauses (like &lt;code&gt;l/==&lt;/code&gt; or &lt;code&gt;l/membero&lt;/code&gt;) &quot;normally&quot;. They are always called indirectly by &lt;code&gt;l/run*&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;So, do I need to do anything special if I want to memoize such &lt;code&gt;defne&lt;/code&gt;s?&lt;/p&gt;
</description>
<category>core.logic</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9905/memoization-in-core-logic</guid>
<pubDate>Sun, 29 Nov 2020 08:07:28 +0000</pubDate>
</item>
<item>
<title>Can core.logic reason about sets?</title>
<link>https://ask.clojure.org/index.php/9889/can-core-logic-reason-about-sets</link>
<description>&lt;p&gt;&lt;code&gt;l/membero&lt;/code&gt; lets you relate list-items and lists, but I did not find an equivalent method in &lt;code&gt;core.logic&lt;/code&gt; for &lt;code&gt;contains?&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;I'm currently converting my set to a list to use with &lt;code&gt;l/membero&lt;/code&gt;, but I was just wondering -- is it possible to reason about sets in general with &lt;code&gt;core.logic&lt;/code&gt;? e.g. relating two sets and their union, and other common operations?&lt;/p&gt;
</description>
<category>core.logic</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9889/can-core-logic-reason-about-sets</guid>
<pubDate>Fri, 27 Nov 2020 19:57:39 +0000</pubDate>
</item>
<item>
<title>Can core.logic ask questions?</title>
<link>https://ask.clojure.org/index.php/9546/can-core-logic-ask-questions</link>
<description>&lt;p&gt;Can core.logic ask a question, ala  prolog:&lt;/p&gt;
&lt;p&gt;commits(Person, Crime) :-&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;crime(Crime),
format('is ~w ?', [commits(Person, Crime)]),
read(yes).
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As seen &lt;a rel=&quot;nofollow&quot; href=&quot;https://stackoverflow.com/questions/11485750/how-can-i-provide-prolog-asks-questions-to-me&quot;&gt;on stackoverflow&lt;/a&gt;.&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9546/can-core-logic-ask-questions</guid>
<pubDate>Fri, 28 Aug 2020 23:08:51 +0000</pubDate>
</item>
<item>
<title>Constrain results from core.logic query?</title>
<link>https://ask.clojure.org/index.php/9243/constrain-results-from-core-logic-query</link>
<description>&lt;p&gt;I am trying to model a graph using core.logic and would like to match all nodes that have edges to two other nodes. I have come up with the below but it returns two results instead of the desired one, as [2 3 4] and [2 4 3] are equivalent. How would I constrain the query to only return the desired result?&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(use ' clojure.core.logic.pldb)

(db-rel edge a b)

(def g
  (db
   [edge 1 2]
   [edge 2 3]
   [edge 3 4]
   [edge 2 4]))

(with-db g
  (run* [q]
    (fresh [x y z]
      (edge x y)
      (edge x z)
      (!= y z)
      (== q [x y z]))))
&lt;/code&gt;&lt;/pre&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9243/constrain-results-from-core-logic-query</guid>
<pubDate>Thu, 16 Apr 2020 11:36:31 +0000</pubDate>
</item>
</channel>
</rss>