Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] caterpillar #1702

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion data/scenarios/Fun/00-ORDER.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
GoL.yaml
logo-burst.yaml
horton.yaml
horton.yaml
caterpillar.yaml
129 changes: 129 additions & 0 deletions data/scenarios/Fun/_caterpillar/solution.sw
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
def bindE = \f. \x. case x inL f end
def fmapE = \f. \x. case x inL (\y. inR $ f y) end

def nil : (a -> b -> b) ->
b ->
b = \_. \x. x end

def cons : a ->
((a -> b -> b) -> b -> b) ->
(a -> b -> b) ->
b ->
b = \x. \xs. \f. \b. f x $ xs f b end

// Accumulator functions

def offsetCoords : (int * int) -> (int * int) -> (int * int) = \coord. \offset.
(fst coord + fst offset, snd coord + snd offset)
end

def getLength : ((a -> int -> int) -> int -> int) -> int = \list.
list (\_. \y. 1 + y) 0
end

def lastAccumulatorFunc : a -> (unit + a) -> (unit + a) = \x. \b.
case b (\_. inR x) inR
end

def getLast : ((a -> (unit + a) -> (unit + a)) -> (unit + a) -> (unit + a)) -> (unit + a) = \list.
list lastAccumulatorFunc $ inL ()
end

def initAccumulatorFunc : a ->
unit + ((a -> b -> b) -> b -> b) ->
unit + ((a -> b -> b) -> b -> b) = \x. \b.
case b (\_. inR nil) (\y. inR $ cons x y)
end

def getInit = \list.
list initAccumulatorFunc $ inL ()
end

// "pop" from the tail;
// returns the last element of the list and the truncated list.
def shift = \list.
list (\x. \b. case b (\_. inR (x, nil)) (\y. inR (fst y, cons x $ snd y))) $ inL ()
end


def getPassthrough = \list.
list cons nil
end



// Scenario-specific functions

def doAtLoc = \currLoc. \targetLoc. \func.
teleport self targetLoc;
func;
teleport self currLoc;
end;

def makeList = \i. \currentList.
if (i > 0) {
move;
myLoc <- whereami;
place "tail";

newList <- makeList (i - 1) $ cons myLoc currentList;
return newList;
} {
return currentList;
};
end

def go2 = \myList.


wait 10;
move;

newLoc <- whereami;

let maybeLastElem = getLast myList in
case maybeLastElem return (\lastElem.
log $ "Going to teleport to: " ++ format lastElem;
doAtLoc newLoc lastElem grab;
);


// let listInit = getPassthrough myList in
// log $ "List length: " ++ format (getLength listInit);

// let shifted = shift myList in

// FIXME: Uncommenting the following line causes a type error!
// let oldRattleLoc = fst shifted in
// let shiftedList = snd shifted in
// doAtLoc newLoc oldRattleLoc grab;

go2 $ cons newLoc myList;
end;


def go =
myList <- makeList 5 $ nil;

let w = myList offsetCoords (0, 0) in
let x = getLength myList in
let y = getLast myList in
let z = bindE getLast $ getInit myList in

let shifted = shift myList in

log "======";
log $ format w;
log $ format x;
log $ format y;
log $ format z;

log "-------";
log $ format $ fmapE fst shifted;
log $ format $ fmapE fst $ bindE shift $ fmapE snd shifted;
log $ format $ fmapE fst $ bindE shift $ fmapE snd $ bindE shift $ fmapE snd shifted;

go2 myList;
end;

go;
66 changes: 66 additions & 0 deletions data/scenarios/Fun/caterpillar.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
version: 1
name: Caterpillar
author: Karl Ostmo
seed: 1
description: |
Crawl along
creative: true
objectives:
- goal:
- |
Eat 30 apples
condition: |
return false
robots:
- name: base
dir: north
devices:
- ADT calculator
- branch predictor
- clock
- comparator
- compass
- dictionary
- GPS receiver
- grabber
- hourglass
- hearing aid
- lambda
- logger
- net
- scanner
- strange loop
- string
- treads
inventory:
- [5, tail]
solution: |
run "scenarios/Fun/_caterpillar/solution.sw"
entities:
- name: tail
display:
char: '@'
attr: green
description:
- |
Segment of snake's tail
properties: [known]
known: [water, boulder, flower]
world:
dsl: |
{grass}
upperleft: [-4, 4]
palette:
'B': [grass, erase, base]
'.': [grass, erase]
's': [stone, erase]
map: |
..sssss..
.sssssss.
sssssssss
sssssssss
ssssBssss
sssssssss
sssssssss
.sssssss.
..sssss..
Loading