test/99.pl @ 72bbdd515725

Rewrite the compiler

A few days ago I found a bug in the compiler that I decided merited basically an
entire rewrite of it.

This was long overdue.  The compiler kind of grew organically and unhealthily
over time as I wrapped my head around how the whole WAM is structured, and now
that I understand a lot more I can do things right.

This new implementation is a lot "flatter" than the old one.  It makes use of
CLOS classes and generic methods to un-nest a lot of the crap that was
previously happening in bigass `labels` blocks.  This is a lot easier to read
and understand because you can take things a piece at a time.

Unfortunately, it's currently a lot slower than the old one.  But at least it's
*correct*, and now I can start taking a look at optimizing the performance with
a cleaner base to start from.

Notes/ideas for the near future:

* Switch to structs instead of CLOS classes for all the bits and bobs in the
  compilation process.
* Inline hot functions in the compilation process.
* Type hint the fucking compiler already.  I've put this off for far too long.
* Move the compiler to its own package for easier profiling and to maintain my
  shreds of sanity.
* Look into that generic-function-inlining library thing I saw on Reddit...
* Remove the last vestiges of `match` and kill the dependency on optima.
author Steve Losh <steve@stevelosh.com>
date Tue, 07 Jun 2016 14:49:20 +0000
parents 5e0cca5174a8
children 4abb7eda96cb
% 99 Prolog Problems
% from http://www.ic.unicamp.br/~meidanis/courses/mc336/2009s2/prolog/problemas/
%
% Solutions to at least a few of these, for testing purposes.

% P01
my_last(X, [X]).
my_last(X, [_ | T]) :-
    my_last(X, T).

% P02
my_lastbutone(X, [X, _]).
my_lastbutone(X, [_ | T]) :-
    my_lastbutone(X, T).

% P03
my_nth(X, [X | _], 1).
my_nth(X, [_ | T], N) :-
    M is N - 1,
    my_nth(X, T, M).

% P04
my_len([], 0).
my_len([_ | T], Length) :-
    my_len(T, M),
    Length is M + 1.

my_len_acc([], A, A).
my_len_acc([_ | T], A, Length) :-
    B is A + 1,
    my_len_acc(T, B, Length).

my_len2(L, Length) :-
    my_len_acc(L, 0, Length).

% P05
my_reverse_acc([], Acc, Acc).
my_reverse_acc([X | T], Acc, Reversed) :-
    my_reverse_acc(T, [X | Acc], Reversed).

my_reverse(L, R) :-
    my_reverse_acc(L, [], R).

% P06
my_palindrome(L) :-
    my_reverse(L, L).

% P07
my_flatten([], []).

my_flatten([Atom | Tail], [Atom | FlatTail]) :-
    \+ is_list(Atom),
    my_flatten(Tail, FlatTail).

my_flatten([HeadList | Tail], Flattened) :-
    is_list(HeadList),
    my_flatten(HeadList, FlatHeadList),
    my_flatten(Tail, FlatTail),
    append(FlatHeadList, FlatTail, Flattened).

% P08
my_compress_acc([], A, A).

my_compress_acc([X | T], [X | Acc], Compressed) :-
    my_compress_acc(T, [X | Acc], Compressed).

my_compress_acc([X | T], [Y | Acc], Compressed) :-
    X \= Y,
    my_compress_acc(T, [X, Y | Acc], Compressed).

my_compress([H | T], Compressed) :-
    my_compress_acc(T, [H], ReverseCompressed),
    my_reverse(ReverseCompressed, Compressed).

their_compress([],[]).
their_compress([X],[X]).
their_compress([X,X|Xs],Zs) :- compress([X|Xs],Zs).
their_compress([X,Y|Ys],[X|Zs]) :- X \= Y, compress([Y|Ys],Zs).

% P09
my_pack([], []).
my_pack([X], [[X]]).

my_pack([X | Tail], [[X] | ResultTail]) :-
    my_pack(Tail, ResultTail),
    ResultTail = [[Y | _] | _],
    X \= Y.

my_pack([X | Tail], [[X, X | XS] | ResultTail]) :-
    my_pack(Tail, [[X | XS] | ResultTail]).

their_pack([],[]).
their_pack([X | Tail], [Chunk | PackedTail]) :-
    their_transfer(X, [X | Tail], Remaining, Chunk),
    their_pack(Remaining, PackedTail).

% transfer(X,Xs,Ys,Z) Ys is the list that remains from the list Xs
%    when all leading copies of X are removed and transfered to Z

their_transfer(_, [], [], []).

their_transfer(X, [Y | Ys], [Y | Ys], []) :-
    X \= Y.

their_transfer(X, [X | Xs], Ys, [X | Zs]) :-
    their_transfer(X, Xs, Ys, Zs).