MPS: Defining baseLanguage predicate in DSL - mps

I want to have as part of my DSL's concept filed which will contain java predicate with following signature (string -> boolean). Which type should I import from baseLanguage to do this?

Looks like you need to import the closures language. FunctionType and ClosureLiteral are the concepts that you are most likely looking for.

Related

Meaning of "=" in type declaration in Hashkell

As I learn Haskell, I can't help but try to understand everything from a formal point of view. After all this is the theoretical coherence I came to look for as a Scala Programmer.
One thing that does compute well in my mind, is fitting the meaning of type declaration in the overraching theory of lambda calculus and everything is an expression. Yes there is Binding, but Binding does not work with type declaration either.
Example
data DataType a = Data a | Datum
Question:
What is the meaning of = here? If it was the declaration of a function, then on the rhs we would get an expression reducible to an irreducible value or another expression (i.e. returning a function). This is not what we have above.
My confusion
We have a type function in DataType a, and Data Constructor a.k.a Value Constructor in Data a and Datum. Clearly a type is not equal to a value, one is at the term level and the other at the type level. Not the same space at all. However it might work to follow the pronunciation provided here https://wiki.haskell.org/Pronunciation. = is pronounced is. As in a DataType is those values. But that is a stretch. because it is not the same semantic as for a function declaration. Hence I'm puzzled. A Type Level function equal a value level function makes no sense to me.
My question reformulated differently so to explain what i am looking for
So in a sense, I would like to understand the semantic of a data declaration and where does it fit in everything is an expression (Haskell theoretical framework) ?
Clarifying the difference with Binding. Generally speaking, when talking about binding, can we say it is an expression of type Unit ? Otherwise, what is it, and what is the type of it, and where does it fit in lambda calculus or whatever we should call the theoretical framework that backs Haskell ?
I think the simplest answer here is that = doesn’t have any one meaning in particular — it is simply the syntax which the Haskell designers decided to use for data types. There is no particular relationship between the = used in function definitions and the = used in ADT definitions; both constructions are used to declare something on the LHS using some sort of specification on the RHS, but that’s about it. The two constructions have more differences than similarities.
As it happens, modern Haskell doesn’t necessarily require the use of = when defining an ADT. The GADTSyntax and GADTs GHCI language extensions enable ‘GADT syntax’, which is an alternate method to define ADTs. It looks like this:
data DataType a where
Data :: a -> DataType a
Datum :: DataType a
This illustrates that the use of = is not necessarily a mandatory part of type declarations: it is simply one convention amongst many.
(As to why the Haskell designers chose to use the convention with =, I’m not entirely sure. Possibly it might have been by analogy with type synonyms, like type Synonym = Maybe Int, in which = does have similarities to its use in function definitions, in that the RHS expresses a type which is then assigned the name on the LHS.)
Since you mentioned you are a Scala programmer, here is what DataType might look like in Scala:
sealed trait DataType[+A]
case class Data[A](a: A) extends DataType[A]
object Datum extends DataType[Nothing]
I read your Haskell code as "A DataType a can be either a Data a or Datum". DataType is a type constructor, whereas Data is a value constructor and Datum is a value. You can think of Data as a function Data :: a -> DataType a, and Datum as something like Datum :: forall a. DataType a (imagine the names were lowercase). The RHS tells you which functions you can use to get a value of type LHS.

Scala String toInt - Int does not take parameters

So I'm learning Scala and I came across this finicky issue...
If we have a String and want to convert it to an Int, all examples that I've found online say, "It's so simple! Just use String.toInt!"
Okay:
var x = readLine().toInt
Simple enough. I'm assuming toInt is a function of String. But if that's the case, I should be able to call toInt with parentheses, right? Coming from Java, this feels more natural to me:
var x = readLine().toInt()
But alas! Scala gives me the following error:
[error] /home/myhome/code/whatever/hello.scala:13: Int does not take
parameters
[error] var x = readLine().toInt()
Curious. What does this error mean? Is toInt not a function of String? Similarly, why can I do both:
var x = readLine().toLowerCase()
var y = readLine().toLowerCase
with out any problems?
Edit: The duplicate question does not address the toLowerCase vs toInt issue.
This is a good question, and I don't think it counts as a duplicate of the question about defining zero-arity methods with or without parentheses.
The difference between toInt and toLowerCase here is that toInt is a syntactic enrichment provided by the standard library's StringOps class. You can check this by using reify and showCode in the REPL:
scala> import scala.reflect.runtime.universe.{ reify, showCode }
import scala.reflect.runtime.universe.{reify, showCode}
scala> showCode(reify("1".toInt).tree)
res0: String = Predef.augmentString("1").toInt
This just desugars the enrichment method call and shows you what implicit conversion has been applied to support it.
If we look at the toInt method on StringOps, we see that it's defined without parentheses. As the answer to the possible duplicate question points out, if a zero-arity method in Scala is defined without parentheses, it can't be called with parentheses (although if it's defined with parentheses it can be called either way).
So that's why "1".toInt() doesn't work. If String were a normal Scala class following normal Scala naming conventions, "ABC".toLowerCase() wouldn't work either, since the method would be defined without parentheses, since it's not side-effecting (this is just a convention, but it tends to be pretty consistently applied in Scala code).
The problem is that String isn't actually a class in the Scala standard library—it's just java.lang.String. At the JVM level, there's no difference between a zero-arity method defined with parentheses and one defined without parentheses—this is purely a Scala distinction, and it's not encoded in the JVM method signature, but in a separate batch of metadata stored in the class file.
The Scala language designers decided to treat all zero-arity methods that aren't defined in Scala (and therefore don't have this extra metadata) as if they were defined with parentheses. Since the toLowerCase method on String is really and truly a method of the java.lang.String class (not a syntactic enrichment method like toInt), it falls into this category, which means you can call it either way.

Is name reuse the only advantage of type classes?

I am trying to understand type classes in Haskell.
I pose the question using a simple example:
Lets consider two datatypes, A and B.
Lets also assume that a1 and a2 are of type A and b1 and b2 are of type B.
Now let's assume that we want to define functions to check if a1 equals a2 and whether b1 is the same as b2.
For this we have two options:
Option 1:
Define two equality functions using separate names like this :
eqA :: A -> A -> Bool
eqA = ...
eqB :: B -> B -> Bool
eqB = ...
Now if we want to check if a1 equals a2 then we write : eqA a1 a2, similarly for B : eqB b1 b2.
Option 2:
We make A and B instances of the Eq typeclass.
In this case we can use the == sign for checking for equality as follows.
a1==a2
b1==b2
So, now the question : as far as I understand the sole purpose of the existence of type classes is that we can reuse the function name == for the equivalence checking function, as shown in Option 2. So if we use type classes we do not have to use different function names for essentially the same purpose ( i.e. checking for equality).
Is that correct? Is name reuse the only advantage that type classes give? Or is there something more to type classes ?
In other words, given any program using type classes, I could rewrite that program to an isomorphic program that does not use type classes by giving separate names (i.e. eqA and eqB) to the overloaded function names (i.e. == in Option 2) that are defined in the type class instance declarations.
In the simple example this transformation would replace Option 2 with Option 1.
Is such a mechanic transformation always possible?
Is that not what the compiler is doing behind the scenes?
Thank you for reading.
EDIT:
Thanks for all the illuminating answers !
There is this very good article regarding type class which shows their desugaring process: https://www.fpcomplete.com/user/jfischoff/instances-and-dictionaries
So, yes, you could do without them, but you will need to have a different function for each type as you did in your question, or you will need to pass the instance dictionary explicitly as described in the article, while the type class do it implicitly so you don't need to bother. In many cases, you would end up with a LOT of functions with different names!
Nevertheless, your question leads also to the fact that type classes are often too much used when other patterns would solve the problem as well:
http://lukepalmer.wordpress.com/2010/01/24/haskell-antipattern-existential-typeclass/
Type classes are syntax sugar, so there is nothing that you cannot do without them. It is just a question of how hard it would be to do without them.
My favorite example is QuickCheck, which uses type classes to recurse over function arguments. (Yes, you can do that.) QuickCheck takes a function with an arbitrary number of arguments, and auto-generates random test data for each argument, and checks that the function always returns true. It uses type classes to figure out how to "randomly" generate data of each individual type. And it uses clever type class tricks to support functions with any number of arguments.
You don't need this; you could have a test1 function, and then a test2 function, and then a test3 function, etc. But it's nice to be able to have just "one function" which automatically figures out how many arguments there are. Indeed, without type classes you'd end up having to write something like
test_Int
test_Double
test_Char
test_Int_Int
test_Int_Double
test_Int_Char
test_Double_Int
test_Double_Double
test_Double_Char
...
...for every possible combination of inputs. And that would just be really annoying. (And good luck trying to extend it within your own code.)
Really, type classes make your code more high-level and declarative. It makes it easier to see what the code is trying to do. (Most of the time.) There are really advanced cases where I start to think maybe the design isn't quite right, but for the vast majority of stuff, type classes work really well.
For the most part the answer is that type classes give you name re-use. Sometimes the amount of name re-use you get is more than you might think.
A Type Class is somewhat similar to an Interface in Object Oriented languages. Occasionally someone might ask "is the only purpose of an Interface to enforce contracts in APIs?". We might see this code in java
public <A> void foo (List<A> list) {
list.clear();
}
List<A> list = new ArrayList<>();
List<A> list2 = new LinkedList<>();
foo(list);
foo(list2);
Both ArrayList and LinkedList implement the List interface. As such, we can write a general function that foo which operates on any List. This is similar to your Eq example. In Haskell we have many higher order functions like this. For example foldl can be implemented by more than just lists, and we can use foldl on anything that uses Foldable from Data.Foldable.
In OO you can use generics to do some really interesting abstractions and inheritance (e.g. in Java things like Foo<A extends Foo<A>>). In Haskell we can use Type Classes to do different types of abstractions.
Let's take the Monad class, which requires you to implement "return". Maybe implements return something like this:
instance Monad Maybe where
return a = Just a
Whilst List implements return like this:
instance Monad [] where
return a = [a]
Return, as it is done in Haskell, is not possible in a language like Java. In Java if you want to create a new instance of something in a container (say a new List<String>) you have to use new ArrayList<String>();. You can't create an instance of something because it fulfills a contract. While in Haskell we can write this code which would not be possible in Java:
foobar :: (Monad m) => (m a -> m b) -> a -> m b
foobar g a = g $ return a
Now I can pass anything to foobar. I could pass a function Maybe a -> Maybe b or [a] -> [b] and foobar will still accept.
Now of course this could all be broken down and functions could be written one by one. Just like Java could ignore interfaces and implement everything from scratch on each class of List. But just like the inheritance that's possible in OO once you factor in generics, in Haskell we can do some interesting things in Type Classes using language extensions. For example, check out http://www.haskell.org/haskellwiki/Functional_dependencies_vs._type_families

How to build Abstract Syntax Trees from grammar specification in Haskell?

I'm working on a project which involves optimizing certain constructs in a very small subset of Java, formalized in BNF.
If I were to do this in Java, I would use a combination of JTB and JavaCC which builds an AST. Visitors are then used to manipulate the tree. But, given the vast libraries for parsing in Haskell (parsec, happy, alex etc), I'm a bit confused in chossing the appropriate library.
So, simply put, when a language is specified in BNF, which library offers the easiest means to build an AST? And what is the best way to go about modifying this tree in idiomatic Haskell?
Well in Haskell there are 2 main ways of parsing something, parse combinators or a parser generator. Since you already have a BNF I'd suggest the latter.
A good one is alex. GHC's parser IIRC is written using this so you'd be in good company.
Next you'll have a big honking stack of data declarations to parse into:
data JavaClass = {
className :: Name,
interfaces :: [Name],
contents :: [ClassContents],
...
}
data ClassContents = M Method
| F Field
| IC InnerClass
and for expressions and whatever else you need. Finally you'll combine these into something like
data TopLevel = JC JavaClass
| WhateverOtherForms
| YouWillParse
Once you have this you'll have the entire AST represented as one TopLevel or a list of them depending on how many you classes/files you parse.
To proceed from here depends on what you want to do. There are a number of libraries such as syb (scrap your boilerplate) that let you write very concise tree traversals and modifications. lens is also an option. At a minimum check out Data.Traversable and Data.Foldable.
To modify the tree, you can do something as simple as
ignoreInnerClasses :: JavaClass -> JavaClass
ignoreInnerContents c = c{contents = filter isClass $ contents c}
-- ^^^ that is called a record update
where isClass (IC _) = True
isClass _ = False
and then you could potentially use something like syb to write
everywhere (mkT ignoreInnerClass) toplevel
which will traverse everything and apply ignoreInnerClass to all JavaClasses. This is possible to do in lens and many other libraries too, but syb is very easy to read.
I've never used bnfc-meta (suggested by #phg), but I would strongly recommend you look into BNFC (on hackage: http://hackage.haskell.org/package/BNFC). The basic approach is that you write your grammar in an annotated BNF style, and it will automatically generate an AST, parser, and pretty-printer for the grammar.
How suitable BNFC is depends upon the complexity of your grammar. If it's not context-free, you'll likely have a difficult time making any progress (I did make some success hacking up context-sensitive extensions, but that code's likely bit-rotted by now). The other downside is that your AST will very directly reflect the grammar specification. But since you already have a BNF specification, adding the necessary annotations for BNFC should be rather straightforward, so it's probably the fastest way to get a usable AST. Even if you decide to go a different route, you might be able to take the generated data types as a starting point for a hand-written version.
Alex + Happy.
There are many approaches to modify/investigate the parsed terms (ASTs). The keyword to search for is "datatype-generic" programming. But beware: it is a complex topic ...
http://people.cs.uu.nl/andres/Rec/MutualRec.pdf
http://www.cs.uu.nl/wiki/GenericProgramming/Multirec
It has a generic implementation of the zipper available here:
http://hackage.haskell.org/packages/archive/zipper/0.3/doc/html/Generics-MultiRec-Zipper.html
Also checkout https://github.com/pascalh/Astview
You might also check out the Haskell Compiler Series which is nice as an introduction to using alex and happy to parse a subset of Java: http://bjbell.wordpress.com/haskell-compiler-series/.
Since your grammar can be expressed in BNF, it is in the class of grammars that are efficiently parseable with a shift-reduce parser (LALR grammars). Such efficient parsers can be generated by the parser generator yacc/bison (C,C++), or its Haskell equivalent "Happy".
That's why I would use "Happy" in your case. It takes grammar rules in BNF form and generates a parser from it directly. The resulting parser will accept the language that is described by your grammar rules and produce an AST (abstract syntax tree). The Happy user guide is quite nice and gets you started quickly:
http://www.haskell.org/happy/doc/html/
To transform the resulting AST, generic programming is a good idea. Here is a classical explanation on how to do this in Haskell in a practical fashion, from scratch:
http://research.microsoft.com/en-us/um/people/simonpj/papers/hmap/
I have used exactly this to build a compiler for a small domain specific language, and it was a simple and concise solution.

OCaml: Type Checking Objects

If I have an object, how can I determine its type? (Is there an OCaml equivalent to Java's instanceof operator?)
OCaml has structural typing for objects rather than nominative typing as in Java. So the type of an object is basically determined (and only determined) by its methods. Objects in OCaml can be created directly, without going through something like a class.
You can write functions which require that its argument objects have certain methods (and that those methods have certain types); for example, the following method takes an argument that is any object with a method "bar":
let foo x = x#bar
There's a discussion of "Matching Objects With Patterns" on Lambda the Ultimate (the paper uses Scala as the language, so won't answer your question). A more relevant Ocaml mailing list thread indicates that there's no RTTI/safe-downcasting for objects.
For algebraic (non object) types you obviously have:
match expr with
Type1 x -> x
Type2 (x,y) -> y
called (pattern) matching
Someone did write an extension that allows down/up-casting Ocaml objects.
In short, you have to encode your own RTTI mechanism. OCaml provides no RTTI or up/down casting (the latter in part because inheritance and subtyping are orthogonal in OCaml rather than unified as in Java).
You could do something with strings or polymorphic variants to encode type information in your classes and objects. I believe that LablGTK does some of this, and provides a utility library to support object tagging and up/down casting.
Somewhat out-of-topic, but the OPA language (which draws heavily from some aspects of OCaml), allows the equivalent of pattern-matching on objects. So it's quite feasible.

Resources