LPCIC / elpi

Embeddable Lambda Prolog Interpreter
GNU Lesser General Public License v2.1
283 stars 35 forks source link

Elpi doesn't load using #require #28

Open andiejs opened 5 years ago

andiejs commented 5 years ago

We installed version 1.4.0 using Opam 2.0.4 (with Ocaml 4.07.0). Next, we tried #require "elpi" in order to experiment with how it works. But this fails with "Error: Reference to undefined global `Grammar'" Any help in getting this going would be appreciated!

In addition -- are there any simple examples as how this can be used as a prolog interpreter?

gares commented 5 years ago

~I must have made a mistake in the dune/META file~. The META file of camlp5 is explicitly not loading camlp5.gramlib (on which elpi depends) when used from an ocaml REPL.

Workaround:


$ readline-editor -- ocaml `camlp5 -where`/gramlib.cma
        OCaml version 4.07.1

# #use "topfind";;
- : unit = ()
# #require "elpi";; (* works *)

In addition -- are there any simple examples as how this can be used as a prolog interpreter?

Can you be a bit more specific? Does this help?

# let header, _ = Elpi.API.Setup.init ~builtins:Elpi.Builtin.std_builtins ~basedir:"." [];;
# let p = Elpi.API.Parse.program_from_stream (Elpi.API.Ast.Loc.initial "stream") (Stream.of_string "age bob 23. age alice 20.");;
# let g = Elpi.API.Parse.goal (Elpi.API.Ast.Loc.initial "stream") "age P 20.";;
# let obj = Elpi.API.Compile.program ~flags:Elpi.API.Compile.default_flags header [p];;
# let obj = Elpi.API.Compile.query obj g;;
# let exe = Elpi.API.Compile.link obj;;
# let outcome = Elpi.API.Execute.once exe;;
# let Elpi.API.Execute.Success { Elpi.API.Data.assignments } = outcome;;
# Elpi.API.Data.StrMap.show Elpi.API.Pp.term assignments;;
- : string = "{{ P -> alice;  }}"

Of course you can also put the program in a file test.elpi and then execute elpi test.elpi and type age P 20 at the prompt. Here I gave you an example of the most basic use of the API, in case you want to embed elpi into an ocaml application. If this is your use case, then let me just point you to the Elpi.API.Query module and its documentation in the API.mli file. Via that module you can more easily ask questions and get answers as ocaml data types, rather than strings.