Skip to content

Instantly share code, notes, and snippets.

@eignnx
Last active April 2, 2020 00:06
Show Gist options
  • Save eignnx/96d743c9b493d2225c981e97a06fc4b4 to your computer and use it in GitHub Desktop.
Save eignnx/96d743c9b493d2225c981e97a06fc4b4 to your computer and use it in GitHub Desktop.
Lesson 3 of the `affix-grammar` beginner guide.
-- # Beginner Tutorial: Lesson 3
-- ### Data Variants
-- A rule can be parameterized by what's called a "data variant." Let's define one to illustrate.
data Gender = nonbinary | feminine | neutral
-- An instance of `Gender` can be one of the three possibilities listed.
--
-- *Aside: this isn't an exhaustive list of genders (that would probably be infinite), just some of the most common in English. See the exercise at the end!*
-- We'll try to generate sentences like:
-- > "They went to sleep."
--
-- > "She fell over."
--
-- > "It flew away."
rule some_sentence
= they.nonbinary "went to sleep."
| they.feminine "fell over."
| they.neutral "flew away."
-- In each of the alternatives, we're using the (as-yet-unwritten) `they` rule, and in each case we "pass in" a gender.
--
-- To define the `they` rule, we write `rule they.Gender` which declares that a `Gender` must be supplied.
rule they.Gender =
.nonbinary -> "They"
.feminine -> "She"
.neutral -> "It"
-- We "pattern match" on the gender and specify what `they` should mean in each
-- case.
--
-- Now click the `Generate` button a few times!
-- ### But we can do better!
-- All of the sentences in the `some_sentence` rule make sense (more or less) regardless of grammatical gender. So lets use an "unknown data variant", or "variable" so that *any* of the genders could be used in *any* of those sentences.
rule some_other_sentence
= they.Gender "went to sleep."
| they.Gender "fell over."
| they.G "flew away."
-- To use a variable, just type the name of the data variant (in this case, `Gender`). Note that as long as it's not ambiguous, you can shorten the name to a substring of the original name.
--
-- We can now generate 9 sentences instead of just 3!
--
-- Finally, we'll write a rule that just prints out both of the previous rules with some text to show from whence they came. Remember that `start` is the root of every generated sentence.
rule start
= "some_sentence:" some_sentence
| "some_other_sentence:" some_other_sentence
-- ## Exercise
-- Go the the `Edit` tab and try adding another gender and it's corresponding 3rd-person pronoun to this grammar!
-- ## Next Lesson
-- [Click here to go to Lesson 4](#)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment