Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help to convert tutorials to running programs within a project #80

Closed
russellcameronthomas opened this issue Mar 19, 2017 · 12 comments
Closed

Comments

@russellcameronthomas
Copy link

russellcameronthomas commented Mar 19, 2017

I am relatively new to Scala (with Java background) and I am teaching myself Functional Programming. I am working my way through your tutorials.

My problem is that I am only able to get them running through the command line REPL. I have been trying to take the (simple) step of inserting each of the tutorials into a file within IntelliJ IDEA so that they print results to the console. My problem is they won't compile without errors, and I don't know why.

For example, taking code from the "Introduction" here is the file I have created:

class Tutorial{

}

object Intro extends App {
  import cats._
  import cats.data._
  import org.atnos.eff._

  type ReaderInt[A] = Reader[Int, A]
  type WriterString[A] = Writer[String, A]

  type Stack = Fx.fx3[WriterString, ReaderInt, Eval]

  import org.atnos.eff.all._
  import org.atnos.eff.syntax.all._

  // useful type aliases showing that the ReaderInt and the WriterString effects are "members" of R
  // note that R could have more effects
  type _readerInt[R]    = ReaderInt |= R
  type _writerString[R] = WriterString |= R


  def program[R: _readerInt : _writerString : _eval]: Eff[R, Int] = for {
  // get the configuration
    n <- ask[R, Int]

    // log the current configuration value
    _ <- tell("the required power is " + n)

    // compute the nth power of 2
    a <- delay(math.pow(2, n.toDouble).toInt)

    // log the result
    _ <- tell("the result is " + a)
  } yield a


  println(program[Stack].runReader(6).runWriter.runEval.run)
}

The compiler error is in the last line: "Cannot resolve symbol 'run'". I have tried many other approaches and structures, and they all give this error, or some other "Cannot resolve..." error.

I have already tried the "clear cache" fix, with restart of IntelliJ IDEA.

Can anyone help?

@reactormonk
Copy link
Contributor

This looks more like a stackoverflow question, could you post the link to the question here?

@etorreborre
Copy link
Member

There are 2 compiler plugins which are necessary for compilation:

// to write types like Reader[String, ?]
addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.3")

// to get types like Reader[String, ?] (with more than one type parameter) correctly inferred
// this plugin is not necessary with Scala 2.12
addCompilerPlugin("com.milessabin" % "si2712fix-plugin_2.11.8" % "1.2.0")

If you want to compile with Intellij you probably have to specify those plugins:
image
image
(but as you can see I am not using Intellij to build my projects :-)).

@russellcameronthomas
Copy link
Author

@reactormonk I considered Stackoverflow because I'm probably missing something basic, but my question is specific to the content of this tutorial. In my opinion, tutorials should provide enough information for anyone to run them, even novices.

@russellcameronthomas
Copy link
Author

@etorreborre Here is my build.sbt file. As you can see I include those compiler plugins. I still get the error.

name := "Tutorial"

version := "1.0"

scalaVersion := "2.11.8"

libraryDependencies += "org.typelevel" %% "cats" % "0.9.0"

libraryDependencies += "org.atnos" %% "eff" % "3.1.0"

// to write types like Reader[String, ?]
addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.3")


// to get types like Reader[String, ?] (with more than one type parameter) correctly inferred
// this plugin is not necessary with Scala 2.12
addCompilerPlugin("com.milessabin" % "si2712fix-plugin_2.11.8" % "1.2.0")

@russellcameronthomas
Copy link
Author

@edmundnoble
Copy link
Contributor

@russellcameronthomas I pasted your program into ammonite shell and it worked just fine. I don't know how you're building your project, but I highly suggest you ditch IntelliJ (for builds) because of issues like this one exactly. The reason why IntelliJ cannot figure out your code in particular looks to be because IntelliJ is not capable of simulating implicit-directed type inference; the Member implicits have a type member, Out, inside them which represents the remaining effect stack. If the IDE cannot figure it out, it subs in a fresh type variable and thus the run ops constructor cannot be called because the inferred type according to IntelliJ is Eff[m.Out, A] and not Eff[NoFx, A].

More to the point: you can fix this by calling eval.runEval instead of postfix runEval and supplying the type arguments. But it won't be pretty. And it might end up requiring that you add the type arguments to every runX call.

@russellcameronthomas
Copy link
Author

@edmundnoble Thanks for all this info.

Yes, your explanation makes sense, at least to the degree that I understand "implicit-directed type inference".

Regarding your "fix", can you post the exact code? At this point, I don't care at all about being verbose or adding boilerplate. What I care about is getting code that runs, so I can learn from it through incremental modifications.

As for "building my project", I entered the build.sbt file, as shown, and then I clicked the "run" button in IntelliJ-IDEA.

@russellcameronthomas
Copy link
Author

russellcameronthomas commented Mar 20, 2017

@etorreborre @edmundnoble

I have resolved my problem -- to a degree.

The IDE still shows the error highlighted, as described above.

But instead of directly running the App object, I clicked Recompile Tutorial.scala from the Build menu. It successfully compiled and ran successfully.

I'm not happy about this, but at least it runs.

Thanks for your help.

@etorreborre : It would be nice if you added a paragraph or two to your Tutorial on "How to run the Tutorial in an IDE"

Context: One of my strong values is that we should be helping novices not just experts in what ever language, libraries, and tools we are using. When I write tutorials (e.g. Probabilistic Programming http://library.meritology.com) I do my best to serve novices as well as experts.

@etorreborre
Copy link
Member

@russellcameronthomas I am glad you found the issue to your problem.

I must also say that my open-source time is fairly limited. For instance I spent a good chunk of my week-end reworking interpreters to make them easier to understand in the next release. I would love to have more time to add more detailed tutorials but there's only so much I can do as an individual 😸 (this is not my only open-source project). So please if you like the library and think the doc should be expanded, make a PR, the 2 paragraphs should probably be added here.

@etorreborre
Copy link
Member

@russellcameronthomas btw the intellij issue you see about highlighting is something I frequently get with "type-aware" highlighting. I have been unchecking this option for at least the past 3 years now :-(.

@reactormonk
Copy link
Contributor

@russellcameronthomas Ah ok, the issue was the assumption that when intellij tells you something doesn't compile, it won't - I just skip directly to the underlying scala compiler and wouldn't call it "it doesn't compile" but "intellij doesn't think it compiles".

@etorreborre
Copy link
Member

I am closing this for now, I hope the current issue + the paragraph added by @reactormonk will help. Let's re-open if that's not the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants