Skip to content

Instantly share code, notes, and snippets.

@kozross
Created November 9, 2018 05:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kozross/ed96329a3b241e7599f1b71aaf8f8394 to your computer and use it in GitHub Desktop.
Save kozross/ed96329a3b241e7599f1b71aaf8f8394 to your computer and use it in GitHub Desktop.
newtype Atom =
Atom Word64
preview :: Atom -> (Maybe Bool, Word64)
preview x =
let raw = coerce x
f = shorten raw
in if | testBit raw 63 -> (Nothing, f)
| testBit raw 62 -> (Just False, f)
| otherwise -> (Just True, f)
review :: (Maybe Bool, Word64) -> Atom
review (b, f) =
let f = shorten f
in case b of
Nothing -> coerce (bit 63 .|. f)
Just False -> coerce (bit 62 .|. f)
Just True -> coerce f
-- I want to have a pattern for Present, which unpacks to (Bool, Word64), and a pattern for Absent, which unpacks to just Word64.
-- So basically it needs to fake this:
-- data AtomB = Present (Bool, Word64) | Absent Word64
-- How would I write these?
@mstksg
Copy link

mstksg commented Nov 9, 2018

preview :: s -> Maybe a
review :: a -> s

pattern MyPat :: a -> s
pattern MyPat x <- (preview -> Just x)
  where
    MyPat x = review x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment