Skip to content

Commit

Permalink
loosen Const restricions. Adds 'if' and 'base'.
Browse files Browse the repository at this point in the history
Also, preserve SrcLoc just in case
  • Loading branch information
kostmo committed Apr 2, 2023
1 parent c45688f commit c45ba7e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
2 changes: 1 addition & 1 deletion data/scenarios/Tutorials/crash.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description: |
Learn how to view built robots and debug them.
objectives:
- goal:
- Before you send your robots far away you need to learn how
- Before you send your robots far away from the `base` you need to learn how
to figure out what went wrong with them if they crash.
- |
In this challenge, you should start by
Expand Down
31 changes: 16 additions & 15 deletions src/Swarm/Doc/Pedagogy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Control.Lens (universe, view)
import Control.Monad (guard, (<=<))
import Control.Monad.Except (ExceptT (..), liftIO)
import Data.List (foldl', sort)
import Data.Map (Map)
import Data.Map qualified as M
import Data.Maybe (mapMaybe)
import Data.Set (Set)
Expand Down Expand Up @@ -54,15 +55,15 @@ commandsWikiPrefix = wikiPrefix <> "Commands-Cheat-Sheet#"
-- tutorials in sequence.
data CoverageInfo = CoverageInfo
{ tutInfo :: TutorialInfo
, novelSolutionCommands :: Set Const
, novelSolutionCommands :: Map Const [SrcLoc]
}

-- | Tutorial scenarios with the set of commands
-- introduced in their solution and descriptions
-- having been extracted
data TutorialInfo = TutorialInfo
{ scenarioPair :: ScenarioInfoPair
, solutionCommands :: Set Const
, solutionCommands :: Map Const [SrcLoc]
, descriptionCommands :: Set Const
}

Expand All @@ -79,7 +80,7 @@ extractCommandUsages :: ScenarioInfoPair -> TutorialInfo
extractCommandUsages siPair@(s, _si) =
TutorialInfo siPair solnCommands $ getDescCommands s
where
solnCommands = S.fromList $ maybe [] getCommands maybeSoln
solnCommands = getCommands maybeSoln
maybeSoln = view scenarioSolution s

-- | Obtain the set of all commands mentioned by
Expand All @@ -95,7 +96,7 @@ getDescCommands s =
allWords = concatMap (T.words . T.toLower) goalTextParagraphs
backtickedWords = mapMaybe (T.stripPrefix "`" <=< T.stripSuffix "`") allWords

commandConsts = filter isCmd allConst
commandConsts = filter isUserFunc allConst
txtLookups = M.fromList $ map (syntax . constInfo &&& id) commandConsts

-- | Extract the command names from the source code of the solution.
Expand All @@ -105,16 +106,17 @@ getDescCommands s =
-- the player did not write it explicitly in their code.
--
-- Also, the code from `run` is not parsed transitively yet.
getCommands :: ProcessedTerm -> [Const]
getCommands (ProcessedTerm (Module stx _) _ _) =
mapMaybe isCommand nodelist
getCommands :: Maybe ProcessedTerm -> Map Const [SrcLoc]
getCommands Nothing = mempty
getCommands (Just (ProcessedTerm (Module stx _) _ _)) =
M.fromListWith (<>) $ mapMaybe isCommand nodelist
where
ignoredCommands = S.fromList [Run, Return, Noop]
ignoredCommands = S.fromList [Run, Return, Noop, Force]

nodelist :: [Syntax' Polytype]
nodelist = universe stx
isCommand (Syntax' _ t _) = case t of
TConst c -> guard (isCmd c && c `S.notMember` ignoredCommands) >> Just c
isCommand (Syntax' sloc t _) = case t of
TConst c -> guard (isUserFunc c && c `S.notMember` ignoredCommands) >> Just (c, [sloc])
_ -> Nothing

-- | "fold" over the tutorials in sequence to determine which
Expand All @@ -132,8 +134,8 @@ computeCommandIntroductions =
usages = extractCommandUsages siPair
usedCmdsForTutorial = solutionCommands usages

updatedEncountered = encounteredPreviously `S.union` usedCmdsForTutorial
novelCommands = usedCmdsForTutorial `S.difference` encounteredPreviously
updatedEncountered = encounteredPreviously `S.union` M.keysSet usedCmdsForTutorial
novelCommands = M.withoutKeys usedCmdsForTutorial encounteredPreviously

-- | Extract the tutorials from the complete scenario collection
-- and derive their command coverage info.
Expand Down Expand Up @@ -170,7 +172,7 @@ renderUsagesMarkdown idx (CoverageInfo (TutorialInfo (s, si) _sCmds dCmds) novel
, [""]
, pure $ "*" <> T.strip (view scenarioDescription s) <> "*"
, [""]
, renderSection "Commands first introduced in this solution" $ renderCmds novelCmds
, renderSection "Commands first introduced in this solution" $ renderCmds $ M.keysSet novelCmds
, [""]
, renderSection "Commands referenced in description" $ renderCmds dCmds
]
Expand All @@ -184,8 +186,7 @@ renderUsagesMarkdown idx (CoverageInfo (TutorialInfo (s, si) _sCmds dCmds) novel
then "<none>"
else T.intercalate ", " . map linkifyCommand . sort . map (T.pack . show) . S.toList $ cmds

-- linkifyCommand c = "[" <> c <> "](" <> commandsWikiPrefix <> c <> ")"
linkifyCommand c = c
linkifyCommand c = "[" <> c <> "](" <> commandsWikiPrefix <> c <> ")"

firstLine =
T.unwords
Expand Down
8 changes: 4 additions & 4 deletions test/unit/TestPedagogy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
module TestPedagogy where

import Control.Lens (view)
import Data.Set qualified as S
import Data.Map qualified as M
import Swarm.Doc.Pedagogy
import Swarm.Game.ScenarioInfo (scenarioPath)
import Swarm.Game.State
Expand All @@ -31,14 +31,14 @@ testPedagogy gs =
(unwords [show idx, scPath])
$ assertBool errMsg allCommandsCovered
where
missingCmds = novelCommands `S.difference` descCommands
missingCmds = M.withoutKeys novelCommands descCommands
errMsg =
unwords
[ "command(s) missing from description:"
, show missingCmds
, show $ M.keysSet missingCmds
]

scPath = view scenarioPath si
allCommandsCovered = S.null missingCmds
allCommandsCovered = M.null missingCmds

testList = zipWith testFromTut [0 ..] tutorialInfos

0 comments on commit c45ba7e

Please sign in to comment.