Version française
Home     About     Download     Resources     Contact us    
Browse thread
how to implement generic operators
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jacques Carette <carette@m...>
Subject: Re: [Caml-list] how to implement generic operators

skaller wrote:

>I would clearly need some representation like:
>
>	Assoc of op * term list
>
>and then some kind of fold which works for all different binary op,
>where now 'op' is defined by the user.
>
>So crudely the question is: what is the representation of 'op'
>required so the generic fold can create a wide class of
>generic n-ary operations from binary ones?
>  
>
You really need your operations to be left-associative for that to make 
sense... But in my toy interpreter for a subset of Maple (written in 
Ocaml), I have
type op = term -> term -> term * term
and then in the eval() function for a term, a case
   | Assoc ((f,init), l) -> List.fold_left f init l

f can be created from a term by applying a term-level lambda, so that 
when you ask for

>Note the data for op will be obtained by parsing in the input
>program, so cannot consist of just a fixed list of variants.
>  
>
that can be done too.

>I have a feeling MetaOcaml can do this already.
>  
>
Yes and no.  Yes, it can be done by staging a very finely crafted 
evaluator.  But doing that is very, very difficult given the current 
state-of-the-art [typed PE for typed languages is hard].  Or merely 
difficult if you write everything CPS style.  But it is most definitely 
not something you get right out of the box.

Jacques C.