Skip to content

Commit

Permalink
Message as String synonym to indicate preprocessing
Browse files Browse the repository at this point in the history
Message remains a type synonym for String, indicating application of message function.
  • Loading branch information
orome committed Nov 20, 2015
1 parent e872275 commit 788dc1f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
10 changes: 4 additions & 6 deletions Crypto/Enigma.hs
Original file line number Diff line number Diff line change
Expand Up @@ -532,26 +532,24 @@ enigmaEncoding ec str =
zipWith encode (enigmaMapping <$> cfgs) (message str) where cfgs = iterate step (step ec)




-- REV - Make 'Message' a class and require explicty use of (replacement) 'message'; see issue 12 <<<
-- Message entry -------------------------------------------------------------

-- | A 'String', to which 'message' is applied by functions taking it as an argument.
-- | A (<https://wiki.haskell.org/Type_synonym#synonym> for) 'String', indicating that 'message' will be applied
-- to the corresponding argument.
type Message = String

-- | Convert a 'String' to valid Enigma machine input: replace any symbols for which there are standard Kriegsmarine
-- substitutions, remove any remaining non-letter characters, and convert to uppercase. This function is applied
-- automatically to 'Message' arguments for functions defined here.
message :: String -> String
message :: String -> Message
message s = filter (`elem` letters) $ foldl1 fmap (uncurry replace <$> subs) $ toUpper <$> s
where
subs = [(" ",""),(".","X"),(",","Y"),("'","J"),(">","J"),("<","J"),("!","X"),
("?","UD"),("-","YY"),(":","XX"),("(","KK"),(")","KK"),
("1","YQ"),("2","YW"),("3","YE"),("4","YR"),("5","YT"),
("6","YZ"),("7","YU"),("8","YI"),("9","YO"),("0","YP")]

-- REV - Possible future version in which 'Message' is at type (and caller is responsible for making Message).
-- REV - Rejected (#12) alternate version in which 'Message' is at class (and caller is responsible for making Message).
-- data Message = Message String deriving Show
--
-- message :: String -> Message
Expand Down
14 changes: 8 additions & 6 deletions Crypto/Enigma/Display.hs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ markedMapping Nothing e = e

-- Machine operation display =================================================

-- Preprocess a message and produce a configuration display for the starting configuration
-- and for each character of the message, using the provided configuration display function.
-- Preprocess a string into a 'Message' (using 'message') and produce a configuration display for the
-- starting configuration and for each character of the message, using the provided configuration display function.
-- Note that while 'showEnigmaOperation' and 'showEnigmaOperationInternal' indicate a 'Message' argument, it is
-- this function, which both call, that applies 'message'.
showEnigmaOperation_ :: (EnigmaConfig -> Char -> String) -> EnigmaConfig -> Message -> String
showEnigmaOperation_ df ec str = unlines $ zipWith df (iterate step ec) (' ':(message str))

Expand Down Expand Up @@ -188,7 +190,7 @@ showEnigmaConfigInternal ec ch =
-- Operation display ---------------------------------------------------------

-- | Show a summary of an Enigma machine configuration (see 'showEnigmaConfig')
-- and for each subsequent configuration as it processes each letter of a message. #showEnigmaOperationEG#
-- and for each subsequent configuration as it processes each letter of a 'Message'. #showEnigmaOperationEG#
--
-- >>> putStr $ showEnigmaOperation (configEnigma "b-γ-V-VIII-II" "LFAP" "UX.MO.KZ.AY.EF.PL" "03.17.04.11") "KRIEG"
-- OHNKJYSBTEDMLCARWPGIXZQUFV LFAP 10 16 24 06
Expand All @@ -201,11 +203,11 @@ showEnigmaConfigInternal ec ch =
-- Note that the first line of the display represents the initial configuration of the machine, but does not
-- perform any encoding (as explained in 'step').
-- Note also that the second line of this display is the same as one displayed in the example for 'showEnigmaConfig'.
showEnigmaOperation :: EnigmaConfig -> String -> String
showEnigmaOperation :: EnigmaConfig -> Message -> String
showEnigmaOperation ec str = showEnigmaOperation_ showEnigmaConfig ec str

-- | Show a schematic of an Enigma machine's internal configuration (see 'showEnigmaConfigInternal' for details)
-- and for each subsequent configuration as it processes each letter of a message.
-- and for each subsequent configuration as it processes each letter of a 'Message'.
--
-- >>> putStr $ showEnigmaOperationInternal (configEnigma "b-γ-V-VIII-II" "LFAP" "UX.MO.KZ.AY.EF.PL" "03.17.04.11") "KR"
-- ABCDEFGHIJKLMNOPQRSTUVWXYZ
Expand Down Expand Up @@ -253,7 +255,7 @@ showEnigmaOperation ec str = showEnigmaOperation_ showEnigmaConfig ec str
-- Note that the first block of the display represents the initial configuration of the machine, but does not
-- perform any encoding (as explained in 'step'). Note also that the second block of this display is the same
-- as one displayed in the example for 'showEnigmaConfigInternal', where it is explained in more detail.
showEnigmaOperationInternal :: EnigmaConfig -> String -> String
showEnigmaOperationInternal :: EnigmaConfig -> Message -> String
showEnigmaOperationInternal ec str = showEnigmaOperation_ showEnigmaConfigInternal ec str


Expand Down

0 comments on commit 788dc1f

Please sign in to comment.