
	Memo on Using PAC Package with Emacs  [2017/05/26]

	Kuniaki Mukai (kuniaki.mukai@gmail.com)


* The current version of PAC is v1.1.9. It works on  Mac OS 10.12 (Sierra)
with Gnu Emacs 26.0 and SWI-Prolog (Multi-threaded, 64 bits,
Version 7.5.6).

* Install the PAC package.

    ?- pack_remove(pac).
    ?- pack_install('http://web.sfc.keio.ac.jp/~mukai/pac-1.1.9.tgz').
    ?- use_module(library(pac)).

* The package is installed at ~/lib/swipl/pack/pac, by default.
  Relevant ubdirectories and contents of this memo are
  under ~/lib/swipl/pack/pac:

	prolog/pac.pl
	prolog/pac/start-pac.pl
	prolog/pac/start-eh.pl
	prolog/pac/start-cgi.pl
	elisp/prolog-process.el
	shell/dump-pac
	shell/dump-eh
	shell/dump-cgi
	config/pac-config.pl	(to be placed at ~/.pac-config.pl )
	document/setup-pac-emacs	(this document)
	javascript/http_request.jp
	etc/emacshandler
	etc/swipl
	etc/pac7-exercise.cgi

	The last three files etc/emacshandler, etc/swipl, and
etc/pac7-exercise.cgi don't exist on installing time. They may be
created as qsaved programs by the user afterward, as explained
below.

* Edit a config file.

Copy the template file config/DOT-pac-config.pl to ~/.pac-config.pl, and
customize it.

* Emacs setting.
	insert the following line into  init.el file.

	(load "~/lib/swipl/pack/pac/elisp/prolog-process.el")

* elisp/prolog-process.el defines
	basic elisp functions, which are necessary to commnucate prolog
	process.  Also some useful shortcuts for them are defined.

* Shell scripts to compile PAC library:
- shell/dump-pac compiles pac/start-pac.pl
	to  etc/swipl for use with edprolog mode.
- shell/dump-eh	compiles pac/start-eh.pl
	to etc/emacshandler, which is restored to edit emacs buffers with
	the PAC library.
- shell/dump-cgi compiles pac/start-cgi.pl
	to etc/pac7-exercise.cgi for Ajax based"CGI in Prolog"

* Shortcuts for PAC are defined in elisp/prolog-process:
-  (kbd "s-p")		restores the qsaved state etc/emacshandler.
-  (kbd "M-s-π")	recompiles the pac/start-eh.pl and restores
					the qsaved state etc/emacshandler
-  (kbd "C-c q")	starts the etc/swipl process.
-  (kbd "C-c q")	recompile the pac/start-pac.pl and
					restart the etc/swipl process.
-  (kbd "C-c t")	consult pac/start-pac.pl

* handle rules
	handle/3 is a predicate for editing emacs buffer.
	It is defined as DCG rules in prolog/pac/emacs-jockey.pl.
	It has many handle rules, which are  easy to be read,
	modified,  and  extended.

	handle/3 has two types of first arguments.
		type 1	handle([x1, ..., xk])   --> ....
		type 2	handle([y1, ..., yj|r]) --> ....

	where x1, ..., xk, y1,...,yj are prolog
	atoms or variables, and r a prolog variable.

* Using handle rules from Emacs buffers.
	The user can select a  handle rule
	by typing words in the minibuffer.

	For example, typing

		compile pac region

	activates the handle([compile, pac, region]) rule, provided that
	a region is selected in  which prolog clauses are with PAC macros.
	Moreover, the following like abbreviation also selects the same
	handle rule, as explained later precisely.

		com  pa re

* A sample use of handle rule.
	hit "C-l" (control l), then a prompt appears in the minibuffer:

		handle:

	type "help" after the "C-l" prompt in the minibuffer like this:

		handle:  help

	Then it displays all the first argument of handle/3 rules
	which is defined in pac/emacs-jockey.pl with handle([help]).

* Selection of a handle rule for query.
	Suppose a query in the minibuffer like this.

		handle: w1 w2 ... wn

	where w1 w2 ... wn are Prolog atoms.  Then a handle rule H is
	selected for the query [w1,...,wn], and H is applied to the
	current region, where H is the first DCG rule such that the list
	[w1, w2, ..., wn] matches the first argument of the clause H.
	The notion "match" is explained later.

* Examples of matches:

	[a, b, c] matches [aa, bb, cc].
	[a, b, c] matches [aa, B, cc]  with B = b.
	[a, bb, c, d] matches [aa, X|Y]  with X = bb and Y=[c, d].

* Precise definition of the match in Prolog:
	A list L matches a list  M if match(L, M) is true, where

    match(X, Y) :- var(Y), X=Y.
    match([A|B],[C|D]) :- atom(C),
    	sub_atom(C, 0, _, _, A),
    	match(B, D).
    match([A|B],[C|D]) :- var(C), A=C,
    	match(B, D).
