Skip to content

Instantly share code, notes, and snippets.

@jfdm
Created August 26, 2011 07:29
Show Gist options
  • Save jfdm/1172911 to your computer and use it in GitHub Desktop.
Save jfdm/1172911 to your computer and use it in GitHub Desktop.
An EMACS derived mode for constraint problems written in Essence', and a cheatsheet for same language.
\RequirePackage[l2tabu,orthodox]{nag}
\documentclass[a4paper,british,10pt,landscape,final]{article}
\usepackage{fixltx2e}
\usepackage[T1]{fontenc}
\usepackage{babel}
\usepackage[strict=true]{csquotes}
\usepackage[iso]{isodate}
\usepackage{microtype}
\usepackage{xspace}
\usepackage[colorlinks]{hyperref}
\usepackage[all,error]{onlyamsmath}
\usepackage[fixamsmath]{mathtools}
\usepackage{multicol}
\usepackage{array}
\usepackage{calc}
\usepackage{mathtools}
\usepackage{ifthen}
\usepackage[landscape,margin=1cm]{geometry}
\pagestyle{empty}
% Redefine section commands to use less space
\makeatletter
\renewcommand{\section}{\@startsection{section}{1}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{0.5ex plus .2ex}%x
{\normalfont\large\bfseries}}
\renewcommand{\subsection}{\@startsection{subsection}{2}{0mm}%
{-1explus -.5ex minus -.2ex}%
{0.5ex plus .2ex}%
{\normalfont\normalsize\bfseries}}
\renewcommand{\subsubsection}{\@startsection{subsubsection}{3}{0mm}%
{-1ex plus -.5ex minus -.2ex}%
{1ex plus .2ex}%
{\normalfont\small\bfseries}}
\newcommand{\eprime}{\ensuremath{\text{\textsc{Essence}}^{\prime}}\xspace}
\makeatother
\setcounter{secnumdepth}{0}
\setlength{\parindent}{0pt}
\setlength{\parskip}{0pt plus 0.5ex}
\setlength{\premulticols}{1pt}
\setlength{\postmulticols}{1pt}
\setlength{\multicolsep}{1pt}
\setlength{\columnsep}{2pt}
% -----------------------------------------------------------------------
\begin{document}
\raggedright
\footnotesize
\begin{center}
\Large{\textbf{\eprime v1.0 Cheat Sheet}} \\
\vspace*{-0.9em}
\rule{0.9\linewidth}{0.25pt}
\end{center}
\begin{multicols}{3}
\section{File Structure}
\begin{center}
\begin{tabular}{l>{\ttfamily}l}
Header & \verb!language Essence' <version>! \\
Parameters & Optional \\
Constants & Optional \\
Variables & Optional \\
Objectives & Optional \\
Constraints & Optional \\
\end{tabular}
\end{center}
\section{Types and Domains}
\subsection{Types}
\begin{center}
\begin{tabular}{l>{\ttfamily}l}
Integer & int \\
Boolean & bool \\
Matrix & matrix \\
Enum & --- \\
\end{tabular}
\end{center}
\subsection{Domains}
\begin{center}
\begin{tabular}{l>{\ttfamily}l}
Boolean & true, false \\
Int.\ Continuous & int(1..2) \\
Int.\ Sparese & int(1,3,5) \\
\end{tabular}
\end{center}
\subsection{Arrays}
\begin{center}
\begin{tabular}{l>{\ttfamily}l}
Dimension & row, column \\
Domain & bool, int \\
\end{tabular}
\end{center}
\begin{center}
\begin{verbatim}
<name> : matrix indexed by [<dimension>] of <domain>
\end{verbatim}
\end{center}
\section{Constant Definitions}
\subsection{Expressions}
\begin{center}
\begin{verbatim}
letting <name> be <constant>
\end{verbatim}
\end{center}
Constant is either a value or expression:
\begin{center}
\begin{verbatim}
letting c be 2
letting d be c*4
\end{verbatim}
\end{center}
\subsection{Domains}
\begin{center}
\begin{verbatim}
letting <name> be domain <domain>
\end{verbatim}
\end{center}
Convention is to use uppercase for variable names:
\begin{center}
\begin{verbatim}
letting INDEX be domain int(1..5)
letting RANGE be domain int(1..c)
\end{verbatim}
\end{center}
\subsection{Arrays}
\begin{center}
\begin{verbatim}
letting <name> <matrix type> be [<values>]
\end{verbatim}
\end{center}
Example:
\begin{center}
\begin{verbatim}
letting m : matrix indexed by [int(1..4) of int(1..10)
be [2,8,5,9]
letting t : matrix indexed
by [int(1..2) of int(1..4)]
of int(1..10)
be [[2,8,5,1], [3,7,9,4]]
\end{verbatim}
\end{center}
\section{Parameter Definitions}
Parameters passed from external file:
\begin{center}
\begin{verbatim}
given <name> : <domain>
\end{verbatim}
\end{center}
Note: Domains can be infinite.
Examples:
\begin{center}
\begin{verbatim}
given c : int
given t : int(1..1000)
given m : matrix indexed
by [int(1..c) of int(1..t)]
of bool
\end{verbatim}
\end{center}
\section{Variable Definitions}
\begin{center}
\begin{verbatim}
find <name> : <domain>
\end{verbatim}
\end{center}
Examples:
\begin{center}
\begin{verbatim}
find x : int(1..10)
find x,y,z : int(1..10)
find m : matrix indexed by [int(1..10)] of bool
\end{verbatim}
\end{center}
\section{Objective Definitions}
\begin{center}
\begin{tabular}{l>{\ttfamily}l}
Maximising & maximising \\
Minimising & minimising \\
\end{tabular}
\end{center}
Form:
\begin{center}
\begin{verbatim}
<objective> <variable|expression>
\end{verbatim}
\end{center}
Example: \verb!maximising x!
\columnbreak
\section{Constraints}
\begin{center}
\begin{verbatim}
such that <expression>, ..., <expression>
\end{verbatim}
\end{center}
\subsection{Operators}
\begin{center}
\begin{tabular}{ll}
Parenthesis & () \\
Arithmetic & \verb! ^ % * / + - min max! \\
Boolean & \verb+ ! /\ \/ => <=>+ \\
Relational & \verb+ > >= < <= = !=+ \\
Lexical & \verb! <lex <=lex >lex >=lex! \\
\end{tabular}
\end{center}
\subsection{Summation}
\begin{center}
\begin{verbatim}
sum <variable(s)> : <domain> . <expression>
\end{verbatim}
\end{center}
\subsection{Quantification}
\begin{center}
\begin{verbatim}
forall <variable(s) : <domain> . <expression>
exists <variable(s)> : <domain> . <expression>
\end{verbatim}
\end{center}
\subsection{Misc}
\begin{center}
\begin{tabular}{ll}
All Different & \verb!alldifferent(<matrix>)! \\
Table & \verb!table(<variable>,<tuple list>)! \\
\end{tabular}
\end{center}
\section{Example \eprime Files}
\subsection{Parameters}
\begin{verbatim}
language ESSENCE' 1.0.0
$ Parameter Instantiation
letting n be 7
\end{verbatim}
\subsection{Model}
\begin{verbatim}
language ESSENCE' 1.0.0
$ Parameter Declaration
given n : int
$ Constants
letting c be 5
$ Objectives and Variables
find x,y : int(1..n)
$ Constraints
such that
x + y >= c,
x + c*y = 0,
true
\end{verbatim}
\rule{0.3\linewidth}{0.25pt}
\scriptsize
Copyright \copyright\xspace2011 Jan de Muijnck-Hughes.\\
Under a Creative Commons \verb!(BY-NC-SA)! License\\
See \url{http://creativecommons.org/licenses/by-nc-sa/3.0/}.
\end{multicols}
\end{document}
;; eprime-mode.el -- Derived mode for writing Essence'
;; Copyright (C) 2011 by Jan de Muijnck-Hughes
;; Author: Jan de Muijnck-Hughes
;;
;;; DESCRIPTION
;;
;; A derived mode to highlight Essence' code. Currently this
;; mode only provides syntax highlighting, and comment support.
;; Future work will seek to add support for smart indentation.
;;
;;; INSTALLATION
;;
;; 1. Place eprime-mode.el in your .emacs.d directory
;; 2. Ensure that .emacs.d is on your load path.
;; 3. Add the following to your emacs init file:
;; (add-to-list 'auto-mode-alist (quote ("\\.eprime$" . eprime-mode)))
;; -------------------------------------------------------------------
;;; CODE
;; ------------------------------------------------------ [ Keywords ]
(defvar eprime-keywords '(
"letting" "be"
"indexed" "by" "of"
"given"
"find"
"maximising" "minimising"
"such" "that"
"max" "min"
"sum"
"forall" "exists" "alldifferent" "table"
))
;; --------------------------------------------------------- [ Types ]
(defvar eprime-types '(
"int" "bool" "matrix" "domain"
))
;; ----------------------------------------------------- [ Constants ]
(defvar eprime-preprocessor '(
"language" "ESSENCE"
))
;; -------------------------------------------------- [ Assign Faces ]
(defvar eprime-font-lock-defaults
;; Need to fix operators
`((
(":\\|\\.\\|\\\\\\|\\/\\|=\\|<\\|>\\|!\\|+\\|-\\|*\\|%"
. font-lock-constant-face)
( ,(regexp-opt eprime-keywords 'words) . font-lock-function-name-face)
( ,(regexp-opt eprime-types 'words) . font-lock-type-face)
( ,(regexp-opt eprime-preprocessor 'words) . font-lock-preprocessor-face)
)))
;; --------------------------------------------------- [ Clear memory ]
(setq eprime-keywords nil
eprime-types nil
eprime-preprocessor nil
)
;; -------------------------------------------------------------------
;; Essence' Definition
;; -------------------------------------------------------------------
(define-derived-mode eprime-mode fundamental-mode "Essence'"
"Major mode for editing Essence' files."
(defgroup eprime-mode nil
"Derived mode for Essance' Files" :group 'languages)
(defvar eprime-mode-hook nil "Hook for eprime-mode")
(modify-syntax-entry ?$ "<" eprime-mode-syntax-table)
(modify-syntax-entry ?\n ">" eprime-mode-syntax-table)
(make-local-variable 'eprime-font-lock-defaults)
(make-local-variable 'comment-start)
(make-local-variable 'comment-end)
(make-local-variable 'comment-start-skip)
(make-local-variable 'comment-column)
(make-local-variable 'comment-multi-line)
(make-local-variable 'comment-indent-function)
(setq font-lock-defaults eprime-font-lock-defaults
comment-start "$ "
comment-end ""
comment-start-skip "+ $+ \n"
comment-column 60
comment-multi-line nil
comment-indent-function 'java-comment-indent
indent-tabs-mode t
)
(run-hooks 'eprime-mode-hook)
)
(provide 'eprime-mode)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE language>
<!--
Copyright (C) 2007-2008
Jan de Muijnck-Hughes <jfdm@st-andrews.ac.uk>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<language name="EssencePrime" version="0.1" kateversion="2.5.9" section="Other" extensions="*.eprime" mimetype="text/eprime" casesensitive="1" author="Jan de Muijnck-Hughes" license="">
<!-- Specifiy items from source file that need to be highlighted-->
<highlighting>
<list name="preproc">
<item> language</item>
<item> ESSENCE' </item>
</list>
<list name="keywords">
<item> find </item>
<item> such </item>
<item> that </item>
<item> maximising</item>
<item> minimising</item>
<item> max</item>
<item> min</item>
<item> given </item>
<item> letting </item>
<item> indexed </item>
<item> by </item>
<item> be </item>
<item> of </item>
<item> forall </item>
<item> sum </item>
</list>
<list name="misc">
<item> int </item>
<item> matrix </item>
<item> alldiff </item>
</list>
<!-- Specifiy language in terms of contexts-->
<contexts>
<context name="Top_Level" attribute="Normal Text" lineEndContext="#stay">
<DetectSpaces />
<IncludeRules context="Strings" />
<IncludeRules context="Multi Char Operators Def" />
<DetectChar context="Commentary" char="$" />
<AnyChar context="#stay" attribute="Single Char Operators" String="|!:^*./=&lt;&gt;" />
<keyword context="#stay" attribute="Misc" String="misc" />
<keyword context="#stay" attribute="KeyWords" String="keywords" />
<keyword context="#stay" attribute="Operator" String="operators" />
<keyword context="#stay" attribute="Preprocessing" String="preproc" />
<RegExpr context="#stay" attribute="Number" String="(\d+(\.\d+)?|\.\d+)([eE][+-]?\d+)?[ij]?" />
<RegExpr context="#stay" attribute="Variable" String="[a-zA-Z]\w*" />
</context>
<context name="Commentary" attribute="Commentary" lineEndContext="#pop">
</context>
<context name="Strings" attribute="Strings" lineEndContext="#stay">
<RegExpr context="#stay" attribute="String" String="'([^'\\]|''|\\'|\\[^'])*'(?=[^']|$)" />
<RegExpr context="#stay" attribute="Incomplete String" String="'([^']|''|\\')*" />
</context>
<context name="Multi Char Operators Def" attribute="Single Char Operators" lineEndContext="#stay">
<StringDetect context="#stay" attribute="Multi Char Operators " String="!="/>
<StringDetect context="#stay" attribute="Multi Char Operators " String="/\"/>
<StringDetect context="#stay" attribute="Multi Char Operators " String="\/"/>
<StringDetect context="#stay" attribute="Multi Char Operators " String="=&gt;"/>
<StringDetect context="#stay" attribute="Multi Char Operators " String="=&lt;"/>
<StringDetect context="#stay" attribute="Multi Char Operators " String="&lt;lex"/>
<StringDetect context="#stay" attribute="Multi Char Operators " String="&lt;=lex"/>
<StringDetect context="#stay" attribute="Multi Char Operators " String="&gt;lex"/>
<StringDetect context="#stay" attribute="Multi Char Operators " String="&gt;=lex"/>
<StringDetect context="#stay" attribute="Multi Char Operators " String="&lt;=&gt;"/>
</context>
</contexts>
<!-- Apply Style information-->
<itemDatas>
<itemData name="Normal Text" defStyleNum="dsNormal"/>
<itemData name="Commentary" defStyleNum="dsComment" color="#009900" italic="1" />
<itemData name="Preprocessing" defStyleNum="dsKeyword"/>
<itemData name="Single Char Operators" defStyleNum="dsDataType" bold="1" />
<itemData name="Multi Char Operators " defStyleNum="dsDataType" bold="1" />
<itemData name="KeyWords" defStyleNum="dsKeyword"/>
<itemData name="Misc" defStyleNum="dsDataType"/>
</itemDatas>
</highlighting>
<!-- Specifiy Code Folding Information and comment information -->
<general>
<folding indentationsensitive="1" />
<emptyLines>
<emptyLine regexpr="\s+" casesensitive="false"/>
<emptyLine regexpr="\s*#.*$" casesensitive="false"/>
</emptyLines>
<comments>
<comment name="singleLine" start="$" position="afterwhitespace"/>
</comments>
<keywords casesensitive="1" />
</general>
</language>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment