Skip to topic | Skip to bottom
Home
Projects
Projects.MetaGeneExamplesr1.6 - 04 Jun 2005 - 14:44 - NicolasPouillard?topic end

Start of topic | Skip to actions
Common C++ meta-programming techniques have their equivalent in functionnal programming. Before begginning Metagene we wrote a list of short ML expressions with a possible translation in C++. This page is anterior to the Metagene project: the way the examples are translated does not reflect the real implementation of Metagene (which is shown in MetaGeneTranslation). This page just shows the possible equivalence between C++ meta-programming and functionnal programming.

Example transformations

Identity

let id x = x

A function becomes a struct, an argument becomes a class template parameter. Variables becomes types.

template <typename x>
struct id
{
  typedef x res;
};

Power

 let rec pow x n =  
    match n with 
     1 -> x 
   | _ -> x * pow x (n - 1) 

Pattern matching is (nearly) equivalent to templates specialization. Nested functions becomes nested template or non-template structs.

template <int x>
struct pow 
{ 
  template<int n> 
  struct res 
  { 
    enum { res = x * pow <x, n - 1> :: res }; 
  };  
   
  template<> 
  struct res<1>  
  { 
    enum { res = x }; 
  }; 
}; 

Partial and total application

let eight = pow 2 3
let power2 = pow 2

A function can be applied partially or totally.

typedef pow<2>::res<3>::res eight;
typedef pow<2>::res         power2;

Lists

type 'a list = EmptyList | List of 'a * 'a list

Type constructors can be translated as template or non-template empty structs.

struct EmptyList {};

template<typename A, typename AList>
struct List   {}; 

Higher order functions

let rec map f l = match l with 
      EmptyList -> EmptyList 
    | List(hd, tl) -> List(f hd, map f tl) 

A higher order function is a class template. C++ allows passing class templates as parameters: a Metagene function can thus takes a higher order function as argument.

template<template<typename> typename F> 
struct map 
{ 
   template<typename L> 
   struct res; 
    
   template<> 
   struct res<EmptyList>    {}; 
   { 
      typedef EmptyList res; 
   }; 
    
   template<typename Hd, typename Tl> 
   struct res< List<Hd, Tl> > 
   { 
      typedef List< typename F<Hd>::res, typename map<F, Tl>::res >   res; 
   }; 
}; 

Higher order functions use

let alist = List(10, List(20, List(30, EmptyList) ) ) 
let anotherlist = map power2 alist 

This example shows the use of a higher order function.

typedef List<Integer<10>, List< Integer<20>, List< Integer<30>, EmptyList > > >   alist; 
typedef map<power2>::res<alist>::res                  anotherlist; 

Summary

These examples gives hints about the translation process but are not a response. Several problems remains, e.g. id (pow 2 3) will not compile, map does not compile due to an inconsistency in the C++ grammar...

At this point, the main goals of Metagene are:

  • Define the Metagene language: a subset of OCaml.
  • Find a unified way to translate ML expressions into effective C++ meta-programs.
  • Find how to inter-operate with C++:
    • How to insert Metagene code into a C++ file?
    • How to manipulate C++ types, and C++ code in Metagene?

-- FrancisMaes? - 13 Mar 2003


MetaGeneZone


to top


Projects.MetaGeneExamples moved from Projects.MetageneExamples on 13 Mar 2003 - 13:35 by FrancisMaes? - put it back
You are here: Projects > MetaGeneExamples

to top

Copyright © 1999-2010 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback