<div dir="ltr"><div><div><div><div>You're right, "Just" is used at two different levels, it's just that it have the same name is both cases.<br></div>One is at type level, the other at value level.<br></div>Since this is two separated levels, there is no risk of confusion and having the same name is fine.<br><br></div>To be clear, you can write:<br><span style="font-family:monospace,monospace;font-size:12.8px"><br>data Maybe tp = Just Int | Nothing</span><br><br></div><div>Then use it as:<br><br></div><div>my_value = Just 3<br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 27, 2015 at 10:44 AM, Shishir Srivastava <span dir="ltr"><<a href="mailto:shishir.srivastava@gmail.com" target="_blank">shishir.srivastava@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Sorry, but I still have some grudges so to say with the way 'Maybe' is defined. <div><br></div><div>By definition '<b>Maybe</b>' takes a type parameter which I will denote here as '<b>tp</b>' for the sake of clarity like below - <div><br></div><div><span style="font-family:monospace,monospace;font-size:12.8000001907349px">data Maybe tp = Just tp | Nothing</span><br><div><br></div><div>Therefore '<b>tp</b>' is not representing a value perse such as '3' or 'XYZ' etc but a data type such as '<b>Int</b>', '<b>Char</b>' etc. </div></div><div><br></div><div>But when we have to create a data type of '<b>Maybe</b>' we write '<b>Just 3</b>' and not '<b>Just Int</b>' or '<b>Just Char</b>' which is how it's defined in the definition (i.e. '<b>Just tp</b>' ) . </div><div><br></div><div>It is this discrepancy in definition and the actual value creation which is slightly troubling me.</div><div><br></div><div>Thanks,</div><div>Shishir</div></div></div><div class="gmail_extra"><span class="HOEnZb"><font color="#888888"><br clear="all"><div><div><div dir="ltr"><font color="#0b5394"><font style="background-color:rgb(255,255,255)"><font face="georgia, serif">Shishir Srivastava</font><br><font face="georgia, serif"> <span></span><span></span><a href="tel:%2B44%20%280%29%20750%20127%205019" value="+447501275019" target="_blank">+44 (0) 750 127 5019</a></font></font><br></font><br></div></div></div></font></span><div><div class="h5">
<br><div class="gmail_quote">On Thu, Mar 26, 2015 at 5:31 PM, Corentin Dupont <span dir="ltr"><<a href="mailto:corentin.dupont@gmail.com" target="_blank">corentin.dupont@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div>Not really, when you define the type "Maybe a":<br><br><span><span style="font-family:monospace,monospace">data Maybe a = Just a | Nothing<br><br></span></span></div><span><span style="font-family:monospace,monospace">Haskell is creating automatically two functions for you:<br><br></span></span><span><span style="font-family:monospace,monospace">Just :: a -> Maybe a<br></span></span></div><span style="font-family:monospace,monospace">Nothing :: Maybe a<br></span><div><div><span><span style="font-family:monospace,monospace"><br></span></span><div><div class="gmail_extra">In the first case, you can think of "Just" and "Nothing" as a sort of tag identifying which element of the sum you have.<br></div><div class="gmail_extra">It the second case it's a function, with the same name.<br></div><div class="gmail_extra">A more informed person than me could say if they are indeed separated of if they are the same thing in GHC...<br></div><div><div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Mar 26, 2015 at 5:34 PM, Shishir Srivastava <span dir="ltr"><<a href="mailto:shishir.srivastava@gmail.com" target="_blank">shishir.srivastava@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">isn't that then cyclic dependency between 'Maybe' and 'Just' ...where the first one is defined in terms of second and vice-versa...?<div class="gmail_extra"><span><font color="#888888"><br clear="all"><div><div><div dir="ltr"><font color="#0b5394"><font style="background-color:rgb(255,255,255)"><font face="georgia, serif">Shishir </font></font><br></font><br></div></div></div></font></span><div><div>
<br><div class="gmail_quote">On Thu, Mar 26, 2015 at 3:48 PM, Corentin Dupont <span dir="ltr"><<a href="mailto:corentin.dupont@gmail.com" target="_blank">corentin.dupont@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div>Hi Shishir,<br></div>I think that's a legitimate question.<br><br></div>By writing <br><span><br><span style="font-family:monospace,monospace">data Maybe a = a | Nothing</span><br><br></span></div>you are saying that the type of the left hand side of the = is the same that right hand side (you are defining a type basically).<br></div><div>Also you can only sum things of the same type.<br></div>So you are saying:<br></div><span style="font-family:monospace,monospace">type "Maybe a" = type "a"</span><br></div>Which is wrong.<br><div><div>That's why "a" should be wrapped into something:<br></div><div>type of "Just a" is indeed "Maybe a".<br><br></div><div>"Just" is a type constructor:<br><span style="font-family:monospace,monospace">Just :: a -> Maybe a</span><br></div><div>It allows you to build the Maybe.<br><br></div><div>Said that, "Just" is a vocabulary choice. <br>Personally I prefer the name choices of OCaml, Rust, Scala etc.:<code> Option a = None | Some a<br></code></div><div><br></div><div><div><div><br></div></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div>On Thu, Mar 26, 2015 at 4:26 PM, Shishir Srivastava <span dir="ltr"><<a href="mailto:shishir.srivastava@gmail.com" target="_blank">shishir.srivastava@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div><div dir="ltr"><span style="font-size:12.8px">ok..but what's with using the keyword 'Just' ? why cannot 'Maybe' be defined like this </span><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">data Maybe a = a | Nothing<br></div><div style="font-size:12.8px"><br></div><div style="font-size:12.8px">what's the point in having 'Just' keyword ?</div><div class="gmail_extra"><span><font color="#888888"><br clear="all"><div><div><div dir="ltr"><font color="#0b5394"><font style="background-color:rgb(255,255,255)"><font face="georgia, serif">Shishir</font></font><br></font><br></div></div></div></font></span><div><div>
<br><div class="gmail_quote">On Thu, Mar 26, 2015 at 10:26 AM, Michael Alan Dorman <span dir="ltr"><<a href="mailto:mdorman@ironicdesign.com" target="_blank">mdorman@ironicdesign.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span>Shishir Srivastava <<a href="mailto:shishir.srivastava@gmail.com" target="_blank">shishir.srivastava@gmail.com</a>> writes:<br>
> After reading and re-reading the haskell tutorials I don't happen to<br>
> see a very convincing or appealing reason for having these data<br>
> types.<br>
<br>
</span>To be clear: Maybe is the data *type*. Just and Nothing are its data<br>
*constructors*.<br>
<span><br>
> Can anyone please explain where Maybe and Just provide the sort of<br>
> functionality that cannot be achieved in other languages which don't<br>
> have these kind.<br>
<br>
</span>The functionality can be achieved in other languages, certainly. The<br>
question is whether the clarity and safety is also achieved.<br>
<br>
When I see (as a totally contrived example):<br>
<br>
fopen :: Maybe FileHandle<br>
<br>
I know that that function may not be able to return a FileHandle value<br>
all the time. The compiler will, in fact, nag me if I do not write the<br>
code that calls it in such a way that it acknowledges that possibility.<br>
<br>
When I see:<br>
<br>
FILE * fopen ( const char * filename, const char * mode );<br>
<br>
It is not immediately clear whether that can fail. Sure, we can make<br>
that inference, based on what we know about filesystems, etc., but the<br>
compiler is never going to complain if I ignore the possibility.<br>
<br>
In my experience, programmers in many languages end up resorting to<br>
convention to try and work around these sorts of ambiguities. Large<br>
projects have strategies for naming functions that try to pass along<br>
information out of band, or languages have a pervasive culture of "lint"<br>
tools that try to use heuristics to make up for what the type system<br>
doesn't make simple.<br>
<br>
That said, I know that doing Maybe sorts of things in languages that<br>
don't have, say, pattern matching, or the idea of a "failure monad",<br>
gets to be a drag very quickly---manually unwrapping things is at best<br>
awkward, having to re-wrap them just to unwrap them again in a sequence<br>
of computations quickly leads one to believe "it's just not worth<br>
it"---or you resort to exception handling, which has its own challenges<br>
to do well.<br>
<br>
Mike.<br>
</blockquote></div><br></div></div></div></div>
<br></div></div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div></div></div></div>
</blockquote></div><br></div></div></div></div></div></div></div>
</blockquote></div><br></div></div></div>
</blockquote></div><br></div>