From 67b5d50f26e464ca0696af48ac2427d93df0f170 Mon Sep 17 00:00:00 2001 From: Karl Ostmo Date: Tue, 2 Jan 2024 10:27:20 -0800 Subject: [PATCH] caterpillar --- data/scenarios/Fun/00-ORDER.txt | 3 +- data/scenarios/Fun/_caterpillar/solution.sw | 129 ++++++++++++++++++++ data/scenarios/Fun/caterpillar.yaml | 66 ++++++++++ 3 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 data/scenarios/Fun/_caterpillar/solution.sw create mode 100644 data/scenarios/Fun/caterpillar.yaml diff --git a/data/scenarios/Fun/00-ORDER.txt b/data/scenarios/Fun/00-ORDER.txt index 162469def..b688f7170 100644 --- a/data/scenarios/Fun/00-ORDER.txt +++ b/data/scenarios/Fun/00-ORDER.txt @@ -1,3 +1,4 @@ GoL.yaml logo-burst.yaml -horton.yaml \ No newline at end of file +horton.yaml +caterpillar.yaml \ No newline at end of file diff --git a/data/scenarios/Fun/_caterpillar/solution.sw b/data/scenarios/Fun/_caterpillar/solution.sw new file mode 100644 index 000000000..ff9d20645 --- /dev/null +++ b/data/scenarios/Fun/_caterpillar/solution.sw @@ -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; \ No newline at end of file diff --git a/data/scenarios/Fun/caterpillar.yaml b/data/scenarios/Fun/caterpillar.yaml new file mode 100644 index 000000000..3e0334eeb --- /dev/null +++ b/data/scenarios/Fun/caterpillar.yaml @@ -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.. \ No newline at end of file