在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:ztangent/Julog.jl开源软件地址:https://github.com/ztangent/Julog.jl开源编程语言:Julia 100.0%开源软件介绍:Julog.jlA Julia package for Prolog-style logic programming. InstallationEnter the package manager by pressing
The latest development version can also be installed by running:
Features
UsageTerms and Horn clauses can be expressed in Prolog-like syntax using the
# This creates a term
@julog teacher(bodhidharma, huike)
# This creates a fact (a term which is asserted to be true)
@julog teacher(bodhidharma, huike) <<= true
# This creates a definite clause
@julog grandteacher(A, C) <<= teacher(A, B) & teacher(B, C) The clauses = @julog [
ancestor(sakyamuni, bodhidharma) <<= true,
teacher(bodhidharma, huike) <<= true,
teacher(huike, sengcan) <<= true,
teacher(sengcan, daoxin) <<= true,
teacher(daoxin, hongren) <<= true,
teacher(hongren, huineng) <<= true,
ancestor(A, B) <<= teacher(A, B),
ancestor(A, C) <<= teacher(B, C) & ancestor(A, B),
grandteacher(A, C) <<= teacher(A, B) & teacher(B, C)
] With the # Query: Is Sakyamuni the dharma ancestor of Huineng?
julia> goals = @julog [ancestor(sakyamuni, huineng)]; # List of terms to query or prove
julia> sat, subst = resolve(goals, clauses);
julia> sat
true
# Query: Who are the grandteachers of whom?
julia> goals = @julog [grandteacher(X, Y)];
julia> sat, subst = resolve(goals, clauses);
julia> subst
4-element Array{Any,1}:
{Y => sengcan, X => bodhidharma}
{Y => daoxin, X => huike}
{Y => hongren, X => sengcan}
{Y => huineng, X => daoxin} Forward-chaining proof search is supported as well, using # Facts derivable from one iteration through the rules
julia> derivations(clauses, 1)
16-element Array{Clause,1}:
teacher(bodhidharma, huike)
⋮
ancestor(sakyamuni, huike)
# The set of all derivable facts (i.e. the closure / fixed-point)
julia> derivations(clauses, Inf)
30-element Array{Clause,1}:
teacher(bodhidharma, huike)
⋮
ancestor(sakyamuni, huineng) More examples can be found in the Syntax
julia> typeof(@julog(Person))
Var
julia> typeof(@julog(person))
Const However, several important operators differ from Prolog, as shown by the examples below:
In words, In addition, when constructing Prolog-style linked-lists, the syntax If Prolog syntax is preferred, the InterpolationSimilar to string interpolation and expression interpolation in Julia, you can interpolate Julia expressions when constructing julia> e = exp(1)
2.718281828459045
julia> term = @julog irrational($e)
irrational(2.718281828459045)
julia> dump(term)
Compound
name: Symbol irrational
args: Array{Term}((1,))
1: Const
name: Float64 2.718281828459045 The second form is term interpolation using the julia> e = Const(exp(1))
2.718281828459045
julia> term = @julog irrational(:e)
irrational(2.718281828459045)
julia> dump(term)
Compound
name: Symbol irrational
args: Array{Term}((1,))
1: Const
name: Float64 2.718281828459045 Interpolation allows us to easily generate Julog knowledge bases programatically using Julia code: julia> people = @julog [avery, bailey, casey, darcy];
julia> heights = [@julog(height(:p, cm($(rand(140:200))))) for p in people]
4-element Array{Compound,1}:
height(avery, cm(155))
height(bailey, cm(198))
height(casey, cm(161))
height(darcy, cm(175)) Custom FunctionsIn addition to standard arithmetic functions, funcs = Dict()
funcs[:pi] = pi
funcs[:sin] = sin
funcs[:cos] = cos
funcs[:square] = x -> x * x
funcs[:lookup] = Dict((:foo,) => "hello", (:bar,) => "world")
@assert resolve(@julog(sin(pi / 2) == 1), Clause[], funcs=funcs)[1] == true
@assert resolve(@julog(cos(pi) == -1), Clause[], funcs=funcs)[1] == true
@assert resolve(@julog(lookup(foo) == "hello"), Clause[], funcs=funcs)[1] == true
@assert resolve(@julog(lookup(bar) == "world"), Clause[], funcs=funcs)[1] == true See Unlike Prolog, Julog also supports extended unification via the evaluation of functional terms. In other words, the following terms unify: julia> unify(@julog(f(X, X*Y, Y)), @julog(f(4, 20, 5)))
Dict{Var, Term} with 2 entries:
Y => 5
X => 4 However, the extent of such unification is limited. If variables used within a functional expression are not sufficiently instantiated at the time of evaluation, evaluation will be partial, causing unification to fail: julia> unify(@julog(f(X, X*Y, Y)), @julog(f(X, 20, 5))) === nothing
true Built-in Predicates
See Conversion UtilitiesJulog provides some support for converting and manipulating logical formulae, for example, conversion to negation, conjunctive, or disjunctive normal form: julia> formula = @julog and(not(and(a, not(b))), c)
julia> to_nnf(formula)
and(or(not(a), b), c)
julia> to_cnf(formula)
and(or(not(a), b), or(c))
julia> to_dnf(formula)
or(and(not(a), c), and(b, c)) This can be useful for downstream applications, such as classical planning. Note however that these conversions do not handle the implicit existential quantification in Prolog semantics, and hence are not guaranteed to preserve equivalence when free variables are involved. In particular, care should be taken with negations of conjunctions of unbound predicates. For example, the following expression states that "All ravens are black.": @julog not(and(raven(X), not(black(X)))) However, @julog or(and(not(raven(X))), and(black(X))) Related PackagesThere are several other Julia packages related to Prolog and logic programming:
AcknowledgementsThis implementation was made with reference to Chris Meyer's Python interpreter for Prolog, as well as the unification and SLD-resolution algorithms presented in An Introduction to Prolog by Pierre M. Nugues. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论