项目作者: vmchale

项目描述 :
Madlang is a language for generative literature
高级语言: Haskell
项目地址: git://github.com/vmchale/madlang.git
创建时间: 2017-01-08T02:24:28Z
项目社区:https://github.com/vmchale/madlang

开源协议:Other

下载


Madlang DSL for generating random text

Windows build status
Build Status
Hackage

This is the Madlang DSL for generating text. You specify a template, and Madlang
will create randomized text from the template.

Madlang is an interpreted language, written in Haskell. Madlang can be used as
an EDSL for Haskell or using the command-line interpreter.

Madlang is intended to explore computational creativity and provide an easy
way to get started with generative literature.

Installation

Binary Releases

Head over to the releases
page
and grab a binary for
your platform.

Cabal

If you do not see you platform listed, you will have to install from source.
Download cabal and
GHC. Then:

  1. $ cabal update
  2. $ cabal new-install madlang

You may need to add $HOME/.local/bin to your PATH. To do so:

  1. $ echo 'export PATH=$HOME/.local/bin:$PATH' >> $HOME/.bashrc
  2. $ source $HOME/.bashrc

Tutorial

The smallest program possible in Madlang is simply a return declaration, viz.

  1. :return
  2. 1.0 "heads"
  3. 1.0 "tails"

The :return tells us this that this will be the final value when run, while
the numbers in front of the strings denote relative weights. Save this as
gambling.mad, and run

  1. $ madlang run gambling.mad
  2. heads

Now let’s try something a little more complicated:

  1. :define person
  2. 1.0 "me"
  3. 1.0 "you"
  4. :return
  5. 1.0 "The only one of us walking out of this room alive is going to be " person "."

A bit more sinister, perhaps. The :define statement there declares a new
identifier, which we can later reference. Save this as fate.mad and run:

  1. $ madlang run fate.mad
  2. The only one of us walking out of this room alive is going to be you.

We can also refer to another identifier within a :define block.

  1. :define coin
  2. 1.0 "heads"
  3. 1.0 "tails"
  4. :define realisticCoin
  5. 1.0 coin
  6. 0.03 "on its side"
  7. :return realisticCoin

In addition to identifiers, we can also define categories. Categories are just
groups of identifiers. We can define one like so:

  1. :define color
  2. 1.0 "yellow"
  3. 1.0 "blue"
  4. :define texture
  5. 1.0 "soft"
  6. 1.0 "scratchy"
  7. 1.0 "dimpled"
  8. :category adjective
  9. | color
  10. | texture
  11. :return
  12. 1.0 adjective

Then, when we can adjective, it will pick one of “yellow”, “blue”,…
“dimpled” with equal probability.

Finally, one of the most powerful features of madlang is the ability to
include libraries in a file. Open the following and save it as gambling.mad:

  1. :library
  2. :define coin
  3. 1.0 "heads"
  4. 1.0 "tails"

Then, open the following and save it in the same directory as
realistic-gambling.mad:

  1. :include gambling.mad
  2. :define realisticGambling
  3. 1.0 gambling-coin
  4. 0.03 "on its side"
  5. :return
  6. 1.0 realisticGambling

Then run it with:

  1. $ madlang run realistic-gambling.mad

madlang comes with several libraries prepackaged. You can install
them for the current user with:

  1. $ madlang install

Try this out:

  1. :include colors.mad
  2. :define weirdDog
  3. 1.0 colors-color "dog"
  4. :return
  5. 1.0 "On my walk today I saw a " weirdDog "."

EDSL

You can use Madlang as a Haskell EDSL, generating values of type RandTok.
This can be done a couple ways. One is to use the file embedder:

  1. randomText :: RandTok
  2. randomText = $(madFile "mad-src/some-bot.mad")

While the other is to use the madlang quasi-quoter:

  1. randomText :: RandTok
  2. randomText = [madlang|
  3. :include adjectives.mad
  4. :return
  5. 1.0 "I am feeling very " adjectives-adjective " today."
  6. |]

You can then transform this into a random text file with:

  1. generateText :: IO Text
  2. generateText = run randomText

Examples

There is a Shakespearean insult generator available to test out at my
site
. For a look at using Madlang as an EDSL,
check out my recursion scheme
generator

Tooling

Vim

There is a vim plugin available here.

Project Templates

There is a project template bundled with
pi, which you can install with

  1. $ curl -LSfs https://japaric.github.io/trust/install.sh | sh -s -- --git vmchale/project-init

and invoke with

  1. $ pi new madlang story

There is also a templated project
here that can be invoked via

  1. pi git vmchale/https://github.com/vmchale/madlang-miso story

Manpages

You can view documentation for madlang on Linux, Mac, or BSD by typing:

  1. $ man madlang

Contributions

Guide

Contributions, bug reports, and feature requests are emphatically welcome.
Please see the CONTRIBUTING.md guide for more specific details.

Release Naming

Releases are named using the releases.mad file found
here. You will need to install
the standard libraries using

  1. $ madlang install

before running

  1. $ just name