CLP(R)

Last updated

CLP(R) is a declarative programming language. It stands for constraint logic programming (real) where real refers to the real numbers. It can be considered and is generally implemented as a superset or add-on package for a Prolog implementation.

Contents

Example rule

The simultaneous linear equations:

are expressed in CLP(R) as:

3*X+4*Y-2*Z=8,X-5*Y+Z=10,2*X+3*Y-Z=20.

and a typical implementation's response would be:

Z = 35.75
Y = 8.25
X = 15.5

Yes

Example program

CLP(R) allows the definition of predicates using recursive definitions. For example a mortgage relation can be defined as relating the principal P, the number of time periods of the loan T, the repayment each period R, the interest rate per period I and the final balance owing at the end of the loan B.

mg(P,T,R,I,B):-T=0,B=R.mg(P,T,R,I,B):-T>=1,P1=P*(1+I)-R,mg(P1,T-1,R,I,B).

The first rule expresses that for a 0 period loan the balance owing at the end is simply the original principal. The second rule expresses that for a loan of at least one time period we can calculate the new owing amount P1 by multiplying the principal by 1 plus the interest rate and subtracting the repayment. The remainder of the loan is treated as another mortgage for the new principal and one less time period.

What can you do with it? You can ask many questions. If I borrow 1000$ for 10 years at 10% per year repaying 150 per year, how much will I owe at the end?

?-mg(1000,10,150,10/100,B).

The system responds with the answer

B=203.129.

How much can I borrow with a 10 year loan at 10% repaying 150 each year to owe nothing at the end?

?-mg(P,10,150,10/100,0).

The system responds with the answer

P=921.685.

What is the relationship between the principal, repayment and balance on a 10 year loan at 10% interest?

?-mg(P,10,R,10/100,B).

The system responds with the answer

P=0.3855*B+6.1446*R.

This shows the relationship between the variables, without requiring any to take a particular value.

Prolog Integration

CLP(R) has first been integrated into a Prolog system in 1994, namely into SICStus Prolog. [1] This implementation has since been ported to many popular Prolog systems, including Ciao, [2] SWI-Prolog [3] and XSB. [4]

See also

Related Research Articles

Logic programming is a programming, database and knowledge representation paradigm based on formal logic. A logic program is a set of sentences in logical form, representing knowledge about some problem domain. Computation is performed by applying logical reasoning to that knowledge, to solve problems in the domain. Major logic programming language families include Prolog, Answer Set Programming (ASP) and Datalog. In all of these languages, rules are written in the form of clauses:

Prolog is a logic programming language that has its origins in artificial intelligence, automated theorem proving and computational linguistics.

In logic and computer science, specifically automated reasoning, unification is an algorithmic process of solving equations between symbolic expressions, each of the form Left-hand side = Right-hand side. For example, using x,y,z as variables, and taking f to be an uninterpreted function, the singleton equation set { f(1,y) = f(x,2) } is a syntactic first-order unification problem that has the substitution { x ↦ 1, y ↦ 2 } as its only solution.

Constraint programming (CP) is a paradigm for solving combinatorial problems that draws on a wide range of techniques from artificial intelligence, computer science, and operations research. In constraint programming, users declaratively state the constraints on the feasible solutions for a set of decision variables. Constraints differ from the common primitives of imperative programming languages in that they do not specify a step or sequence of steps to execute, but rather the properties of a solution to be found. In addition to constraints, users also need to specify a method to solve these constraints. This typically draws upon standard methods like chronological backtracking and constraint propagation, but may use customized code like a problem-specific branching heuristic.

<span class="mw-page-title-main">Annual percentage rate</span> Interest rate for a whole year

The term annual percentage rate of charge (APR), corresponding sometimes to a nominal APR and sometimes to an effective APR (EAPR), is the interest rate for a whole year (annualized), rather than just a monthly fee/rate, as applied on a loan, mortgage loan, credit card, etc. It is a finance charge expressed as an annual rate. Those terms have formal, legal definitions in some countries or legal jurisdictions, but in the United States:

In computer science, the occurs check is a part of algorithms for syntactic unification. It causes unification of a variable V and a structure S to fail if S contains V.

SWI-Prolog is a free implementation of the programming language Prolog, commonly used for teaching and semantic web applications. It has a rich set of features, libraries for constraint logic programming, multithreading, unit testing, GUI, interfacing to Java, ODBC and others, literate programming, a web server, SGML, RDF, RDFS, developer tools, and extensive documentation.

Constraint Handling Rules (CHR) is a declarative, rule-based programming language, introduced in 1991 by Thom Frühwirth at the time with European Computer-Industry Research Centre (ECRC) in Munich, Germany. Originally intended for constraint programming, CHR finds applications in grammar induction, type systems, abductive reasoning, multi-agent systems, natural language processing, compilation, scheduling, spatial-temporal reasoning, testing, and verification.

Answer set programming (ASP) is a form of declarative programming oriented towards difficult search problems. It is based on the stable model semantics of logic programming. In ASP, search problems are reduced to computing stable models, and answer set solvers—programs for generating stable models—are used to perform search. The computational process employed in the design of many answer set solvers is an enhancement of the DPLL algorithm and, in principle, it always terminates.

XSB is the name of a dialect of the Prolog programming language and its implementation developed at Stony Brook University in collaboration with the Katholieke Universiteit Leuven, the New University of Lisbon, Uppsala University and software vendor XSB, Inc.

λProlog, also written lambda Prolog, is a logic programming language featuring polymorphic typing, modular programming, and higher-order programming. These extensions to Prolog are derived from the higher-order hereditary Harrop formulas used to justify the foundations of λProlog. Higher-order quantification, simply typed λ-terms, and higher-order unification gives λProlog the basic supports needed to capture the λ-tree syntax approach to higher-order abstract syntax, an approach to representing syntax that maps object-level bindings to programming language bindings. Programmers in λProlog need not deal with bound variable names: instead various declarative devices are available to deal with binder scopes and their instantiations.

Logtalk is an object-oriented logic programming language that extends and leverages the Prolog language with a feature set suitable for programming in the large. It provides support for encapsulation and data hiding, separation of concerns and enhanced code reuse. Logtalk uses standard Prolog syntax with the addition of a few operators and directives.

B-Prolog was a high-performance implementation of the standard Prolog language with several extended features including matching clauses, action rules for event handling, finite-domain constraint solving, arrays and hash tables, declarative loops, and tabling. First released in 1994, B-Prolog is now a widely used CLP system. The constraint solver of B-Prolog was ranked top in two categories in the Second International Solvers Competition, and it also took the second place in P class in the second ASP solver competition and the second place overall in the third ASP solver competition. B-Prolog underpins the PRISM system, a logic-based probabilistic reasoning and learning system. B-Prolog is a commercial product, but it can be used for learning and non-profit research purposes free of charge. B-Prolog is not anymore actively developed, but it forms the basis for the Picat programming language.

The following Comparison of Prolog implementations provides a reference for the relative feature sets and performance of different implementations of the Prolog computer programming language. A comprehensive discussion of the most significant Prolog systems is presented in an article published in the 50-years of Prolog anniversary issue of the journal Theory and Practice of Logic Programming (TPLP).

A symbol in computer programming is a primitive data type whose instances have a human-readable form. Symbols can be used as identifiers. In some programming languages, they are called atoms. Uniqueness is enforced by holding them in a symbol table. The most common use of symbols by programmers is to perform language reflection, and the most common indirectly is their use to create object linkages.

The syntax and semantics of Prolog, a programming language, are the sets of rules that define how a Prolog program is written and how it is interpreted, respectively. The rules are laid out in ISO standard ISO/IEC 13211 although there are differences in the Prolog implementations.

HiLog is a programming logic with higher-order syntax, which allows arbitrary terms to appear in predicate and function positions. However, the model theory of HiLog is first-order. Although syntactically HiLog strictly extends first order logic, HiLog can be embedded into this logic.

SICStus Prolog is a proprietary, ISO-conforming implementation of the logic programming language Prolog. It is developed by the Swedish Institute of Computer Science since 1985 and puts a strong focus on performance and scalability.

BNR Prolog, also known as CLP(BNR), is a declarative constraint logic programming language based on relational interval arithmetic developed at Bell-Northern Research in the 1980s and 1990s. Embedding relational interval arithmetic in a logic programming language differs from other constraint logic programming (CLP) systems like CLP(R) or Prolog-III in that it does not perform any symbolic processing. BNR Prolog was the first such implementation of interval arithmetic in a logic programming language. Since the constraint propagation is performed on real interval values, it is possible to express and partially solve non-linear equations.

Logic programming is a programming paradigm that includes languages based on formal logic, including Datalog and Prolog. This article describes the syntax and semantics of the purely declarative subset of these languages. Confusingly, the name "logic programming" also refers to a specific programming language that roughly corresponds to the declarative subset of Prolog. Unfortunately, the term must be used in both senses in this article.

References

  1. Holzbaur, Christian (1995). "OEFAI clp(q,r) Manual Revision 1.3.2". ÖFAI Technical Reports Online (95–09). Archived from the original on 20 May 2001.
  2. "Introduction — The Ciao System v1.22". ciao-lang.org. Retrieved 2023-11-06.
  3. "SWI-Prolog manual". www.swi-prolog.org. Retrieved 2023-11-06.
  4. Swift, Theresa (13 May 2022). "Constraint Packages". The XSB System, Version 5.0, Volume 2: Interfaces and Packages. p. 71.