Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Pattern matching in concatenative languages

Pattern matching in concatenative languages

Ratings: (0)|Views: 562 |Likes:
Published by microdan8710

More info:

Published by: microdan8710 on Feb 25, 2009
Copyright:Public Domain


Read on Scribd mobile: iPhone, iPad and Android.
download as PDF or read online from Scribd
See more
See less



Pattern matching in concatenativeprogramming languages
Daniel EhrenbergCarleton Collegeehrenbed@carleton.eduFebruary 25, 2009
Pattern matching is a useful, convenient tool for processing alge-braic data types in functional programming languages. This paperdiscusses a method for porting pattern matching to a family of pro-gramming languages called
, which does not use namedvariables. The system is not fixed, like a traditional pattern matchingsystem, but rather arbitrarily extensible: any function can be usedas a pattern if the right information is given to the pattern matcher.This generalization of pattern matching has been useful in librariesfor XML processing and unit conversion.
1 Introduction
The idea of pattern matching is that, to see the contents of an object, thesame syntax can be used as in creating the object, only on the left hand sideof a binding rather than the right. For example, a Haskell [2] function to getthe head of a list can be implemented declaratively, simply as
head (x:xs) = x
signifies a cons cell, where
is the head and
is the tail. The
function extracts the head of the list
and returns it. Patternmatching can also be used to decide which branch of several to take. Forexample,
can be implemented as1
 map f [] = [] map f (x:xs) = (f x):(map f xs)
In many functional programming languages, including Haskell, pattern match-ing is a fundamental built-in construct. Pattern matching systems have alsobeen constructed as macro libraries in languages like Common Lisp.Certain programming languages, termed concatenative programming lan-guages, express a program in terms of the composition of several smaller pro-grams, typically without the use of named variables. The first language tobe called concatenative was Joy [8]. A more common one is Forth. Most con-catenative programming languages, including Joy and Factor, follow Forth’sstack-based model.It is not immediately obvious how to port pattern matching to such asystem, without the inclusion of named free variables on the left hand side.A fundamental rethink of pattern matching, from the deconstruction of sum-of-product data types to a partial inverse of a function, leads to a clearsolution to this problem.We have constructed an efficient pattern matching library for the dynam-ically typed concatenative programming language Factor [3]. A compositionof functions, each of whose inverse is known, can easily be inverted using theformula (
. Functions that are used in pattern matchingare typically not surjective.For example,
is not surjective onto the
type, as the empty listis not in its codomain. As a result, the inverse of 
is a partial function.In general, the inverse of the constructor for an algebraic data type wouldbe a partial function defined on things from that constructor, returning thearguments of the constructor. From these partial functions we can create a
-like construct, which attempts each inverse on a given input until oneof them returns a result.This conception of pattern matching naturally leads to a significant gen-eralization of existing systems. One simple thing that this new system cando is “pattern match” over certain simple bijective arithmetical formulas,inverting them. Inverting math formulas could be done in a constraint satis-faction system, but our system is much simpler. The system has been usedto construct a practical library for encoding numbers with units. With thislibrary, centimeters can be converted to inches by applying the centimeterconstructor and then inverting the inches constructor.2
2 Background
2.1 Pattern matching in Haskell
In this paper, we use Haskell as a model for the typical semantics of pat-tern matching. Haskell is a statically typed, non-strict, purely functionalprogramming language. Haskell pattern matching, like that of most func-tional languages, operates on algebraic data types. For example, we couldimplement a list of integers as
data List = Cons Int List | Nil
This statement declares that a list is either a
or a
, where the latterhas two slots, one for an integer and one for another list. To construct alist, we can write code which treats
as ordinary functions,where
takes two arguments and
takes none. For example, the list
’(1 2 3)
can be constructed as
Cons 1 (Cons 2 (Cons 3 Nil))
. To getinformation out of the slots, pattern matching is used. For example, to getthe head of the list, the
constructor can be used on the left hand sizeof 
head (Cons x xs) = x
Pattern matching only succeeds if the argument to
is actually a
,rather than a
. This can be used to dispatch on what kind of argumentis given. For example, a function to sum a list can be implemented as
sum (Cons x xs) = x + (sum xs)sum Nil = 0
Conceptually, when
are put on the left hand side of the assign-ment, they’re used backwards. You try to undo
into the variables
, and if this fails, you go on to trying to undo
. This is not howpattern matching in Haskell is typically explained, but it is equivalent to thestandard definition, and this observation leads to a generalization which weuse.
2.2 The concatenative programming model
Factor is a stack-based programming language. This means that parametersare passed around on an implicit stack, rather than used as explicit arguments3

Activity (3)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
ganeshkkkumar liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->