# Represent Accounting Equation Using SWI-Prolog

**URL:** <https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465>\
**Category:** Help!\
**Tags:** swish\
**Created:** [November 10, 2019, 5:03pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465 "2019-11-10T17:03:29Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [November 12, 2019, 12:18pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/21 "2019-11-12T12:18:16Z")

</div>

> [@CharlesHoffmanCPA](#):
>
> I tried this:
> 
> ```prolog
> Asset = fact(term(Asset),entity(Asset_entity),Asset_value),
> Liabilities = fact(term(Liabilities),entity(Liabilities_entity),Liabilities_value),
> Equity = fact(term(Equity),entity(Equity_entity),Equity_value)
> 
> Asset_value is Liabilities_value + Equity_value.
> 
> fact(term(Asset), entity(Microsoft), 5000).
> fact(term(Liabilities), entity(Microsoft), 1000).
> fact(term(Equity), entity(Microsoft), 4000).
> 
> does_balance_sheet_balance(entity(Entity)) := 
> fact(term(Asset),entity(Entity),Asset_value),
> fact(term(Liabilities),entity(Entity),Liabilities_value),
> fact(term(Equity),entity(Entity),Equity_value),
> Asset_value is Liabilities_value + Equity_value.
> 
> ```

Would a simple DSL work in your case? For example, one that would allow you to write instead:

```
microsoft asset = 5000.
microsoft liabilities = 1000.
microsoft equity = 4000.

Entity asset = Entity liabilities + Entity equity.
Entity balance = Entity asset.

```

Using term-expansion (e.g. [term\_expansion/2](https://www.swi-prolog.org/pldoc/man?predicate=term_expansion/2)), you could then translate this DSL into a set of Prolog facts and rules.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 12, 2019, 12:29pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/22 "2019-11-12T12:29:48Z")

</div>

> [@pmoura](#):
>
> microsoft asset = 5000.

As I have never really done any term expansion or used qusi-quoations, is the use of `=` in what I am guessing is a fact allowed?

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [November 12, 2019, 12:38pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/23 "2019-11-12T12:38:46Z")

</div>

Assuming that `asset` is defined as an postfix operator, yes. For example:

```
?- op(1100, xf, asset).
true.

?- read(Fact), write_canonical(Fact).
|: microsoft asset = 5000.
asset(=(microsoft,5000))
Fact = (microsoft=5000 asset).

```

You could then define a term-expansion rule that expands these user-defined facts into a suitable set of facts and rules for actual processing. For example:

```
term_expansion(
    asset(=Entity,Value),
    asset(Entity,Value)
).
```

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 12, 2019, 12:40pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/24 "2019-11-12T12:40:11Z")

</div>

> [@pmoura](#):
>
> Assuming that `asset` is defined as a postfix operator

Ahh! Did not see `asset` as an operator. Nice trick!

---

<div class="post-metadata">

**Author:** ![pmoura](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pmoura/32/11_2.png) [@pmoura](https://swi-prolog.discourse.group/u/pmoura)\
**Post date:** [November 12, 2019, 12:42pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/25 "2019-11-12T12:42:50Z")

</div>

Oops. I meant to write postfix operator. Fixed my reply. When defining DSLs, my advice regarding operators is (1) reuse as much as possible standard operators and (2) when defining your own operators, prefer descriptive words to combination of graphic characters.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 12, 2019, 12:46pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/26 "2019-11-12T12:46:47Z")

</div>

> [@pmoura](#):
>
> (2) when defining your own operators, prefer descriptive words to combination of graphic characters.

I can’t count how many times when I read some math research papers or books that I have to keep cheat sheets of the operator symbols handy to look up what they mean an how to use them. I get it that writing a symbol on the black board is a lot faster and smaller than writing a word, but my brain has a limited capacity.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [November 12, 2019, 1:09pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/27 "2019-11-12T13:09:37Z")

</div>

You can also use nice Unicode symbols. Hard to type and remember, but can look really nice and geeky 🙂

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 12, 2019, 1:13pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/28 "2019-11-12T13:13:01Z")

</div>

Yes, totally agree. I do this so often that in trying to identify the symbols I have found this nice web page that lets you draw the symbol and it shows you similar Unicode characters.

> **[Shapecatcher: Draw the Unicode character you want!](http://shapecatcher.com/)**
>
> You need to find a specific Unicode character? With Shapecatcher.com you can search through a database of characters by simply drawing your character into a box. It can find the most similar character shapes for your drawing.

e.g.

![image](https://global.discourse-cdn.com/free1/uploads/swiprolog/original/1X/e4f03985a664eb786a184bc85846ca37c4b920d0.png)

* * *

Note to others:

Be aware that when using other characters some are seen as variables and some as constants, e.g.

> [@Unification](https://swi-prolog.discourse.group/t/unification/691):
>
> How to: [Unification](http://www.swi-prolog.org/pldoc/man?predicate=%3D/2) Note: This is a work in progress. When it is complete these notes will be removed. Note: Do not reply to this topic, questions, concerns, comments, etc. are to handled in [Wiki Discussion: How to - Unification](https://swi-prolog.discourse.group/t/wiki-discussion-how-to-unification/692) Note: This is just to get the topic started and hopefully others to jump in and make this useful. Even if you are brand new to Prolog, this is such a basic and fundamental concept that you can and should join in to help improve the value of this Wiki. Join the d…

Also as I use SWI-Prolog on Windows, not all of the Unicode characters appear as expected so check that they will appear as needed before using in production code.

My favorite exploit of this is that a snowman will unify with a snowman.

```porlog
?- =(☃,☃).
true.

```

---

<div class="post-metadata">

**Author:** ![CharlesHoffmanCPA](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/charleshoffmancpa/32/698_2.png) [@CharlesHoffmanCPA](https://swi-prolog.discourse.group/u/CharlesHoffmanCPA)\
**Post date:** [November 12, 2019, 3:12pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/29 "2019-11-12T15:12:30Z")

</div>

This is probably exactly what you need, a conformance suite. It has both positive and negative examples. It is not very extensive yet:

[https://tinyurl.com/vn58o6l](https://tinyurl.com/vn58o6l)

I can certainly build out simple examples that have one error and would if you are interested in processing the XBRL files to validate the logic.

---

<div class="post-metadata">

**Author:** ![anniepoo](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/anniepoo/32/12_2.png) [@anniepoo](https://swi-prolog.discourse.group/u/anniepoo)\
**Post date:** [November 12, 2019, 3:17pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/30 "2019-11-12T15:17:10Z")

</div>

In that case, that IS the format for your domain experts - assuming they’ve seen plenty of that already. Read in using the SGML package, get the parts you want out with SWI-prolog XPATH library.  
One thing you might do, if you want to internally work with it in some other form, is to make a quasiquoter.

I refer you to the online docs for how to do that - it’s fairly straightforward.

Jan was thinking of a situation like, your users are musicians - let them write in something that looks like the musical notation they’re used to.

`[qtr(bb), half(a), half(a#)]`

Some representation like that .  
Then compile that to executable prolog yourself.

---

<div class="post-metadata">

**Author:** ![CharlesHoffmanCPA](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/charleshoffmancpa/32/698_2.png) [@CharlesHoffmanCPA](https://swi-prolog.discourse.group/u/CharlesHoffmanCPA)\
**Post date:** [November 12, 2019, 3:17pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/31 "2019-11-12T15:17:24Z")

</div>

I am with you here. The storage format is the global standard syntax for business reporting which is XBRL. The question is to how best process the XBRL to make sure the information being conveyed by the XBRL syntax is logical per domain semantics; in this case accounting and financial reporting.

This is a big opportunity that is only in it’s infancy now. There is a boatload of information on XBRL on my blog: [http://xbrl.squarespace.com/](http://xbrl.squarespace.com/)

I would be very happy to answer any questions.

---

<div class="post-metadata">

**Author:** ![CharlesHoffmanCPA](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/charleshoffmancpa/32/698_2.png) [@CharlesHoffmanCPA](https://swi-prolog.discourse.group/u/CharlesHoffmanCPA)\
**Post date:** [November 12, 2019, 3:35pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/33 "2019-11-12T15:35:05Z")

</div>

XBRL is a global standard language for business reports and financial reports. Every public company in the United States is required to submit XBRL-based quarterly and annual financial statements with the U.S. Securities and Exchange Commission. A good way to see some of those documents is via XBRL Cloud’s Edgar Dashboard:

[https://edgardashboard.xbrlcloud.com/edgar-dashboard/](https://edgardashboard.xbrlcloud.com/edgar-dashboard/)

The European Single Market Authority (ESMA) has similarly required that every listed company in the European Union submit financial statements in the XBRL format beginning in January 2020.

But that is small potatoes. The BIG OPPORTUNITY is the 25 million private companies in the US and similar number globally. There are additional opportunities in special purpose financial reporting.

Currently, the missing piece of the XBRL puzzle is a “rules engine” that is powerful to process the logic of a financial report so that the XBRL-based financial reports can be created CORRECTLY. Currently, for example, the reports submitted to the SEC have errors.

The opportunity is to provide some sort of “rules engine” or “logic processor” or “expert system” that helps professional accountants create reports correctly so that the quality is high. This document summarizes many aspects of the opportunity:

> **[Financial Report Genome Project - Blog: Digital Financial Reporting...](http://xbrl.squarespace.com/journal/2019/11/8/financial-report-genome-project.html)**
>
> I have undertaken something that, for lack of a better term, I am calling the Financial Report Gen...

There are many ways to approach this. One way is partnering with other software vendors that do not have these capabilities. I have many, many good contacts.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 12, 2019, 3:49pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/34 "2019-11-12T15:49:13Z")

</div>

> [@CharlesHoffmanCPA](#):
>
> I can certainly build out simple examples that have one error and would if you are interested in processing the XBRL files to validate the logic.

Let me work with the files in the conformance suite first and learn from them. I think the format is easy enough that I can create the simple examples needed, but would need you to very the are correct from a business perspective. Then later adding some more complex examples where the problems are nested (e.g. the individual items will pass, but as a group they will fail) I would need help.

Also I plan to do all of this with [unit test](https://www.swi-prolog.org/pldoc/doc_for?object=section(%27packages/plunit.html%27)) so that it can be reproducible.

What if any are the licenses on the conformance suite? ([XBRL-based Digital Financial Reporting Conformance Suite Tests](http://xbrlsite.azurewebsites.net/2019/Prototype/conformance-suite/Production/index.xml))

I don’t want to repackage the data into a test case and find that I have violated some copyright or something.

---

<div class="post-metadata">

**Author:** ![CharlesHoffmanCPA](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/charleshoffmancpa/32/698_2.png) [@CharlesHoffmanCPA](https://swi-prolog.discourse.group/u/CharlesHoffmanCPA)\
**Post date:** [November 12, 2019, 3:57pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/35 "2019-11-12T15:57:47Z")

</div>

One more important piece of information. One of the issues with XBRL is that it does not do a particularly good job of communicating the logical model of a business report or of a financial report. So help solve that problem, the Object Management Group (OMG) is creating a global standard called Standard Business Report Model (SBRM) which is a logical conceptualization of a business report. Basically, what they are doing is formalizing the somewhat informal model that I have been creating for the past 5 years. For more information search my [blog](http://xbrl.squarespace.com/) for Object Management Group SBRM, (I cannot seem to post a link).

Also, when you think about artificial intelligence and how to actually implement it and what people are saying about “The Fourth Industrial Revolution”; global standard technical syntax’s that provide the “information” and tools like Prolog that help you USE that information make a whole lot of sense. Search my blog for Adapting to Changes Caused by the Fourth Industrial Revolution (I cannot seem to post links to my blog)

Personally, I think Prolog and likely the more safe Datalog (as I understand it) will play a big role in accounting, reporting, auditing, and analysis in next 100 years. Very, very big opportunity.

(To get to my blog search on “XBRL Squarespace”)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 12, 2019, 4:05pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/36 "2019-11-12T16:05:30Z")

</div>

> [@CharlesHoffmanCPA](#):
>
> For more information search my [blog](http://xbrl.squarespace.com/) for Object Management Group SBRM, (I cannot seem to post a link).

Is this it?

[Standard Business Report Model](https://www.omgwiki.org/SBRM/doku.php)

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [November 12, 2019, 4:09pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/37 "2019-11-12T16:09:05Z")

</div>

> [@CharlesHoffmanCPA](#):
>
> when you think about artificial intelligence

When I think about AI, at the high level I break it down into two sets,

[open-world](https://en.wikipedia.org/wiki/Open-world_assumption)  
[closed-world](https://en.wikipedia.org/wiki/Closed-world_assumption)

Prolog is great for closed-world, but I shy away from it for open-world.  
Neural Networks are great at open-world.

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [November 12, 2019, 4:14pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/38 "2019-11-12T16:14:50Z")

</div>

> [@CharlesHoffmanCPA](#):
>
> more safe Datalog

Datalog has better termination properties while its performance is less sensitives to e.g., ordering than standard Prolog due to bottom-up evaluation. Prolog with tabling (SLG resolution) has similar properties while providing a richer language. Performance and scalability of Datalog is probably better in the domains where it shines.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [November 12, 2019, 4:47pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/39 "2019-11-12T16:47:33Z")

</div>

I don’t think that there’s anything inherently bottom-up in Datalog other than it allows bottom-up implementation.

Google’s Yedalog is a Datalog whose syntax might be nicer for people who aren’t used to Prolog, and which transparently layers on top of databases, distributed file systems, mapreduce, etc.  
[https://ai.google/research/pubs/pub43462](https://ai.google/research/pubs/pub43462)

I’ve implemented accounting systems (not using Prolog) and can assure you that Prolog (or Datalog) would be a fine way of adding business rules to databases. But I am unaware of non-experimental implementations. (Yedalog, AFAIK, is (a) experimental and (b) not publicly available.)

As an aside: many databases use the accounting profession’s “never erase a journal entry” methodology to reduce lock contention (in effect, allowing “shared nothing” parallelism) – although I’m pretty sure the idea was discovered independently.

---

<div class="post-metadata">

**Author:** ![CharlesHoffmanCPA](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/charleshoffmancpa/32/698_2.png) [@CharlesHoffmanCPA](https://swi-prolog.discourse.group/u/CharlesHoffmanCPA)\
**Post date:** [November 12, 2019, 5:12pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/40 "2019-11-12T17:12:23Z")

</div>

Yes, that is the OMG web site for the Standard Business Report Model (SBRM). You might want to consider reading the 2 page summary of what SBRM is and the RDF section 6 which describes what SBRM needs to do.

---

<div class="post-metadata">

**Author:** ![CharlesHoffmanCPA](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/charleshoffmancpa/32/698_2.png) [@CharlesHoffmanCPA](https://swi-prolog.discourse.group/u/CharlesHoffmanCPA)\
**Post date:** [November 12, 2019, 5:13pm UTC](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465/41 "2019-11-12T17:13:30Z")

</div>

Yes, XBRL-based reporting makes the closed-world assumption.

[Previous page](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465.md?page=1)

[Next page](https://swi-prolog.discourse.group/t/represent-accounting-equation-using-swi-prolog/1465.md?page=3)
