Skip to content

Instantly share code, notes, and snippets.

View swannodette's full-sized avatar

David Nolen swannodette

View GitHub Profile
@austinhaas
austinhaas / findall.clj
Created May 9, 2013 04:54
core.logic findall sketch
(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
@zmaril
zmaril / core.clj
Last active December 16, 2015 14:19
Simple rejection sampling based probabilistic programming library. An approximation of church.
(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."
(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]
(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 {})))
(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]
(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]))
@swannodette
swannodette / notes.clj
Last active July 5, 2022 13:32
Generates lists of size M containing natural numbers which add up to N
;; 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)
; '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]))
@daveray
daveray / fail-goal.clj
Created November 23, 2012 07:00
core.logic goal failure check
; 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
@lynaghk
lynaghk / gist:4116442
Created November 20, 2012 06:46
Possible syntaxes for flexible unifier w/ multi-lvar constraints
;;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]