2
$\begingroup$

I have the following custom expression structure I am parsing:

Theorema`Knowledge`Q$TM[
 Theorema`Language`VAR$[Theorema`Knowledge`VAR$x$TM]]

I already was able to match the inner structure so that I can just output the value x, like so:

parser[Theorema`Language`VAR$[a_]] := 
    Module[{varName = SymbolName[Unevaluated[a]]},
        "\\VarTM{" <> StringReplace[varName, {"Theorema`Knowledge`VAR$" -> "", "$TM" -> ""}] <> "}"
    ];

(Happy to hear if anyone has a more elegant way to achieve this!)

But in the previous parsing example, the structure is different from the predicate structure Theorema`Knowledge`Q$TM[] because there is no outside structure I can reference the inner one in terms of.

I want to achieve something like (this does not work):

parser[Theorema`Knowledge`symbol_ /; StringEndsQ[SymbolName[Unevaluated[Theorema`Knowledge`symbol]], "$TM"][a___]] := "Test2! "

Where my stand-in string take on the value of symbol, so "Q" in the example, meant to be generic predicate of some sort. Does anyone have an idea for how to do this kind of partial/intra-matching, with a wild card for my predicate name (maybe longer than just one letter, also)?

$\endgroup$

1 Answer 1

1
$\begingroup$

You can use Context to match only the symbols in the desired context.

Clear[parser];

parser[sym_[a___] /; Context[sym] === "Theorema`Knowledge`" && 
  StringStartsQ[SymbolName[Unevaluated[sym]], "Q"]] := "Hit!"
parser[___] := "Miss"

expr = Theorema`Knowledge`Q$TM[x];
parser[expr]
(* Hit! *)

expr = Theorema`Knowledge`wrongSymbol[x];
parser[expr]
(* Miss *)

expr = WrongContext`Q$TM[x];
parser[expr]
(* Miss *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.