• Stars
    star
    129
  • Rank 279,262 (Top 6 %)
  • Language
    Haskell
  • License
    Other
  • Created over 6 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A preprocessor for a Haskell record syntax using dot

record-dot-preprocessor Hackage version Stackage version Build status

In almost every programming language a.b will get the b field from the a data type, and many different data types can have a b field. The reason this feature is ubiquitous is because it's useful. The record-dot-preprocessor brings this feature to modern GHC versions. This feature has been proposed for Haskell as RecordDotSyntax. Since GHC 9.2 the OverloadedRecordDot and OverloadedRecordUpdate extensions implement much the same functionality. Some examples:

data Company = Company {name :: String, owner :: Person}
data Person = Person {name :: String, age :: Int}

display :: Company -> String
display c = c.name ++ " is run by " ++ c.owner.name

nameAfterOwner :: Company -> Company
nameAfterOwner c = c{name = c.owner.name ++ "'s Company"}

Here we declare two records both with name as a field, then write c.name and c.owner.name to get those fields. We can also write c{name = x} as a record update, which still works even though name is no longer unique.

How do I use this magic?

First install record-dot-preprocessor with either stack install record-dot-preprocessor or cabal update && cabal install record-dot-preprocessor. Then at the top of the file add:

  • Either: {-# OPTIONS_GHC -F -pgmF=record-dot-preprocessor #-} for the preprocessor.
  • Or: {-# OPTIONS_GHC -fplugin=RecordDotPreprocessor #-} and {-# LANGUAGE DuplicateRecordFields, TypeApplications, FlexibleContexts, DataKinds, MultiParamTypeClasses, TypeSynonymInstances, FlexibleInstances, UndecidableInstances, GADTs #-} for the GHC plugin.

The GHC plugin only runs on GHC 8.6 or higher, has some issues on Windows and has much better error messages. In contrast, the preprocessor runs everywhere and has more features.

You must make sure that the OPTIONS_GHC is applied both to the file where your records are defined, and where the record syntax is used. The resulting program will require the record-hasfield library.

What magic is available, precisely?

Using the preprocessor or the GHC plugin you can write:

  • expr.lbl is equivalent to getField @"lbl" expr (the . cannot have whitespace on either side).
  • expr{lbl = val} is equivalent to setField @"lbl" expr val (the { cannot have whitespace before it).
  • (.lbl) is equivalent to (\x -> x.lbl) (the . cannot have whitespace after).

Using the preprocessor, but not the GHC plugin:

  • expr{lbl1.lbl2 = val} is equivalent to expr{lbl1 = (expr.lbl1){lbl2 = val}}, performing a nested update.
  • expr{lbl * val} is equivalent to expr{lbl = expr.lbl * val}, where * can be any operator.
  • expr{lbl1.lbl2} is equivalent to expr{lbl1.lbl2 = lbl2}.

These forms combine to offer the identities:

  • expr.lbl1.lbl2 is equivalent to (expr.lbl1).lbl2.
  • (.lbl1.lbl2) is equivalent to (\x -> x.lbl1.lbl2).
  • expr.lbl1{lbl2 = val} is equivalent to (expr.lbl1){lbl2 = val}.
  • expr{lbl1 = val}.lbl2 is equivalent to (expr{lbl1 = val}).lbl2.
  • expr{lbl1.lbl2 * val} is equivalent to expr{lbl1.lbl2 = expr.lbl1.lbl2 * val}.
  • expr{lbl1 = val1, lbl2 = val2} is equivalent to (expr{lbl1 = val1}){lbl2 = val2}.

How does this magic compare to other magic?

Records in Haskell are well known to be pretty lousy. There are many proposals that aim to make Haskell records more powerful using dark arts taken from type systems and category theory. This preprocessor aims for simplicity - combining existing elements into a coherent story. The aim is to do no worse than Java, not achieve perfection.

Any advice for using this magic?

The most important consideration is that all records used by a.b or a{b=c} syntax must have HasField instances, which requires either running the preprocessor/plugin over the module defining them, or writing orphan instances by hand. To use records which don't have such instances use normal selector functions (e.g. b a) and insert a space before the { (e.g. a {b=c}).

Limitations

  • The preprocessor doesn't deal with anti-quoted expressions inside QuasiQuotes, e.g. [D.pgSQL|$ SELECT ${dummy.x} :: text|].

More Repositories

1

hlint

Haskell source code suggestions
Haskell
1,459
star
2

ghcid

Very low feature GHCi based IDE
Haskell
1,130
star
3

shake

Shake build system
Haskell
744
star
4

hoogle

Haskell API search engine
Haskell
689
star
5

tagsoup

Haskell library for parsing and extracting information from (possibly malformed) HTML/XML documents
Haskell
231
star
6

bake

UNMAINTAINED: Continuous integration server
Haskell
130
star
7

weeder

Detect dead exports or package imports
Haskell
124
star
8

debug

Haskell library for debugging
JavaScript
121
star
9

rattle

Forward build system with speculation and caching
Haskell
102
star
10

spaceleak

Notes on space leaks
Haskell
101
star
11

extra

Extra Haskell functions
Haskell
93
star
12

cmdargs

Haskell library for command line argument processing
Haskell
91
star
13

build-shootout

Comparison of build program expressive power
Haskell
88
star
14

uniplate

Haskell library for simple, concise and fast generic operations.
Haskell
74
star
15

safe

Haskell library for safe (pattern match free) functions
Haskell
45
star
16

ghc-make

An alternative to ghc --make which supports parallel compilation of modules and runs faster when nothing needs compiling.
Haskell
40
star
17

interpret

Rust
38
star
18

neil

General tools for Neil
Haskell
38
star
19

profiterole

GHC prof manipulation script
Haskell
30
star
20

nsis

Haskell DSL for producing Windows Installer using NSIS
Haskell
26
star
21

derive

A Haskell program and library to derive instances for data types
TeX
25
star
22

supero

Haskell optimisation tool based on supercompilation
Haskell
25
star
23

hexml

A bad XML parser
C
19
star
24

offline-stack

Install Stack without internet access
Haskell
18
star
25

catch

Haskell pattern match analsyis checker
Haskell
15
star
26

js-jquery

Haskell library to obtain minified jQuery code
Haskell
9
star
27

rexe

.exe forwarder, to allow replacing binaries on PATH
Haskell
8
star
28

office

Macros for Microsoft Office
VBA
7
star
29

VSHaskell

Visual Studio 2010 addin
C#
7
star
30

record-hasfield

A version of HasField that will be available in future GHC
Haskell
7
star
31

shake-paper

Paper on the new GHC Shake-based build system
HTML
6
star
32

lasagna

Checker for Haskell layering violations
Haskell
5
star
33

guihaskell

A graphical REPL and development environment for Haskell
Haskell
5
star
34

hwwg

Haskell Website Working Group
5
star
35

shake-bazel

Experimenting with Shake and Bazel combined
Haskell
4
star
36

blogs

TeX
4
star
37

haskell-parser

Haskell parser based on that from GHC
Haskell
4
star
38

filepattern

A file path matching library
Haskell
4
star
39

idris-playground

Playing around with Idris
Idris
4
star
40

proplang

A Haskell library for functional GUI development
Haskell
4
star
41

thesis

My PhD thesis - Transformation and Analysis of Functional Programs
Haskell
3
star
42

js-flot

Haskell library to obtain minified Flot code
Haskell
3
star
43

qed

Experiments writing a prover
Haskell
3
star
44

core-playground

Simple Core language for Haskell experiments
Haskell
3
star
45

js-dgtable

Haskell library to obtain minified jquery.dgtable code
JavaScript
3
star
46

neil-check

Run neil and hlint on all my projects
Haskell
3
star
47

shake-examples

3
star
48

proof

Haskell library for writing proofs
Haskell
2
star
49

ci-check

Test the various supported CI's
2
star
50

hogle-dead

This repo has been moved to the master branch of https://github.com/ndmitchell/hoogle.
Haskell
2
star
51

stack-3137

Reproduce https://github.com/commercialhaskell/stack/issues/3137
Haskell
2
star
52

winhaskell

Windows Haskell GUI interpretter
C++
1
star
53

fossilizer

Generate 3D images of fossil bedding surfaces
JavaScript
1
star
54

bug-i386-ghc84

1
star
55

proposition

Haskell library for manipulating propositions.
Haskell
1
star
56

tex2hs

A program to check for type errors in a Latex document
Haskell
1
star
57

ndmitchell

1
star
58

hlint-test

Haskell
1
star
59

ghc-process

Compiling and linking files in a single process using the GHC API
Haskell
1
star
60

shark

A bad replacement for cabal/stack
1
star
61

index-search

Searching compressed text indicies
C
1
star
62

awesomo

Prototype optimiser for Haskell programs
Haskell
1
star
63

ninjasmith

Ninja file generator and tester
Haskell
1
star
64

firstify

A Haskell library to transform Yhc Core programs to first-order
TeX
1
star