# Grammar as Functions

Grammar concepts can be thought of like mathematical functions.

Grammar concepts can be viewed in terms of functions. This provides another valid way to look at and understand them. It’s useful for people who are good at math or programming, so they can use knowledge they already have to make learning grammar easier. And it’s useful for people who want to understand grammar in depth by analyzing it from multiple angles.

I’ll give a brief explanation of functions, but if you don’t already know about functions, then this article will probably be hard. You’ll also need familiarity with parts of speech in English (like nouns and prepositions).

## What’s a Function?

A function maps a set of inputs to a set of outputs using a defined set of rules.

A map tells you correspondences between things. You can think of the possible inputs written as a vertical list and the possible outputs as a second list. Then a map is just a bunch of arrows pointing from things on the first list to things on the second list. If a function takes multiple inputs, then the list of inputs should have groups. E.g. if we’re dealing with a function to divide two numbers, then (1, 2) is a valid input. So is (25, 777) or (2, 1). Every unique ordering of every unique group is a different input.

If the function in the diagram is used with a 1 as input, then the output is 3. If it has two inputs (a 2 and a 1 in that order), then the output is 4.

Inputs to a function are usually any number in a category, e.g. positive integers, though functions with only five valid inputs are possible. Function outputs normally follow a clear pattern based on the inputs, but any arbitrary mapping is a valid function.

We’ll only consider functions with exactly one output which is deterministic. Deterministic means a function always gives the same output for the same inputs. Other functions can be indeterministic by e.g. including a dice roll step.

Outputs are also called return values.

An example function is plus-three. It takes a single number as input, and returns a single number as output which is 3 larger than the input. If the input is 10, then the output is always 13. We can write it many ways including like this:

define-function plus-three(x): x+3

The important things here are the name of the function (plus-three), the list of inputs (x) and the output (x+3). Inputs are given names so we can conveniently refer to them when writing the output. In this case, the input is named “x”. Here’s the general format:

define-function name(inputs): output

You can view a function as a procedure, method, or list of steps. This function says to take the input named x, then add 3 to it, then return that value.

Functions can be complex and have a lot of steps, but for our grammar analysis we won’t even specify the steps to get from the inputs to the output. We’ll just talk about what functions give what final outputs.

## Words as Grammatical Functions

A preposition is an English function which takes a noun as input and returns a modifier. For example:

define-function with(noun): modifier

define-function on(noun): modifier

define-function at(noun): modifier

So a whole group of words – a preposition and its noun (and its noun’s modifiers) – functions as a modifier for something else.

All prepositions follow the same pattern. They all map one noun to one modifier. Going forward, I’ll write about grammar functions in terms of general patterns like:

define-function preposition(noun): modifier

But we’re not really talking about a single function named “preposition”. Each preposition has its own function. But they all fit a pattern which is what I want to point out.

I’ll go through the function patterns for the other parts of speech.

define-function noun(modifiers…): noun

A noun takes an unlimited number of modifiers and returns a noun (a thing). Zero modifiers is fine too. The modifiers change the meaning of the thing some, so the returned noun is different than the original noun. Like before you had a box, and now you have a purple box. The box gained a trait.

define-function modifier(modifiers…): modifier

Modifiers can be modified too, e.g. “hot” can be modified with “very”. They return a modifier which is different than the original one, e.g. by being more or less intense.

define-function action-verb(subject, object, indirect-object, modifiers…): finite-clause

The objects and modifiers are optional but the subject is mandatory.

This is saying that an action-verb, such as “swim”, takes some inputs (a subject, and possibly an object, indirect object or modifiers) and returns a finite-clause as its output. The valid inputs can depend on the specific verb. Some verbs can’t take an object. “Swim” usually doesn’t take an object but it can. “Swim” can’t take an indirect object.

define-function linking-verb(subject, complement, modifiers…): finite-clause

For linking verbs, the complement is mandatory while the modifiers are optional.

There are two different ways to look at conjunctions. I’ll call the one a “conjunction” and the other a “subordinator”. Sometimes one is more appropriate, but there’s often some flexibility or ambiguity so neither way is wrong. When there are multiple valid options, it’s helpful to understand all of them.

define-function conjunction(things…): thing

The things in the input have to all be the same type, e.g. modifiers, nouns, or finite-clauses. A minimum of two things is required, though sometimes people start a sentence with a conjunction like “and” so you’d have to look in another sentence to find the second thing. Some conjunctions take two or more inputs, while others take exactly two. The output from a conjunction is a thing of the same type as the inputs. E.g. if you conjoin some nouns, the output is a noun too.

define-function subordinator(finite-clause): modifier

A subordinator takes one finite clause and returns a modifier. How is this like a conjunction? It joins two things. The clause becomes a modifier, so it’s joined to whatever it modifies.

If the subordinator is a “subordinating conjunction”, then the modifier it returns must modify a clause. For example, the phrase “because I like reading” can’t modify a noun. It has to modify a clause like “I read a book”.

Relative pronouns like “who” are a type of subordinator that can create other types of modifiers, e.g. modifiers for nouns. For example, “I met a person who was tall.” uses the clause “who was tall” to modify the noun “person”.

Since subordinating conjunctions take one clause as input and their output modifies another clause, they join two finite clauses together. They tell you a relationship between two clauses. At least that’s one viewpoint. I usually prefer to treat subordinating conjunctions as conjunctions rather than as subordinators.

For more completeness, here are the non-finite verbs:

define-function gerund(subject, object, modifiers…): noun

define-function participle(subject, object, modifiers…): modifier

define-function infinitive(subject, object, modifiers…): noun or modifier

For non-finite verbs, all the inputs are optional. I didn’t specify indirect objects or complements but they could be present too. If you wanted, you could define action- and linking- versions separately or list more optional inputs. This stuff is somewhat approximate anyway – it doesn’t have the same precision as math or software - so I’m not concerned with trying to make the details perfect.

That covers a lot of English up to the sentence level. You could define more functions, e.g. a paragraph function that takes sentences and returns a big thought. An essay function would take multiple paragraphs and return a complete argument.

The point is to give you a conceptual model of grammar which is sometimes useful.

## Nested Functions

A sentence involves functions nested within other functions. In other words, the output of one function may be the input of another function. Consider the sentence “I ate juicy steak.” We’ll treat words as functions only when they have one or more inputs. Then we get:

ate(I, steak(juicy))

One of the inputs to the “ate” function is the output of another function (“steak”).

To evaluate nested functions in steps, start by evaluating the most nested functions which have no functions in their inputs. After you replace them with their output, there are now additional functions with no functions in their inputs. Here, we evaluate steak(juicy) first to get the concept of a juicy steak. That gives us:

ate(I, juicy-steak)

Now the “ate” can be evaluated because it has only simple inputs, not functions as inputs. That gives us our final understanding of the sentence. An eating action happened in the past, the eater was me, and the thing eaten was juicy-steak.

## Grammar Trees

Grammar functions are related to grammar trees (read about trees in general here). Child nodes are the inputs for their parent node. The output of a function provides the input to its parent node (the functions are nested).

To evaluate a tree, start by looking at nodes with no grandchildren. You can evaluate those. (Nodes with no children are simple and don’t need attention. They have no inputs. Nothing happens there.) When you evaluate those, you remove them plus their children and replace that whole subtree with their output. Now you’ve removed one level from that part of the tree. As you do this, you’ll find that now some higher nodes have no grandchildren and can be evaluated. Repeating this process will let you evaluate the entire tree. This is basically the same process as evaluating computer code execution or nested math functions. If you see f(g(x)) you first figure out what g(x) is. Let’s say it’s z. Then you have f(z) which you can evaluate. You don’t evaluate f first because there’s an unevaluated function in its inputs. My video How to do arithmetic with tree diagrams may be helpful.

Grammar gives you nested functions like:

verb(noun(modifiers), noun(modifiers), modifiers)

So you have to evaluate the noun(modifiers) parts before the verb part. More concretely, it could be:

threw(John(tall), ball(the, small), quickly(very), vigorously)

That corresponds to the sentence “Tall John vigorously threw the small ball very quickly.”

English has flexible word order in some cases, so there’s sometimes more than one valid sentence that grammar functions may correspond to. Modifiers for verbs, like “quickly”, actually have four typical places they may go (before or after the verb they modify, or at the start or end of their verb’s clause).

And English is often ambiguous. An ambiguous sentence can be converted to functions in more than one way. Treating a subordinating conjunction as a conjunction or subordinator is a way to get two valid function representations for one sentence. That highlights a common ambiguity in English. Sentences are routinely unclear about whether they intend a subordinating conjunction to function as a conjunction or subordinator. I think that ambiguity is ignored because it’s reasonably harmless because it doesn’t make much difference to the meaning of sentences.

If you’re familiar with s-expressions, you may prefer them over regular function notation. Going back and forth between s-expressions and trees is convenient.

## Grammar Functions List

For reference, here’s a list of the grammar function patterns from this article with “define-function” omitted for readability:

action-verb(subject, object, indirect-object, modifiers…): finite-clause

linking-verb(subject, complement, modifiers…): finite-clause

noun(modifiers…): noun

modifier(modifiers…): modifier

preposition(noun): modifier

gerund(subject, object, modifiers…): noun

participle(subject, object, modifiers…): modifier

infinitive(subject, object, modifiers…): noun or modifier

conjunction(things…): thing

subordinator(finite-clause): modifier