CoffeeScript+Prolog+some Haskell A Logic Programming Language
CoffeeLog logic programming language with declarative and logic programming elements. It based on CoffeeScript and Prolog. Easily describe it is a preprocessor for CoffeeScript. Some ideas borrowed form Haskell and GOAP. Now it is only proof of concept language.
Here is a first easy sample. Function definition in Haskell style.
fib 0 := 0
fib 1 := 1
fib n when n<0 := console.error "fib(n): n should be a positive number"
fib n := fib(n-1) + fib(n-2)
fib string(n) := console.error "fib(n): n should be a positive number, not string"
fib args... := if args.length > 1 then console.error "fib(n): oly one argument allowed"
Allowed types:
- number(x)
- string(x)
- object(x)
- function(x)
- array(x)
- Animal(x) # any class name
add number(x),number(y) := x + y
sum args… := ret += (a for a in args)
sum3 [a,b,c] := a+b+c
head [x,y...] := x
tail [x,y...] := y
equalHeads [x1,y1...],[x2,y2...] := x1 is x2
mySum x,_,y,_,z = x+y-z
someFun [_,args...] = args.length
myAbs x when x >= 0 := x
myAbs x when x < 0 := -x
or you can use functions for guard expressions:
myAbs 0 := 0
myAbs x when isPositive(x) := x
myAbs x when isNegative(x) := -x
isPositive = (x) -> x > 0
isNegative = (x) -> x < 0
more complex samples:
mySum2 x,y := x+y
mySum3 args... when args.length is 3 := args[0] + args[1] + args[2]
You can define facts the same way Like in Prolog.
animal(zebra).
color(zebra, white).
color(bear,brown) {comment: "Regular color of forest bear"}.
color(bear,white).
comment: "Regular color of polar bear"
prob: 0.5
Again you can define rules like in Prolog:
man(sokrat).
mortal(X) :- man(X).
The following three lines are equivalent:
console.log solve(mortal(sokrat))
console.log solve(mortal(X))
> ["sokrat"]
console.log solve(mortal(X),CFL_FIRST)
> "sokrat"
man(sokrat) {priority: 200}.
man(plato) {priority: 300}.
mortal(X) :- man(x).
console.log solve(mortal(X))
> ["sokrat", "plato"]
console.log solve(mortal(X),CFL_PRIORITY)
> ["plato", "sokrat"]
-: At(A),Level(Low),BoxAt(C),BananasAt(B).
Move(x,y) :- At(x),Level(Low) -: At(y).
ClimbUp(location) :- At(location), BoxAt(location), Level(low) -: Level(high).
ClimbDown(location) :- At(location), BoxAt(location), Level(High) -: Level(Low).
MoveBox(x,y) :- At(x), BoxAt(x), Level(Low) -: BoxAt(y), At(y).
TakeBananas(location) :- At(location), BananasAt(location), Level(High) -: Have(Bananas).
strategy = goal Have(Banana)
console.log strategy.plan
> [Move(A,B),ClimbUp(C),TakeBananas(C)]
make strategy
Traffic :- Accident {prob:0.01}.
Traffic :- President {prob:0.1}.
console.log probability(Traffic :- Accident)
> 0.14
A:-B.
B:-C.
console.log path C,A.
> [C,B,A]
It just idea.