This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns pettomato.findall | |
(:refer-clojure :exclude [==]) | |
(:use | |
[clojure.core.logic.protocols]) | |
(:require | |
[clojure.core.logic :refer :all])) | |
(defn findall | |
"A goal that unifies l with a lazy sequence containing all possible | |
instantiations of v that could be generated by applying the goal g |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns hacklheber.core) | |
(defn flip | |
"A function which returns true or false randomly. Can optionally be | |
supplied a number for a bias." | |
([] (> 0.5 (rand))) | |
([p] (> p (rand)))) | |
(defn- memo-bangs | |
"If a variable is bound with a bang, then it will be memoized." |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns puzzle | |
(:refer-clojure :exclude [==]) | |
(:use [clojure.core.logic]) | |
(:require [clojure.core.logic.set :as set])) | |
(defn existso [q ps] (fresh [x] (featurec x ps) (membero x q))) | |
(defn ruleo [q p v tp tv] (existso q {p v tp tv})) | |
(defn neg-ruleo [q p v tp tv] (fresh [x] (!= x tv)) (existso q {p v tp x})) | |
(defn earliero [q p v op ov] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns logic-play.puzzle | |
(:refer-clojure :exclude [==]) | |
(:use [clojure.core.logic]) | |
(:require [clojure.tools.macro :as mu] | |
[clojure.set :as set] | |
[clojure.core.logic.fd :as fd])) | |
;; ----- | |
;; CLP(Set) Boilerplate | |
(defn index [xs] (->> xs (map-indexed (fn [i x] [x (inc i)])) (into {}))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns net.cgrand.decay | |
"Exponentially decaying lists. See http://awelonblue.wordpress.com/2013/01/24/exponential-decay-of-history-improved/ | |
for background and http://clj-me.cgrand.net/2013/02/12/decaying-lists-log-scale-for-lists/ for documentation") | |
;; PRNG, formulas straight from java.util.Random javadoc | |
(defn- seed ^long [^long n] | |
(bit-and (unchecked-multiply n 0x5DEECE66D) | |
(unchecked-dec (bit-shift-left 1 48)))) | |
(defn- next-seed ^long [^long seed] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(unifier/unify | |
{:as '{?x (?y ?z)} | |
:constraints {'?z (fnc [n] (number? n))} | |
'(?x ?a) '(["foo" 9] [1 2 3])) | |
;; previously you couldn't name '(?y ?z) | |
(unifier/unify | |
{:constraints {'?z (fnc [n] (number? n)}} | |
'((?y ?z) ?a) '(["foo" 9] [1 2 3])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; list comprehensions are often used as a poor man's Prolog | |
;; consider the following, it has only one solution | |
;; [1 1 1 1 1 1 1 1 1 1] yet we actually consider 10^10 possibilities | |
(for [a (range 1 11) | |
b (range 1 11) | |
c (range 1 11) | |
d (range 1 11) | |
e (range 1 11) | |
f (range 1 11) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 'in returns seq | |
; 'lte returns (reduce join seq) | |
; 'in? filters by pattern | |
; 'lte? tests value by lattice ordering | |
; 'is? matches by pattern (non-monotonic) | |
; path membership | |
(d/deduct (in :path [a c]) | |
(in? :edge [?a ?b]) | |
(in? :path [?b ?c])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; clojure 1.4.0, core.logic 0.8.0-beta2 | |
user=> (use 'clojure.core.logic) | |
nil | |
user=> (defn list?o | |
[v] | |
(conde | |
[(emptyo v)] | |
[(fresh [f] (firsto v f))])) | |
#'user/list?o |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;Support for simple constraints on lvars during unification was added in 76fd4d7c: | |
;; | |
;; https://github.com/clojure/core.logic/commit/76fd4d7c155161d74ad5cd8d87955148eece1fe0 | |
;; | |
;;but these constraints apply to a single lvar only. | |
;;The unifier could support more general constraints if it accepted an explicit predicate function. | |
;;For instance, given this `data-numeric?` predicate and `spec` data: | |
(defn data-numeric? [data dimension] |