9

Is there a way to do logic programming (think of Prolog) in Lua?

In particular: is there any Lua module for logic programming (miniKanren implemenatation will be the best, but it isn't strictly required)? Because I couldn't find any [1]. And if not, are there any known (preferably tried) ways how to do logic programming in Lua?

Also: is there anybody who has tried to do something like logic programming in Lua?


[1] So far I've found only blog post mentioning the possibility of writing one in Metalua, but I would rather see one compatible with the standard Lua.

mnicky
  • 1,308
  • 11
  • 24
  • 1
    Isn't all programming logic? You might elaborate a bit for people not familiar with logic programming. I'm sure you can concoct something using metatables. – jpjacobs Feb 18 '12 at 15:09
  • Ok, I've added some hints on Logic programming... You know, I can try something like playing with metatables, but at first I wanted to know whether anybody else has tried to do something like this before me... – mnicky Feb 19 '12 at 15:01
  • Second hit on google for 'Lua "logic programming" library' gives you a pdf about multiple paradigm programming in Lua, where section 5 has some references to prolog. While not a complete library (doesn't seem there is one), it might give you some pointers. – jpjacobs Feb 19 '12 at 18:05
  • Yes, I've seen that before, but thanks :-). That text gives a few points (though nothing substantial), but it really seems there isn't anything more written about logic programming in Lua world yet..... – mnicky Feb 23 '12 at 23:56
  • 2
    You may find this article useful: http://www.fawkesrobotics.org/publications/2010/aaai2010spring-golog-lua/ – Alexander Gladysh May 05 '12 at 04:24
  • @AlexanderGladysh I'll check that. Thanks! – mnicky Sep 03 '12 at 20:48

3 Answers3

1

There is a forward-chaining inference engine in Lua called lua-faces. In addition to MiniKanRen, there are several other logic programming systems in JavaScript that could be automatically translated into Lua using Castl.

I also wrote a translator that converts a subset of Lua into Prolog. Given this input:

function print_each(The_list)
    for _, Item in pairs(The_list) do
        print(Item)
    end
end

it will produce this output in Prolog:

print_each(The_list) :- 
    forall(member(Item,The_list),(

        writeln(Item)
    )).
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
0

Logic programming is a paradigm and thus is just a form of specific syntax where you state some facts and base result on logical equation of those facts, while facts themselves could be results of other equations.

Lua is not specifically designed for this, but you can easily simulate this behavior by defining all logic programming operators as functions - i.e. function and(...) that would return true only if all its arguments true, etc., and making defining your "facts" as a table with lazy evaluation provided by metatable.

Oleg V. Volkov
  • 21,719
  • 4
  • 44
  • 68
0

Would ASP be helpful? https://potassco.org/

Check section 3.1.14 of the manual https://github.com/potassco/guide/releases/download/v2.1.0/guide.pdf

JuanPi
  • 789
  • 1
  • 6
  • 19