________________________________________________________________________

This file is part of Logtalk <https://logtalk.org/>  
Copyright 1998-2019 Paulo Moura <pmoura@logtalk.org>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
________________________________________________________________________


% start by loading the example:

| ?- logtalk_load(metainterpreters(loader)).
...


% direct call of p/1:

| ?- database::p(X).

X = 1 ;
X = 2 
yes


% solver - a simple meta-interpreter for pure Prolog:

| ?- database::solve(p(X)).

X = 1 ;
X = 2 
yes

| ?- database::proof_tree(p(X), Tree).

X = 1
Tree = p(1):- (q(1, a):- (s(1):-true), (t(1, a):-true)), (r(a):-true) ;
X = 2
Tree = p(2):- (q(2, b):- (s(2):-true), (t(2, b):-true)), (r(b):-true) 
yes


% tracer - a simple meta-interpreter for tracing goal proofs using pure Prolog:

| ?- database::trace(p(X)).
1 call: p(_G180)
2 call: q(_G180, _G316)
3 call: s(_G180)
3 exit: s(1)
3 call: t(1, _G316)
3 exit: t(1, a)
2 exit: q(1, a)
2 call: r(a)
2 exit: r(a)
1 exit: p(1)

X = 1 ;
1 redo: p(1)
2 redo: r(a)
2 fail: r(a)
2 redo: q(1, a)
3 redo: t(1, a)
3 fail: t(1, _G316)
3 redo: s(1)
3 exit: s(2)
3 call: t(2, _G316)
3 exit: t(2, b)
2 exit: q(2, b)
2 call: r(b)
2 exit: r(b)
1 exit: p(2)

X = 2 ;
1 redo: p(2)
2 redo: r(b)
2 fail: r(b)
2 redo: q(2, b)
3 redo: t(2, b)
3 fail: t(2, _G316)
3 redo: s(2)
3 exit: s(3)
3 call: t(3, _G316)
3 fail: t(3, _G316)
3 redo: s(3)
3 fail: s(_G180)
2 fail: q(_G180, _G316)
1 fail: p(_G180)

no

% counting the number of resolution steps:

| ?- lists::steps(reverse([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],_), Steps).

Steps = 496
yes


% another example: expert system rules:

| ?- rules::prove(weather(Weather)).

Weather = raining
yes

| ?- rules::prove(goto(Where)).

Where = cinema
yes
