Skip to content

Instantly share code, notes, and snippets.

@swannodette
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save swannodette/26e4bf19a15ca28f9ba3 to your computer and use it in GitHub Desktop.
Save swannodette/26e4bf19a15ca28f9ba3 to your computer and use it in GitHub Desktop.
string length constraint
(ns strlenc.core
(:refer-clojure :exclude [==])
(:require [clojure.core.logic :as l :refer [run* fresh ==]]
[clojure.core.logic.protocols :as lp])
(:import []))
(defn -strlenc
([str len]
(reify
lp/IConstraintStep
(-step [this s]
(reify
clojure.lang.IFn
(invoke [_ s]
(let [str (lp/walk s str)]
(when (<= (count str) len)
((l/remcg this) s))))
lp/IRunnable
(-runnable? [_]
(l/ground-term? str s))))
lp/IConstraintOp
(-rator [_]
`strlenc)
(-rands [_]
[str len])
lp/IReifiableConstraint
(-reifyc [_ v r s]
`(strlenc ~str ~len))
lp/IConstraintWatchedStores
(-watched-stores [this] #{::subst}))))
(defn strlenc
[str len]
(l/cgoal (-strlenc str len)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment