473,842 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Implementing fp pattern matching, using C++

First, bear with my xpost. This goes to
comp.lang.funct ional
with follow-up to comp.lang.c++

- I want to discuss an aspect of using C++ to implement a
functional language, and I'd like the attention of fp as well
as C++ gurus if available.

The language I'm implementing - PILS - is dynamically
typed and heavily depending on pattern matching. Like
with Lisp, programs are just data that happen to be
executed. Generally, programs are built from rules, each
consisting of a pattern and an action. It is not rare for
rules to construct other rules and use them.

While the language is generally interpreted rather than
compiled, the patterns are compiled for speed. This
happens dynamically: whenever a rule is constructed
by the parser or by some PILS program, a code snippet
for matching an arbitrary datum to the pattern is
immediately generated behind the scenes.

My current working implementation of PILS is a NASM
shitpile, PILS data being represented as v-table based

The pattern matching works by generating and executing
strings of native code. The compiler works in two passes:
first, it collects variable bindings and computes the code
length, then (after allocating memory as required) code is

When rules are trashed, their code memory is recycled.

All objects have vtable methods that control what code
should be generated if the object occurs in a pattern.

Registers and stack are set up so that rejection of misfits
is very fast.

The generated code is stuff like:

- immediate compares, to verify constants,
- type checks to verify lists, nodes, numbers etc.,
- loads, pushes, pops, to inspect substructures,
- stores into slots, for variable firsts,
- compares againts slots, for recurring variables,
- stack management, allocation of slots, etc.,
- calls of special routines for string searching etc.

With C++, I can't do heavy native code generation and
maintain a sensible level of portability - dynamic native
code generation sort of doesn't fit in with C++ programming,
so I have to think of something else.

What should I do - and what have others done???

Should I design a bytecode system, and use a giant
switch statement or a function pointer array to execute it?
(One problem with this approach is alignment - the code
will have to include larger operands, should they be in
separate segments, or inline aligned, or inline unaligned...)

Or should I create arrays of function pointers instead of
bytecode strings? This might be faster, or it might be just
waste of space.

Or should I create trees or linked lists of specialized
vtable based matcher objects?

Or should I take the trouble of placing the matcher
objects consecutively, to save indirections and better
utilize caches? (I think this is what I'm going to do,
but I might be talked out of it...)

How should type checks be done? By RTTI? By executing
specialized virtual methods? By executing a single virtual
method that returns a typecode? By somehow grafting the
typecode directly into the vtable? By comparing specific
methods in the vtable? (The two latter is what I used in
NASM but I'm not sure the C++ community will endorse

Type checking is complicated by the fact that there are
lots of specialisations of general types. Nodes with
particular combinations of attributes are recognized as
conditional statements, additions, rules, you name it...
and for these purposes given special vtables, but they
should still be recognizable as just nodes. This means
I can't just compare type ids.

I don't expect someone to come up with the one and
only solution - I'd just like to hear: did you ever do
something similar, how did you do it, and why, and
how did you like the result and what do you wish you
had done instead?

Or did you happen to stumble on a "become a dynamic
pattern compiler expert in 21 days" manual recently?

Ole Nielsby
(maker of the still widely unknown PILS programming
Sep 18 '06 #1
2 3402
Ole Nielsby wrote:
Should I design a bytecode system, and use a giant
switch statement or a function pointer array to execute it?
Raid the source of Ruby, Smalltalk, Python, Prolog, and Perl. They all solve
generally the same problem. You also ought to target the Java VM, and have a
huge installed platform base to run in.

http://www.greencheese.us/ZeekLand <-- NOT a blog!!!
Sep 18 '06 #2
Ole Nielsby wrote:
First, bear with my xpost. This goes to
comp.lang.funct ional
with follow-up to comp.lang.c++
This is a crappy idea. What do you expect the people on clf to do,
subscribe to clc++ just to follow the conversation?

Sep 18 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

by: gsv2com | last post by:
One of my weaknesses has always been pattern matching. Something I definitely need to study up on and maybe you guys can give me a pointer here. I'm looking to remove all of this code and just use pattern matching to determine if the proper amount of numeric characters has been met. Here is the function I've already done. Any help you can give in a pattern matching solution would be much appreciated and very educational.
by: Thomas Reichelt | last post by:
Moin, short question: is there any language combining the syntax, flexibility and great programming experience of Python with static typing? Is there a project to add static typing to Python? Thank you, -- greetz tom
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images files from gif # format to png format. Now you need to change the # html code to use the .png files. So, essentially
by: Henry | last post by:
I have a table that stores a list of zip codes using a varchar column type, and I need to perform some string prefix pattern matching search. Let's say that I have the columns: 94000-1235 94001 94100 If I run a pattern matching search for the string "940", then I should get the top two rows of data back. If I run a pattern matching search for the string "94", then I should get all the three rows of data back.
by: bpontius | last post by:
The GES Algorithm A Surprisingly Simple Algorithm for Parallel Pattern Matching "Partially because the best algorithms presented in the literature are difficult to understand and to implement, knowledge of fast and practical algorithms is not commonplace." Hume and Sunday, "Fast String Searching", Software - Practice and Experience, Vol. 21 # 11, pp 1221-48
by: olaufr | last post by:
Hi, I'd need to perform simple pattern matching within a string using a list of possible patterns. For example, I want to know if the substring starting at position n matches any of the string I have a list, as below: sentence = "the color is $red" patterns = pos = sentence.find($)
by: phl | last post by:
hi, My question is: 1. To avoid possible memory leaks, when you use this pattern, after you have dealth with the unmanaged resources and before you take your object off the finalize queue, how are you sure that your managed object resources are completely freed up of resources it's might be using? In my case below I have a private bool variable. Are there any other managed resource that you might need to explicitly free up in
by: VanKha | last post by:
I write this program for pattern-matching,but it gives wrong result: #include<iostream> #include<conio.h> #include<string.h> using namespace std; main() { char text,pat; cout<<"text:";cin>>text;
by: Mohamed Mansour | last post by:
Hello, What is the purpose of implementing the Observer Pattern if we can trigger an event easily? For example (from books), You have a "Forecaster" which notifies "Observable" when a prediction is ready, then there is a "WeatherViewer" which calls methods from the "Observer Interface".
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.