clafer: Compiles Clafer models to other formats: Alloy, JavaScript, JSON, HTML, Dot.

[ library, mit, model, program ] [ Propose Tags ]

Clafer is a general purpose, lightweight, structural modeling language developed at GSD Lab, University of Waterloo, and MODELS group at IT University of Copenhagen. Lightweight modeling aims at improving the understanding of the problem domain in the early stages of software development and determining the requirements with fewer defects. Clafer's goal is to make modeling more accessible to a wider range of users and domains. The tool provides a reference language implementation. It translates models to other formats (e.g. Alloy, JavaScript, JSON) to allow for reasoning with existing tools.

[Skip to Readme]
Versions [faq] 0.3.5,, 0.3.6,, 0.3.7, 0.3.8, 0.3.9, 0.3.10, 0.4.0, 0.4.1, 0.4.2,, 0.4.3, 0.4.4, 0.4.5 (info)
Change log
Dependencies aeson (>= && <, array (>=, base (>= && <5), bytestring (>=, clafer (==0.4.1), cmdargs (>=0.10.12), containers (>=, data-stringmap (>=, directory (>=, executable-path (>=0.0.3), filepath (>=, HTTP (>=4000.2.10), json-builder (>=0.3), lens (>=, lens-aeson (>=, mtl (>=, mtl-compat (>=0.2.1), network (>=, network-uri (>=, parsec (>=3.1.5), process (>=, split (>=0.2.2), string-conversions (>=, text (>=, transformers (>=, transformers-compat (>=0.3 && <0.5) [details]
License MIT
Author Kacper Bak, Jimmy Liang, Michal Antkiewicz, Ed Zulkoski, Luke Michael Brown, Paulius Juodisius, Rafael Olaechea
Maintainer Michal Antkiewicz <>
Revised Revision 1 made by mantkiew at 2015-09-24T02:00:50Z
Category Model
Home page
Source repo head: git clone git://
Uploaded by mantkiew at 2015-09-01T19:16:25Z
Distributions NixOS:0.4.5
Executables clafer
Downloads 9505 total (67 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs not available [build log]
Last success reported on 2015-12-11 [all 8 reports]


  • Language
    • Language.Clafer
      • Language.Clafer.ClaferArgs
      • Language.Clafer.Comments
      • Language.Clafer.Common
      • Language.Clafer.Css
      • Front
        • Language.Clafer.Front.AbsClafer
        • Language.Clafer.Front.ErrM
        • Language.Clafer.Front.LayoutResolver
        • Language.Clafer.Front.LexClafer
        • Language.Clafer.Front.ParClafer
        • Language.Clafer.Front.PrintClafer
      • Generator
        • Language.Clafer.Generator.Alloy
        • Language.Clafer.Generator.Choco
        • Language.Clafer.Generator.Concat
        • Language.Clafer.Generator.Graph
        • Language.Clafer.Generator.Html
        • Language.Clafer.Generator.Python
        • Language.Clafer.Generator.Stats
      • Intermediate
        • Language.Clafer.Intermediate.Desugarer
        • Language.Clafer.Intermediate.Intclafer
        • Language.Clafer.Intermediate.Resolver
        • Language.Clafer.Intermediate.ResolverInheritance
        • Language.Clafer.Intermediate.ResolverName
        • Language.Clafer.Intermediate.ResolverType
        • Language.Clafer.Intermediate.ScopeAnalysis
        • Language.Clafer.Intermediate.SimpleScopeAnalyzer
        • Language.Clafer.Intermediate.StringAnalyzer
        • Language.Clafer.Intermediate.Tracing
        • Language.Clafer.Intermediate.Transformer
        • Language.Clafer.Intermediate.TypeSystem
      • Language.Clafer.JSONMetaData
      • Optimizer
        • Language.Clafer.Optimizer.Optimizer
      • Language.Clafer.QNameUID
      • Language.Clafer.SplitJoin
    • Language.ClaferT
  • Paths_clafer


Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

For package maintainers and hackage trustees

Readme for clafer-0.4.1

[back to package description]

Hackage Build Status

Clafer, the Language


Clafer is a general-purpose lightweight structural modeling language developed by GSD Lab, University of Waterloo, and MODELS group at IT University of Copenhagen. Clafer can be used for modeling of static hierarchical structures but has no support for modeling the change of the structures over time (behavior). The main goal of Clafer is to make modeling more accessible to a wider range of users and domains.

There are many possible applications of Clafer; however, three are prominent:

  1. Product-Line Modeling - aims at representing and managing commonality and variability of assets in product lines and creating and verifying product configurations. Clafer naturally supports multi-staged configuration.

  2. Multi-Objective Product Optimization - aims at finding a set of products in a given product line that are optimal with respect to a set of objectives. Clafer multi-objective optimizer generates a Pareto front of optimal product configurations.

  3. Domain Modeling - aims at improving the understanding of the problem domain in the early stages of software development and determining the requirements with fewer defects. This is also known as Concept Modeling or Ontology Modeling.

Clafer, the Compiler

Clafer compiler provides a reference implementation of Clafer, the language. It translates models in Clafer to other formats (e.g., Alloy, JSON, Python, JS, HTML, DOT) to allow for reasoning and processing with existing tools (Alloy Analyzer, Choco3, and Z3 SMT solver, GraphViz).

Currently, the compiler is used by


Getting the Clafer Compiler

Clafer can be installed from a binary distribution (preferred), from Hackage, and from the source code.

Dependencies for running

Regardless of the installation method, the following are


  • Java Platform (JDK) v8+, 32bit on Windows, 64bit otherwise
    • needed only for running Alloy validation
  • Alloy4.2
    • needed only for Alloy output validation
  • GraphViz
    • the program dot is needed only in the html mode for SVG graph generation

Installation from binaries

Binary distributions of the release 0.4.1 of Clafer Tools for Windows, Mac, and Linux, can be downloaded from Clafer Tools - Binary Distributions.

  1. download the binaries and unpack <target directory> of your choice
  2. add the <target directory> to your system path so that the executables can be found

Installation from Hackage


Clafer is now available on Hackage and it can be installed using

  1. cabal update
  2. cabal install clafer
  3. on Windows cd C:\Users\<user>\AppData\Roaming\cabal\i386-windows-ghc-7.10.2\clafer-0.4.1
  4. on Linux ca ~/.cabal/share/x86_64-linux-ghc-7.10.2/clafer-0.4.1/
  5. to automatically download Alloy jars, execute
    • cd tools
    • make

Installation from the source code


On Windows

  • MSYS2
    • download MSYS2 installer
    • in MSYS2 console, execute
      • pacman -Syu
      • pacman -S make wget unzip diffutils

Important: Branches must correspond

All related projects are following the simultaneous release model. The branch master contains releases, whereas the branch develop contains code under development. When building the tools, the branches should match. Releases from branches 'masterare guaranteed to work well together. Development versions from branchesdevelop` should work well together but this might not always be the case.


  1. install the dependencies
  2. open the command line terminal. On Windows, open MSYS2 terminal.
  3. in some <source directory> of your choice, execute
    • git clone git://
  4. in <source directory>/clafer, execute
    • cabal update
    • make init
    • make


  1. Execute
    • make install to=<target directory>


On Windows, use / with the make command instead of \.

Integration with Sublime Text 2/3

See ClaferToolsST

Integration with VIM

See clafer-vim


Clafer Compiler

(As printed by clafer --help)

Clafer 0.4.1

clafer [OPTIONS] [FILE]

Common flags:
  -m --mode=CLAFERMODE                    Generated output type. Available
                                          CLAFERMODEs are: 'alloy' (default,
                                          Alloy 4.2); 'json' (intermediate
                                          representation of Clafer model);
                                          'clafer' (analyzed and desugared
                                          clafer model); 'html' (original model
                                          in HTML); 'graph' (graphical
                                          representation written in DOT
                                          language); 'cvlgraph' (cvl notation
                                          representation written in DOT
                                          language); 'python' (generates IR in
                                          python); 'choco' (Choco constraint
                                          programming solver). Multiple modes
                                          can be specified at the same time,
                                          e.g., '-m alloy -m html'.
  -o --console-output                     Output code on console.
  -i --flatten-inheritance                Flatten inheritance ('alloy' mode
     --timeout-analysis=INT               Timeout for analysis.
  -l --no-layout                          Don't resolve off-side rule layout.
  -n --nl --new-layout                    Use new fast layout resolver
  -c --check-duplicates                   Check duplicated clafer names in
                                          the entire model.
  -f --skip-resolver                      Skip name resolution.
  -k --keep-unused                        Keep uninstantated abstract clafers
                                          ('alloy' mode only).
  -s --no-stats                           Don't print statistics.
  -v --validate                           Validate outputs of all modes. Uses
                                          'tools/alloy4.2.jar' for Alloy
                                          models, and Clafer translator for
                                          desugared Clafer models. Use
                                          '--tooldir' to override the default
                                          location of these tools.
     --tooldir=DIR                        Specify the tools directory
                                          ('validate' only). Default: 'tools/'.
  -a --alloy-mapping                      Generate mapping to Alloy source
                                          code ('alloy' mode only).
     --self-contained                     Generate a self-contained html
                                          document ('html' mode only).
     --add-graph                          Add a graph to the generated html
                                          model ('html' mode only). Requires
                                          the "dot" executable to be on the
                                          system path.
     --sr --show-references               Whether the links for references
                                          should be rendered. ('html' and
                                          'graph' modes only).
     --add-comments                       Include comments from the source
                                          file in the html output ('html' mode
  -e --ecore2clafer                       Translate an ECore model into
     --ss=SCOPESTRATEGY --scope-strategy  Use scope computation strategy:
                                          none or simple (default).
     --check-afm --afm                    Throws an error if the cardinality
                                          of any of the clafers is above 1.
     --meta-data                          Generate a 'fully qualified
                                          name'-'unique ID' map ('.cfr-map').
                                          In Alloy and Choco modes, generate
                                          the scopes map ('.cfr-scope').
  -? --help                               Display help message
  -V --version                            Print version information
     --numeric-version                    Print just the version number

The dependencies among the command line arguments are described on the model wiki.

Multiple modes can be used at the same time. For example,

clafer model.cfr -m alloy -m json -m html -m graph --self-contained --show-references --no-stats

The mode -m alloy is only the default mode if no other modes are given. When other modes are given, the mode -m alloy must be added explicitly if needed.

Additionally, [OPTIONS] can also be specified directly in the model file by inserting the following compiler directive as the first line of the file:


for example

//# --keep-unused -m=alloy

Options given at command line override the options given in the file using //# which, in turn, override the defaults.

Using compiler directives

Compiler directives are comments of the form

//# <directive name>

The following directives are markers of locations in the input files for different purposes:

  • //# FRAGMENT - marks the beginning of the new module fragment.
  • //# GRAPH - marks the insertion point for a graph rendering. The graph is only produced in HTML mode with the argument --add-graph.
  • //# STATS - marks the insertion point for module statistics. The statistics can be omitted using the argument --no-stats.
  • //# SUMMARY - shorthand for //# GRAPH and //# STATS

Developing Clafer

Here is some information about the development of the Clafer compiler.


We are following the simplified version of the successful Git branching model. The branch master is for releases and hot fixes only. The branch develop is for minor development and for integration of features from feature branches. For any substantial work, branch off from develop and create a pull request back into develop after the work is completed. We do testing and code review before merging into develop. If the develop is ahead, merge it into the feature branch and perform integration testing there. To make a release, we create a pull request from develop into master. We tag master with version numbers after each release merge.


We have both automated tests and regression tests.

To run the automated tests (including both unit tests and doctests), execute

cabal test

To only run unit tests, execute cabal test test-suite.

To only run doctests, execute cabal test doctests.

For instructions for adding new modules to the doctest suite, see cabal integration.

To run all the automated tests and the regression tests, execute

make test

We do test-driven development in the following way:

  1. create a test case Clafer model in either test/positive or test/negative depending on whether a test case should compile successfully or return an error. For example, see a positive test case test/positive/redefinition.cfr.
  2. produce the intended compiler output automatically if possible and manually fix the output. Save the intended output as a regression test case. For example, see test/regression/redefinition.cfr.reg.
  3. implement the feature to reproduce the intended output: compiler the test case and execute
cd test
make diffRegressions

this will show you how the current output diverges from the intended output.

Modifying the grammar

We are using a customized version of BNCF. Clone the repository and install a binary of bnfc so that it's visible in your PATH. After changing the grammar, execute

make grammar

Need help?