%%% -*- prolog -*-

%% "with cons" or "wc" representation for lists:
%%  nil = empty list
%%  cons(E,L) for list starting w/ E and continuing w/ list L
mem_wc(X,cons(X,_)).
mem_wc(X,cons(_,L)) :- mem_wc(X,L).

%%% rewrite "has" using lists & mem

equal(X,X).

has_wc(L) :- equal(L,cons(_,cons(_,cons(_,nil)))),
	mem_wc(1,L), mem_wc(2,L), mem_wc(3,L).

%%% Change names, cons -> ., nil -> []

mem_wd(X,.(X,_)).
mem_wd(X,.(_,L)) :- mem_wd(X,L).

has_wd(L) :- equal(L,.(_,.(_,.(_,[])))),
	mem_wd(1,L), mem_wd(2,L), mem_wd(3,L).

%%% Syntactic Sugar for LISTS:

%% empty list: []
%% list starting w/ E continuing w/ L, uses .(E,L)
%% sugared syntax: [E|L] for .(E,L)
%%                 [x,y,z,a,b,c] for .(x,.(y,.(z,.(a,.(b,.(c,[]))))))
%%                 [x,y,z|L] for .(x,.(y,.(z,L)))
%%  the list [x,y,z|L] has length 3 + length of L.

%% can use predefined member/2 instead of mem_wd/2.

%% can use predefined '='(X,Y) or X=Y instead of our equal(X,Y).

%%% relations vs functions

%%% in math, function y=f(x), think of f as a set, f = {(x0,y0), (x1,y1), ...}
%%% relation between two variables, g(x,y) is a set of values for x & y that
%%% the relationship allows, g = {(x0,y0), (x1,y1), ...}

%%% arithmetic, done with relation: is/2


%%% Peg Puzzle
%%% 1D

%%% represent state of puzzle (configuration of board) by list,
%%% x's for holes w/ peg, o's for empty holes.

%%% jump(B,C) means can get from board B to board C w/ one jump.

jump([x, x, o | R], [o, o, x | R]).
jump([o, x, x | R], [x, o, o | R]).
jump([H|R],[H|S]) :- jump(R,S).

jump2(B1,B3) :- jump(B1,B2), jump(B2,B3).

%% Goals:
%% jump([o,x,x,o],B).
%% jump(B,[o,x,x,o]).
%% jump(B8,[o,o,o,o,o,o,x,o,o,o,o,o,o]), jump(B7,B8), jump(B6,B7), jump(B5,B6), jump(B4,B5), jump(B3,B4), jump(B2,B3), jump(B1,B2), jump(B,B1).
