Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  Basic "data" question (Ozgur Akgun)
   2. Re:  Basic "data" question (Brent Yorgey)
   3. Re:  Basic "data" question (Brent Yorgey)
   4. Re:  data design for a questionnaire (retitled +  update)
      (Antoine Latter)
   5. Re:  Beginners Digest, Vol 41, Issue 33 (Google Wave)
   6. Re:  data design for a questionnaire (retitled +  update)
      (Peter Hall)


----------------------------------------------------------------------

Message: 1
Date: Thu, 24 Nov 2011 12:26:23 +0000
From: Ozgur Akgun <ozgurak...@gmail.com>
Subject: Re: [Haskell-beginners] Basic "data" question
To: "Costello, Roger L." <coste...@mitre.org>
Cc: "beginners@haskell.org" <beginners@haskell.org>
Message-ID:
        <CALzazPC0ApF7v4wxQSukcbH=7c7qzaf_c5zg4qg3rxkghhm...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi Roger,

On 24 November 2011 10:43, Costello, Roger L. <coste...@mitre.org> wrote:

> data Var = V | W | X | Y | Z
>
> data Lambda = Variable Var | Application Lambda Lambda | Abstraction
> (Variable Var) Lambda
>

Variable is a constructor of the data type Lambda. Abstraction is another.
You use Variable as if it was a data type while defining Abstraction.

data Lambda = Variable Var | Application Lambda Lambda | Abstraction Lambda
Lambda

You can use the above declaration to successfully create your example
lambda terms. However, it doesn't stop you from constructing things like:

Abstraction (Application .. ..) (Variable ..)

If you want to go to that land, you either need smart constructors
(simpler) or GADTs (more hairy, unfortunately).

HTH,
Ozgur
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20111124/8f9357bf/attachment-0001.htm>

------------------------------

Message: 2
Date: Thu, 24 Nov 2011 09:56:48 -0500
From: Brent Yorgey <byor...@seas.upenn.edu>
Subject: Re: [Haskell-beginners] Basic "data" question
To: beginners@haskell.org
Message-ID: <20111124145648.ga22...@seas.upenn.edu>
Content-Type: text/plain; charset=us-ascii

On Thu, Nov 24, 2011 at 10:43:58AM +0000, Costello, Roger L. wrote:
> Hi Folks,
> 
> I am trying to define a data type for this:
> 
>     A Lambda term is one of these:
>         - Variable
>         - Application of a Lambda term to a Lambda term
>         - Abstraction of a Variable in a Lambda term
> 
> Here's an example of a Lambda term:
> 
> term = Abstraction (Variable X) (Application (Variable X) (Variable Y))
> 
> Is there a way to define Lambda term?
> 
> Here's an attempt at defining it:
> 
> data Var = V | W | X | Y | Z
> 
> data Lambda = Variable Var | Application Lambda Lambda | Abstraction
> (Variable Var) Lambda

The right way to do it would be

  data Var = V | W | X | Y | Z

  data Lambda = Variable Var | Application Lambda Lambda | Abstraction Var 
Lambda

However, I also note that it is a bit strange to use an enumeration
type for your variables; what if you need more than five?

-Brent



------------------------------

Message: 3
Date: Thu, 24 Nov 2011 10:24:58 -0500
From: Brent Yorgey <byor...@seas.upenn.edu>
Subject: Re: [Haskell-beginners] Basic "data" question
To: beginners@haskell.org
Message-ID: <20111124152458.ga4...@seas.upenn.edu>
Content-Type: text/plain; charset=us-ascii

On Thu, Nov 24, 2011 at 03:01:49PM +0000, Costello, Roger L. wrote:
> Hi Brent,
> 
> Thanks for the feedback. 
> 
> >  data Var = V | W | X | Y | Z
> >
> >  data Lambda = Variable Var | Application Lambda Lambda | Abstraction Var 
> > Lambda
> 
> Unfortunately, that's not quite correct. The first argument of
> Abstraction must be a Lambda term, specifically Variable Var.

No, that isn't possible (and it also doesn't make sense).  It isn't
possible because there is no way to express "this field may only
contain values built with such-and-such constructor".*  It doesn't
make sense because why must the first argument of Abstraction be a
lambda term?  You yourself said "Abstraction of a Variable in a
Lambda term" -- it is not "abstraction of a lambda term in a lambda
term", but abstraction of a *variable*.  Variables have type Var.
Hence,

  Abstraction Var Lambda

(I can also use an argument from authority: I have coded many variants
of the lambda-calculus in Haskell, and I can tell you that this is the
way it is done. =)

-Brent

* Actually, it is sometimes possible to encode this using GADTs.  But
  that wouldn't help you here.



------------------------------

Message: 4
Date: Thu, 24 Nov 2011 09:34:12 -0600
From: Antoine Latter <aslat...@gmail.com>
Subject: Re: [Haskell-beginners] data design for a questionnaire
        (retitled +     update)
To: Alia <alia_kho...@yahoo.com>
Cc: "beginners@haskell.org" <beginners@haskell.org>
Message-ID:
        <CAKjSnQHWT=UCsA6pEv1hF+=c3-zvcnwvp1tqky_toff+djv...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

On Thu, Nov 24, 2011 at 3:32 AM, Alia <alia_kho...@yahoo.com> wrote:
>
> extract :: Question' -> Question a
> extract q = case q of
> ??? QuestionS x -> extractQString q
> ??? QuestionI x -> extractQInt q
> ??? QuestionD x -> extractQDouble q
>

What is a caller supposed to do with this function? How were you
imagining it would be called?

In Haskell98 (or Haskell2010), the type signature `Question' ->
Question a` would mean that the function returns a `Question` of
whatever type the caller chooses. Since this is not what the function
in fact does, the compiler rejects it.

Antoine



------------------------------

Message: 5
Date: Fri, 25 Nov 2011 01:10:12 +0800
From: Google Wave <cyj1...@gmail.com>
Subject: Re: [Haskell-beginners] Beginners Digest, Vol 41, Issue 33
To: beginners@haskell.org
Message-ID:
        <cadnn8rrqs-y8yfcifo-t5wwjdmrjzwbxqxozobnyt6gtvp5...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

2011/11/24 <beginners-requ...@haskell.org>

> Send Beginners mailing list submissions to
>        beginners@haskell.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
>        http://www.haskell.org/mailman/listinfo/beginners
> or, via email, send a message with subject or body 'help' to
>        beginners-requ...@haskell.org
>
> You can reach the person managing the list at
>        beginners-ow...@haskell.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Beginners digest..."
>
>
> Today's Topics:
>
>   1.  Basic "data" question (Costello, Roger L.)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Thu, 24 Nov 2011 10:43:58 +0000
> From: "Costello, Roger L." <coste...@mitre.org>
> Subject: [Haskell-beginners] Basic "data" question
> To: "beginners@haskell.org" <beginners@haskell.org>
> Message-ID:
>        <b5fee00b53cf054aa8439027e8fe177518191...@imcmbx04.mitre.org>
> Content-Type: text/plain; charset="us-ascii"
>
> Hi Folks,
>
> I am trying to define a data type for this:
>
>    A Lambda term is one of these:
>        - Variable
>        - Application of a Lambda term to a Lambda term
>        - Abstraction of a Variable in a Lambda term
>
> Here's an example of a Lambda term:
>
> term = Abstraction (Variable X) (Application (Variable X) (Variable Y))
>
> Is there a way to define Lambda term?
>
> Here's an attempt at defining it:
>
> data Var = V | W | X | Y | Z
>
> data Lambda = Variable Var | Application Lambda Lambda | Abstraction
> (Variable Var) Lambda
>
> But that yields the following error:
>
> test.hs:5:71: Not in scope: type constructor or class 'Variable'
>
> Any suggestions would be much appreciated.
>
> /Roger
>
>
>
> ------------------------------
>
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
> End of Beginners Digest, Vol 41, Issue 33
> *****************************************
>
value constructor  followed by types.  And (Variable Var)  belong to Lambda
type.

Could define like this:
data Lambda var= Variable var | Application (Lambda var) (Lambda var)

var be any types
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20111125/2611b2fd/attachment-0001.htm>

------------------------------

Message: 6
Date: Thu, 24 Nov 2011 19:24:06 +0000
From: Peter Hall <peterj...@gmail.com>
Subject: Re: [Haskell-beginners] data design for a questionnaire
        (retitled +     update)
To: Alia <alia_kho...@yahoo.com>
Cc: "beginners@haskell.org" <beginners@haskell.org>
Message-ID: <da676cd4-687c-4b1d-82a1-c677d144f...@gmail.com>
Content-Type: text/plain; charset="us-ascii"

> For example, I can ask a question which requires a Double as an 
answer and another which requires an Int as an answer:

Might this be easier if you made all the answers strings? If you need to permit 
things like 0.5 vs .5 you could instead use a String->String normalisation 
function which could vary with the "type" of the answer but not affect the data 
type of the question.

It would also be easier to use strings if you wanted to store the questions in 
a database or load from a file.  

Peter

On 24 Nov 2011, at 09:32, Alia <alia_kho...@yahoo.com> wrote:

> Hi folks,
> 
> As the code (from my prior post) is not so relevant to the issue
> at hand, it was quite reasonably suggested that I should simplify my
> question somewhat. So here goes:
> 
> In my model, I have a Question record type, which has a type parameter
> to take into account that answers can be evaluated to different types.
> For example, I can ask a question which requires a Double as an 
> answer and another which requires an Int as an answer:
> 
> data Question a = Question {text:: String, answer:: Maybe a}
> 
> This question type is wrapped/boxed in another type to allow the 
> different questions to referenced in a list. Hence,
> 
> data Question' = QuestionS (Question String)
>                | QuestionI (Question Int)
>                | QuestionD (Question Double)
> 
> and I can now store the questions as follows:
> 
> questions = [ QuestionI {text="What's 1+1?", answer=Maybe 2}
>             , QuestionD {text="What's 1.0+1.5?", answer=Maybe 2.5}
>             ]
> 
> Now to extract Question a from Question' I would have liked to write 
> the following:
> 
> extract :: Question' -> Question a
> extract q = case q of
>     QuestionS x -> extractQString q
>     QuestionI x -> extractQInt q
>     QuestionD x -> extractQDouble q
> 
> but instead, I had to produce the following instead:
> 
> extractQString :: Question' -> Question String
> extractQString (QuestionS q) = q
> 
> extractQInt :: Question' -> Question Int
> extractQInt (QuestionI q) = q
> 
> extractQDouble :: Question' -> Question Double
> extractQDouble (QuestionD q) = q
> 
> From what I understand so far, that specialized extraction or unboxing
> functions are the way to go unless one uses language extensions like
> GADTs which can be conceptually more complicated. Is this indeed the case,
> or are there other ways to achieve generalized type unboxing while
> remaining within the Haskell98 specification?
> 
> Best,
> 
> Alia 
>  
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20111124/d6c346c9/attachment.htm>

------------------------------

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 41, Issue 34
*****************************************

Reply via email to