swi-prolog

Example how to use predsort(:Compare, +List, -Sorted) in prolog

不打扰是莪最后的温柔 提交于 2019-12-06 04:24:45
问题 I want to order a custom list. The list I want to order will be in this form... [n(_,2,_),n(_,1,_),n(_,3,_)] I have wrote a comparator cheaper(n(_,C1,_),n(_,C2,_)) :- C1>C2. How do I use this with predsort. I wrote a sorting algorithm using bubble sort, but I have very large lists so it very slow. Is it possible to do predsort(cheaper, [n(_,2,_),n(_,1,_),n(_,3,_)] , X). Thank you :) 回答1: Try this : cheaper(>, n(_,C1,_),n(_,C2,_)) :- C1>C2. cheaper(<, n(_,C1,_),n(_,C2,_)) :- C1<C2. cheaper(=,

how to split a sentence in swi-prolog

瘦欲@ 提交于 2019-12-06 03:35:46
问题 I am trying my hands on SWI-Prolog in win xp. I am trying to understand how to split a sentence in Prolog into separate atoms. Ex : Say I have a sentence like this : "this is a string" Is there any way to get individual words to get stored in a variable? like : X = this Y = is .... and so forth. Can anyone please explain how this works? Thanks. 回答1: I would use atomic_list_concat/3. See http://www.swi-prolog.org/pldoc/man?predicate=atomic_list_concat%2F3 Normally it is meant to insert a

How to enable GMP on freebsd?

别来无恙 提交于 2019-12-05 21:50:53
I installed swi-prolog as below: $./configure --prefix=/home/***/swi-prolog/ --enable-gmp $gmake && gmake check && gmake install however, it still reports no GMP syupport: ?- random(33). Warning: This version of SWI-Prolog is not compiled with GMP support. Warning: Floating point random operations are not supported. ERROR: is/2: Arithmetic: `random_float/0' is not a functionenter code here $find /usr/local/lib -name "libgmp*" /usr/local/lib/libgmp.so.10 /usr/local/lib/libgmp.so /usr/local/lib/libgmp.la /usr/local/lib/libgmpxx.so.6 /usr/local/lib/libgmpxx.so /usr/local/lib/libgmpxx.la /usr

Reading a string (from a file) in Prolog

橙三吉。 提交于 2019-12-05 21:13:19
I have written a lexer and a parser in Prolog. It unifies a string with its AST. This is part for a compiler/interpreter project I am working on. Naturally, I now want to read the string from a file to parse it. However, the predicates I have found for this is read , and it only reads Prolog atoms and predicates, like files with hello. I have been twiddling with the double_quotes settings, but with no success. I want to be able to read a file with something like this let id = \x.x in id (S (S Z)) and then send this string to the parsing predicates. You can use read_line_to_codes/2 or read_line

Parsing command line arguments

余生颓废 提交于 2019-12-05 13:06:14
I'm very confused with prolog, it's way different to any language I've ever used (many languages) How do I go about getting argv[0] from: current_prolog_flag(argv, Argv), write(Argv). Now if I tried to type Argv[0] or Argv(0) or Argv<0> it fails.. this leaves me with no clue and very little help from the documentation.. it seems that they expect you to already be a prolog expert :D Another question, how would I assign Argv[0] to a variable so I can print it later using "write" ? Prolog uses matching. ?- current_prolog_flag(argv, [File | Rest]). File = 'C:\\Program Files\\pl\\bin\\swipl-win.exe

How to use JPL (bidirectional Java/Prolog interface) on windows?

谁说胖子不能爱 提交于 2019-12-05 12:04:58
I'm interested in embedding a Prolog interpreter in Java. One option is using JPL , but the download links on the JPL site are broken, and the installation page mentions a jpl.zip that I can't find. I downloaded SWI-Prolog which seems to include JPL (it lists it as a component when installing), but I'm still not sure how I'd use it along with Java. Any ideas on how to use JPL on Windows? Is there another library I could use to achieve the same thing? I've come across a few but they don't seem as stable as JPL. JPL is no longer an additional download, so you don't need jpl.zip. If you download

Optimisation in swi prolog

拈花ヽ惹草 提交于 2019-12-05 10:55:50
Say I want to find argmax(x,y,z) -1/2(20x^2+32xy +16y^2)+2x+2y. subject to: x>=0, y>=0,z>=0 and -x-y+z =0. I know the partial derivatives being set to 0 is : -20x-16y+2=0 and -16x-16y+2 =0 so we could have x= 0 and y =1/8 and z=1/8. How would I do this in Swi-prolog? I see that there is library simplex for linear solving, but this is a quadratic problem but the partial derivatives are not. (I am a bit confused!) This is what I have: :- use_module(library(simplex)). my_constraints(S):- gen_state(S0), constraint([-20*x, -16*y] = 0, S0, S1), constraint([-16*x,-16*y] = 0, S1,S2), constraint([x] >=

catch/3 and call_with_time_limit/2 predicates in SWI-Prolog

吃可爱长大的小学妹 提交于 2019-12-05 03:27:50
I want to use catch(:Goal, +Catcher, :Recover) where Goal is call_with_time_limit(+Time, :Goal) It's messed up and I can't find the right way to know when one of the above happened: 1) Goal stopped because of time out. 2) Goal failed (it's suppose to fail sometimes). I tried: (catch(call_with_time_limit(Time, Goal), Catcher, Recover) -> (ground(Catcher), Catcher = time_limit_exceeded), **TIMEOUT ACTIONS**) ; (**SUCCESS ACTIONS**)) ; **FAILURE ACTIONS** ) * EDIT * Pattern: I use the following pattern: ((catch(call_with_time_limit(6,goal), Exception, true),(var(Exception);Exception=time_limit

Embedding SWI-Prolog in a dll

半城伤御伤魂 提交于 2019-12-04 17:11:05
I'm building a C++ library (windows, DLL) and I'd like to embed swi-prolog for some functionalities. What I'm doing is: #include <Windows.h> #include <SWI-Prolog.h> BOOL WINAPI DllMain( HINSTANCE hinstDLL, // DLL モジュールのハンドル DWORD fdwReason, // 関数を呼び出す理由 LPVOID lpvReserved // 予約済み ) { BOOL result = TRUE; switch(fdwReason) { case DLL_PROCESS_ATTACH: { char* av[]{"libswipl.dll"}; _putenv(R"(SWI_HOME_DIR=C:\Program Files (x86)\swipl\)"); if(!PL_initialise(1, av)) { result = TRUE; } else { PL_halt(1); result = FALSE; } break; } case DLL_PROCESS_DETACH: { result = PL_cleanup(1); break; } case DLL

swi prolog mysql + web

穿精又带淫゛_ 提交于 2019-12-04 16:11:19
Sorry for my English. I want to create a simple website that will take data from the mysql database and display it on the page. I have two problems: 1) work with a database made in separate modules: % database.pl :- module(database, [ create_db_connect/0, use_database/0, query_to_database/1, disconnect_database/0 ]). :- use_module(library(odbc)). create_db_connect :- odbc_connect('test', _, [ user('root'), password('123') alias(myblog), open(once) ]). use_database :- odbc_query(myblog, 'use test', _). query_to_database(X):- odbc_query(myblog, 'SELECT data FROM testtable where id = 4', row(X)).