THE GAME:

Mahjongg (also known as Taipei) is a game consisting of a number of square
tiles.  Tiles are distinguished in types and there are several tiles of each
type (preferably in even number).  At the beginning of the game, the tiles
are arranged according to some initial configuration.

A tile is covered if there is another tile on top of it, hiding it in
totality or in part.  Two tiles are adjacent if their left or right side
are in contact.  A tile is free if it is not covered and there is no adjacent
tile either on its left or on its right.

A move in the game consists in identifying two free tiles of the same type
and removing this pair from the configuration.  The game is won if it is
possible to match in this manner all tiles.

ENCODING:

Each tile is represented by the term
      (tile Type Instance)
where Type denotes the type of the tile, and Instance permits to distinguish
different occurrences of this tile.  Type and Instance are user-defined object 
of type tp and inst respectively.

Tile T being present in the current configuration is represented by the
formula
      free T
if T has been removed, we use the formula
      out T

The board is represented by one linear clause for each tile.  The clause
represents the constraints on the freedom of the tile.  If tile T is covered
by tiles T_top_1, ..., T_top_n (n = 4, at most), has T_left_1, ..., T_left_l
as its left neighbors and T_right_1, ..., T_right_r as its right neighbors
(r and l are at most 2), the constraints on T are represented by the clause
      name ^   (free T  <-  out T_top_1   <-  ...  <-  out T_top_n
                        <-  out T_left_1  <-  ...  <-  out T_left_l)
             & (free T  <-  out T_top_1   <-  ...  <-  out T_top_n
                        <-  out T_right_1 <-  ...  <-  out T_right_r)
we simplify this expression when some of the neighboring tiles are not
present in the initial configuration.

Removing a tile T from the board is achieved by solving the goal
      free T
and asserting in the intuitionistic context
      out T
This fact needs to be intuitionistic since T might constrain more than one
other tile (and possibly zero).
The goal
      free T
succeeds only if the corresponding constraint is solvable, i.e. if one of
the two additive conjuncts succeeds.  This happens only if all the "out"
statements in one of these it have been asserted in precedence.

A tile is present on the board only if its constraint is in the linear
context.  When the tile is removed, the constraint itself is consumed.

ORIGINAL GAME

In its original presentation, mahjongg consists of 144 tiles, 36 types with
4 instances each.  The standard initial configuration is represented below.
It has 5 layers.  The overlapping points are shaded.  Numerous variations
on the shape of the initial configuration typically are offered.

    +---+---+---+---+---+---+---+---+---+---+---+---+       Level 1 (lowest)
    |   |   |   |   |   |   |   |   |   |   |   |   |
    |   |   |   |   |   |   |   |   |   |   |   |   |
    +---+---+---+---+---+---+---+---+---+---+---+---+
            |   |...|...|...|...|...|...|   |
            |   |...|...|...|...|...|...|   |
        +---+---+---+---+---+---+---+---+---+---+
        |   |   |...|...|...|...|...|...|   |   |
        |   |   |...|...|...|...|...|...|   |   |
    +---+---+---+---+---+---+---+---+---+---+---+---+
    |   |   |   |...|...|...|...|...|...|   |   |   |
+---+   |   |   |...|...|...|...|...|...|   |   |   +---+---+
|   +---+---+---+---+---+---+---+---+---+---+---+---+   |   |
+---+   |   |   |...|...|...|...|...|...|   |   |   +---+---+
    |   |   |   |...|...|...|...|...|...|   |   |   |
    +---+---+---+---+---+---+---+---+---+---+---+---+
        |   |   |...|...|...|...|...|...|   |   |
        |   |   |...|...|...|...|...|...|   |   |
        +---+---+---+---+---+---+---+---+---+---+
            |   |...|...|...|...|...|...|   |
            |   |...|...|...|...|...|...|   |
    +---+---+---+---+---+---+---+---+---+---+---+---+
    |   |   |   |   |   |   |   |   |   |   |   |   |
    |   |   |   |   |   |   |   |   |   |   |   |   |
    +---+---+---+---+---+---+---+---+---+---+---+---+

                +---+---+---+---+---+---+                   Level 2
                |   |   |   |   |   |   |
                |   |   |   |   |   |   |
                +---+---+---+---+---+---+
                |   |...|...|...|...|   |
                |   |...|...|...|...|   |
                +---+---+---+---+---+---+
                |   |...|...|...|...|   |
                |   |...|...|...|...|   |
                +---+---+---+---+---+---+
                |   |...|...|...|...|   |
                |   |...|...|...|...|   |
                +---+---+---+---+---+---+
                |   |...|...|...|...|   |
                |   |...|...|...|...|   |
                +---+---+---+---+---+---+
                |   |   |   |   |   |   |
                |   |   |   |   |   |   |
                +---+---+---+---+---+---+

                    +---+---+---+---+                       Level 3
                    |   |   |   |   |
                    |   |   |   |   |
                    +---+---+---+---+
                    |   |...|...|   |
                    |   |...|...|   |
                    +---+---+---+---+
                    |   |...|...|   |
                    |   |...|...|   |
                    +---+---+---+---+
                    |   |   |   |   |
                    |   |   |   |   |
                    +---+---+---+---+

                        +---+---+                           Level 4
                        |   |   |
                        |  .|.  |
                        +---+---+
                        |  .|.  |
                        |   |   |
                        +---+---+

                          +---+                             Level 5 (highest)
                          |   |
                          |   |
                          +---+

There are the following types of tiles:
- plants (pl)
- seasons (ss)
- squares (sq)
- chinese letters F C N S E W (cF, cC, cN, cS, cE, cW)
- coins 1 to 9 (y1, y2, y3, y4, y5, y6, y7, y8, y9)
- birds (bi)
- sticks 2 to 9 (s2, s3, s4, s5, s6, s7, s8, s9)
- chinese numbers 1 to 9 (c1, c2, c3, c4, c5, c6, c7, c8, c9)


PRAGMATICS

The size of the search space of an instance of the original Mahjongg game
makes LLF search unfeasible (in any reasonable amount of time).  We consider a 
simplified instance (and an interpretation of it) in the file "socks.llf".
