Skip to content

Instantly share code, notes, and snippets.

@darkf
Created July 3, 2013 01:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkf/5914869 to your computer and use it in GitHub Desktop.
Save darkf/5914869 to your computer and use it in GitHub Desktop.
Proposal for optional parentheses in Possum

If we extend the Possum language to include optional parentheses, which group together tokens (atoms and constants), and when evaluated act as function applications, and when quoted act as homoiconic representations of a program.

Consider the following pseudo-Possum program:

defun compose f g is
  -> x is
    f g x
  end
end

Let's assume we interpret this as function composition, such that compose add1 add1 would produce a function that, when applied, is the result of add1 add1 x. The type of compose is compose :: (b -> c) -> (a -> b) -> (a -> c).

This is invalid if we want to statically parse the program into AST form because the parser cannot resolve the arity of f, and it would interpret g as a variable instead of a function application. One way to resolve this is to use temporary variables with eval-quote:

defun compose f g is
    -> x is
        define f' eval-quote f
        define g' eval-quote g
        f' g' x
    end
end

However, this is clunky and reduces readability. It is evidence of a failed abstraction of the language.

In order to solve this problem, I propose adding optional parentheses, much like LISP S-expressions, so that we may write the following:

 defun compose f g is
   -> x is
       (f (g x))
   end
 end

The interpreter will interpret (f ...) and (g x) as explicit function application, solving any ambiguity in type or arity. Transitively, this allows our parser to be more deterministic, and parse into an AST form instead of just a list of tokens.

When the parser encounters a ( token, it should do the following:

  • If the next token is a (, parse it and use that as the procedure.
  • If the next token is an atom, assume the atom is the name of a procedure and use that.
  • Otherwise, a parser error has occurred.

After parsing the procedure, it should parse all following expressions as arguments until the terminating ) is reached.


In addition, quoting parentheses could afford us homoiconicity - consider:

quote (print "hi"
       print "there")

This would evaluate to a list of tokens that can later be passed to eval to be evaluated, or edited by the program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment