├── .github └── workflows │ ├── test.yml │ ├── snapshot.yml │ ├── doc-build.yml │ ├── release.yml │ └── cljs_test.yml ├── run-tests.sh ├── .gitignore ├── docs ├── Using.md ├── Building.md ├── README.md ├── Including.md └── release-notes │ └── release-0.5.2.markdown ├── CONTRIBUTING.md ├── src ├── test │ ├── cljs │ │ └── clojure │ │ │ └── core │ │ │ └── unify_test_runner.cljs │ └── clojure │ │ └── clojure │ │ └── core │ │ └── unify_test.cljc └── main │ └── clojure │ └── clojure │ └── core │ └── unify.cljc ├── deps.edn ├── pom.xml ├── README.md ├── LICENSE └── epl-v10.html /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Test 2 | 3 | on: [push] 4 | 5 | jobs: 6 | call-test: 7 | uses: clojure/build.ci/.github/workflows/test.yml@master 8 | -------------------------------------------------------------------------------- /run-tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | versions="1.8 1.9 1.10 1.11 1.12" 4 | for v in $versions 5 | do 6 | time clojure -M:test:runner:$v 7 | done 8 | time clojure -M:test:cljs-runner 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .calva/repl.calva-repl 2 | .clj-kondo/.cache/ 3 | .cpcache/ 4 | .lein* 5 | .lsp/.cache/ 6 | .nrepl-port 7 | .portal/vs-code.edn 8 | cljs-test-runner-out/ 9 | lib 10 | multi-lib 11 | target 12 | -------------------------------------------------------------------------------- /.github/workflows/snapshot.yml: -------------------------------------------------------------------------------- 1 | name: Snapshot on demand 2 | 3 | on: [workflow_dispatch] 4 | 5 | jobs: 6 | call-snapshot: 7 | uses: clojure/build.ci/.github/workflows/snapshot.yml@master 8 | secrets: inherit 9 | -------------------------------------------------------------------------------- /.github/workflows/doc-build.yml: -------------------------------------------------------------------------------- 1 | name: Build API Docs 2 | 3 | on: 4 | workflow_dispatch: 5 | 6 | jobs: 7 | call-doc-build-workflow: 8 | uses: clojure/build.ci/.github/workflows/doc-build.yml@master 9 | with: 10 | project: clojure/core.unify 11 | -------------------------------------------------------------------------------- /docs/Using.md: -------------------------------------------------------------------------------- 1 | # Using core.unify 2 | 3 | *note: see the page on [including core.unify](./Including.md) before you begin this section* 4 | 5 | ## Basic usage patterns 6 | 7 | TODO 8 | 9 | ## Intermediate usage patterns 10 | 11 | TODO 12 | 13 | ## Advanced usage patterns 14 | 15 | TODO 16 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | This is a [Clojure contrib] project. 2 | 3 | Under the Clojure contrib [guidelines], this project cannot accept 4 | pull requests. All patches must be submitted via [JIRA]. 5 | 6 | See [Contributing] on the Clojure website for 7 | more information on how to contribute. 8 | 9 | [Clojure contrib]: https://clojure.org/community/contrib_libs 10 | [Contributing]: https://clojure.org/community/contributing 11 | [JIRA]: http://dev.clojure.org/jira/browse/UNIFY 12 | [guidelines]: https://clojure.org/community/contrib_howto 13 | -------------------------------------------------------------------------------- /docs/Building.md: -------------------------------------------------------------------------------- 1 | Building core.unify 2 | ===================== 3 | 4 | 1. Clone the [core.unify git repository](http://github.com/clojure/core.unify) or download its [source archive](https://github.com/clojure/core.unify/zipball/master) 5 | 6 | 2. Run `mvn package` to generate a Jar file 7 | 8 | 3. Run `mvn install` to install the Jar into your local Maven repository 9 | 10 | To test that the build succeeded try: 11 | 12 | mvn clojure:repl 13 | 14 | This will launch a Clojure REPL. Try the following to exercise core.unify: 15 | 16 | ```clojure 17 | :TODO 18 | ``` 19 | 20 | TODO 21 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release on demand 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | releaseVersion: 7 | description: "Version to release" 8 | required: true 9 | snapshotVersion: 10 | description: "Snapshot version after release" 11 | required: true 12 | 13 | jobs: 14 | call-release: 15 | uses: clojure/build.ci/.github/workflows/release.yml@master 16 | with: 17 | releaseVersion: ${{ github.event.inputs.releaseVersion }} 18 | snapshotVersion: ${{ github.event.inputs.snapshotVersion }} 19 | secrets: inherit -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | core.unify 2 | ========== 3 | 4 | * Overview (this page) 5 | * [Including core.unify in your projects](./Including.md) 6 | * [Example usages of core.unify](./Using.md) 7 | * [Building core.unify](./Building.md) 8 | 9 | ## The problem 10 | 11 | TODO 12 | 13 | ## Overview 14 | 15 | core.unify is a Clojure contrib library providing the following features: 16 | 17 | * TODO 18 | * TODO 19 | * TODO 20 | 21 | ## Places 22 | 23 | * [Source code](https://github.com/clojure/core.unify) 24 | * [Ticket system](http://dev.clojure.org/jira/browse/UNIFY) 25 | * [Examples and documentation](http://github.com/clojure/core.unify/wiki) -------------------------------------------------------------------------------- /src/test/cljs/clojure/core/unify_test_runner.cljs: -------------------------------------------------------------------------------- 1 | ; Copyright (c) Rich Hickey. All rights reserved. 2 | ; The use and distribution terms for this software are covered by the 3 | ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 4 | ; which can be found in the file epl-v10.html at the root of this distribution. 5 | ; By using this software in any fashion, you are agreeing to be bound by 6 | ; the terms of this license. 7 | ; You must not remove this notice, or any other, from this software. 8 | 9 | (ns ^{:doc "A unification library for Clojure." 10 | :author "Michael Fogus"} 11 | clojure.core.unify-test-runner 12 | (:require [clojure.core.unify-test] 13 | [clojure.test :refer [run-tests]])) 14 | 15 | (run-tests 16 | 'clojure.core.unify-test) -------------------------------------------------------------------------------- /docs/Including.md: -------------------------------------------------------------------------------- 1 | Including core.unify in your projects 2 | ===================================== 3 | 4 | The core.unify releases and snapshots are stored in the following repositories: 5 | 6 | * Release versions stored at [Maven Central](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22core.unify%22) 7 | * Snapshot versions stored at [Sonatype](https://oss.sonatype.org/index.html#nexus-search;gav~org.clojure~core.unify~~~) (url at ) 8 | 9 | ## Leiningen 10 | ` 11 | You can use core.unify in your [Leiningen](https://github.com/technomancy/leiningen) projects with the following `:dependencies` directive in your `project.clj` file: 12 | 13 | [org.clojure/core.unify "0.5.6"] 14 | 15 | ## Maven 16 | 17 | For Maven-driven projects, use the following slice of XML in your `pom.xml`'s `` section: 18 | 19 | 20 | org.clojure 21 | core.unify 22 | 0.5.6 23 | 24 | 25 | Enjoy! 26 | -------------------------------------------------------------------------------- /deps.edn: -------------------------------------------------------------------------------- 1 | {:paths ["src/main/clojure"] 2 | :aliases {:test 3 | {:extra-paths ["src/test/clojure"] 4 | :extra-deps {org.clojure/test.check {:mvn/version "1.1.1"}}} 5 | :1.8 {:override-deps {org.clojure/clojure {:mvn/version "1.8.0"}}} 6 | :1.9 {:override-deps {org.clojure/clojure {:mvn/version "1.9.0"}}} 7 | :1.10 {:override-deps {org.clojure/clojure {:mvn/version "1.10.3"}}} 8 | :1.11 {:override-deps {org.clojure/clojure {:mvn/version "1.11.4"}}} 9 | :1.12 {:override-deps {org.clojure/clojure {:mvn/version "1.12.0"}}} 10 | ;; suitable for Clojure 1.8 or later: 11 | :runner 12 | {:extra-deps {io.github.cognitect-labs/test-runner 13 | {:git/tag "v0.5.1" :git/sha "dfb30dd"}} 14 | :main-opts ["-m" "cognitect.test-runner" 15 | "-d" "src/test/clojure"]} 16 | :cljs-runner 17 | {:extra-deps {olical/cljs-test-runner {:mvn/version "3.8.1"}} 18 | :main-opts ["-m" "cljs-test-runner.main" 19 | "-d" "src/test/clojure"]}}} 20 | -------------------------------------------------------------------------------- /.github/workflows/cljs_test.yml: -------------------------------------------------------------------------------- 1 | name: ClojureScript Test 2 | on: [push] 3 | 4 | jobs: 5 | cljs-test: 6 | name: ClojureScript Test 7 | runs-on: ubuntu-latest 8 | steps: 9 | - uses: actions/checkout@v4 10 | 11 | - uses: actions/setup-java@v4 12 | with: 13 | distribution: 'temurin' 14 | java-version: '21' 15 | 16 | - uses: DeLaGuardo/setup-clojure@12.5 17 | with: 18 | tools-deps: '1.12.0.1479' 19 | 20 | - name: Cache maven 21 | uses: actions/cache@v4 22 | env: 23 | cache-name: cache-maven 24 | with: 25 | path: ~/.m2 26 | key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/deps.edn') }} 27 | restore-keys: | 28 | ${{ runner.os }}-${{ env.cache-name }}- 29 | 30 | - name: Cache gitlibs 31 | uses: actions/cache@v4 32 | env: 33 | cache-name: cache-gitlibs 34 | with: 35 | path: ~/.gitlibs 36 | key: ${{ runner.os }}-${{ env.cache-name }}-${{ hashFiles('**/deps.edn') }} 37 | restore-keys: | 38 | ${{ runner.os }}-${{ env.cache-name }}- 39 | 40 | - name: Run tests 41 | run: clojure -M:test:cljs-runner 42 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | core.unify 5 | 0.7.1-SNAPSHOT 6 | core.unify 7 | A unification library for Clojure 8 | jar 9 | 10 | 11 | 12 | Eclipse Public License 1.0 13 | https://opensource.org/license/epl-1-0/ 14 | repo 15 | 16 | 17 | 18 | 19 | org.clojure 20 | pom.contrib 21 | 1.3.0 22 | 23 | 24 | 25 | 26 | fogus 27 | Fogus 28 | https://fogus.me 29 | 30 | 31 | 32 | 33 | scm:git:git://github.com/clojure/core.unify.git 34 | https://github.com/clojure/core.unify 35 | HEAD 36 | 37 | 38 | 39 | 40 | sonatype-oss-snapshots 41 | https://oss.sonatype.org/content/repositories/snapshots 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /docs/release-notes/release-0.5.2.markdown: -------------------------------------------------------------------------------- 1 | core.unify v0.5.2 Release Notes 2 | =============================== 3 | 4 | core.unify is a Clojure contrib library providing the following features: 5 | 6 | * Factory functions for constructing unification binding, subst, and unification functions, with or without occurs checking 7 | 8 | * Packaged functions for unification binding, subst, and unification functions, with or without occurs checking, recognizing variables tagged as symbols prefixed with `?` characters 9 | 10 | core.unify is based on a library named Unifycle, found at http://github.com/fogus/unifycle that has been deprecated. 11 | 12 | 13 | Absorb 14 | ------ 15 | 16 | You can use core.unify in your [Leiningen](https://github.com/technomancy/leiningen) and [Cake](https://github.com/flatland/cake) projects with the following `:dependencies` directive in your `project.clj` file: 17 | 18 | [org.clojure/core.unify "0.5.2"] 19 | 20 | For Maven-driven projects, use the following slice of XML in your `pom.xml`'s `` section: 21 | 22 | 23 | org.clojure 24 | core.unify 25 | 0.5.2 26 | 27 | 28 | Enjoy! 29 | 30 | Places 31 | ------ 32 | 33 | * [Source code](https://github.com/clojure/core.unify) 34 | * [Ticket system](http://dev.clojure.org/jira/browse/UNIFY) 35 | * [Examples and documentation](http://fogus.me/fun/unifycle) -- in progress 36 | 37 | 38 | Changes from version 0.5.1 39 | -------------------------- 40 | 41 | * Removed all reflection warnings in the code. (thanks goes to André Thieme for pointing them out) 42 | * More tests around common unification edge cases. 43 | 44 | Plans 45 | ----- 46 | 47 | The following capabilities are under design, development, or consideration for future versions of core.unify: 48 | 49 | * High-performant unification based on unrolling recursive backtracking into polymorphic calls 50 | * Iterative unification option 51 | * Boolean unification 52 | * Implicit variable recognition option(s) 53 | * More examples 54 | * More documentation 55 | 56 | More planning is needed around capabilities not listed nor thought of. 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | clojure.core.unify 2 | ======================================== 3 | 4 | core.unify is a Clojure & ClojureScript contrib library providing the following features: 5 | 6 | * Factory functions for constructing unification binding, subst, and unification functions, with or without occurs checking 7 | 8 | * Packaged functions for unification binding, subst, and unification functions, with or without occurs checking, recognizing variables tagged as symbols prefixed with `?` characters 9 | 10 | core.unify is based on a library named Unifycle, found at https://github.com/fogus/unifycle that has been deprecated. 11 | 12 | *[more information](https://fogus.me/fun/unifycle)* 13 | 14 | Differences from core.logic 15 | =========================== 16 | 17 | core.unify provides a la carte unification facilities that are not deeply tied into the operation of a logic engine. While core.logic does provide a similar simple unifier interface with support for specifying fine-grained constraints, if you have no need for a logic programming system, core.unify may be a better fit. 18 | 19 | Releases and Dependency Information 20 | ======================================== 21 | 22 | Latest stable release: 0.7.0 23 | 24 | * [All Released Versions](https://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22org.clojure%22%20AND%20a%3A%22core.unify%22) 25 | * [Development Snapshot Versions](https://oss.sonatype.org/index.html#nexus-search;gav~org.clojure~core.unify~~~) 26 | 27 | [CLI/`deps.edn`](https://clojure.org/reference/deps_and_cli) dependency information: 28 | ```clojure 29 | org.clojure/core.unify {:mvn/version "0.7.0"} 30 | ``` 31 | 32 | [Leiningen](https://github.com/technomancy/leiningen) dependency information: 33 | 34 | [org.clojure/core.unify "0.7.0"] 35 | 36 | [Maven](https://maven.apache.org/) dependency information: 37 | 38 | 39 | org.clojure 40 | core.unify 41 | 0.7.0 42 | 43 | 44 | 45 | 46 | Example Usage 47 | ======================================== 48 | 49 | ```clojure 50 | (use 'clojure.core.unify) 51 | 52 | (unifier '((?a * ?x ** 2) + (?b * ?x) + ?c) 53 | '(?z + (4 * 5) + 3)) 54 | 55 | ;=> ((?a * 5 ** 2) + (4 * 5) + 3) 56 | ``` 57 | 58 | Refer to docstrings in the `clojure.core.unify` namespace. 59 | 60 | 61 | 62 | Developer Information 63 | ======================================== 64 | 65 | * [GitHub project](https://github.com/clojure/core.unify) 66 | * [Bug Tracker](https://clojure.atlassian.net/browse/UNIFY) 67 | * [Continuous Integration](https://github.com/clojure/core.unify/actions/workflows/test.yml) 68 | 69 | 70 | Change Log 71 | ==================== 72 | 73 | * Release 0.7.0 on 2024.11.06 74 | - Add ClojureScript support 75 | - Drop Clojure 1.6 and 1.7 support 76 | * Release 0.6.0 on 2024.02.19 77 | - Update parent pom version 78 | * Release 0.5.7 on 2016.07.25 79 | - Fixed macro that produced invalid fn names 80 | * Release 0.5.6 on 2013.03.07 81 | - Removed nil bindings 82 | * Release 0.5.5 on 2013.01.25 83 | - Exposed `lvar?` function 84 | - Added `extract-lvars` function 85 | - Exposed `flatten-bindings` function 86 | * Release 0.5.4 on 2013.01.24 87 | - Added 3-arg version of `unify` and `unify-` allowing an 88 | additional environment map. 89 | * Release 0.5.3 on 2012.05.25 90 | - Added `variable?` function 91 | - Unification of seqs of differing lengths fails (as expected) 92 | * Release 0.5.2 on 2012.01.08 93 | - Removed reflection warnings 94 | * Release 0.5.1 on 2011.10.11 95 | - Rolled in basis of Unifycle 96 | 97 | 98 | Copyright and License 99 | ======================================== 100 | 101 | Copyright (c) Rich Hickey and Michael Fogus. All rights reserved. The use and distribution terms for this software are covered by the Eclipse Public License 1.0 (https://opensource.org/license/epl-1-0/) which can be found in the file epl-v10.html at the root of this distribution. By using this software in any fashion, you are agreeing to be bound bythe terms of this license. You must not remove this notice, or any other, from this software. 102 | -------------------------------------------------------------------------------- /src/test/clojure/clojure/core/unify_test.cljc: -------------------------------------------------------------------------------- 1 | ; Copyright (c) Rich Hickey. All rights reserved. 2 | ; The use and distribution terms for this software are covered by the 3 | ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 4 | ; which can be found in the file epl-v10.html at the root of this distribution. 5 | ; By using this software in any fashion, you are agreeing to be bound by 6 | ; the terms of this license. 7 | ; You must not remove this notice, or any other, from this software. 8 | 9 | (ns ^{:doc "A unification library for Clojure." 10 | :author "Michael Fogus"} 11 | clojure.core.unify-test 12 | (:require [clojure.core.unify :refer [unify]] 13 | [clojure.test :refer [deftest is testing]])) 14 | 15 | #?(:clj (println "\nTesting with Clojure" (clojure-version)) 16 | :cljs (println "\nTesting with ClojureScript" *clojurescript-version*)) 17 | 18 | #?(:cljs 19 | (defn uppercase? [s] 20 | (let [c (.charCodeAt s 0)] 21 | (and (>= c 65) 22 | (<= c 90))))) 23 | 24 | (def CAPS #(and (symbol? %) (#?(:clj Character/isUpperCase :cljs uppercase?) (first (name %))))) 25 | 26 | (deftest test-garner-unifiers 27 | (is (= {} (#'clojure.core.unify/garner-unifiers '(a b) '(a b)))) 28 | (is (= {} (#'clojure.core.unify/garner-unifiers '(?a ?b) '(?a ?b)))) 29 | (is (nil? (#'clojure.core.unify/garner-unifiers '(?a ?a) 'a))) 30 | (is (= '{?x 1} (#'clojure.core.unify/garner-unifiers '(f ?x ?y) '(f 1 ?y)))) 31 | (is (= '{?b 2, ?a 1} (#'clojure.core.unify/garner-unifiers '(?a ?b) '(1 2)))) 32 | (is (= '{?a (q)} (#'clojure.core.unify/garner-unifiers '(?a ?b) '((q) ?b)))) 33 | (is (= '{?x y} (#'clojure.core.unify/garner-unifiers '?x 'y))) 34 | (is (= '{?x ?y} (#'clojure.core.unify/garner-unifiers '(?x ?x) '(?y ?y)))) 35 | (is (= '{?x ?y} (#'clojure.core.unify/garner-unifiers '(?x ?x ?x) '(?y ?y ?y)))) 36 | (is (= '{?x ?y} (#'clojure.core.unify/garner-unifiers '(?x ?y) '(?y ?x)))) 37 | (is (nil? (#'clojure.core.unify/garner-unifiers '(f ?x ?y) '(g ?x ?y)))) ; clash 38 | (is (nil? (#'clojure.core.unify/garner-unifiers '(?a ?a) '(1 2)))) ; clash 39 | (is (nil? (#'clojure.core.unify/garner-unifiers '(f ?a) '(g 42)))) ; clash 40 | (is (nil? (#'clojure.core.unify/garner-unifiers '(?a ?a) 'a))) ; clash 41 | (is (= '{?y (h), ?x (h)} (#'clojure.core.unify/garner-unifiers '(f ?x (h)) '(f (h) ?y)))) 42 | (is (thrown? #?(:clj IllegalStateException :cljs js/Error) 43 | (#'clojure.core.unify/garner-unifiers '(f (g ?x) ?y) '(f ?y ?x)))) ; cycle 44 | (is (thrown? #?(:clj IllegalStateException :cljs js/Error) 45 | (#'clojure.core.unify/garner-unifiers '?x '(f ?x)))) ; cycle 46 | (is (= '{?y (g ?x)} (#'clojure.core.unify/garner-unifiers '(f (g ?x) ?y) '(f ?y (g ?x))))) 47 | (is (= '{?z (g ?x), ?y (g ?x)} (#'clojure.core.unify/garner-unifiers '(f (g ?x) ?y) '(f ?y ?z)))) 48 | (is (= '{?a a} (#'clojure.core.unify/garner-unifiers '?a 'a))) 49 | (is (= '{?y :bar, ?x :foo} (#'clojure.core.unify/garner-unifiers '{?x 42 ?y 108} '{:foo 42 :bar 108}))) 50 | (is (= '{B 2, A 1} (#'clojure.core.unify/garner-unifiers CAPS '(A B) '(1 2)))) 51 | (is (= '{Bar 2, Foo 1} (#'clojure.core.unify/garner-unifiers CAPS '(Foo Bar) '(1 2)))) 52 | (is (= '{?y a, ?x ?y} (#'clojure.core.unify/garner-unifiers '(?x ?y a) '(?y ?x ?x))))) 53 | 54 | (deftest test-range-variables 55 | (is (= '{?x 1 ?y (2 3)} (#'clojure.core.unify/garner-unifiers '(?x & ?y) [1 2 3]))) 56 | (is (= '{?x 1 ?y 2 ?z (3)} (#'clojure.core.unify/garner-unifiers '(?x ?y & ?z) [1 2 3]))) 57 | (is (= '{?x 1} (#'clojure.core.unify/garner-unifiers '(?x & ?y) [1])))) 58 | 59 | (deftest test-ignore-variables 60 | (is (= '{?x 1 ?y 3} (#'clojure.core.unify/garner-unifiers '(?x _ ?y) [1 2 3]))) 61 | (is (= '{} (#'clojure.core.unify/garner-unifiers '(_ _ _) [1 2 3]))) 62 | (is (= '{?x 1} (#'clojure.core.unify/garner-unifiers '(?x & _) [1 2 3]))) 63 | (is (= '{?y (2 3)} (#'clojure.core.unify/garner-unifiers '(_ & ?y) [1 2 3])))) 64 | 65 | (deftest test-flatten-bindings 66 | (is (= '{?y a, ?x a} (#'clojure.core.unify/flatten-bindings '{?y a, ?x ?y})))) 67 | 68 | 69 | (deftest test-unifier* 70 | (is (= '(a a a) (#'clojure.core.unify/unifier* CAPS '(X Y a) '(Y X X) '{Y a, X a}))) 71 | (is (= '(a a a) (#'clojure.core.unify/unifier* CAPS '(X Y a) '(Y X X)))) 72 | (is (= '((?a * 5 ** 2) + (4 * 5) + 3) (#'clojure.core.unify/unifier* '((?a * ?x ** 2) + (?b * ?x) + ?c) '(?z + (4 * 5) + 3))))) 73 | 74 | (deftest test-unifier 75 | (is (= '((?a * 5 ** 2) + (4 * 5) + 3) (#'clojure.core.unify/unifier '((?a * ?x ** 2) + (?b * ?x) + ?c) '(?z + (4 * 5) + 3)))) 76 | (is (= 42 (#'clojure.core.unify/unifier '?x 42))) 77 | (is (= '{a 2} (#'clojure.core.unify/unifier (hash-map 'a '?x) (hash-map 'a 2)))) 78 | (is (= #{2 3 4} (#'clojure.core.unify/unifier #{'?a '?b '?c} #{2 3 4})))) 79 | 80 | (deftest test-unifier-no-occurs 81 | (is (= '((?a * 5 ** 2) + (4 * 5) + 3) (#'clojure.core.unify/unifier- '((?a * ?x ** 2) + (?b * ?x) + ?c) '(?z + (4 * 5) + 3)))) 82 | (is (= 42 (#'clojure.core.unify/unifier- '?x 42))) 83 | (is (= '{a 2} (#'clojure.core.unify/unifier- (hash-map 'a '?x) (hash-map 'a 2)))) 84 | (is (= #{2 3 4} (#'clojure.core.unify/unifier- #{'?a '?b '?c} #{2 3 4})))) 85 | 86 | (deftest test-mk-unifier 87 | (let [u (#'clojure.core.unify/make-occurs-unifier-fn #(and (symbol? %) 88 | (re-matches #"^\?.*" (name %))))] 89 | (is (= '((?a * 5 ** 2) + (4 * 5) + 3) (u '((?a * ?x ** 2) + (?b * ?x) + ?c) '(?z + (4 * 5) + 3)))))) 90 | 91 | (deftest test-subs 92 | (is (= [true false] 93 | (clojure.core.unify/subst '[?a ?b] {'?a true '?b false})))) 94 | 95 | (deftest test-aux 96 | (is (#'clojure.core.unify/composite? "foo"))) 97 | 98 | (deftest test-norvig-bug-cases 99 | (testing "that the unification of the problem cases in Norvig's paper 100 | 'Correcting A Widespread Error in Unification Algorithms'. An 101 | incorrect unifier will return nil or loop forever." 102 | (is (= '{?x ?y} (unify '(p ?x ?y) '(p ?y ?x)))) 103 | (is (= '{?y a, ?x ?y} (unify '(p ?x ?y a) '(p ?y ?x ?x)))) 104 | ;; higher-order predicates! 105 | (is (= '{?x ?y, ?z (p ?x ?y)} (unify '(q (p ?x ?y) (p ?y ?x)) '(q ?z ?z)))))) 106 | 107 | (deftest regressions 108 | (testing "That seqs of different lengths do not unify per UNIFY-4" 109 | (is (= {} (unify '[1 ?x] '[1]))) 110 | (is (= {} (unify '[1 ?x ?y ?z] '[1]))))) 111 | 112 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Eclipse Public License - v 1.0 2 | 3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 4 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 5 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 6 | 7 | 1. DEFINITIONS 8 | 9 | "Contribution" means: 10 | 11 | a) in the case of the initial Contributor, the initial code and documentation 12 | distributed under this Agreement, and 13 | b) in the case of each subsequent Contributor: 14 | i) changes to the Program, and 15 | ii) additions to the Program; 16 | 17 | where such changes and/or additions to the Program originate from and are 18 | distributed by that particular Contributor. A Contribution 'originates' 19 | from a Contributor if it was added to the Program by such Contributor 20 | itself or anyone acting on such Contributor's behalf. Contributions do not 21 | include additions to the Program which: (i) are separate modules of 22 | software distributed in conjunction with the Program under their own 23 | license agreement, and (ii) are not derivative works of the Program. 24 | 25 | "Contributor" means any person or entity that distributes the Program. 26 | 27 | "Licensed Patents" mean patent claims licensable by a Contributor which are 28 | necessarily infringed by the use or sale of its Contribution alone or when 29 | combined with the Program. 30 | 31 | "Program" means the Contributions distributed in accordance with this 32 | Agreement. 33 | 34 | "Recipient" means anyone who receives the Program under this Agreement, 35 | including all Contributors. 36 | 37 | 2. GRANT OF RIGHTS 38 | a) Subject to the terms of this Agreement, each Contributor hereby grants 39 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 40 | reproduce, prepare derivative works of, publicly display, publicly 41 | perform, distribute and sublicense the Contribution of such Contributor, 42 | if any, and such derivative works, in source code and object code form. 43 | b) Subject to the terms of this Agreement, each Contributor hereby grants 44 | Recipient a non-exclusive, worldwide, royalty-free patent license under 45 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 46 | transfer the Contribution of such Contributor, if any, in source code and 47 | object code form. This patent license shall apply to the combination of 48 | the Contribution and the Program if, at the time the Contribution is 49 | added by the Contributor, such addition of the Contribution causes such 50 | combination to be covered by the Licensed Patents. The patent license 51 | shall not apply to any other combinations which include the Contribution. 52 | No hardware per se is licensed hereunder. 53 | c) Recipient understands that although each Contributor grants the licenses 54 | to its Contributions set forth herein, no assurances are provided by any 55 | Contributor that the Program does not infringe the patent or other 56 | intellectual property rights of any other entity. Each Contributor 57 | disclaims any liability to Recipient for claims brought by any other 58 | entity based on infringement of intellectual property rights or 59 | otherwise. As a condition to exercising the rights and licenses granted 60 | hereunder, each Recipient hereby assumes sole responsibility to secure 61 | any other intellectual property rights needed, if any. For example, if a 62 | third party patent license is required to allow Recipient to distribute 63 | the Program, it is Recipient's responsibility to acquire that license 64 | before distributing the Program. 65 | d) Each Contributor represents that to its knowledge it has sufficient 66 | copyright rights in its Contribution, if any, to grant the copyright 67 | license set forth in this Agreement. 68 | 69 | 3. REQUIREMENTS 70 | 71 | A Contributor may choose to distribute the Program in object code form under 72 | its own license agreement, provided that: 73 | 74 | a) it complies with the terms and conditions of this Agreement; and 75 | b) its license agreement: 76 | i) effectively disclaims on behalf of all Contributors all warranties 77 | and conditions, express and implied, including warranties or 78 | conditions of title and non-infringement, and implied warranties or 79 | conditions of merchantability and fitness for a particular purpose; 80 | ii) effectively excludes on behalf of all Contributors all liability for 81 | damages, including direct, indirect, special, incidental and 82 | consequential damages, such as lost profits; 83 | iii) states that any provisions which differ from this Agreement are 84 | offered by that Contributor alone and not by any other party; and 85 | iv) states that source code for the Program is available from such 86 | Contributor, and informs licensees how to obtain it in a reasonable 87 | manner on or through a medium customarily used for software exchange. 88 | 89 | When the Program is made available in source code form: 90 | 91 | a) it must be made available under this Agreement; and 92 | b) a copy of this Agreement must be included with each copy of the Program. 93 | Contributors may not remove or alter any copyright notices contained 94 | within the Program. 95 | 96 | Each Contributor must identify itself as the originator of its Contribution, 97 | if 98 | any, in a manner that reasonably allows subsequent Recipients to identify the 99 | originator of the Contribution. 100 | 101 | 4. COMMERCIAL DISTRIBUTION 102 | 103 | Commercial distributors of software may accept certain responsibilities with 104 | respect to end users, business partners and the like. While this license is 105 | intended to facilitate the commercial use of the Program, the Contributor who 106 | includes the Program in a commercial product offering should do so in a manner 107 | which does not create potential liability for other Contributors. Therefore, 108 | if a Contributor includes the Program in a commercial product offering, such 109 | Contributor ("Commercial Contributor") hereby agrees to defend and indemnify 110 | every other Contributor ("Indemnified Contributor") against any losses, 111 | damages and costs (collectively "Losses") arising from claims, lawsuits and 112 | other legal actions brought by a third party against the Indemnified 113 | Contributor to the extent caused by the acts or omissions of such Commercial 114 | Contributor in connection with its distribution of the Program in a commercial 115 | product offering. The obligations in this section do not apply to any claims 116 | or Losses relating to any actual or alleged intellectual property 117 | infringement. In order to qualify, an Indemnified Contributor must: 118 | a) promptly notify the Commercial Contributor in writing of such claim, and 119 | b) allow the Commercial Contributor to control, and cooperate with the 120 | Commercial Contributor in, the defense and any related settlement 121 | negotiations. The Indemnified Contributor may participate in any such claim at 122 | its own expense. 123 | 124 | For example, a Contributor might include the Program in a commercial product 125 | offering, Product X. That Contributor is then a Commercial Contributor. If 126 | that Commercial Contributor then makes performance claims, or offers 127 | warranties related to Product X, those performance claims and warranties are 128 | such Commercial Contributor's responsibility alone. Under this section, the 129 | Commercial Contributor would have to defend claims against the other 130 | Contributors related to those performance claims and warranties, and if a 131 | court requires any other Contributor to pay any damages as a result, the 132 | Commercial Contributor must pay those damages. 133 | 134 | 5. NO WARRANTY 135 | 136 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN 137 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR 138 | IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE, 139 | NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each 140 | Recipient is solely responsible for determining the appropriateness of using 141 | and distributing the Program and assumes all risks associated with its 142 | exercise of rights under this Agreement , including but not limited to the 143 | risks and costs of program errors, compliance with applicable laws, damage to 144 | or loss of data, programs or equipment, and unavailability or interruption of 145 | operations. 146 | 147 | 6. DISCLAIMER OF LIABILITY 148 | 149 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 150 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 151 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 152 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 153 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 154 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 155 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 156 | OF SUCH DAMAGES. 157 | 158 | 7. GENERAL 159 | 160 | If any provision of this Agreement is invalid or unenforceable under 161 | applicable law, it shall not affect the validity or enforceability of the 162 | remainder of the terms of this Agreement, and without further action by the 163 | parties hereto, such provision shall be reformed to the minimum extent 164 | necessary to make such provision valid and enforceable. 165 | 166 | If Recipient institutes patent litigation against any entity (including a 167 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 168 | (excluding combinations of the Program with other software or hardware) 169 | infringes such Recipient's patent(s), then such Recipient's rights granted 170 | under Section 2(b) shall terminate as of the date such litigation is filed. 171 | 172 | All Recipient's rights under this Agreement shall terminate if it fails to 173 | comply with any of the material terms or conditions of this Agreement and does 174 | not cure such failure in a reasonable period of time after becoming aware of 175 | such noncompliance. If all Recipient's rights under this Agreement terminate, 176 | Recipient agrees to cease use and distribution of the Program as soon as 177 | reasonably practicable. However, Recipient's obligations under this Agreement 178 | and any licenses granted by Recipient relating to the Program shall continue 179 | and survive. 180 | 181 | Everyone is permitted to copy and distribute copies of this Agreement, but in 182 | order to avoid inconsistency the Agreement is copyrighted and may only be 183 | modified in the following manner. The Agreement Steward reserves the right to 184 | publish new versions (including revisions) of this Agreement from time to 185 | time. No one other than the Agreement Steward has the right to modify this 186 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 187 | Eclipse Foundation may assign the responsibility to serve as the Agreement 188 | Steward to a suitable separate entity. Each new version of the Agreement will 189 | be given a distinguishing version number. The Program (including 190 | Contributions) may always be distributed subject to the version of the 191 | Agreement under which it was received. In addition, after a new version of the 192 | Agreement is published, Contributor may elect to distribute the Program 193 | (including its Contributions) under the new version. Except as expressly 194 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 195 | licenses to the intellectual property of any Contributor under this Agreement, 196 | whether expressly, by implication, estoppel or otherwise. All rights in the 197 | Program not expressly granted under this Agreement are reserved. 198 | 199 | This Agreement is governed by the laws of the State of New York and the 200 | intellectual property laws of the United States of America. No party to this 201 | Agreement will bring a legal action under this Agreement more than one year 202 | after the cause of action arose. Each party waives its rights to a jury trial in 203 | any resulting litigation. 204 | 205 | 206 | -------------------------------------------------------------------------------- /src/main/clojure/clojure/core/unify.cljc: -------------------------------------------------------------------------------- 1 | ; Copyright (c) Rich Hickey. All rights reserved. 2 | ; The use and distribution terms for this software are covered by the 3 | ; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 4 | ; which can be found in the file epl-v10.html at the root of this distribution. 5 | ; By using this software in any fashion, you are agreeing to be bound by 6 | ; the terms of this license. 7 | ; You must not remove this notice, or any other, from this software. 8 | 9 | (ns ^{:doc "A unification library for Clojure." 10 | :author "Michael Fogus"} 11 | clojure.core.unify 12 | #?(:cljs (:require-macros [clojure.core.unify :refer [create-var-unification-fn]])) 13 | (:require [clojure.zip :as zip] 14 | [clojure.walk :as walk])) 15 | 16 | (defn ignore-variable? [sym] (= '_ sym)) 17 | 18 | ;; ### Utilities 19 | 20 | (def lvar? #(or (ignore-variable? %) 21 | (and (symbol? %) (re-matches #"^\?.*" (name %))))) 22 | 23 | (defn extract-lvars 24 | "Takes a datastructure and returns a distinct set of the logical 25 | variables found within." 26 | ([form] 27 | (extract-lvars lvar? form)) 28 | ([lv-fn form] 29 | (set 30 | (walk/walk #(when (lv-fn %) %) 31 | #(keep identity %) 32 | form)))) 33 | 34 | (defn- composite? 35 | "Taken from the old `contrib.core/seqable?`. Since the meaning of 'seqable' is 36 | questionable, I will work on phasing it out and using a more meaningful 37 | predicate. At the moment, the only meaning of `composite?` is: 38 | Returns true if `(seq x)` will succeed, false otherwise." 39 | [x] 40 | #?(:clj (or (coll? x) 41 | (nil? x) 42 | (instance? Iterable x) 43 | (-> x class .isArray) 44 | (string? x) 45 | (instance? java.util.Map x)) 46 | :cljs (seqable? x))) 47 | 48 | (declare garner-unifiers) 49 | 50 | (defn- occurs? 51 | "Does v occur anywhere inside expr?" 52 | [variable? v expr binds] 53 | (loop [z (zip/zipper composite? seq #(do % %2) [expr])] 54 | (let [current (zip/node z)] 55 | (cond 56 | (zip/end? z) false 57 | (= v current) true 58 | (and (variable? current) 59 | (find binds current)) 60 | (recur (zip/next (zip/insert-right z (binds current)))) 61 | :else (recur (zip/next z)))))) 62 | 63 | (defn- bind-phase 64 | [binds variable expr] 65 | (if (or (nil? expr) 66 | (ignore-variable? variable)) 67 | binds 68 | (assoc binds variable expr))) 69 | 70 | (defn- determine-occursness 71 | [want-occurs? variable? v expr binds] 72 | (if want-occurs? 73 | `(if (occurs? ~variable? ~v ~expr ~binds) 74 | #?(:clj (throw (IllegalStateException. (str "Cycle found in the path " ~expr))) 75 | :cljs (throw (js/Error. (str "Cycle found in the path " ~expr)))) 76 | (bind-phase ~binds ~v ~expr)) 77 | `(bind-phase ~binds ~v ~expr))) 78 | 79 | #?(:clj 80 | (defmacro create-var-unification-fn 81 | [want-occurs?] 82 | (let [varp (gensym) 83 | v (gensym) 84 | expr (gensym) 85 | binds (gensym)] 86 | `(fn ~'var-unify 87 | [~varp ~v ~expr ~binds] 88 | (if-let [vb# (~binds ~v)] 89 | (garner-unifiers ~varp vb# ~expr ~binds) 90 | (if-let [vexpr# (and (~varp ~expr) (~binds ~expr))] 91 | (garner-unifiers ~varp ~v vexpr# ~binds) 92 | ~(determine-occursness want-occurs? varp v expr binds))))))) 93 | 94 | (def ^{:doc "Unify the variable v with expr. Uses the bindings supplied and possibly returns an extended bindings map." 95 | :private true} 96 | unify-variable (create-var-unification-fn true)) 97 | 98 | (def ^{:doc "Unify the variable v with expr. Uses the bindings supplied and possibly returns an extended bindings map." 99 | :private true} 100 | unify-variable- (create-var-unification-fn false)) 101 | 102 | (defn wildcard? [form] 103 | (and (composite? form) 104 | (#{'&} (first form)))) 105 | 106 | (defn- garner-unifiers 107 | ([x y] (garner-unifiers unify-variable lvar? x y {})) 108 | ([variable? x y] (garner-unifiers unify-variable variable? x y {})) 109 | ([variable? x y binds] (garner-unifiers unify-variable variable? x y binds)) 110 | ([uv-fn variable? x y binds] 111 | (cond 112 | (not binds) nil 113 | (= x y) binds 114 | (variable? x) (uv-fn variable? x y binds) 115 | (variable? y) (uv-fn variable? y x binds) 116 | (wildcard? x) (uv-fn variable? (second x) (seq y) binds) 117 | (wildcard? y) (uv-fn variable? (second y) (seq x) binds) 118 | (every? composite? [x y]) (garner-unifiers uv-fn 119 | variable? 120 | (rest x) 121 | (rest y) 122 | (garner-unifiers uv-fn 123 | variable? 124 | (first x) 125 | (first y) 126 | binds))))) 127 | 128 | (defn flatten-bindings 129 | "Flattens recursive bindings in the given map to the same ground (if possible)." 130 | ([binds] (flatten-bindings lvar? binds)) 131 | ([variable? binds] 132 | (into {} 133 | (remove (comp nil? second) 134 | (map (fn [[k v]] 135 | [k (loop [v v] 136 | (if (variable? v) 137 | (recur (binds v)) 138 | v))]) 139 | binds))))) 140 | 141 | (defn- try-subst 142 | [variable? x binds] 143 | {:pre [(map? binds) (fn? variable?)]} 144 | (walk/prewalk (fn [expr] 145 | (if (variable? expr) 146 | (if-let [bind (find binds expr)] 147 | (val bind) 148 | expr) 149 | expr)) 150 | x)) 151 | 152 | (defn- unifier* 153 | ([x y] (unifier* lvar? x y)) 154 | ([variable? x y] 155 | (unifier* variable? x y (garner-unifiers variable? x y))) 156 | ([variable? x y binds] 157 | (->> binds 158 | (flatten-bindings variable?) 159 | (try-subst variable? y)))) ;; y is arbitrary 160 | 161 | ;; # PUBLIC API 162 | 163 | ;; ## OCCURS 164 | 165 | (defn make-occurs-unify-fn 166 | "Given a function to recognize unification variables, returns a function to 167 | return a bindings map for two expressions. This function uses an 'occurs check' 168 | methodology for detecting cycles." 169 | [variable-fn] 170 | (fn 171 | ([x y] (garner-unifiers unify-variable variable-fn x y {})) 172 | ([x y binds] (garner-unifiers unify-variable variable-fn x y binds)))) 173 | 174 | (defn make-occurs-subst-fn 175 | "Given a function to recognize unification variables, returns a function that 176 | will attempt to substitute unification bindings between two expressions. 177 | This function uses an 'occurs check' methodology for detecting cycles." 178 | [variable-fn] 179 | (partial try-subst variable-fn)) 180 | 181 | (defn make-occurs-unifier-fn 182 | "Given a function to recognize unification variables, returns a function to 183 | perform the unification of two expressions. This function uses an 'occurs check' 184 | methodology for detecting cycles." 185 | [variable-fn] 186 | (partial unifier* variable-fn)) 187 | 188 | 189 | (def ^{:doc "Attempt to unify x and y with the given bindings (if any). Potentially returns a map of the 190 | unifiers (bindings) found. Will throw an `IllegalStateException` if the expressions 191 | contain a cycle relationship. Will also throw an `IllegalArgumentException` if the 192 | sub-expressions clash. Note: This function is implemented with an occurs-check." 193 | :arglists '([expression1 expression2])} 194 | unify (make-occurs-unify-fn lvar?)) 195 | 196 | (def ^{:doc "Attempts to substitute the bindings in the appropriate locations in the given expression." 197 | :arglists '([expression bindings])} 198 | subst (make-occurs-subst-fn lvar?)) 199 | 200 | (def ^{:doc "Attempts the entire unification process from garnering the bindings to substituting 201 | the appropriate bindings. Note: This function is implemented with an occurs-check." 202 | :arglists '([expression1 expression2])} 203 | unifier (make-occurs-unifier-fn lvar?)) 204 | 205 | ;; ## NO OCCURS 206 | 207 | (defn make-unify-fn 208 | "Given a function to recognize unification variables, returns a function to 209 | return a bindings map for two expressions." 210 | [variable-fn] 211 | (fn 212 | ([x y] (garner-unifiers unify-variable- variable-fn x y {})) 213 | ([x y binds] (garner-unifiers unify-variable- variable-fn x y binds)))) 214 | 215 | (defn make-subst-fn 216 | "Given a function to recognize unification variables, returns a function that 217 | will attempt to substitute unification bindings between two expressions." 218 | [variable-fn] 219 | (partial try-subst variable-fn)) 220 | 221 | (defn make-unifier-fn 222 | "Given a function to recognize unification variables, returns a function to 223 | perform the unification of two expressions." 224 | [variable-fn] 225 | (fn [x y] 226 | (unifier* variable-fn 227 | x 228 | y 229 | (garner-unifiers unify-variable- variable-fn x y {})))) 230 | 231 | 232 | (def ^{:doc "Attempt to unify x and y with the given bindings (if any). Potentially returns a map of the 233 | unifiers (bindings) found. Will throw an `IllegalStateException` if the expressions 234 | contain a cycle relationship. Will also throw an `IllegalArgumentException` if the 235 | sub-expressions clash. Note: This function is implemented **without** an occurs-check." 236 | :arglists '([expression1 expression2])} 237 | unify- (make-unify-fn lvar?)) 238 | 239 | 240 | (def ^{:doc "Attempts the entire unification process from garnering the bindings to substituting 241 | the appropriate bindings. Note: This function is implemented **without** an occurs-check." 242 | :arglists '([expression1 expression2])} 243 | unifier- (make-unifier-fn lvar?)) 244 | 245 | 246 | (comment 247 | 248 | (def T '[?a ?b]) 249 | 250 | (defn make-matcher 251 | [root] 252 | (fn [tmpl req] 253 | (-> req 254 | :url 255 | (.replace root "") 256 | (.split "/") 257 | seq 258 | (unify tmpl)))) 259 | 260 | (def match (make-matcher "http://foo.com/foo/")) 261 | 262 | (match T {:url "http://foo.com/foo/fee/fi"}) 263 | ;;=> {?a "fee", ?b "fi"} 264 | 265 | (match T {:url "http://foo.com/foo/flub"}) 266 | ;;=> {?a "flub"} 267 | 268 | (defn make-genr 269 | [root] 270 | (fn [tmpl binds] 271 | (->> binds 272 | (subst tmpl) 273 | (interpose "/") 274 | (cons root) 275 | (apply str)))) 276 | 277 | (def gen (make-genr "http://foo.com/foo/")) 278 | 279 | (gen T '{?a 1, ?b 2}) 280 | ;;=> "http://foo.com/foo/1/2" 281 | 282 | (unify {:first '?first 283 | :last '?last 284 | :genre :giallo} 285 | 286 | {:first "Dario" 287 | :last "Argento" 288 | :genre :giallo}) 289 | 290 | ;;=> {?first "Dario", ?last "Argento"} 291 | 292 | (unifier ['?first "Argento"] 293 | ["Dario" '?last]) 294 | 295 | ;;=> ["Dario" "Argento"] 296 | 297 | 298 | (subst '[1 2 ?x ?y] 299 | '{?x [3 4 ?y 6]}) 300 | 301 | ;;=> [1 2 [3 4 ?y 6] ?y] 302 | 303 | 304 | (unifier '[(?a * ?x | 2) + (?b * ?x) + ?c] 305 | '[?z + (4 * 5) + 3]) 306 | 307 | ;;=> [(?a * 5 | 2) + (4 * 5) + 3] 308 | 309 | (unify '[(?a * ?x | 2) + (?b * ?x) + ?c] 310 | '[?z + (4 * 5) + 3]) 311 | 312 | ;;=> {?c 3, ?x 5, ?b 4, ?z (?a * ?x | 2)} 313 | 314 | 315 | (unify '[(?a * ?x | 2) + (?b * ?x) + ?c] 316 | '[(?a * 5 | 2) + (4 * 5) + 3]) 317 | 318 | ;;=> {?c 3, ?b 4, ?x 5} 319 | 320 | (unify '[(?a * 5 | 2) + (4 * 5) + 3] 321 | '[?z + (4 * 5) + 3]) 322 | 323 | ;;=> {?z (?a * 5 | 2)} 324 | 325 | (= (subst '[?z + (4 * 5) + 3] 326 | '{?c 3, ?b 4, ?x 5 327 | ?z (?a * 5 | 2)}) 328 | 329 | (subst '[(?a * ?x | 2) + (?b * ?x) + ?c] 330 | '{?c 3, ?b 4, ?x 5 331 | ?z (?a * 5 | 2)})) 332 | 333 | ;;=> true 334 | 335 | (subst '[(?a * ?x | 2) + (?b * ?x) + ?c] 336 | '{?c 3, ?b 4, ?x 5}) 337 | 338 | ;;=> [(?a * 5 | 2) + (4 * 5) + 3] 339 | 340 | (unify [1 2 3] '[?x & ?more]) 341 | ;;=> {?more (2 3), ?x 1} 342 | 343 | (unify [1 2 3] '[_ _ _ & ?more]) 344 | ;;=> {} 345 | 346 | (unify [1 2 3 4 5] '[_ _ _ & ?more]) 347 | ;;=> {?more (4 5)} 348 | 349 | (unify [1 2 3 4 5] '[_ ?b _ & ?more]) 350 | ;;=> {?more (4 5), ?b 2} 351 | 352 | (unify [:foo 1 2] '[?head & _]) 353 | 354 | ;;=> {?head :foo} 355 | 356 | (unifier [1 2 3] '[?x & ?more]) 357 | 358 | ) 359 | -------------------------------------------------------------------------------- /epl-v10.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | Eclipse Public License - Version 1.0 8 | 25 | 26 | 27 | 28 | 29 | 30 |

Eclipse Public License - v 1.0

31 | 32 |

THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE 33 | PUBLIC LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR 34 | DISTRIBUTION OF THE PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS 35 | AGREEMENT.

36 | 37 |

1. DEFINITIONS

38 | 39 |

"Contribution" means:

40 | 41 |

a) in the case of the initial Contributor, the initial 42 | code and documentation distributed under this Agreement, and

43 |

b) in the case of each subsequent Contributor:

44 |

i) changes to the Program, and

45 |

ii) additions to the Program;

46 |

where such changes and/or additions to the Program 47 | originate from and are distributed by that particular Contributor. A 48 | Contribution 'originates' from a Contributor if it was added to the 49 | Program by such Contributor itself or anyone acting on such 50 | Contributor's behalf. Contributions do not include additions to the 51 | Program which: (i) are separate modules of software distributed in 52 | conjunction with the Program under their own license agreement, and (ii) 53 | are not derivative works of the Program.

54 | 55 |

"Contributor" means any person or entity that distributes 56 | the Program.

57 | 58 |

"Licensed Patents" mean patent claims licensable by a 59 | Contributor which are necessarily infringed by the use or sale of its 60 | Contribution alone or when combined with the Program.

61 | 62 |

"Program" means the Contributions distributed in accordance 63 | with this Agreement.

64 | 65 |

"Recipient" means anyone who receives the Program under 66 | this Agreement, including all Contributors.

67 | 68 |

2. GRANT OF RIGHTS

69 | 70 |

a) Subject to the terms of this Agreement, each 71 | Contributor hereby grants Recipient a non-exclusive, worldwide, 72 | royalty-free copyright license to reproduce, prepare derivative works 73 | of, publicly display, publicly perform, distribute and sublicense the 74 | Contribution of such Contributor, if any, and such derivative works, in 75 | source code and object code form.

76 | 77 |

b) Subject to the terms of this Agreement, each 78 | Contributor hereby grants Recipient a non-exclusive, worldwide, 79 | royalty-free patent license under Licensed Patents to make, use, sell, 80 | offer to sell, import and otherwise transfer the Contribution of such 81 | Contributor, if any, in source code and object code form. This patent 82 | license shall apply to the combination of the Contribution and the 83 | Program if, at the time the Contribution is added by the Contributor, 84 | such addition of the Contribution causes such combination to be covered 85 | by the Licensed Patents. The patent license shall not apply to any other 86 | combinations which include the Contribution. No hardware per se is 87 | licensed hereunder.

88 | 89 |

c) Recipient understands that although each Contributor 90 | grants the licenses to its Contributions set forth herein, no assurances 91 | are provided by any Contributor that the Program does not infringe the 92 | patent or other intellectual property rights of any other entity. Each 93 | Contributor disclaims any liability to Recipient for claims brought by 94 | any other entity based on infringement of intellectual property rights 95 | or otherwise. As a condition to exercising the rights and licenses 96 | granted hereunder, each Recipient hereby assumes sole responsibility to 97 | secure any other intellectual property rights needed, if any. For 98 | example, if a third party patent license is required to allow Recipient 99 | to distribute the Program, it is Recipient's responsibility to acquire 100 | that license before distributing the Program.

101 | 102 |

d) Each Contributor represents that to its knowledge it 103 | has sufficient copyright rights in its Contribution, if any, to grant 104 | the copyright license set forth in this Agreement.

105 | 106 |

3. REQUIREMENTS

107 | 108 |

A Contributor may choose to distribute the Program in object code 109 | form under its own license agreement, provided that:

110 | 111 |

a) it complies with the terms and conditions of this 112 | Agreement; and

113 | 114 |

b) its license agreement:

115 | 116 |

i) effectively disclaims on behalf of all Contributors 117 | all warranties and conditions, express and implied, including warranties 118 | or conditions of title and non-infringement, and implied warranties or 119 | conditions of merchantability and fitness for a particular purpose;

120 | 121 |

ii) effectively excludes on behalf of all Contributors 122 | all liability for damages, including direct, indirect, special, 123 | incidental and consequential damages, such as lost profits;

124 | 125 |

iii) states that any provisions which differ from this 126 | Agreement are offered by that Contributor alone and not by any other 127 | party; and

128 | 129 |

iv) states that source code for the Program is available 130 | from such Contributor, and informs licensees how to obtain it in a 131 | reasonable manner on or through a medium customarily used for software 132 | exchange.

133 | 134 |

When the Program is made available in source code form:

135 | 136 |

a) it must be made available under this Agreement; and

137 | 138 |

b) a copy of this Agreement must be included with each 139 | copy of the Program.

140 | 141 |

Contributors may not remove or alter any copyright notices contained 142 | within the Program.

143 | 144 |

Each Contributor must identify itself as the originator of its 145 | Contribution, if any, in a manner that reasonably allows subsequent 146 | Recipients to identify the originator of the Contribution.

147 | 148 |

4. COMMERCIAL DISTRIBUTION

149 | 150 |

Commercial distributors of software may accept certain 151 | responsibilities with respect to end users, business partners and the 152 | like. While this license is intended to facilitate the commercial use of 153 | the Program, the Contributor who includes the Program in a commercial 154 | product offering should do so in a manner which does not create 155 | potential liability for other Contributors. Therefore, if a Contributor 156 | includes the Program in a commercial product offering, such Contributor 157 | ("Commercial Contributor") hereby agrees to defend and 158 | indemnify every other Contributor ("Indemnified Contributor") 159 | against any losses, damages and costs (collectively "Losses") 160 | arising from claims, lawsuits and other legal actions brought by a third 161 | party against the Indemnified Contributor to the extent caused by the 162 | acts or omissions of such Commercial Contributor in connection with its 163 | distribution of the Program in a commercial product offering. The 164 | obligations in this section do not apply to any claims or Losses 165 | relating to any actual or alleged intellectual property infringement. In 166 | order to qualify, an Indemnified Contributor must: a) promptly notify 167 | the Commercial Contributor in writing of such claim, and b) allow the 168 | Commercial Contributor to control, and cooperate with the Commercial 169 | Contributor in, the defense and any related settlement negotiations. The 170 | Indemnified Contributor may participate in any such claim at its own 171 | expense.

172 | 173 |

For example, a Contributor might include the Program in a commercial 174 | product offering, Product X. That Contributor is then a Commercial 175 | Contributor. If that Commercial Contributor then makes performance 176 | claims, or offers warranties related to Product X, those performance 177 | claims and warranties are such Commercial Contributor's responsibility 178 | alone. Under this section, the Commercial Contributor would have to 179 | defend claims against the other Contributors related to those 180 | performance claims and warranties, and if a court requires any other 181 | Contributor to pay any damages as a result, the Commercial Contributor 182 | must pay those damages.

183 | 184 |

5. NO WARRANTY

185 | 186 |

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS 187 | PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS 188 | OF ANY KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 189 | ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY 190 | OR FITNESS FOR A PARTICULAR PURPOSE. Each Recipient is solely 191 | responsible for determining the appropriateness of using and 192 | distributing the Program and assumes all risks associated with its 193 | exercise of rights under this Agreement , including but not limited to 194 | the risks and costs of program errors, compliance with applicable laws, 195 | damage to or loss of data, programs or equipment, and unavailability or 196 | interruption of operations.

197 | 198 |

6. DISCLAIMER OF LIABILITY

199 | 200 |

EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT 201 | NOR ANY CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, 202 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING 203 | WITHOUT LIMITATION LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF 204 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 205 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OR 206 | DISTRIBUTION OF THE PROGRAM OR THE EXERCISE OF ANY RIGHTS GRANTED 207 | HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

208 | 209 |

7. GENERAL

210 | 211 |

If any provision of this Agreement is invalid or unenforceable under 212 | applicable law, it shall not affect the validity or enforceability of 213 | the remainder of the terms of this Agreement, and without further action 214 | by the parties hereto, such provision shall be reformed to the minimum 215 | extent necessary to make such provision valid and enforceable.

216 | 217 |

If Recipient institutes patent litigation against any entity 218 | (including a cross-claim or counterclaim in a lawsuit) alleging that the 219 | Program itself (excluding combinations of the Program with other 220 | software or hardware) infringes such Recipient's patent(s), then such 221 | Recipient's rights granted under Section 2(b) shall terminate as of the 222 | date such litigation is filed.

223 | 224 |

All Recipient's rights under this Agreement shall terminate if it 225 | fails to comply with any of the material terms or conditions of this 226 | Agreement and does not cure such failure in a reasonable period of time 227 | after becoming aware of such noncompliance. If all Recipient's rights 228 | under this Agreement terminate, Recipient agrees to cease use and 229 | distribution of the Program as soon as reasonably practicable. However, 230 | Recipient's obligations under this Agreement and any licenses granted by 231 | Recipient relating to the Program shall continue and survive.

232 | 233 |

Everyone is permitted to copy and distribute copies of this 234 | Agreement, but in order to avoid inconsistency the Agreement is 235 | copyrighted and may only be modified in the following manner. The 236 | Agreement Steward reserves the right to publish new versions (including 237 | revisions) of this Agreement from time to time. No one other than the 238 | Agreement Steward has the right to modify this Agreement. The Eclipse 239 | Foundation is the initial Agreement Steward. The Eclipse Foundation may 240 | assign the responsibility to serve as the Agreement Steward to a 241 | suitable separate entity. Each new version of the Agreement will be 242 | given a distinguishing version number. The Program (including 243 | Contributions) may always be distributed subject to the version of the 244 | Agreement under which it was received. In addition, after a new version 245 | of the Agreement is published, Contributor may elect to distribute the 246 | Program (including its Contributions) under the new version. Except as 247 | expressly stated in Sections 2(a) and 2(b) above, Recipient receives no 248 | rights or licenses to the intellectual property of any Contributor under 249 | this Agreement, whether expressly, by implication, estoppel or 250 | otherwise. All rights in the Program not expressly granted under this 251 | Agreement are reserved.

252 | 253 |

This Agreement is governed by the laws of the State of New York and 254 | the intellectual property laws of the United States of America. No party 255 | to this Agreement will bring a legal action under this Agreement more 256 | than one year after the cause of action arose. Each party waives its 257 | rights to a jury trial in any resulting litigation.

258 | 259 | 260 | 261 | 262 | --------------------------------------------------------------------------------