Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6906927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:22:21+00:00 2026-05-27T08:22:21+00:00

I am starting to learn restrictions in prolog at the moment using SICStus Prolog.

  • 0

I am starting to learn restrictions in prolog at the moment using SICStus Prolog. Though I know how solve simpe problems using this, I have one exercise where I must solve a Jigsaw puzzle. However I have no idea how to solve this since I will have several different elements with different proprieties (pieces) , can anyone give me an example of how to make represent a list of piece in prolog and what kind of restrictions should I use?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:22:21+00:00Added an answer on May 27, 2026 at 8:22 am

    For a first try, I would do something along these lines:

    For each field, use 5 variables. The first variable denotes the puzzle piece that goes into the field. Each piece has its own id. In the case of the puzzle that you mentioned in your comment above there are 12 pieces, so each variable would have a domain of {1..12}:

    P #:: 1..12,
    

    The other four variables denote the four edges of each field, and whether the puzzle piece placed in the field has a dent or tab there. In your puzzle, each “up” or “down” side has a tab and a dent, so you denote whether the tab is left or right. Again, boolean decision variables.

    [EL,EU,ER,ED] #:: 0..1,
    

    A puzzle piece can the be encoded like this:

    piece(1,  [](0,1,1,0)).
    

    This for instance is piece A in the description of your puzzle on the website that you gave in the comment.

    Now you can define the neighbour relation between two adjacent fields. Since you have boolean variables, and a tab needs to meet a dent, you set a constraint (not “restriction”) requiring that the sum be one:

    R1 + L2 #= 1,
    

    I.e., the right edge of the piece in field one must match the left edge of the piece in field 2.

    You also set similar constraints for the edges along the borders.

    The you set a constraint requiring that all fields must contain different pieces:

    alldifferent(Fields),
    

    (where Fields is the list containing the P variables)

    You need a variable that denotes the orientation of the puzzle pieces:

    O #:: 0..1,
    

    You need just one, since in your puzzle, all pieces must be orientated in the same direction.

    Finally, you need constraints that combine the piece, orientation and edge variables for each field, so that when you select a piece for a field (and the orientation is known) you can set the edge variables appropriately:

     once(piece(N, [](L,U,V,D))),
     ( ((P#=N) #/\ (O#=0)) #=> ((EL#=L) #/\ (EU#=  U) #/\ (ER#=R) #/\ (ED#=  D)) ),
     ( ((P#=N) #/\ (O#=1)) #=> ((EL#=R) #/\ (EU#=1-D) #/\ (ER#=L) #/\ (ED#=1-U)) ),
    

    (the syntax is not for Sicstus, but ECLiPSe. The FD constraint libraries should be similar enough, though)

    Note that I had to invert the encoding for the down edge when turning a piece upside-down. This is allowed me to keep the “sum equals one” constraints for up/down edges, but is sub-optimal, since it prevented me from easily propagating information from the edge variables to the piece variables (a piece may get the same encoding as another when turned upside down). But I was too lazy to change the encoding for this first try.

    (Edit:this should be easy to fix by inverting the encoding for the down edge, e.g.: piece(1, [](0,1,1,1))., and using constraints that require up and down edges of neighbouring pieces to have the same value instead of having sum one.)

    Throwing all together and simply labeling the P variables (after instantiating the orientation variable first) gives the two solutions:

    ?- rapids(S,O).
    S = []([](5, 2, 8, 6), [](11, 10, 1, 4), [](3, 9, 12, 7))
    O = 0
    Yes (0.01s cpu, solution 1, maybe more)
    S = []([](5, 3, 11, 12), [](8, 9, 7, 4), [](2, 10, 6, 1))
    O = 1
    Yes (0.04s cpu, solution 2, maybe more)
    No (0.05s cpu)
    

    I used matrices instead of lists, in order to make the rows of the puzzle field more prominent. Matrices also allow faster access to neighbouring fields in different rows.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Starting to learn Canvas and have two classes so far (main one to call
Just starting to learn Ruby on Rails. I'm using RoR 3. I have read
Just starting to learn ASP.NET (C#) and I am using Visual Studio 2008. I
I'm starting to learn Haskell and I've heard that many Haskell compilers have especially
I am just starting to learn how to use RMI, and I have a
I'm starting to learn OOP in JS and I came across this issue where
I'm just starting to learn AspectJ, and I have use-case for say, User login.
I am starting to learn jQuery. Looking though the MVC3 project that makes use
I'm starting to learn JavaScript, so far no problem but I have a hard
Hi just starting to learn this language, more of a javascript/PHP guy... I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.