Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7439771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:45:13+00:00 2026-05-29T10:45:13+00:00

I’ve got some code like this: type boolean = T | F type bexp

  • 0

I’ve got some code like this:

  type boolean = T | F
  type  bexp = Const of boolean 
  |  Var of variable
  |  Bop of bop * bexp * bexp
  |  Not of bexp
  and bop = And | Or | Xor 
  and variable = { name: string; mutable value: boolean }

Later on if I want to create a variable I have to do:

let full         = Var({name ="full"; value  = F});;   

I’d rather not have to repeat the ‘full’ twice and would like to come up with some introspective way to get the name as a string. I’m thinking camlp4 could work for this, but have no idea where to start.

So ultimately I’d like to be able to do something like:

let full          = Var({name = :letname:; value = F});;

Where :letname: would fill in the current let binding as a string in place of (in this case “full”). (the syntax :letname: is just a suggestion, other ideas for syntax that won’t clash with OCaml’s syntax?)

A more concise syntax like this would probably be preferable:

var full = F 

Which would then expand to:

let full = Var({name = "full"; value = F});;

Is this possible to do with camlp4 and if so, how would I go about it?

(upon further consideration, the :letname: syntax or something similar would be more general-purpose and useful in more areas)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T10:45:13+00:00Added an answer on May 29, 2026 at 10:45 am

    Try the following. In a separate file, such as test.ml

    (* A simple syntax extension for getting the name of an identifier. *) 
    open Camlp4 
    
    (* Create an ID for the macro*) 
    module Id : Sig.Id = struct 
        let name = "let'"
        let version = "1.0" 
    end 
    
    module Make (Syntax : Sig.Camlp4Syntax) = struct 
        open Sig 
        include Syntax 
    
        (* Extend the syntax with a let' x=e1 in e2 construction*) 
        EXTEND Gram 
        expr: [ 
            [ "let'"; x = patt ; "=" ; e1=expr; "in"; e2=expr -> 
                let name=begin match x with
                    | Ast.PaId (_,Ast.IdLid(_,name)) -> name
                    | _ -> failwith "Require an identifier in a let' statement."
                end in
                <:expr<
                    let $x$ = $e1$ in ($str:name$,$e2$)
                >>
            ] 
        ]; 
        END 
    end 
    
    module M = Register.OCamlSyntaxExtension(Id)(Make) 
    

    Next, compile with

     ocamlc -c -I +camlp4 dynlink.cma camlp4lib.cma -pp camlp4of.opt test.ml
    

    On the top level

     ocaml dynlink.cma -I +camlp4 camlp4of.cma
    

    Then

    # #load "test03.cmo";;
    # let' x=1 in x;;
    - : string * int = ("x", 1)
    

    In order to compile with the extension create a different file such as test2.ml

    let' x=1 in x
    

    Then, compile with

    ocamlc -pp "camlp4of test.cmo" test2.ml
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.