• Stars
    star
    201
  • Rank 194,491 (Top 4 %)
  • Language
    Haskell
  • License
    Other
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

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

Repository Details

A tiny language, a subset of Haskell aimed at aiding teachers teach Haskell

Duet

A tiny language, a subset of Haskell (with type classes) aimed at aiding teachers teach Haskell

Run

Running code in Duet literally performs one substitution step at time. For example, evaluating (\x -> x + 5) (2 * 3), we get:

$ duet run demo.hs
(\x -> x + 5) (2 * 3)
(2 * 3) + 5
6 + 5
11

Note that this demonstrates basic argument application and non-strictness.

Docker run

Run with the docker distribution, to easily run on any platform:

$ docker run -it -v $(pwd):/w -w /w chrisdone/duet run foo.hs

(This should work on Linux, OS X or Windows PowerShell.)

The image is about 11MB, so it's quick to download.

Differences from Haskell

See also the next section for a complete example using all the available syntax.

  • Duet is non-strict, but is not lazy. There is no sharing and no thunks.
  • No module or import module system whatsoever.
  • No let syntax, no parameters in definitions e.g. f x = .. you must use a lambda. Representing let in the stepper presents a design challenge not currently met.
  • Kinds * are written Type: e.g. class Functor (f :: Type -> Type).
  • Kind inference is not implemented, so if you want a kind other than Type (aka * in Haskell), you have to put a kind signature on the type variable.
  • Indentation is stricter, a case's alts must be at a column larger than the case.
  • Duet does not have seq, but it does have bang patterns in cases. case x of !x -> .. is a perfectly legitimate way to force a value.
  • Infix operators are stricter: an infix operator must have spaces around it. You cannot have more than one operator without parentheses, therefore operator precedence does not come into play in Duet (this is intentional). This also permits you to write -5 without worrying about where it rests.
  • Superclasses are not supported.
  • Operator definitions are not supported.
  • There is only Integer and Rational number types: they are written as 1 or 1.0.
  • Any _ or _foo means "hole" and the interpreter does not touch them, it continues performing rewrites without caring. This is good for teaching.
  • There is no standard Prelude. The only defined base types are:
    • String
    • Char
    • Integer
    • Rational
    • Bool
  • You don't need a Show instance to inspect values; the interpreter shows them as they are, including lambdas.

View examples/syntax-buffet.hs for an example featuring all the syntax supported in Duet.

Print built-in types and classes

To print all types (primitive or otherwise), run:

$ duet types

Example output:

data Bool
  = True
  | False
data String
data Integer
data Rational

For classes and the instances of each class:

$ duet classes

Example output:

class Num a where
  plus :: forall a. (a -> a -> a)
  times :: forall a. (a -> a -> a)
instance Num Rational
instance Num Integer

class Neg a where
  negate :: forall a. (a -> a -> a)
  subtract :: forall a. (a -> a -> a)
  abs :: forall a. (a -> a)
instance Neg Rational
instance Neg Integer

class Fractional a where
  divide :: forall a. (a -> a -> a)
  recip :: forall a. (a -> a)
instance Fractional Rational

class Monoid a where
  append :: forall a. (a -> a -> a)
  empty :: forall a. a
instance Monoid String

class Slice a where
  drop :: forall a. (Integer -> a -> a)
  take :: forall a. (Integer -> a -> a)
instance Slice String

String operations

Strings are provided as packed opaque literals. You can unpack them via the Slice class:

class Slice a where
  drop :: Integer -> a -> a
  take :: Integer -> a -> a

You can append strings using the Monoid class:

class Monoid a where
  append :: a -> a -> a
  empty :: a

The String type is an instance of these classes.

main = append (take 2 (drop 7 "Hello, World!")) "!"

Evaluates strictly because it's a primop:

append (take 2 (drop 7 "Hello, World!")) "!"
append (take 2 "World!") "!"
append "Wo" "!"
"Wo!"

You can use this type and operations to teach parsers.

I/O

Basic terminal input/output is supported.

For example,

$ duet run examples/terminal.hs --hide-steps
Please enter your name:
Chris
Hello, Chris

And with steps:

$ duet run examples/terminal.hs
PutStrLn "Please enter your name: " (GetLine (\line -> PutStrLn (append "Hello, " line) (Pure 0)))
Please enter your name:
GetLine (\line -> PutStrLn (append "Hello, " line) (Pure 0))
Chris
(\line -> PutStrLn (append "Hello, " line) (Pure 0)) "Chris"
PutStrLn (append "Hello, " "Chris") (Pure 0)
Hello, Chris
Pure 0

How does this work? Whenever the following code is seen in the stepper:

PutStrLn "Please enter your name: " <next>

The string is printed to stdout with putStrLn, and the next expression is stepped next.

Whenever the following code is seen:

GetLine (\line -> <next>)

The stepper runs getLine and feeds the resulting string into the stepper as:

(\line -> <next>) "The line"

This enables one to write an example program like this:

data Terminal a
 = GetLine (String -> Terminal a)
 | PutStrLn String (Terminal a)
 | Pure a

main =
  PutStrLn
    "Please enter your name: "
    (GetLine (\line -> PutStrLn (append "Hello, " line) (Pure 0)))

More Repositories

1

elisp-guide

A quick guide to Emacs Lisp programming
1,397
star
2

intero

Haskell
1,021
star
3

jquery-console

A simple JQuery console emulator
JavaScript
668
star
4

hell

Haskell-based shell scripting language
Haskell
520
star
5

jl

Functional sed for JSON
Haskell
473
star
6

lucid

Clear to write, read and edit DSL for writing HTML
Haskell
284
star
7

vado

A demo web browser engine written in Haskell
Haskell
281
star
8

z

A strict, impure, curried, partially applied programming language with rather peculiar syntax.
Haskell
277
star
9

dynamic

Dynamic typing in Haskell
Haskell
193
star
10

tryhaskell

Try Haskell
Haskell
187
star
11

hulk

Haskell IRC daemon.
Haskell
142
star
12

emacs-config

My Emacs config
Emacs Lisp
118
star
13

haskell-style-guide

95
star
14

ircbrowse

An IRC analysis server.
Haskell
80
star
15

purify

Reproducible builds for PureScript
Haskell
73
star
16

labels

Declare and access tuple fields with labels
Haskell
61
star
17

descriptive

Self-describing consumers/parsers
Haskell
41
star
18

ghci-reload-demo

A demo of using GHCi as a persistent development environment
Haskell
41
star
19

chrisdone-xmonad

My xmonad configuration.
Haskell
33
star
20

freenect

Haskell interface to Kinect.
Haskell
26
star
21

bdo

Do things in the browser from Emacs, namely update the stylesheet (but maybe more later)
Haskell
26
star
22

ats-examples

Examples from Introduction to Programming in ATS
ATS
25
star
23

present

Make presentations for data types
Haskell
25
star
24

audit

Code auditing mode for Emacs
Emacs Lisp
25
star
25

webshow

Show programming language printed values in a web UI
Haskell
22
star
26

sdl2-sprite

Create and animate sprites easily with sdl2 (Haskell)
Haskell
22
star
27

prana

Interpreter for GHC Haskell
Haskell
22
star
28

flycheck-stack

A flycheck checker that uses stack ghci
Emacs Lisp
22
star
29

ini

Quick and easy INI configuration files for Haskell
Haskell
22
star
30

zenburn

Fork of Zenburn theme for emacs
Emacs Lisp
20
star
31

inflex

Pure, statically typed, content-addressable, programming language for spreadsheet use
Haskell
20
star
32

ghc-server

A server interface to GHC.
Haskell
20
star
33

advent-2017-maze-rust-haskell

Haskell
17
star
34

google-closure-purescript

Dockerfile
16
star
35

streaming-parsers

Streaming parsers collection
Haskell
15
star
36

ace

Attempto Controlled English parser and printer
Haskell
15
star
37

scrobble

Scrobbling server. A library providing server-side and client-side support for the Audioscrobbler Realtime Submission protocol: http://www.audioscrobbler.net/development/protocol/
Haskell
15
star
38

flo

Generate flow charts from your code base.
Haskell
14
star
39

conditions

Conditions for Haskell
Haskell
14
star
40

pgsql-simple

A mid-level client library for the PostgreSQL database, intended to be fast and easy to use.
Haskell
13
star
41

sandbox

Small random demonstrations of code
Haskell
12
star
42

duta

Haskell
12
star
43

pure-io

Pure IO monad.
Haskell
11
star
44

caseof

A simple way to query constructors, like cases but slightly more concise
Haskell
11
star
45

css

Monadic Haskell DSL for CSS.
Haskell
10
star
46

rocksdb-haskell-ng

Haskell
9
star
47

snappy

A reactive library for using SVG in the browser for Haskell
Haskell
9
star
48

display

Haskell
8
star
49

ety

Simple API for etymology online
Haskell
8
star
50

copy-paste-sync

An easy way to share clipboard (not encrypted) on a trusted LAN
Haskell
8
star
51

tdiff

Simply print the time difference between lines from stdin
Haskell
8
star
52

frisby

Linear time composable parser for PEG grammars
Haskell
7
star
53

forge

Haskell form library
Haskell
7
star
54

asp-mode

A simple ASP mode for Emacs which does syntax highlighting and indentation support
Emacs Lisp
7
star
55

basic-lens

Basic lens type and functions
Haskell
6
star
56

number

Emacs number manipulation
Emacs Lisp
6
star
57

osdkeys

Show keys pressed with an on-screen display (Linux only)
Haskell
6
star
58

emacs-magit-config

Handy pre-made Emacs config for using magit
Emacs Lisp
6
star
59

org-focus

Emacs Lisp
6
star
60

haskell-exercises

Haskell training test suite
Haskell
6
star
61

gmail

GMail client for Emacs
Emacs Lisp
6
star
62

maintainer

Haskell
5
star
63

hex-server

A minimal X11 server written in Haskell
Haskell
5
star
64

cabal-sign

Haskell
5
star
65

blogination

Very simple blog software
Haskell
5
star
66

sorting

Sorting algorithms
Haskell
4
star
67

stack-doc

Show stack's documentation in Emacs
Emacs Lisp
4
star
68

fore

Haskell Core to JavaScript compiler
Haskell
4
star
69

cron-daemon

Run a program as a daemon
Haskell
4
star
70

keyboard-stats

Gather and produce statistics about keyboard typing habbits
Haskell
4
star
71

codeparty

Online code sharing teaching platform
JavaScript
4
star
72

pdfinfo

Simple pdfinfo wrapper
Haskell
4
star
73

haskelldb-demo

Haskell
4
star
74

hog

IRC logger bot
Haskell
3
star
75

clockin

Track clocking in and out of work.
Haskell
3
star
76

sourcemap

Implementation of source maps as proposed by Mozilla and Google
Haskell
3
star
77

proclog

Haskell
3
star
78

haskell-trace

Add tracing to modules.
Haskell
2
star
79

hamlet-mode

Emacs Lisp
2
star
80

pid1-rust

Rust
2
star
81

senza

An interface to blaze-html without the need for operators.
Haskell
2
star
82

env-args

Source program arguments from the environment in a predictable way
Haskell
2
star
83

url-generic

Parse/format generic key/value URLs from record data types.
Haskell
2
star
84

typegraph

Haskell
1
star
85

riker

Simple reverse proxy, replacement for nginx
Haskell
1
star
86

servant-example

Haskell
1
star
87

liquid-post

Haskell
1
star
88

terminal

Haskell
1
star
89

snap-app

Small snap modules for MVC.
Haskell
1
star
90

yesod-lucid

Haskell
1
star
91

novella

Haskell
1
star
92

codepad

Haskell library for pasting to CodePad
Haskell
1
star