# 'let rec' limitation? parser combinator

trying to write a Parser Combinator in ReScript (for fun, and Advent of Code).
Getting into problems with certain recursion, tho.

Plain `oneOf(exact"hello"), exact("world"))` works fine, both parsers produce the same type, but for a recursive type, I run into problems:

``````type rec expr =
| Int(int)
| Sub(expr)

let rec exprExpr = oneOf(intExpr, subExpr)
and intExpr = ...
and subExpr = ...  // needs to call exprExpr
``````

The compiler tells me:
`87 │ let rec exprExpr = oneOf(intExpr, subExpr)`
`This kind of expression is not allowed as right-hand side of 'let rec'`
I’m not even sure what that means…
The types seem fine - with all kinds of variations I can get partial results.
What is the compiler complaining about (and what can I do to avoid that problem)?

Can you create a minimal reproduction? Maybe post it on the playground?

OCaml - Language extensions has some details on the limitations of recursive values. I ran into the same error recently and adding a parameter to the function fixed the error for me.

Here’s an example of mutually recursive functions that may help: https://rescript-lang.org/try?code=DYUwLgBAlhC8ECcQDMAUBGAlAKG6SSAxhMgPalwSqZwB8EA3thNMlVAHSGkB2YIfAM4R6AVhpMWLTtz4Cwg5hAC+EEMEEhGS6V178hlGfvnCA1BHQBuHdD1zDAKggAjAIYJqS5dh9ueACauHpTUdNrSbKjGDgoiEOIRUjEGCt5qGlqSyfapwvApphAW1raFTiTkXiw+PkA

The minimal case (thx tom) appears to be

``````let rec subExpr: unit => unit = () => finalExpr()

and finalExpr = subExpr
``````

whereas

``````let rec subExpr: unit => unit = () => subExpr()

let finalExpr = subExpr
``````

compiles fine. In terms of the OCaml ref (thx Kevin), the former case would be “immediately linked” in the recursive definition (I think). In the second case, there’s no recursion, so no problem. Unexpected, but there it is.

I believe the solution to my original problem lies in the direction of

``````let rec subExpr: unit => unit = () => finalExpr()

and finalExpr = () => subExpr()
``````

(that can probably massaged into something better than using `exprExpr()` instead of `exprExpr` in various places)

2 Likes
{"ft_lang_label":"__label__en","ft_lang_prob":0.7873096,"math_prob":0.9007543,"size":1391,"snap":"2023-14-2023-23","text_gpt3_token_len":341,"char_repetition_ratio":0.14131218,"word_repetition_ratio":0.11344538,"special_character_ratio":0.25664988,"punctuation_ratio":0.122047246,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9607345,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-31T18:22:48Z\",\"WARC-Record-ID\":\"<urn:uuid:300be4d6-449d-4910-9d4f-6d7fe31b36ce>\",\"Content-Length\":\"21091\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:7a8d9cf8-f4f7-4b10-96b1-8722b9970bb8>\",\"WARC-Concurrent-To\":\"<urn:uuid:d6d262f0-8535-42f3-ba44-26c653d6d391>\",\"WARC-IP-Address\":\"64.227.27.123\",\"WARC-Target-URI\":\"https://forum.rescript-lang.org/t/let-rec-limitation-parser-combinator/2752\",\"WARC-Payload-Digest\":\"sha1:4BE5IWJXMAJBFYDIUBMMMPDARQ5S2QLL\",\"WARC-Block-Digest\":\"sha1:B5L7RBQ23XQQOZADZYCRVY2QEZJFKYB2\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296949678.39_warc_CC-MAIN-20230331175950-20230331205950-00567.warc.gz\"}"}