# Sample queries in functional syntax on toplevel query

**URL:** <https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885>\
**Category:** General\
**Created:** [October 13, 2023, 5:53pm UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885 "2023-10-13T17:53:07Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [October 13, 2023, 5:53pm UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/1 "2023-10-13T17:53:07Z")

</div>

My pack pac was aimed at clojure in Prolog. The latest version is 1.7.4. I have noticed that functional syntax of the pac works also on toplevel query on terminal, though I don’t understand well why it works. Usually I use it while editing on Emacs (ediprolog.el). I hope you could reproduce the sample queries below on terminal. Unfortunately I could post only sample queries for now, without boring explanations.

> **At terminal**
>
> ```prolog
> % swipl
> Welcome to SWI-Prolog (threaded, 64 bits, version 9.1.16-37-ga0900ca3d)
> SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
> Please run ?- license. for legal details.
> 
> For online help and background, visit https://www.swi-prolog.org
> For built-in help, use ?- help(Topic). or ?- apropos(Word).
> 
> ?- use_module(library(pac)).
> true.
> 
> ?- module(pac).
> true.
> 
> pac: ?- append([1,2])@[3,4]@V.
> V = [1, 2, 3, 4].
> 
> pac: ?- append@X@Y@[1,2,3].
> X = [],
> Y = [1, 2, 3] ;
> X = [1],
> Y = [2, 3] ;
> X = [1, 2],
> Y = [3] ;
> X = [1, 2, 3],
> Y = [] ;
> false.
> 
> pac: ?- flip(is)@(1+2)@V.
> V = 3.
> 
> pac: ?- eval@(append@[1,2]@[3,4])@X.
> X = append([1, 2], [3, 4]).
> 
> pac: ?- eval@(:append@[1,2]@[3,4])@X.
> X = [1, 2, 3, 4].
> 
> pac: ?- eval(:append@[1,2]@[3,4], X).
> X = [1, 2, 3, 4].
> 
> pac: ?- call(append@[1,2]@[3,4]@X).
> X = [1, 2, 3, 4].
> 
> pac: ?- call@(append@[1,2]@[3,4]@X).
> X = [1, 2, 3, 4].
> 
> pac: ?- #(:flip@flip(is))@X@(1+2).
> X = 3.
> 
> ```

> **Emacs (ediprolog.el)**
>
> ```prolog
> % ?- append@X@Y@[a,b].
> %@ X = [],
> %@ Y = [a, b] ;
> %@ X = [a],
> %@ Y = [b] ;
> %@ X = [a, b],
> %@ Y = [] ;
> %@ false.
> 
> % ?- (=)@X@2.
> %@ X = 2.
> 
> % ?- =(X)@2.
> %@ X = 2.
> 
> % ?- flip(is)@(1+2+3)@X.
> %@ X = 6.
> 
> % ?- eval(:flip(is)@(1+2+3), V).
> %@ V = 6.
> 
> % ?- eval@(:flip(is)@(1+2+3))@V.
> %@ V = 6.
> 
> % ?- #(:flip@(:flip@(flip(is))))@(1+2+3)@F.
> %@ F = 6.
> 
> % ?- #(:flip@(flip(is)))@V@(1+2+3).
> %@ V = 6.
> 
> % ?- #(:flip@(:flip@is))@V@(1+2+3).
> %@ V = 6.
> 
> % ?- call(flip(is)@(1+2+3)@V).
> %@ V = 6.
> 
> % ?- call@(flip(is)@(1+2+3)@V).
> %@ V = 6.
> 
> % ?- flip(is)@(1+2+3)@V.
> 
> % ?- call(flip(is), (1+2+3), V).
> %@ V = 6.
> 
> % ?- call(flip(is) @ (1+2+3), V).
> %@ V = 6.
> 
> % ?- #(call @ flip(is) @ (1+2+3)) @ V.
> %@ V = 6.
> 
> % ?- permutation([1,2,3], P), show(flip(P, append)), fail.
> %@ pac:pac#54, where
> %@ pac:pac#54(A,B,C):-pac:append(A,B,C)
> %@ pac:pac#55, where
> %@ pac:pac#55(A,B,C):-pac:append(A,C,B)
> %@ pac:pac#56, where
> %@ pac:pac#56(A,B,C):-pac:append(B,A,C)
> %@ pac:pac#57, where
> %@ pac:pac#57(A,B,C):-pac:append(B,C,A)
> %@ pac:pac#58, where
> %@ pac:pac#58(A,B,C):-pac:append(C,A,B)
> %@ pac:pac#59, where
> %@ pac:pac#59(A,B,C):-pac:append(C,B,A)
> %@ false.
> 
> ```

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [October 14, 2023, 10:02am UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/2 "2023-10-14T10:02:38Z")

</div>

I’m sorry to say that something must be missing in [pac](https://www.swi-prolog.org/pack/list?p=pac).  
On a (relatively) clean Windows machine, after a successul

```prolog
:- pack_install(pac).

```

I get a broken system if I attempt to

```prolog
:- use_module(library(pac)).

```

> **errors...**
>
> ````prolog
> ?- use_module(library(pac)).
> ERROR: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:42:
> ERROR: source_sink `'/users/a_user_name/.config/cgi-config'' does not exist
> Warning: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:42:
> Warning: Goal (directive) failed: '$pac$':(prolog_load_context(directory,_2198),setup_root_home_user(_2198),setup_cgi_config)
> ERROR: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:80:
> ERROR: source_sink `pac('expand-pac')' does not exist
> Warning: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:80:
> Warning: Goal (directive) failed: '$pac$':use_module(pac('expand-pac'))
> ERROR: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:81:
> ERROR: source_sink `util('env-dict')' does not exist
> Warning: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:81:
> Warning: Goal (directive) failed: '$pac$':use_module(util('env-dict'))
> ERROR: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:82:
> ERROR: source_sink `util(file)' does not exist
> Warning: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:82:
> Warning: Goal (directive) failed: '$pac$':use_module(util(file))
> true.```
> 
> ````

After that, I must quit, since the REPL gets broken:

```prolog
?- ls.
ERROR: expand_query/4: Unknown procedure: pac:expand_query/3
ERROR: However, there are definitions for:
ERROR: expand_query/4

```

Maybe you should set the appropriate search paths for your pack, or are the source where `cgi_eqns(Eqns)` etc are defined really lost ?

---

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [October 14, 2023, 11:34am UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/3 "2023-10-14T11:34:20Z")

</div>

It seems that I have uploaded a wrong version, which is for prolog cgi in some host machine. I will check soon later.  
If my guess is the case, fixing may be easy. Otherwise, it may be serious.

Thanks for reporting, and I am sorry for your taking time for this trouble.

---

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [October 14, 2023, 2:54pm UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/4 "2023-10-14T14:54:36Z")

</div>

**Edit**  
I have found another cofiguration mistake. Sorry.

I have updated the pack pac to 1.7.7, with hope it works.  
If my guess is correct, pac 1.7.5 also works if you put  
a dummy file ~/.config/cgi-config.pl  
with this two lines.

```prolog
:- module(config, [cgi_eqns/1]).
cgi_eqns([]).

```

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [October 14, 2023, 3:15pm UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/5 "2023-10-14T15:15:09Z")

</div>

Alas, it’s not working.

Seems the problem is not more related to cgi-config.pl, but your suggestion about `~/.config/cgi-config.pl` make me think that you could have some implicit dependencies on \*nix folders structure. Anyway, I created the ~/.config/cgi-config.pl file with the content, but the errors didn’t changed.

> **errors...**
>
> ```prolog
> ?- pack_list_installed.
> Installed packages (2):
> 
> i pac@1.7.7 - Anonymous predicates expansion utility
> i scasp@0.10.0 - Goal directed ASP solver
> true.
> 
> ?- use_module(library(pac)).
> ERROR: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:45:
> ERROR: setup_cgi_config/0: Unknown procedure: '$pac$':file_exits/1
> Warning: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:45:
> Warning: Goal (directive) failed: '$pac$':(prolog_load_context(directory,_47548),setup_root_home_user(_47548),setup_cgi_config)
> ERROR: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:83:
> ERROR: source_sink `pac('expand-pac')' does not exist
> Warning: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:83:
> Warning: Goal (directive) failed: '$pac$':use_module(pac('expand-pac'))
> ERROR: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:84:
> ERROR: source_sink `util('env-dict')' does not exist
> Warning: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:84:
> Warning: Goal (directive) failed: '$pac$':use_module(util('env-dict'))
> ERROR: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:85:
> ERROR: source_sink `util(file)' does not exist
> Warning: c:/users/a_user_name/appdata/local/swi-prolog/pack/pac/prolog/pac.pl:85:
> Warning: Goal (directive) failed: '$pac$':use_module(util(file))
> true.
> 
> ```

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [October 14, 2023, 3:17pm UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/6 "2023-10-14T15:17:50Z")

</div>

```prolog
...
ERROR: setup_cgi_config/0: Unknown procedure: '$pac$':file_exits/1
...

```

`file_exits` should be 'file\_exist` Is this a typo ?

---

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [October 14, 2023, 3:27pm UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/7 "2023-10-14T15:27:49Z")

</div>

Yes. I have amended.  
How many mistakes I am making !  
Thanks.

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [October 14, 2023, 3:54pm UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/8 "2023-10-14T15:54:16Z")

</div>

I changed `file_exits` to `exists_file/1` and it’s working…  
So, now I’m just wondering what pac:flip is about 🙂 and how to cleanly quit the pac: prompt

---

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [October 14, 2023, 3:59pm UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/9 "2023-10-14T15:59:29Z")

</div>

> [@CapelliC](#):
>
> but your suggestion about `~/.config/cgi-config.pl` make me think that you could have some implicit dependencies on \*nix folders structure.

Yes. In fact, assuming the “nix” folders structure, file\_search\_path  
were generated from some module prefix skeletal tree with its root directory and the home directory, which could not work for non “nix” folders structure. I have no idea  
for non “nix” folders structure.

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [October 14, 2023, 4:01pm UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/10 "2023-10-14T16:01:04Z")

</div>

> [@kuniaki.mukai](#):
>
> I have no idea  
> for non “nix” folders structure.

Don’t worry, it works in Windows as well

---

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [October 14, 2023, 4:33pm UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/11 "2023-10-14T16:33:54Z")

</div>

A motivation of this post was my “rediscovery” that four primitives  
call, eval(:), apply(@), and arguments scoping( #) works in harmony with  
the prolog term structure for functional syntax in Prolog, and no other  
primitives (pragma ?) seem necessary as far as my experiences concerned, which is, though, limited of course.

---

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [October 14, 2023, 8:51pm UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/12 "2023-10-14T20:51:29Z")

</div>

> [@CapelliC](#):
>
> So, now I’m just wondering what pac:flip is about

If remember correctly, once flip/1 was built-in in some Prolog system to swap arguments like this.

```prolog
?- flip((1+2+3) is X ).
X = 6.

```

As I could not find such flip/1 builtin in swipl, I wrote it for my DCG form based snippet handler for example. The flip works for not only binary predicate call but also  
for ones of any arity:

```
flip([j1, ..., jn], g(a1, ..., an))

```

is equivalent to

```
g(b1, ..., bn),

```

where bk = a(jk). For example, flip([2,1], is(1+2, X)) runs as X is 1+2. (n = 2)

I am not sure that the following is relevant to your second question, but  
it might be better that @-operator should be declared in the user module  
for writing queries in the functional syntax. I thought that user defined operators in a package should not be declared in the user module to avoid conflicts  
against other packages, though I am not sure on this.

````prolog
?- context_module(X).
X = user.

?- is @X @(1+2).
ERROR: Syntax error: Operator expected
ERROR: is
ERROR: **here**
ERROR: @X @(1+2) . 
?- @(@(is, X), 1+2).
X = 3.

?- pac:current_op(X, Y, @).
X = 10,
Y = fy ;
X = 200,
Y = yfx.

?- pac:current_op(X, yfx, @), user:op(X, yfx, @).
X = 200.

?- is @X @(1+2).
X = 3.

```
````

---

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [December 22, 2023, 10:57pm UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/14 "2023-12-22T22:57:44Z")

</div>

> [@anon95304481](#):
>
> Again same problem?

Perhaps again same problem. I thought I had changed so that  
it also works without `~/.config/cgi-config` file. Thanks for reporting.  
I will fix and update to pac-1.8.6 with other bug fix and new stuff on ZDD.

---

<div class="post-metadata">

**Author:** ![kuniaki.mukai](https://avatars.discourse-cdn.com/v4/letter/k/c67d28/32.png) [@kuniaki.mukai](https://swi-prolog.discourse.group/u/kuniaki.mukai)\
**Post date:** [December 23, 2023, 6:34am UTC](https://swi-prolog.discourse.group/t/sample-queries-in-functional-syntax-on-toplevel-query/6885/16 "2023-12-23T06:34:16Z")

</div>

If you have not a file `~/.config/cgi-config.pl` then  
replace the whole content of the pac.pl with the following lines.  
I hope this fix the trouble.

> **Replace pac.pl by this**
>
> ```prolog
> :- module('$pac$', []).
> :- user:set_prolog_flag(encoding, utf8).
> :- user:set_prolog_flag(optimise, true).
> :- user:set_prolog_flag(qcompile, auto).
> :- user:set_prolog_flag(editor, emacsclient).
> :- user:set_prolog_flag(history, 0).
> :- setenv('EDITOR', emacsclient).
> 
> :- use_module(library(macros)).
> 
> #define(local_cgi_config, '/.config/cgi-config').
> 
> :- dynamic user:file_search_path/2.
> :- multifile user:file_search_path/2.
> 
> :- use_module('setup-aux').
> 
> pac_directories(
> [	(pac_root : []) -
> [	test:test,
> :(prolog) -
> [	pac : pac,
> util	: util,
> zdd : zdd,
> tmp : tmp,
> gb : "util/grobner",
> other	: other
> ]]]
> ).
> 
> setup_root_home_user(Dir):-
> split_string(Dir, /, '', [_, Root, User|_]),
> atomic_list_concat([/,Root,/,User], Home),
> setenv(home, Home),
> setenv(user, User),
> setenv(pac_root, Dir).
> 
> setup_cgi_config:- getenv(home, H),
> atom_concat(H, #local_cgi_config, Mod),
> atom_concat(Mod, ".pl", Modpl),
> (	exists_file(Modpl) ->
> use_module(Mod),
> cgi_eqns(Eqns),
> maplist(setup_env, Eqns)
> ;	true
> ),
> pac_directories(DirStr),
> mk_file_search_path(DirStr).
> 
> :- prolog_load_context(directory, Dir),
> setup_root_home_user(Dir),
> setup_cgi_config.
> 
> % ?- getenv(user, U), getenv(home, H), getenv(host_user, HU).
> % :- pac_directories(DirStr),
> % mk_file_search_path(DirStr).
> 
> % :- set_prolog_flag(color_term, false).
> % :- set_stream(user_input, tty(true)).
> % :- set_prolog_flag(tty_control, false).
> 
> user: enable_pac_query	:- set_prolog_flag(pac, true).
> user: disable_pac_query	:- set_prolog_flag(pac, false).
> user: chk_pac_query	:- current_prolog_flag(pac, true).
> 
> user: enable_odict	:- set_prolog_flag(odict, true).
> user: disable_odict	:- set_prolog_flag(odict, false).
> user: chk_odict :- current_prolog_flag(odict, true).
> 
> :- use_module([
> library(clpfd),
> library(dif),
> library(lists),
> library(sort),
> library(ordsets),
> library(url),
> library(ugraphs),
> library(readutil),
> library(apply),
> library(apply_macros)
> ]).
> %
> user: expand_query(X, Y, Z, Z) :- user:chk_pac_query, !,
> '$current_typein_module'(C),
> pac:expand_query(C, X, Y).
> user: expand_query(X, X, Z, Z).
> 
> :- use_module(pac('expand-pac')).
> :- use_module(util('env-dict')).
> :- use_module(util(file)).
> 
> :- user:enable_pac_query.
> :- user:enable_odict.
> 
> ```
