Clojure core.logic and miniKanren

Web Technology   Digital Transformation Technology Artificial Intelligence Technology   Natural Language Processing Technology   Semantic Web Technology   Deep Learning Technology   Online Learning & Reinforcement Learning Technology  Chatbot and Q&A Technology   User Interface Technology   Knowledge Information Processing Technology   Reasoning Technology  Clojure Programming

miniKanren is an embedded domain-specific language for logic programming, similar to the aforementioned prolog. The core language is very simple, with only three logical operators and one interface operator. The core language is very simple, with only three logical operators and one interface operator. The languages implemented are as diverse as Scheme, Racket, Clojure, Haskell, Python, JavaScript, Scala, Ruby, OCaml, and PHP. In Clojure, core.logic provides a DSL similar to miniKanren.

Here is a code example similar to the aforementioned prolog in core.logic. First, the dependencies of leiningen are as follows

[org.clojure/core.logic "1.0.0"]

The following code is used to implement logic programming

(ns logic-test.core
  (:require [clojure.core.logic.pldb :as pldb]
            [clojure.core.logic :refer :all]))


(pldb/db-rel shape f s) ;; Define the shape of the fruit.
(pldb/db-rel color f c) ;; Define the color of the fruit

(def facts1
  (pldb/db
  [shape :apple :sphere]
  [shape :orange :sphere]
  [shape :banana :stick]
  [shape :strawberry :cone]
  [color :apple :red]
  [color :orange :orange]
  [color :banana :yellow]
  [color :strawberry :red]))

(pldb/with-db facts1
  (run* [q] (shape q :sphere)))  
;;-> (:orange :apple)  Output a round object

(pldb/with-db facts1
  (run* [q]
    (shape q :sphere)
    (color q :red)))   
;;->(:apple) Output a round, red object.

(pldb/with-db facts1
  (run* [q]
    (conde
     [(shape q :sphere)]
     [(color q :red)])))  
;;->(:orange :apple :apple :strawbery) Output a round or red object (apple is output twice because it applies to both)

It differs from prolog in that it has a variety of macros for conditional systems, such as the following

/* Yo
(conde     ;; [Case] (The Reasoned Schemer's condi-tion)
  [a b c]  ;; If a b c are all valid and
  [d e f]  ;; If d e f all hold and
  [g h i]) ;; g h i are all true, respectively.

(condu     ;; [Select]
  [a b c]  ;; If a b c are all true, then this is the overall result.
  [d e f]  ;; If the above does not hold and d e f all hold, then this is the overall result.
  [g h i]) ;; If the above does not hold, and g h i all hold, then we take this as the overall result.

(conda     ;; [conditional branch]
  [a b c]  ;; If a holds, we take the result of a b c to be the overall result.
  [d e f]  ;; If a does not hold and d holds, then the result of d e f is the overall result.
  [g h i]) ;; If a and d do not hold, and g holds, then the result of g h i is the overall result.

(matche [a b c]
  ([?x ?x ?x] ...) ;; Matches if a, b, and c are the same
  ([?x ?x _] ...)  ;; Match if a, b are the same
  ([_ ?x ?x] ...)) ;; Match if b, c are the same

(defne funcname [a b c] ...)
;; (defn funcname [a b c] The same as (matche [a b c] ...)) 

;; The following are in the same relationship
;; conde  <--> condu  <--> conda
;; matche <--> matchu <--> matcha
;; defne  <--> defnu  <--> defna

 

コメント

Exit mobile version
タイトルとURLをコピーしました