Skip to content

Instantly share code, notes, and snippets.

@jpaugh
Last active December 16, 2015 04:59
Show Gist options
  • Save jpaugh/5380885 to your computer and use it in GitHub Desktop.
Save jpaugh/5380885 to your computer and use it in GitHub Desktop.
Creative way to represent Biblical references in Haskell.
-- |A list of `Range's. This is the comprehensive ref type, and
-- can form an arbitrarily complex reference to the scripture.
data Reference = Reference [Range] -- [ x ] means list of x's
deriving (Show, Eq)
-- |A Range of verses, or a single ref. "Range" is a bit of a misnomer,
-- since we can have a single reference "Range".
data Range
= Range
{ startAt :: Ref -- e.g., Rom. 7-9, Gen 3:6-Ex 5:2
, endAt :: Ref }
| SingleRef -- vertical bar | means either/or
{ at :: Ref } -- e.g. Gen 5:4
deriving (Eq)
-- |A single Ref. Chapter and verse are represened with
-- OrdinableInt which are "magic"
-- integers with two extra values: First and Last.
-- An UnresolvedRef is one with missing information.
-- Perhaps the user didn't feel like typing in the book name or chapter.
-- "Maybe" means it might be missing. We can (usually) figure out what the
-- user meant, and fill in the Maybes. Then, it turns into a ResolvedRef.
-- A ResolvedRef is one that's got all 3 components. All refs must be converted
-- to this form for most of the internal operations. (E.g., we can't print a verse if
-- we're not sure exactly which one it is.)
data Ref
= UnresolvedRef -- A @Ref@ which may have pieces missing
{ u_book :: Maybe BookName -- e.g. Gen 9, Ps 105, 4:18
, u_chapt :: Maybe OrdinableInt
, u_verse :: Maybe OrdinableInt
}
| ResolvedRef -- A unambiguous @Ref@, containing all three components
{ r_book :: BookName -- Gen 9:1, Ps 105:3, Cor. 4:17
, r_chapt :: OrdinableInt
, r_verse :: OrdinableInt
}
deriving (Eq)
-- Just a bit of hocus pocus to encode the concept of "firstness",
-- "lastness", or "exactly is-ness"
data OrdinableInt
= OrdinallyFirst -- the first one, 1
| Cardinally Int -- some exact number
| OrdinallyLast -- the largest chapter or verse number that makes sense:
-- e.g., Gen 50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment