\NeedsTeXFormat{LaTeX2e}

\RequirePackage{amsmath}
\RequirePackage{amsthm}
\RequirePackage{amsfonts}
\RequirePackage{etoolbox}
\RequirePackage{xspace}
\RequirePackage{xcolor}
\RequirePackage{mdframed}
\RequirePackage{hyperref}
\RequirePackage{cleveref}
\RequirePackage[nolabel, inline]{showlabels}


% Links
% =====

\hypersetup{
  colorlinks=true,
  linkcolor=blue,
  urlcolor=blue
}


% Printable labels
% ================

% `\printlabel' behaves exactly as `\label' but its argument is printed in the
% PDF.
\showlabels{printlabel}
\newcommand{\printlabel}[1]{\label{#1}}


% Top-level sections
% ==================

% Numbered environments for top-level sections
\newtheoremstyle{formalized}% name of the style to be used
    {4pt}% measure of space to leave above the theorem. E.g.: 3pt
    {4pt}% measure of space to leave below the theorem. E.g.: 3pt
    {\normalfont} % name of font to use in the body of the theorem
    {0pt}% measure of space to indent
    {\bfseries}% name of head font
    {}% punctuation between head and body
    { }% space after theorem head; " " = normal inter-word space
    {\thmname{#1}\thmnumber{ #2}\thmnote{ (#3)}.}

% Unnumbered environments for top-level sections
\newtheoremstyle{unnumbered}% name of the style to be used
  {4pt}% measure of space to leave above the theorem. E.g.: 3pt
  {4pt}% measure of space to leave below the theorem. E.g.: 3pt
  {\normalfont} % name of font to use in the body of the theorem
  {0pt}% measure of space to indent
  {\bfseries}% name of head font
  {}% punctuation between head and body
  { }% space after theorem head; " " = normal inter-word space
  {\thmname{#1}\thmnote{ (#3)}.}

\theoremstyle{formalized}
\newcounter{theoremcount}

\newtheorem{axiom}[theoremcount]{Axiom}
\newtheorem{corollary}[theoremcount]{Corollary}
\newtheorem{definition}[theoremcount]{Definition}
\newtheorem{fact}[theoremcount]{Fact}
\newtheorem{lemma}[theoremcount]{Lemma}
\newtheorem{proposition}[theoremcount]{Proposition}
\newtheorem{remark}[theoremcount]{Remark}
\newtheorem{signature}[theoremcount]{Signature}
\newtheorem{theorem}[theoremcount]{Theorem}

\newtheorem*{definition*}{Definition}
\newtheorem*{signature*}{Signature}
\newtheorem*{axiom*}{Axiom}
\newtheorem*{theorem*}{Theorem}
\newtheorem*{proposition*}{Proposition}
\newtheorem*{lemma*}{Lemma}
\newtheorem*{corollary*}{Corollary}


% ForTheL environment
% ===================

% Space between paragraphs in forthel environments
\newlength{\ftlparskip}
\setlength{\ftlparskip}{0.5em}

% ForTheL environment
\newenvironment{forthel}{%
  \begin{mdframed}[backgroundcolor=lightgray!30, linecolor=lightgray!30]%
  \rmfamily%
  \setbool{forthel}{true}%
  \setlength{\parindent}{0pt}%
  \setlength{\parskip}{\ftlparskip}%
}{%
  \end{mdframed}%
  \rmfamily%
  \setbool{forthel}{false}%
}

% Wrapper for ForTheL environments. Warning: In the PDF it looks exactly like
% an ordinary ForTheL environment, but its content is not checked by Naproche.
% So use it wisely!
\newenvironment{greybox}{
  \begin{forthel}
}{
  \end{forthel}
}


% Comprehension terms
% ===================

\RequirePackage{linegoal}

% Gets the left-hand and right-hand side, resp., of an expression of the form
% "foo|bar"
\def\getfirst#1|#2\relax{#1}
\def\getsecond#1|#2\relax{#2}

% Comprehension term: "\class{... | ...}".
% Naproche allows to enclose the RHS within "\text{...}", e.g.
% "\class{x | \text{$x$ is greater than $y$}}".
% Use "\classtext{...}" instead of "\text{...}" to enable automatic line breaks
% in the RHS.
\newcommand{\class}[1]{%
  \left\{%
  \getfirst#1\relax%
  ~\middle\vert~%
  \linebreak[1]%
  \getsecond#1\relax%
  \right\}%
}

\newcommand{\classtext}[1]{\parbox{\linegoal}{#1}}


% More visible line breaks in proofs.
% ===================================

\newbool{forthel}

% Set length of \parskip to \ftlparskip in proofs in ForTheL environments.
%
% NOTE: "\vspace*{-\parskip}" is necessary to avoid additional space between
% theorem and proof
\renewenvironment{proof}[1][\unskip]{%
  \ifbool{forthel}{\setlength{\parskip}{\ftlparskip}\vspace*{-\parskip}}%
  \noindent\textit{Proof\xspace#1. }%
  \relax%
}
{%
  \hfill\qed
}


% Predefined symbols
% ==================

\newcommand{\dom}{\operatorname{dom}}
\newcommand{\fun}{\mathrm{\lambda}}


% Naproche logo
% =============

\newcommand{\Naproche}{\ensuremath{\mathbb{N}}aproche\xspace}


% Options
% =======

\newbool{foundations}
\newbool{arithmetic}
\newbool{settheory}

\DeclareOption{numberswithinsection}{%
    \counterwithin{theoremcount}{section}
}
\DeclareOption{numberswithinsubsection}{%
    \counterwithin{theoremcount}{subsection}
}
\DeclareOption{unmarked}{%
  \renewenvironment{forthel}{%
    \setbool{forthel}{true}%
  }{%
    \setbool{forthel}{false}%
  }
}
\DeclareOption{foundations}{%
    \setbool{foundations}{true}
}
\DeclareOption{arithmetic}{%
    \setbool{arithmetic}{true}
}
\DeclareOption{settheory}{%
    \setbool{settheory}{true}
}
\DeclareOption*{\PackageWarning{naproche}{Unknown ‘\CurrentOption’}}
\ProcessOptions\relax


% Foundations library
% ===================

\iffoundations
    \RequirePackage{amssymb, pgffor}

    \DeclareMathOperator{\id}{id}
    \DeclareMathOperator{\range}{range}

    \newcommand{\symdiff}{\mathop{\triangle}}
    \newcommand{\pow}{\mathcal{P}}
    \newcommand{\onto}{\twoheadrightarrow}
    \newcommand{\into}{\hookrightarrow}

    % E.g. `\set{1, 2, 3, 4}` prints the numbers 1, 2, 3 and 4 as a comma
    % separated list which is enclosed within curly braces.
    \def\set#1{%
      \ensuremath{%
        \{%
        \foreach[count=\i] \x in {#1}{%
          \ifnum\i>1,\,\fi%
          \x%
        }%
        \}
      }%
    }
\fi


% Arithmetic library
% ==================

\ifarithmetic
    \RequirePackage{amssymb}

    \newcommand{\Nat}{\mathbb{N}}
    \newcommand{\Prime}{\mathbb{P}}

    \let\succ\relax
    \let\exp\relax

    \DeclareMathOperator{\succ}{succ}
    \DeclareMathOperator{\pred}{pred}
    \DeclareMathOperator{\add}{add}
    \DeclareMathOperator{\mul}{mul}
    \DeclareMathOperator{\exp}{exp}
    \DeclareMathOperator{\fac}{fac}

    \renewcommand{\div}{\mathop{\operatorname{div}}}
    \renewcommand{\mod}{\mathop{\operatorname{mod}}}
\fi


% Set theory library
% ==================

\ifsettheory
    \RequirePackage{amssymb}

    \let\succ\relax

    \DeclareMathOperator{\succ}{succ}
    \DeclareMathOperator{\pred}{pred}

    \newcommand{\V}{\mathbf{V}}
    \newcommand{\Ord}{\mathbf{Ord}}
    \newcommand{\Card}{\mathbf{Card}}
\fi
