/********** Direction A teacher said that it should be decided by the student what direction the subject in each sentence should face. For example, in the sentence, "John should sit in the chair", John should face east. The algorithm direction1/7 finds which direction one is facing in a room. direction1([[0, [1, 1]], [1, [0, 0]], [2, [1, 2]], [3, [3, 1]]], [[1, 2], [3, 3]],0, 3, 0, 2, Direction). Direction = north ; direction1([[0, [1, 1]], [1, [0, 0]], [2, [1, 2]], [3, [3, 1]]], [[1, 3], [2, 2]], 0, 3, 0, 2, Direction). Direction = east ; direction1([[0, [1, 1]], [1, [0, 0]], [2, [1, 2]], [3, [3, 1]]], [[0, 1], [3, 3]], 0, 3, 0, 2, Direction). Direction = south ; direction1([[0, [1, 1]], [1, [0, 0]], [2, [1, 2]], [3, [3, 1]]], [[0, 1], [2, 2]], 0, 3, 0, 2, Direction). Direction = west ; direction1(+Map, +View, +XMin, +XMax, +YMin, +YMax, -Direction) (+ means input, - means output) Direction1/7 takes a list of blocks (a map), a view facing either north, east, south or west from a person’s position in the room, and determines the view the person has facing each direction, until the given view is found to match the determined view, and the direction the person is facing is found. Map – A list of blocks in the room in the format [Identifier, [X, Y]]. Block 0 is the person. View - A view of the blocks from the person’s position, facing Direction, in the format the list of [Position, Identifier], where Position is the X or Y value, depending on whether the person is facing north or south, or east or west respectively and Identifier is the identifier of the block. XMin – The lower bound in the X axis. XMax – The upper bound in the X axis. YMin – The lower bound in the Y axis. YMax – The upper bound in the Y axis. Direction – The direction the person is facing (north, east, south or west). **********/ direction1(Map1, View1, XMin, XMax, YMin, YMax, Direction) :- PersonCoords = [0, Coords], member(PersonCoords, Map1), delete(Map1, PersonCoords, Map2), direction2(Map2, Coords, XMin, XMax, YMin, YMax, Direction, View2), delete1(View2, [], View1). /********** direction2(+Map, +Coords, +XMin, +XMax, +_YMin, +YMax, -Direction, -View1) Direction2/8 finds the view facing either north, east, south or west from the person’s coordinates and returns it to direction1/7 to test it. A list of the X or Y co-ordinates of the blocks in front of the person, depending on whether the person is facing east or west, or north or south respectively, is generated. This list is reversed for the north and east directions, so that each view containing the furthest blocks, successively replaced by closer blocks is created. Map - A list of blocks in the room in the format [Identifier, [X, Y]]. Coords – The person’s X and Y co-ordinates. XMin – The lower bound in the X axis. XMax – The upper bound in the X axis. YMin – The lower bound in the Y axis. YMax – The upper bound in the Y axis. Direction - The direction the person is facing (north, east, south or west). View1 - A view of the blocks from the person’s position, facing Direction, in the format the list of [Position, Identifier], where Position is the X or Y value, depending on whether the person is facing north or south, or east or west respectively and Identifier is the identifier of the block. **********/ direction2(Map, Coords, XMin, XMax, _YMin, YMax, Direction, View1) :- Coords = [_X, Y], generatelevels(Y, YMax, [Y], Levels1), reverse(Levels1, Levels2), createview1(Map, Levels2, XMin, XMax, View1, ns), Direction = north. direction2(Map, Coords, _XMin, XMax, YMin, YMax, Direction, View1) :- Coords = [X, _Y], generatelevels(X, XMax, [X], Levels1), reverse(Levels1, Levels2), createview1(Map, Levels2, YMin, YMax, View1, ew), Direction = east. direction2(Map, Coords, XMin, XMax, YMin, _YMax, Direction, View1) :- Coords = [_X, Y], generatelevels(YMin, Y, [YMin], Levels1), createview1(Map, Levels1, XMin, XMax, View1, ns), Direction = south. direction2(Map, Coords, XMin, _XMax, YMin, YMax, Direction, View1) :- Coords = [X, _Y], generatelevels(XMin, X, [XMin], Levels1), createview1(Map, Levels1, YMin, YMax, View1, ew), Direction = west. /********** createview1(+Map, +Levels, +Min, +Max, -View1, +Type) Createview1/6 creates a blank initial view facing a direction. Map - A list of blocks in the room in the format [Identifier, [X, Y]]. Levels – a list of the X or Y co-ordinates of the blocks in front of the person, depending on whether the person is facing east or west, or north or south respectively. Min – the minimum X or Y value, depending on whether the person is facing north or south, or east or west respectively. Max – the maximum X or Y value, depending on whether the person is facing north or south, or east or west respectively. View1 - A view of the blocks from the person’s position, facing Direction, in the format the list of [Position, Identifier where Position is the X or Y value, depending on whether the person is facing north or south, or east or west respectively and Identifier is the identifier of the block. Type –“ns” if the view is facing north or south, or “ew” if the view is facing east or west. **********/ createview1(Map, Levels, Min, Max, View1, Type) :- Item = [Min, []], generaterange(Min, Max, [Item], View2), createview2(Map, Levels, View2, View1, Type). /********** createview2(+Map, +Levels, +View1, -View2, +Type) Createview2/5 superimposes views coming closer and closer to the person. Map - A list of blocks in the room in the format [Identifier, [X, Y]]. Levels – an initial list of the X or Y co-ordinates of the blocks in front of the person, depending on whether the person is facing east or west, or north or south respectively. View1 – An initial view of the blocks from the person’s position, facing Direction, in the format the list of [Position, Identifier], where Position is the X or Y value, depending on whether the person is facing north or south, or east or west respectively and Identifier is the identifier of the block. View2 – The final view of the blocks from the person’s position, facing Direction, in the format the list of [Position, Identifier], where Position is the X or Y value, depending on whether the person is facing north or south, or east or west respectively and Identifier is the iddentifier of the block. Type –“ns” if the view is facing north or south, or “ew” if the view is facing east or west. **********/ createview2(_Map, [], View, View, _Type). createview2(Map, Levels1, View1, View2, Type) :- Levels1 = [Level | Levels2], createview3(Map, Level, View1, [], View3, Type), createview2(Map, Levels2, View3, View2, Type). /********** createview3(+Map, +Levels, +View1, -View2, +Type) Createview3/5 creates a row of the view, by adding a block, leaving a blank space or replacing a blank space in a view with a block, at each of the positions. Map - A list of blocks in the room in the format [Identifier, [X, Y]]. Level – the X or Y co-ordinate of the blocks in front of the person, depending on whether the person is facing east or west, or north or south respectively. View1 – An initial view of the blocks from the person’s position, facing Direction, in the format the list of [Position, Identifier], where Position is the X or Y value, depending on whether the person is facing north or south, or east or west respectively and Identifier is the identifier of the block. View2 – The current view of the blocks from the person’s position, facing Direction, in the format the list of [Position, Identifier], where Position is the X or Y value, depending on whether the person is facing north or south, or east or west respectively and Identifier is the identifier of the block. View3 – The final view of the blocks from the person’s position, facing Direction, in the format the list of [Position, Identifier], where Position is the X or Y value, depending on whether the person is facing north or south, or east or west respectively and Identifier is the identifier of the block. Type –“ns” if the view is facing north or south, or “ew” if the view is facing east or west. **********/ createview3(_Map, _Level, [], View, View, _Type) :- !. createview3(Map, Level, View1, View2, View3, Type) :- View1 = [Item1 | Items2], Item1 = [Position, Identifier1], coordstype(Position, Level, Type, Coords), modifyitem(Map, Coords, Identifier1, Identifier2), ViewItem = [Position, Identifier2], append(View2, [ViewItem], View4), createview3(Map, Level, Items2, View4, View3, Type). /********** coordstype(+Position, +Level, +Type, -Coords) Coordstype/4 determines the order of Position and Level in Coords, used to construct the view to test that is facing a particular direction, depending on the being north or south, or east or west. Position – The position of the block the person is facing. Level – The level of the block (the X or Y co-ordinate of the block in front of the person, depending on whether the person is facing east or west, or north or south respectively.) Type – “ns” if the view is facing north or south, or “ew” if the view is facing east or west. Coords – Position is before Level in north and south views, and after it in east and west views. **********/ coordstype(Position, Level, ns, Coords) :- Coords = [Position, Level]. coordstype(Position, Level, ew, Coords) :- Coords = [Level, Position]. /********** delete1(+View1, +View2, -View3) Delete1/3 deletes the items in the view, which do not represent a block. View1 – The initial view of the blocks from the person’s position, facing Direction, in the format the list of [Position, Identifier], where Position is the X or Y value, depending on whether the person is facing north or south, or east or west respectively and Identifier is the identifier of the block. View2 – The current view of the blocks from the person’s position, facing Direction, in the format the list of [Position, Identifier], where Position is the X or Y value, depending on whether the person is facing north or south, or east or west respectively and Identifier is the identifier of the block. View3 – The final view of the blocks from the person’s position, facing Direction, in the format the list of [Position, Identifier], where Position is the X or Y value, depending on whether the person is facing north or south, or east or west respectively and Identifier is the identifier of the block. **********/ delete1([], View, View) :- !. delete1(View1, View2, View3) :- View1 = [Item | Items], Item = [_Position, []], delete1(Items, View2, View3), !. delete1(View1, View2, View3) :- View1 = [Item | Items], append(View2, [Item], View4), delete1(Items, View4, View3). /********** generatelevels(+Min, +Max, +Levels1, -Levels2) Generatelevels/4 generates a list of levels to create a view from. Min – The initial level. Max – The final level. Levels1 – The initial list of levels. Levels2 – The final list of levels. **********/ generatelevels(Max, Max, Levels, Levels) :- !. generatelevels(Min, Max, Levels1, Levels2) :- Level is Min + 1, append(Levels1, [Level], Levels3), generatelevels(Level, Max, Levels3, Levels2). /********** generaterange(+Min, +Max, +Range1, -Range2) Generaterange/4 generates a range of positions to create a view from. Min – The initial position. Max – The final position. Range1 – The initial list of positions. Range2 – The final list of position. **********/ generaterange(Max, Max, Range, Range) :- !. generaterange(Min, Max, Range1, Range2) :- Position is Min + 1, Item = [Position, []], append(Range1, [Item], Range3), generaterange(Position, Max, Range3, Range2). /********** modifyitem(+Map, +Coords, +Identifier1, -Identifier2) Modifyitem/4 adds a block, leaves a blank space or replaces a blank space in a view with a block. Min – The initial position. Max – The final position. Range1 – The initial list of positions. Range2 – The final list of position. **********/ modifyitem(Map, Coords, _Identifier1, Identifier2) :- member([Identifier2, Coords], Map). modifyitem(Map, Coords, Identifier1, Identifier1) :- not(member([_Identifier2, Coords], Map)). /********** The following arguments argue for and against the room, described by the algorithm direction1/7. 1. Chebyshev argued that the defendant (person 1) in a court room should have a serious expression while testifying, therefore the juror (person 0, facing north) should trust him. This is shown by the sample query: direction1([[0, [0, 0]], [1, [0, 1]]], [[0, 1]], 0, 0, 0, 1, Direction). The output is: Direction = north ; In other words, person 0 (at 0, 0) saw person 1 (at 0, 1), with a view at position 0 of person 1, when he faced north. Person 1 should have a serious expression, as shown by the following logic: The variable mouthlength(1) is the displacement between ends of the mouth of person 1. mouthlength(1) = 0.04 m If person 1's mouth length is 0.04 m (relaxed), then he has a serious expression. mouthlength(1) -> serious(1) If person 1 has a serious expression, then person 0 should trust him. serious(1) -> trusts(0, 1) Therefore, the juror (person 0) should trust the defendant (person 1). 2. He also argued that the politician should pour water into a container through a funnel, therefore he should conserve water. This is shown by the sample query: ?- direction1([[0, [0, 1]], [1, [0, 0]]], [[0, 1]], 0, 0, 0, 1, Direction). The output is: Direction = south In other words, person 0 (at 0, 1) used the funnel (object 1 at 0, 0), with a view at position 0 of object 1, when he faced south. Person 0 should pour water through object 1 because it is wider at the top than the water pouring in. 3. He also argued that a secondary school teacher should smell his sandwich, therefore he should test that it is fresh. U parts smell fresh 4. He also argued that the primary school student should face his classmate, who is throwing him the ball, therefore he should be true to himself. ball in middle 5. He also argued that a peace pandit should estimate the amount of time that it will take to reach the peace palace, therefore she should be on time. t=d/v 6. Green argued that the contents of the room which the algorithm direction1/7 is about, shouldn't fail. He argued that a feminist shouldn't skid on an apple's peel, therefore she shouldn't avoid speaking in public. talk with people individually - walk until reach south side of person 7. He argued that a psychologist shouldn't trip on pebbles he used to mark his path in a maze. not(member(Path)) 8. He argued that a person shouldn't not be interested in a linguist, therefore he shouldn't frown. eyes narrow = 0.01 m 9. He argued that a mathematician shouldn't cut off a path of an algorithm, therefore a person following a path representing a path of an algorithm shouldn't collide with a barrier. barrier immediately north 10. He argued that a computer scientist shouldn't not describe a Prolog program with at least five instances of the Prolog commands (for example: is, = , append, member, delete), therefore he shouldn't skip a step and shouldn't trip over by walking past a block being replaced when createview1 is executed. barrier > 0.25 m **********/