caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] An awkwardness with type parameters
@ 2019-02-26  6:23 Hendrik Boom
  2019-02-26  6:39 ` Jacques Garrigue
  0 siblings, 1 reply; 5+ messages in thread
From: Hendrik Boom @ 2019-02-26  6:23 UTC (permalink / raw)
  To: Ocaml Mailing List

I have a (broken) function definition starting:

let mixfix : 'token1 'phrase1.
  ('token1, 'phrase1, ('token1, 'phrase1) Phrasestream.phrasestream) grammar
        -> ('token1, 'phrase1) parser
   =  fun gram -> (
  ...

It goes o for a hundred-odd lines.
The type-parameterized types Phrasestream.phrasestream, grammar, and parser   
have been previously defined, and there are also operations defined on these
types.

The problem I'm having is that the OCaml type-checker ends up identifying
the type parameters 'token1 an 'phrase1 because of type errors I've made in
the body of the mixfix function.  Somewhere I ended up using an
operator that's supposed to work on values of type 'token1
on a value of type 'phrase1 instead, and identification of 'token1 and 
'phrase1 is the natural result.

Is there some way of forcing the type checker to treat 'token1 and 
'phrase1 as different types so that I can get meaningful type errors
at the point were they occur?

With the present setup I only get type violation messages much later, 
when I try to use the mixfix function.  It's far too late to trace the 
problem back to the actual error by that time.

-- hendrik



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] An awkwardness with type parameters
  2019-02-26  6:23 [Caml-list] An awkwardness with type parameters Hendrik Boom
@ 2019-02-26  6:39 ` Jacques Garrigue
  2019-02-27  1:59   ` Hendrik Boom
  2019-02-27 18:21   ` Hendrik Boom
  0 siblings, 2 replies; 5+ messages in thread
From: Jacques Garrigue @ 2019-02-26  6:39 UTC (permalink / raw)
  To: Hendrik Boom; +Cc: Mailing List OCaml


2019/02/26 15:23, Hendrik Boom <hendrik@topoi.pooq.com>:
> 
> I have a (broken) function definition starting:
> 
> let mixfix : 'token1 'phrase1.
>  ('token1, 'phrase1, ('token1, 'phrase1) Phrasestream.phrasestream) grammar
>        -> ('token1, 'phrase1) parser
>   =  fun gram -> (
>  ...
> 
> It goes o for a hundred-odd lines.
> The type-parameterized types Phrasestream.phrasestream, grammar, and parser   
> have been previously defined, and there are also operations defined on these
> types.
> 
> The problem I'm having is that the OCaml type-checker ends up identifying
> the type parameters 'token1 an 'phrase1 because of type errors I've made in
> the body of the mixfix function.  Somewhere I ended up using an
> operator that's supposed to work on values of type 'token1
> on a value of type 'phrase1 instead, and identification of 'token1 and 
> 'phrase1 is the natural result.
> 
> Is there some way of forcing the type checker to treat 'token1 and 
> 'phrase1 as different types so that I can get meaningful type errors
> at the point were they occur?

You can use locally abstract types, which are often used with GADTs but are not
restricted to them:
  let mixfix : type token1 phrase1.
    (token1, phrase1, (token1, phrase1) Phrasestream.phrasestream) grammar
       -> (token1, phrase1) parser
  =  fun gram -> (
 …
This is the same as the type you wrote, but additionally prevents the types from being
instantiated inside the definition of mixdix.

Jacques Garrigue

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] An awkwardness with type parameters
  2019-02-26  6:39 ` Jacques Garrigue
@ 2019-02-27  1:59   ` Hendrik Boom
  2019-02-27  3:46     ` Jacques Garrigue
  2019-02-27 18:21   ` Hendrik Boom
  1 sibling, 1 reply; 5+ messages in thread
From: Hendrik Boom @ 2019-02-27  1:59 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: Mailing List OCaml

On Tue, Feb 26, 2019 at 03:39:24PM +0900, Jacques Garrigue wrote:
> 
> 2019/02/26 15:23, Hendrik Boom <hendrik@topoi.pooq.com>:
> > 
> > I have a (broken) function definition starting:
> > 
> > let mixfix : 'token1 'phrase1.
> >  ('token1, 'phrase1, ('token1, 'phrase1) Phrasestream.phrasestream) grammar
> >        -> ('token1, 'phrase1) parser
> >   =  fun gram -> (
> >  ...
> > 
> > It goes o for a hundred-odd lines.
> > The type-parameterized types Phrasestream.phrasestream, grammar, and parser   
> > have been previously defined, and there are also operations defined on these
> > types.
> > 
> > The problem I'm having is that the OCaml type-checker ends up identifying
> > the type parameters 'token1 an 'phrase1 because of type errors I've made in
> > the body of the mixfix function.  Somewhere I ended up using an
> > operator that's supposed to work on values of type 'token1
> > on a value of type 'phrase1 instead, and identification of 'token1 and 
> > 'phrase1 is the natural result.
> > 
> > Is there some way of forcing the type checker to treat 'token1 and 
> > 'phrase1 as different types so that I can get meaningful type errors
> > at the point were they occur?
> 
> You can use locally abstract types, which are often used with GADTs but are not
> restricted to them:
>   let mixfix : type token1 phrase1.
>     (token1, phrase1, (token1, phrase1) Phrasestream.phrasestream) grammar
>        -> (token1, phrase1) parser
>   =  fun gram -> (

Thus *with* the type keyword, but *without* the apostrophes on token1 
and phrase1?

-- hendrik

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] An awkwardness with type parameters
  2019-02-27  1:59   ` Hendrik Boom
@ 2019-02-27  3:46     ` Jacques Garrigue
  0 siblings, 0 replies; 5+ messages in thread
From: Jacques Garrigue @ 2019-02-27  3:46 UTC (permalink / raw)
  To: Hendrik Boom; +Cc: Mailing List OCaml

[-- Attachment #1: Type: text/plain, Size: 1868 bytes --]

On 2019/02/27 10:59, Hendrik Boom wrote:
> 
> On Tue, Feb 26, 2019 at 03:39:24PM +0900, Jacques Garrigue wrote:
>> 
>> 2019/02/26 15:23, Hendrik Boom <hendrik@topoi.pooq.com <mailto:hendrik@topoi.pooq.com>>:
>>> 
>>> I have a (broken) function definition starting:
>>> 
>>> let mixfix : 'token1 'phrase1.
>>> ('token1, 'phrase1, ('token1, 'phrase1) Phrasestream.phrasestream) grammar
>>>       -> ('token1, 'phrase1) parser
>>>  =  fun gram -> (
>>> ...
>>> 
>>> It goes o for a hundred-odd lines.
>>> The type-parameterized types Phrasestream.phrasestream, grammar, and parser   
>>> have been previously defined, and there are also operations defined on these
>>> types.
>>> 
>>> The problem I'm having is that the OCaml type-checker ends up identifying
>>> the type parameters 'token1 an 'phrase1 because of type errors I've made in
>>> the body of the mixfix function.  Somewhere I ended up using an
>>> operator that's supposed to work on values of type 'token1
>>> on a value of type 'phrase1 instead, and identification of 'token1 and 
>>> 'phrase1 is the natural result.
>>> 
>>> Is there some way of forcing the type checker to treat 'token1 and 
>>> 'phrase1 as different types so that I can get meaningful type errors
>>> at the point were they occur?
>> 
>> You can use locally abstract types, which are often used with GADTs but are not
>> restricted to them:
>>  let mixfix : type token1 phrase1.
>>    (token1, phrase1, (token1, phrase1) Phrasestream.phrasestream) grammar
>>       -> (token1, phrase1) parser
>>  =  fun gram -> (
> 
> Thus *with* the type keyword, but *without* the apostrophes on token1 
> and phrase1?

Exactly. This means that in the body of your function their are abstract types, and you cannot unify them. Aside of that the behavior is the same as what we had written first.

Jacques


[-- Attachment #2: Type: text/html, Size: 5924 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [Caml-list] An awkwardness with type parameters
  2019-02-26  6:39 ` Jacques Garrigue
  2019-02-27  1:59   ` Hendrik Boom
@ 2019-02-27 18:21   ` Hendrik Boom
  1 sibling, 0 replies; 5+ messages in thread
From: Hendrik Boom @ 2019-02-27 18:21 UTC (permalink / raw)
  To: Jacques Garrigue; +Cc: Mailing List OCaml

On Tue, Feb 26, 2019 at 03:39:24PM +0900, Jacques Garrigue wrote:
> 
> 2019/02/26 15:23, Hendrik Boom <hendrik@topoi.pooq.com>:
> > 
> > I have a (broken) function definition starting:
> > 
> > let mixfix : 'token1 'phrase1.
> >  ('token1, 'phrase1, ('token1, 'phrase1) Phrasestream.phrasestream) grammar
> >        -> ('token1, 'phrase1) parser
> >   =  fun gram -> (
> >  ...
> > 
> > It goes o for a hundred-odd lines.
> > The type-parameterized types Phrasestream.phrasestream, grammar, and parser   
> > have been previously defined, and there are also operations defined on these
> > types.
> > 
> > The problem I'm having is that the OCaml type-checker ends up identifying
> > the type parameters 'token1 an 'phrase1 because of type errors I've made in
> > the body of the mixfix function.  Somewhere I ended up using an
> > operator that's supposed to work on values of type 'token1
> > on a value of type 'phrase1 instead, and identification of 'token1 and 
> > 'phrase1 is the natural result.
> > 
> > Is there some way of forcing the type checker to treat 'token1 and 
> > 'phrase1 as different types so that I can get meaningful type errors
> > at the point were they occur?
> 
> You can use locally abstract types, which are often used with GADTs but are not
> restricted to them:
>   let mixfix : type token1 phrase1.
>     (token1, phrase1, (token1, phrase1) Phrasestream.phrasestream) grammar
>        -> (token1, phrase1) parser
>   =  fun gram -> (
>  …
> This is the same as the type you wrote, but additionally prevents the types from being
> instantiated inside the definition of mixdix.
> 
> Jacques Garrigue

Thank you.  I've already found the bug, but I made this change and it will
keep it from coming back.

-- hendrik


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2019-02-27 18:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-26  6:23 [Caml-list] An awkwardness with type parameters Hendrik Boom
2019-02-26  6:39 ` Jacques Garrigue
2019-02-27  1:59   ` Hendrik Boom
2019-02-27  3:46     ` Jacques Garrigue
2019-02-27 18:21   ` Hendrik Boom

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).