A short introduction: the ML language has a variant data type, tuples, and pattern matching construct. For example type operand = Imm of int | Reg of string type instruction = Insn of string | Pseudo of string Now, we can create tuples, e.g. (Insn "push", Reg "%rbp") is of type operand * instruction (a tuple of two typed elements). In function definitions, it is possible to do pattern matching as (it could be written slightly better in ML, but I want to show the match syntactic construct): let f x = match x with | (Insn "addq", Reg "%rsp") -> (* do something *) | (_, Imm x) -> (* any 1st item; the 2nd must be Imm type, and the x variable is bound to the value carried by the 2nd item *) | (_, Reg "%rsp") -> (* any value for 1st item _except_ Insn "addq" which has already been handled in the first alternative - alternatives are matched in order and first matching is taken *) | _ -> (* default case for anything else *) This is only a simplified example; but even this example would be much more ugly if rewritten with if/else statements, even if tuple tiers were used. Boost provides both the safe variant type and the tuple type. Would it be possible to construct pattern matching with syntax similar to the above in C++ ? [Yes, there are also other problems - incomplete matches: ML just throws an exception in that case; or redundant cases - nothing happens.]
Hello Zeljko,
may be another approach? Why not using a template method and static concepts
inside a function, which ensure that a parameter is of correct type?
With Kind Regards,
Ovanes
On 10/18/07, Zeljko Vrba
A short introduction: the ML language has a variant data type, tuples, and pattern matching construct. For example
type operand = Imm of int | Reg of string type instruction = Insn of string | Pseudo of string
Now, we can create tuples, e.g. (Insn "push", Reg "%rbp") is of type operand * instruction (a tuple of two typed elements).
In function definitions, it is possible to do pattern matching as (it could be written slightly better in ML, but I want to show the match syntactic construct):
let f x = match x with | (Insn "addq", Reg "%rsp") -> (* do something *) | (_, Imm x) -> (* any 1st item; the 2nd must be Imm type, and the x variable is bound to the value carried by the 2nd item *) | (_, Reg "%rsp") -> (* any value for 1st item _except_ Insn "addq" which has already been handled in the first alternative - alternatives are matched in order and first matching is taken *) | _ -> (* default case for anything else *)
This is only a simplified example; but even this example would be much more ugly if rewritten with if/else statements, even if tuple tiers were used. Boost provides both the safe variant type and the tuple type. Would it be possible to construct pattern matching with syntax similar to the above in C++ ?
[Yes, there are also other problems - incomplete matches: ML just throws an exception in that case; or redundant cases - nothing happens.]
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On Thu, Oct 18, 2007 at 09:04:39AM +0200, Ovanes Markarian wrote:
Hello Zeljko,
may be another approach? Why not using a template method and static concepts inside a function, which ensure that a parameter is of correct type?
Hmm, I didn't quite get you. But I want to match both on *type* and *value* as shown in this rule:
| (Insn "addq", Reg "%rsp") -> (* do something *)
The type here is (Insn, Reg) [particular types of larger variants]; each component is also matched on the particular value ("addq" and "%rsp" strings).
| (_, Imm x) -> (* any 1st item; the 2nd must be Imm type, and the x variable is bound to the value carried by the 2nd
The type here is doesn't matter and (Imm x) - this is a match only on type where the variable x gets bound to the value carried by the variant type.
participants (2)
-
Ovanes Markarian
-
Zeljko Vrba