%%
%% Calculi names.
%%
\newcommand{\Links}{Links\xspace}
\newcommand{\CoreLinks}{\ensuremath{\mathsf{CoreLinks}}\xspace}
\newcommand{\BCalc}{\ensuremath{\lambda_{\mathsf{b}}}\xspace}
\newcommand{\BCalcRec}{\ensuremath{\lambda_{\mathsf{b}+\mathsf{rec}}}\xspace}
\newcommand{\HCalc}{\ensuremath{\lambda_{\mathsf{h}}}\xspace}
\newcommand{\SCalc}{\ensuremath{\lambda_{\mathsf{h^\dagger}}}\xspace}
\newcommand{\HSCalc}{\ensuremath{\lambda_{\mathsf{h^\delta}}}\xspace}
\newcommand{\EffCalc}{\ensuremath{\lambda_{\mathsf{eff}}}\xspace}
\newcommand{\UCalc}{\ensuremath{\lambda_{\mathsf{u}}}\xspace}

%%
%% Calculi terms and types type-setting.
%%
\newcommand{\revto}{\ensuremath{\leftarrow}}

\newcommand{\dec}[1]{\mathsf{#1}}
\newcommand{\keyw}[1]{\mathbf{#1}}
\newcommand{\Handle}{\keyw{handle}}
\newcommand{\ShallowHandle}{\ensuremath{\keyw{handle}^\dagger}}
\newcommand{\With}{\keyw{with}}
\newcommand{\Let}{\keyw{let}}
\newcommand{\Rec}{\keyw{rec}}
\newcommand{\In}{\keyw{in}}
\newcommand{\Do}{\keyw{do}}
\newcommand{\Return}{\keyw{return}}
\newcommand{\Val}{\keyw{val}}
\newcommand{\Case}{\keyw{case}}
\newcommand{\If}{\keyw{if}}
\newcommand{\Then}{\keyw{then}}
\newcommand{\Else}{\keyw{else}}
\newcommand{\Absurd}{\keyw{absurd}}
\newcommand{\Record}[1]{\ensuremath{\langle #1 \rangle}}
\newcommand{\Unit}{\Record{}}
\newcommand{\Inl}{\keyw{inl}}
\newcommand{\Inr}{\keyw{inr}}
\newcommand{\Thunk}{\lambda \Unit.}

\newcommand{\Pre}[1]{\mathsf{Pre}(#1)}
\newcommand{\Abs}{\mathsf{Abs}}
\newcommand{\Presence}{\mathsf{Presence}}
\newcommand{\Row}{\mathsf{Row}}
\newcommand{\Type}{\mathsf{Type}}
\newcommand{\Ground}{\mathsf{ground}}

\newcommand{\Comp}{\mathsf{Comp}}
\newcommand{\Effect}{\mathsf{Effect}}
\newcommand{\Handler}{\mathsf{Handler}}

\newcommand{\ZeroType}{0}
\newcommand{\UnitType}{1}
\newcommand{\One}{1}
\newcommand{\Int}{\mathsf{Int}}
\newcommand{\Bool}{\mathsf{Bool}}
\newcommand{\List}{\mathsf{List}}
\newcommand{\Nat}{\mathsf{Nat}}
\newcommand{\Choose}{\dec{Choose}}
\newcommand{\Count}{\dec{count}}
\newcommand{\GenericSearch}{\dec{genericSearch}}
\newcommand{\Predicate}{\dec{Predicate}}
\newcommand{\Point}{\dec{Point}}
\newcommand{\Branch}{\dec{Branch}}
\newcommand{\Get}{\dec{Get}}
\newcommand{\Put}{\dec{Put}}
\newcommand{\Zero}{\dec{Zero}}
\newcommand{\Fail}{\dec{Fail}}
\newcommand{\Read}{\dec{Read}}
\newcommand{\Write}{\dec{Write}}

\newcommand{\True}{\mathsf{true}}
\newcommand{\False}{\mathsf{false}}

\newcommand{\eff}{!}
\newcommand{\typ}[2]{#1 \vdash #2}
\newcommand{\typv}[2]{#1 \vdash #2}
\newcommand{\typc}[3]{#1 \vdash #2 \eff #3}
\newcommand{\Harrow}{\Rightarrow}

\newcommand{\FTV}{\ensuremath{\mathrm{FTV}}}
\newcommand{\FV}{\ensuremath{\mathrm{FV}}}

\newcommand{\reducesto}[0]{\ensuremath{\leadsto}}
\newcommand{\areducesto}{\ensuremath{\reducesto_{\textrm{a}}}}
\newcommand{\stepsto}[0]{\ensuremath{\longrightarrow}}
\newcommand{\EC}{\ensuremath{\mathcal{E}}}

\newcommand{\BL}{\ensuremath{\mathsf{BL}}}

\newcommand{\dom}{\ensuremath{\mathsf{dom}}}

\newcommand{\Res}{\keyw{res}}

%% Handler projections.
\newcommand{\hret}{H^{\mathrm{ret}}}
\newcommand{\hval}{\hret}
\newcommand{\hops}{H^{\mathrm{ops}}}
%\newcommand{\hex}{H^{\mathrm{ex}}}
\newcommand{\hell}{H^{\ell}}

\newcommand{\depth}{\delta}

\newcommand{\alertbox}[2]{{\par\noindent\small\color{red} \framebox{\parbox{\dimexpr\linewidth-2\fboxsep-2\fboxrule}{\textbf{#1:} #2}}}}
\newcommand{\todo}[1]{\alertbox{TODO}{#1}}
\newcommand{\dhil}[1]{\alertbox{Daniel}{#1}}

%%
%% Labels
%%
\newcommand{\slab}[1]{\textrm{#1}}
\newcommand{\rulelabel}[2]{\ensuremath{\mathsf{#1\textrm{-}#2}}}
\newcommand{\klab}[1]{\rulelabel{K}{#1}}
\newcommand{\semlab}[1]{\rulelabel{S}{#1}}
\newcommand{\usemlab}[1]{\rulelabel{U}{#1}}
\newcommand{\tylab}[1]{\rulelabel{T}{#1}}
\newcommand{\mlab}[1]{\rulelabel{M}{#1}}
\newcommand{\siglab}[1]{\rulelabel{Sig}{#1}}
\newcommand{\rowlab}[1]{\rulelabel{R}{#1}}

%%
%% Syntactic categories.
%%
\newcommand{\CatName}[1]{\textrm{#1}}
\newcommand{\CompCat}{\CatName{Comp}}
\newcommand{\UCompCat}{\CatName{UComp}}
\newcommand{\UValCat}{\CatName{UVal}}
\newcommand{\SCompCat}{\CatName{SComp}}
\newcommand{\SValCat}{\CatName{SVal}}
\newcommand{\SPatCat}{\CatName{SPat}}
\newcommand{\ValCat}{\CatName{Val}}
\newcommand{\VarCat}{\CatName{Var}}
\newcommand{\ValTypeCat}{\CatName{VType}}
\newcommand{\CompTypeCat}{\CatName{CType}}
\newcommand{\HandlerTypeCat}{\CatName{HType}}
\newcommand{\PresenceCat}{\CatName{Presence}}
\newcommand{\TypeCat}{\CatName{Type}}
\newcommand{\TyVarCat}{\CatName{TVar}}
\newcommand{\KindCat}{\CatName{Kind}}
\newcommand{\RowCat}{\CatName{Row}}
\newcommand{\EffectCat}{\CatName{Effect}}
\newcommand{\TermCat}{\CatName{Term}}
\newcommand{\LabelCat}{\CatName{Label}}
\newcommand{\TyEnvCat}{\CatName{TyEnv}}
\newcommand{\KindEnvCat}{\CatName{KindEnv}}
\newcommand{\EvalCat}{\CatName{Cont}}
\newcommand{\UEvalCat}{\CatName{UCont}}
\newcommand{\HandlerCat}{\CatName{HDef}}

%%
%% Lindley's array stuff.
%%
\newcommand{\ba}{\begin{array}}
\newcommand{\ea}{\end{array}}

\newcommand{\bl}{\ba[t]{@{}l@{}}}
\newcommand{\el}{\ea}


%%
%% Lindley's syntax, reductions, equations, and derivation environments.
%%
\newenvironment{syntax}{\[\ba{@{}l@{\quad}r@{~}c@{~}l@{}}}{\ea\]\ignorespacesafterend}
\newenvironment{reductions}{\[\ba{@{}l@{\qquad}@{}r@{~~}c@{~~}l@{}}}{\ea\]\ignorespacesafterend}

\newenvironment{eqs}{\ba{@{}r@{~}c@{~}l@{}}}{\ea}
\newenvironment{equations}{\[\ba{@{}r@{~}c@{~}l@{}}}{\ea\]\ignorespacesafterend}
\newenvironment{derivation}{\begin{displaymath}\ba{@{}r@{~}l@{}}}{\ea\end{displaymath}\ignorespacesafterend}
\newcommand{\reason}[1]{\quad (\text{#1})}


\newenvironment{smathpar}{\vspace{-3ex}\small\begin{mathpar}}{\end{mathpar}\normalsize\ignorespacesafterend}

%%
%% Defined-as equality
%%
\newcommand{\defas}[0]{\mathrel{\overset{\makebox[0pt]{\mbox{\normalfont\tiny\text{def}}}}{=}}}
\newcommand{\defnas}[0]{\mathrel{:=}}
\newcommand{\simdefas}[0]{\mathrel{\overset{\makebox[0pt]{\mbox{\normalfont\tiny\text{def}}}}{\simeq}}}

%%
%% Partiality
%%
\newcommand{\pto}[0]{\ensuremath{\rightharpoonup}}

%%
%% Lists
%%
\newcommand{\nil}{\ensuremath{[]}}
\newcommand{\cons}{\ensuremath{::}}

\newcommand{\concat}{\mathbin{+\!\!+}}
\newcommand{\revconcat}{\mathbin{\widehat{\concat}}}

%%
%% CPS notation
%%
% static / dynamic stuff
\newcommand{\scol}[1]{{\color{blue}#1}}
\newcommand{\dcol}[1]{{\color{red}#1}}

\newcommand{\static}[1]{\scol{\overline{#1}}}
\newcommand{\dynamic}[1]{\dcol{\underline{#1}}}
\newcommand{\nary}[1]{\overline{#1}}

\newcommand{\slam}{\static{\lambda}}
\newcommand{\dlam}{\dynamic{\lambda}}
\newcommand{\sapp}{\mathbin{\static{@}}}
\newcommand{\dapp}{\mathbin{\dynamic{@}}}

\newcommand{\reify}{\mathord{\downarrow}}
\newcommand{\reflect}{\mathord{\uparrow}}

\newcommand{\scons}{\mathbin{\static{\cons}}}
\newcommand{\dcons}{\mathbin{\dynamic{\cons}}}

\newcommand{\snil}{\static{\nil}}
\newcommand{\dnil}{\dynamic{\nil}}

\newcommand{\sRecord}[1]{\static{\langle}#1\static{\rangle}}
\newcommand{\dRecord}[1]{\dynamic{\langle}#1\dynamic{\rangle}}

\newcommand{\sQ}{\mathcal{Q}}
\newcommand{\sV}{\mathcal{V}}
\newcommand{\sW}{\mathcal{W}}

\newcommand{\sM}{\mathcal{M}}
\renewcommand{\snil}{\reflect \dnil}

\newcommand{\cps}[1]{\ensuremath{\llbracket #1 \rrbracket}}
\newcommand{\pcps}[1]{\top\cps{#1}}

\newcommand{\hforward}{M_{\textrm{forward}}}
\newcommand{\hid}{V_{\textrm{id}}}

% continuation application
\newcommand{\kapp}{\keyw{app}}

%%% Continuation names
%%% The following set of macros are a bit more consistent with those
%%% currently used by the abstract machine, and don't use the plural
%%% convention of functional programming.

% dynamic
\newcommand{\dlf}{f}    % let frames
\newcommand{\dlk}{s}    % let continuations
\newcommand{\dhf}{q}    % handler frames
\newcommand{\dhk}{ks}    % handler continuations
\newcommand{\dhkr}{rk}  % reverse handler continuations
\newcommand{\dLet}{\dynamic{\Let}}
\newcommand{\dIn}{\dynamic{\In}}

% static
\newcommand{\slf}{\phi}    % let frames
\newcommand{\slk}{\sigma}  % let continuations
\newcommand{\shf}{\theta}  % handler frames
\newcommand{\shk}{\kappa}  % handler continuations
\newcommand{\sLet}{\static{\Let}}
\newcommand{\sIn}{\static{\In}}
% \newcommand{\sk}{\kappa}
% \newcommand{\sks}{\mathit{\kappa s}}
\newcommand{\sks}{\kappa}
% \newcommand{\sh}{\eta}
\newcommand{\sk}{\theta}
\newcommand{\sh}{\chi}
\newcommand{\sx}{\varepsilon}

\newcommand{\sP}{\mathcal{P}}
\newcommand{\VS}{VS}
\newcommand{\Vmap}{\keyw{vmap}}
\newcommand{\Vmapsnd}{\keyw{vmapsnd}}
\newcommand{\Fun}{\keyw{fun}}

% Canonical variables for handler components
\newcommand{\vhret}{h^{\mathrm{ret}}}
\newcommand{\vhops}{h^{\mathrm{ops}}}
\newcommand{\svhret}{\chi^{\mathrm{ret}}}
\newcommand{\svhops}{\chi^{\mathrm{ops}}}

% \newcommand{\dk}{\dRecord{fs,\dRecord{\vhret,\vhops}}}
\newcommand{\dk}{k}

