This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Could quasiquotes do this?

6 replies
milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.

One thing that I'm finding myself wanting quite frequently these days
is a way of having the compiler verify for me that something otherwise
well-formed doesn't in fact typecheck. The test suite and examples for
shapeless are littered with commented out lines like,

  def acceptOption[L <: HList : TC[Option]#λ](l : L) = true

val l1 = Option(23) :: Option(true) :: Option("foo") :: HNil
  val l2 = Option(23) :: true :: Option("foo") :: HNil

 acceptOption(l1)  // Compiles
 //acceptOption(l2)  // Does not compile

which is pretty useless if I should ever break things in such a way
that the last line actually _did_ compile.

Is there something that quasiquotation could do for us here? Would it
be possible to support something like,

 typeError(acceptOption(l2))

which typechecks iff acceptOption(l2) is a type error (but not if, eg.
l2 is undefined)?

Cheers,

Miles

Eugene Burmako
Joined: 2011-09-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Could quasiquotes do this?
This maps directly onto macros. 
You can write typeError as a macro, which accepts an untyped argument (currently not implemented, but soon will be: https://issues.scala-lang.org/browse/SI-5404). Then you just take this argument (an AST) and typecheck it using services of scala.reflect.macro.Context (typechecking is not yet there, but it will be eventually). If typechecking returns an error, you just return from a macro. If not, you raise an error yourself (using some API that is still not in place).

All in all, this can be done with macros, though it has to wait for the next macro-related pull request.
On 5 February 2012 20:48, Miles Sabin <miles@milessabin.com> wrote:
One thing that I'm finding myself wanting quite frequently these days
is a way of having the compiler verify for me that something otherwise
well-formed doesn't in fact typecheck. The test suite and examples for
shapeless are littered with commented out lines like,

  def acceptOption[L <: HList : TC[Option]#λ](l : L) = true

 val l1 = Option(23) :: Option(true) :: Option("foo") :: HNil
  val l2 = Option(23) :: true :: Option("foo") :: HNil

  acceptOption(l1)  // Compiles
  //acceptOption(l2)  // Does not compile

which is pretty useless if I should ever break things in such a way
that the last line actually _did_ compile.

Is there something that quasiquotation could do for us here? Would it
be possible to support something like,

  typeError(acceptOption(l2))

which typechecks iff acceptOption(l2) is a type error (but not if, eg.
l2 is undefined)?

Cheers,


Miles

--
Miles Sabin
tel: +44 7813 944 528
gtalk: miles@milessabin.com
skype: milessabin
g+: http://www.milessabin.com
http://twitter.com/milessabin
http://www.chuusai.com/

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Could quasiquotes do this?

On Sun, Feb 5, 2012 at 7:59 PM, Eugene Burmako wrote:
> This maps directly onto macros.
>
> You can write typeError as a macro, which accepts an untyped argument
> (currently not implemented, but soon will be:
> https://issues.scala-lang.org/browse/SI-5404). Then you just take this
> argument (an AST) and typecheck it using services of
> scala.reflect.macro.Context (typechecking is not yet there, but it will be
> eventually). If typechecking returns an error, you just return from a macro.
> If not, you raise an error yourself (using some API that is still not in
> place).
>
> All in all, this can be done with macros, though it has to wait for the next
> macro-related pull request.

Woo hoo! Good stuff :-)

Cheers,

Miles

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Could quasiquotes do this?

On Sun, Feb 5, 2012 at 8:03 PM, Miles Sabin wrote:
> On Sun, Feb 5, 2012 at 7:59 PM, Eugene Burmako wrote:
>> This maps directly onto macros.
>>
>> You can write typeError as a macro, which accepts an untyped argument
>> (currently not implemented, but soon will be:
>> https://issues.scala-lang.org/browse/SI-5404). Then you just take this
>> argument (an AST) and typecheck it using services of
>> scala.reflect.macro.Context (typechecking is not yet there, but it will be
>> eventually). If typechecking returns an error, you just return from a macro.
>> If not, you raise an error yourself (using some API that is still not in
>> place).
>>
>> All in all, this can be done with macros, though it has to wait for the next
>> macro-related pull request.
>
> Woo hoo! Good stuff :-)

Hmm ... although, now I think about it, are you sure this wouldn't
allow me to construct a type-level liar?

Cheers,

Miles

geoff
Joined: 2008-08-20,
User offline. Last seen 1 year 25 weeks ago.
Re: Could quasiquotes do this?

On Feb 5, 2012, at 12:48 PM, Miles Sabin wrote:
> Is there something that quasiquotation could do for us here? Would it
> be possible to support something like,
>
> typeError(acceptOption(l2))
>
> which typechecks iff acceptOption(l2) is a type error (but not if, eg.
> l2 is undefined)?

This would be really great to have. It could be even easier if the tree a macro def gets doesn't need to be well typed. The macro could then rewrite the tree to be well typed. In this case it would rewrite an ill typed tree to a unit value and a well typed tree to an error value.

Eugene Burmako
Joined: 2011-09-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Could quasiquotes do this?

Type-level liar?

On Feb 5, 9:08 pm, Miles Sabin wrote:
> On Sun, Feb 5, 2012 at 8:03 PM, Miles Sabin wrote:
> > On Sun, Feb 5, 2012 at 7:59 PM, Eugene Burmako wrote:
> >> This maps directly onto macros.
>
> >> You can write typeError as a macro, which accepts an untyped argument
> >> (currently not implemented, but soon will be:
> >>https://issues.scala-lang.org/browse/SI-5404). Then you just take this
> >> argument (an AST) and typecheck it using services of
> >> scala.reflect.macro.Context (typechecking is not yet there, but it will be
> >> eventually). If typechecking returns an error, you just return from a macro.
> >> If not, you raise an error yourself (using some API that is still not in
> >> place).
>
> >> All in all, this can be done with macros, though it has to wait for the next
> >> macro-related pull request.
>
> > Woo hoo! Good stuff :-)
>
> Hmm ... although, now I think about it, are you sure this wouldn't
> allow me to construct a type-level liar?
>
> Cheers,
>
> Miles
>
> --
> Miles Sabin
> tel: +44 7813 944 528
> gtalk: mi...@milessabin.com
> skype: milessabin
> g+:http://www.milessabin.comhttp://twitter.com/milessabinhttp://www.chuusai.com/

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 3 days ago.
Re: Could quasiquotes do this?
As a pre-macros approach to this:
Rogue includes some tests which check that certain expressions don't type-check. It does this by embedding an interpreter in the test suite and checking the output of the interpreter.
See the "thingsThatShouldntCompile" test: https://github.com/foursquare/rogue/blob/master/src/test/scala/com/foursquare/rogue/QueryTest.scala#L562
--j

On Sun, Feb 5, 2012 at 2:48 PM, Miles Sabin <miles@milessabin.com> wrote:
One thing that I'm finding myself wanting quite frequently these days
is a way of having the compiler verify for me that something otherwise
well-formed doesn't in fact typecheck. The test suite and examples for
shapeless are littered with commented out lines like,

  def acceptOption[L <: HList : TC[Option]#λ](l : L) = true

 val l1 = Option(23) :: Option(true) :: Option("foo") :: HNil
  val l2 = Option(23) :: true :: Option("foo") :: HNil

  acceptOption(l1)  // Compiles
  //acceptOption(l2)  // Does not compile

which is pretty useless if I should ever break things in such a way
that the last line actually _did_ compile.

Is there something that quasiquotation could do for us here? Would it
be possible to support something like,

  typeError(acceptOption(l2))

which typechecks iff acceptOption(l2) is a type error (but not if, eg.
l2 is undefined)?

Cheers,


Miles

--
Miles Sabin
tel: +44 7813 944 528
gtalk: miles@milessabin.com
skype: milessabin
g+: http://www.milessabin.com
http://twitter.com/milessabin
http://www.chuusai.com/

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland