[CakeML-dev] compiler explorer

Rikard Hjort hjortr at student.chalmers.se
Fri Jun 30 17:24:49 UTC 2017

Hi Ramana,

The offset is not used. We were told it is used to specify at what character number (as an ordinal) the location is. So in a file with the following contents



the "," would be at offset 6 (assuming 1-indexing) and "W" at offset 7. This might be useful for some use cases of the datatype, but for our purposes we don't need it. Start position and end position is enough to uniquely identify an expression. So what you are seeing is correct.

As for the second problem, what you are doing as it stands is assign the same trace to several expressions. This is indeed problematic, as we would want each expression to have a unique trace. The only exception is expressions which exactly share trace with their ancestor, i.e., an expression they were derived from.

In general, when a trace appears more than once in the right hand side of a declaration, it should be split using the Cons constructor to ensure uniqueness of every trace. So I would write it something like this:

  (compile_exp t env (ast$App op es) =
    if op = AallocEmpty then
      FOLDR (Let (Cons t 1) NONE) (modLang$App (Cons t 2) Aalloc [Lit (Cons t 3) (IntLit (&0));Lit (Cons t 4) (IntLit (&0))])
        (REVERSE (compile_exps (Cons t 5) env es))
      modLang$App (Cons t 1) (astOp_to_modOp op) (compile_exps (Cons t 2) env es))

Of course, a bad thing about this way to write it is that as the new trace gets passed to compile_exps, it also grows. Ideally, you would have a number n which would serve as a counter, telling you the next number to add in a Cons. If that was an extra parameter, it would instead be:

  (compile_exp t n env (ast$App op es) =
    if op = AallocEmpty then
      FOLDR (Let (Cons t n) NONE) (modLang$App (Cons t (n+1)) Aalloc [Lit (Cons t (n+2)) (IntLit (&0));Lit (Cons t (n+3)) (IntLit (&0))])
        (REVERSE (compile_exps t (n+4) env es))
      modLang$App (Cons t n) (astOp_to_modOp op) (compile_exps t (n+1) env es))

This is cleaner in the sense that all the newly created expressions would then have the same trace, only wrapped in a single Cons, rather than the traces growing increasingly bigger.

Från: Magnus Myreen <magnus.myreen at gmail.com>
Skickat: den 30 juni 2017 08:50:37
Till: Ramana Kumar
Kopia: developers at cakeml.org
Ämne: Re: [CakeML-dev] compiler explorer

On 30 June 2017 at 08:39, Ramana Kumar <Ramana.Kumar at cl.cam.ac.uk> wrote:
> The compiler explorer seems to have been written prior to some changes to
> the locations type. Looking at this line in particular, I notice that the
> offset field of the locn record is not used. Is this correct?
>   (compile_exp t env (Lannot e (Locs st en)) =
>     let t' = if t = None then t else (Cons (Cons (Cons (Cons Empty st.row)
> st.col) en.row) en.col) in
>       compile_exp t' env e)
> for reference:
> val _ = Datatype `
>  locn = <| row : num;  col : num; offset : num |>`;
> val _ = Datatype `
>   locs = Locs locn locn
> `

I don't think it matters. As far as I know, the offset is just another
way of representing the same information.

By the way, the (Cons (Cons (Cons (Cons Empty st.row) pattern should
have constructor `SourceLoc num num num num` of its own in the tra
type (backend_common).

> Another problematic case, where probably I am not passing along the right
> traces in the AallocEmpty case:
>   (compile_exp t env (ast$App op es) =
>     if op = AallocEmpty then
>       FOLDR (Let t NONE) (modLang$App t Aalloc [Lit t (IntLit (&0)); Lit t
> (IntLit (&0))])
>         (REVERSE (compile_exps t env es))
>     else
>       modLang$App t (astOp_to_modOp op) (compile_exps t env es))

Yup, the function needs to become a little bit more complicated in
order to ensure that each constructor has a unique trace t.

Developers mailing list
Developers at cakeml.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.cakeml.org/pipermail/developers/attachments/20170630/15b18a82/attachment.html>

More information about the Developers mailing list