COMP24412 Prolog 分析

Academic Session: 2021-22
Lab 1: Prolog
Assessed exercises
Joe and Giles
Submission is via git for this lab - see the section at the end of this document about submission.
You should use your comp24412 repository (branch lab1) as a starting point. Please let me know if
running git checkout lab1 doesn’t work. You should see a file called
Learning Objectives
At the end of this lab you should be able to:

  1. Use the Prolog interactive mode to load and query Prolog programs
  2. Model simple constraint solving problems in Prolog
  3. Define recursive relations in Prolog
    Additionally, if you complete the formative exercises you should be able to:
  4. Explain issues surrounding termination in Prolog
  5. Use an accumulator approach to detect cycles in reasoning
    Part 0: Getting Started
    There are no mark available for the tasks in Part 0, but you will probably find them helpful before
    starting the assessed exercises. In fact, it is probably best to work through a few of the formative
    exercises, described in the other document provided, even though they carry no marks.
    Running Prolog (read this)
    Open up a terminal and run swipl. You have just started the SWI-Prolog interactive mode, it should
    look a bit like this.
    Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.7.11)
    Copyright (c) 1990-2009 University of Amsterdam.
    SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
    and you are welcome to redistribute it under certain conditions.
    Please visit for details.
    For help, use ?- help(Topic). or ?- apropos(Word).
    The ?- is the prompt where we can type things. To get out of interactive mode you should
    type halt. (the . is necessary).
    The expected usage is that your Prolog program (knowledge base) is stored in a file and
    you load this into the interactive mode and query it. For the warmup exercises we suggest
    you create a file (open it in gedit or similar) and run swipl in the same directory. Alternatively,
    we can add queries directly to a program and run this from the command line using the pl
    command but we assume that you are using the interactive mode.
    Warning. Not all Prolog implementations are the same. We are using SWI-Prolog in this course
    because it is what is on the teaching machines. They also have Sictus Prolog if you prefer to use that.
    To load your file into interactive mode type
  6. ?- [warmup].
    The true response tells you that the file was loaded correctly. You can now type queries directly in
    the interactive mode. Add the line loves(mouse,cheese) and loves(monkey,banana) to
    and run the following query
  7. ?- loves(X,Y).
    X = mouse,
    Y = cheese
    (You probably need to reload the file). Recall that this returns an answer substitution (assignment to
    variables). Type ; to get more answers or . to stop searching. In this example what happens to the
    answers you get if you swap the two lines you have in
    If you change you can reload it into interactive mode by typing [warmup] again. You
    don’t need to stop and restart interactive mode.
    The rest of this document contains the assessed exercises. Formative exercises in more of a tutorial
    style continue in the other document provided.
    Part 1: Electrical Interference
    You have joined a team working on the space program of a small country. The team is working
    hard trying to design a probe to send to a distant planet. The electrical engineering department is
    working on a list of components of the probe. For each pair of components, they have done tests to
    see whether the components can be put close to each other without interfering with each other and
    breaking. They have summarised their results so far in the following Prolog database (there is a copy
    of this in in the lab1 branch of your git repo.).
    Your task is to write software in Prolog to help the designers of the probe check that their designs
    will work, in that they only place components close together if the engineers have shown those components
    don’t interfere. You should create a file to work in (make sure it is added
    to the lab1 branch of your repo!). Note that to gain full marks you must provide an explanation of
    what you have done (see mark scheme at the end).
    Exercise 1
    To get started, write a predicate safe_list/1 (i.e. a predicate with functor name safe_list and
    taking 1 argument) which takes a list of components and which succeeds if and only if every component
    in the list is safe for use with every other component. For example safe_list([cpu,imu,radar])
    should succeed, but safe_list([ram,cpu,radar]) should fail. You may define extra predicates if
    you wish (this is true for all exercises in this lab).
    Now, the engineers don’t just want to study lists of components, they also want to analyse abstract
    designs for the probe. A design is a list where each element is either of the form part(C) for some
    component C, or else of the form shield(L), where L is a design and the first element of L is of the
    form part(c). For example
    [part(imu), shield([
    is a design, but
    [part(imu), shield([
    is not, because all the elements in the argument of the outermost shield are themselves of the form
    shield(X) – the first one is not of the form part(X) as required.
    You can assume that you will be given a properly formatted design. However, you may find it instructive
    to write a predicate to check whether a design is properly formatted (no marks for this).
    The meaning of sheild(L) is that the elements of L have been electrically shielded from the outside
    world. That means that we can check whether a design is safe by checking whether all the parts
    which are not inside shields are safe when used with each other, then recursively checking whether
    the contents of each shield are a safe design. For example, the design
    [part(radar), part(imu), shield([
    is safe, because the radar and imu are safe together, and the antenna and transponder are safe
    together. But
    [part(radar), part(camera), shield([
    is not safe because it puts the radar and camera together without surrounding one of them with
    shielding, while
    [part(radar), part(imu), shield([
    is not safe because the camera and antenna are not safe together.
    Exercise 2
    Write a predicate safe_design/1 which, when given a design as input, succeeds if and only if the
    design is safe in the above sense. Note it is unlikely that you will be able to use the predicate
    safe_list/1 from Exercise 1 directly, however the structure of your answer to this exercise is likely
    to be similar. It is likely that you will need to define auxiliary predicates. You do not need to worry
    about how your predicate behaves if the argument is not a design according to the definition above.
    If you are struggling with this part, you might find the next one easier.
    Since the finished probe is supposed to be carried into space by a rocket, it is important for it to
    be as light as possible. The engineers are trying to use as little shielding as possible. As a first
    approximation, they want the software to count the number of times shield is used in a design.
    Exercise 3
    Write a predicate count_shields/2 which succeeds if and only if its second argument is the number
    of times shield occurs in its first argument. It must be possible to use your predicate in the query
    count_shields(D,N) where D is a design and N is a variable, so that Prolog returns the number.
    For example
    should return N = 3. You do not need to worry about the behaviour of your predicate if the first
    argument is not a design according to the first definition.
    Finally, the engineers want to check that a design does not have any components missing. The idea
    is that they will provide a design and a list of components, and the software should be able to tell if
    each component in the list is used exactly once somewhere in the design.
    Exercise 4
    Write a predicate design_uses/2 which accepts a design and a non-empty list of components as
    arguments, and succeeds if and only if the first argument is a design, and the components used in
    the design are exactly those listed in the list. Each element in the list must be used exactly once in
    the design (if a component is repeated, it should be occur as many times in the design as there are
    repetitions of it in the list). Note that the elements may appear in a different order in the design
    compared to the list. Finally, the predicate should fail if the first argument is not a design as defined
    above (we do count the empty list as a design).
    Before attempting to write design_uses/2, you should first write a predicate split_list/3 with
    three arguments, which succeeds if the first argument can be split up into the other two arguments.
    For example, the query split_list([1,2,3],X,Y) should return the results
    It may be helpful to think about going through the list element by element, and allocating each element
    either to the second or third argument, before recursively allocating the rest. You may then
    like to contemplate the results of the query split_list([1,2,3],[H],R).
    Make sure your test your solution(s). You should ensure that your solution works where components
    are repeated in the component list, for example, design_uses([part(imu),shield([part(imu)])],[imu,imu]).
    More testing data may be provided via Blackboard. Please keep an eye out for announcements.
    Exercise 5
    Once you have finished the exercises above, you should try running the query design_uses(D,[imu,cpu]).
    Ideally this will list all possible designs using one imu and one cpu. This will depend on ensuring your
    previous definitions are not unnecessarily restrictive. If this works, try running the query
    although you might prefer to see if you can find a solution manually first.
    It might be that your Prolog code goes into an infinite loop! If you’re really interested in Prolog,
    ensure that your implementation of design_uses\2 does not go into an infinite loop for the input
    given. Use comments to describe clearly when your implementation runs forever, and discuss whether
    it would be efficient to run on large inputs, with clear reasoning for your claims.
    Submission and Mark Scheme
    Submission is via git. You must work on the lab1 branch and tag your submission as lab1 solution.
    The provided script does this for you. Note that you must push your tagged commit to
    Gitlab before the deadline (it is not good enough just to make the commit locally).
    Your work will be marked and returned to you with comments. We may automate some tests on your
    code so please do not edit the general layout (e.g. headings) of the provided file and make sure you
    use the specified predicate names.
    The marking scheme (10 marks total) is as follows.
    (1 mark) for correct safe_list/1
    (2 marks) for correct safe_design/1, 1 mark if missing a case
    (2 marks) for correct count_shields/2, 1 mark if missing a case
    (4 marks) with 2 marks for split_list/2 and 2 marks for the final design_uses/2
    (1 mark) for an implementation of design_uses/2 which does not go into an infinite loop
    together with clear and correct comments answering the questions asked at the end of Exercise
    You must provide explanations of the work you have done as comments in the submitted files. These
    should go beyond standard comments in programs and should not simply reiterate the rules in English
    e.g. aim to explain the idea not the code. In all cases, up to half of the marks may be deducted if the
    explanation of the code is not sufficient. You should aim to write in full sentences. You may include
    brief examples.
1 声望
0 粉丝
0 条评论
一、常见数据结构简单数据结构(必须理解和掌握)有序数据结构:栈、队列、链表。有序数据结构省空间(储存空间小)无序数据结构:集合、字典、散列表,无序数据结构省时间(读取时间快)复杂数据结构树、 堆图二...

白鲸鱼9阅读 6.5k

算法可视化:一文弄懂 10 大排序算法
在本文中,我们将通过动图可视化加文字的形式,循序渐进全面介绍不同类型的算法及其用途(包括原理、优缺点及使用场景)并提供 Python 和 JavaScript 两种语言的示例代码。除此之外,每个算法都会附有一些技术说...

破晓L7阅读 838

00 后清华学霸用 AI 打败大气层「魔法攻击」,还原宇宙真面貌
内容一览:从诞生的那一刻起,人类对宇宙的探索就从未停止。如今,这门古老的科学再次借助 AI 获得加速度。本文将展示 AI 与天文学的结合擦出了怎样的火花。关键词:AI 天文图像 弱引力透镜

超神经HyperAI阅读 86.1k

【TVM 学习资料】快速入门:编译深度学习模型
这个例子展示了如何用 Relay Python 前端构建神经网络,并为装有 TVM 的 NVIDIA GPU 生成 runtime 库。注意,构建 TVM 需要启用 CUDA 和 LLVM。

超神经HyperAI阅读 34.3k

6 大经典机器学习数据集,3w+ 用户票选得出,建议收藏
内容一览:本期汇总了超神经下载排名众多的 6 个数据集,涵盖图像识别、机器翻译、遥感影像等领域。这些数据集质量高、数据量大,经历人气认证值得收藏码住。关键词:数据集 机器翻译 机器视觉

超神经HyperAI2阅读 3.1k

DeepMind 发布强化学习通用算法 DreamerV3,AI 成精自学捡钻石
内容一览:强化学习是多学科领域的交叉产物,其本质是实现自动决策且可做连续决策。本文将介绍 DeepMind 最新研发成果:扩大强化学习应用范围的通用算法 DreamerV3。关键词:强化学习 DeepMind 通用算法

超神经HyperAI1阅读 1.2k

最近在GitHub上发现了一个爆火的开源项目。好家伙,凑近一看,居然还是由微软开源,并且和最近炙手可热的ChatGPT息息相关。项目的名字叫做:Visual ChatGPT。[链接]这个项目最早是3月上旬微软开源的,项目宣布开...

CodeSheep2阅读 1.8k

1 声望
0 粉丝