Re: [Haskell-cafe] Re: distinguish functions from non-functions in a class/instances

2007-12-07 Thread Luke Palmer
On Dec 7, 2007 6:27 AM, Victor Nazarov [EMAIL PROTECTED] wrote:
 Cool solution and not so complicated and ad-hoc. But I'd like to ask
 isn't the following definition is more natural and simple?

 nary 0 x [] = x
 nary n f (x:xs) | n  0 = nary (n-1) (f $ read x) xs

Sometimes it helps to write type signatures for functions.  As in this
case, where you'll find you won't be able to...  :-)

Luke
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] group-by (Was: Nested guards?)

2007-12-07 Thread Henning Thielemann

On Fri, 7 Dec 2007, Simon Peyton-Jones wrote:

 | And I think that the solution is not to make the language larger and larger
 | everytime someone wants a feature but to give people the tools to provide
 | features without language changes.

 Of course that would be even better!  (Provided of course the resulting
 programs were comprehensible.)  Haskell is already pretty good in this
 respect, thanks to type classes, higher order functions, and laziness;
 that's why it's so good at embedded domain-specific languages.

When I learned about GROUP BY and HAVING in SQL with its rules about what
is allowed in GROUP BY and SELECT I considered GROUP BY a terrible hack,
that was just introduced because the SQL people didn't want to allow types
different from TABLE, namely lists of tables. I try to convince my data
base colleagues that GROUP BY can nicely be handled in relational algebra
by allowing sets of sets and that this is a fine combinatorial approach. I
think we simply need a function like
  buckets :: (a - key) - [a] - [(key, [a])]
  (where (a - key) is a simple selector)
 or
  buckets :: (a - (key, rest)) - [a] - [(key, [rest])]
  (where  (a - (key, rest)) is a bijection)
 or
  buckets :: Ord key = ... ah no :-)
  buckets :: Indexable key = (a - (key, rest)) - [a] - Map key [rest]
  buckets f = Map.fromListWith (++) . map (\ a - let (k,r) = f a in (k, [r]))



Btw. since I often need fromListWith with Maps of list types, I wonder
whether there should be variants of fromListWith and insertWith, which can
use (:) instead of (++):

fromListCons :: Indexable k =
 (b - a - a)-- ^ add a new sub-element to the dictionary element, for 
example (:)
  - a-- ^ empty dictionary element, for example []
  - [(k, b)]
  - Data.Map.Map k a


insertCons :: Indexable k =
  (b - a - a) - a - k - b - Data.Map.Map k a - Data.Map.Map k a
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Re: type class question

2007-12-07 Thread Jules Bean

Peter Padawitz wrote:
Functional dependencies don't work in my case. Actually, I don't see why 
they should.


Ah well, it's cruel to say that without explaining to us why!

I'm not sure why a complete cyclic dep a - b - c - d - a isn't what 
you want.


What seems to be needed here is a type class construct with a kind of 
record parameter so that instance conflicts cannot occur.


I'm not entirely sure what you intend to mean by this, but if you mean 
what I guess you mean:


class Java (a,b,c,d) where 

then I think that would appear to be the same thing as a complete cyclic 
fundep to me...


Jules
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Re: type class question

2007-12-07 Thread Peter Padawitz
Functional dependencies don't work in my case. Actually, I don't see why 
they should.


What seems to be needed here is a type class construct with a kind of 
record parameter so that instance conflicts cannot occur.


Jules Bean wrote:


Ben Franksen wrote:


Ryan Ingram wrote:


On 12/5/07, Ben Franksen [EMAIL PROTECTED] wrote:
You would have to use functional dependencies or associated types to
eliminate this error.  Alternatively, you can add a dummy argument 
of type
block and pass undefined :: BlockType in to help choose the 
instance

declaration.



Sounds reasonable, and in fact that was what I tried first. However

data Command = Skip

class Java block command | command - block where
  block_ :: [command] - block

  compBlock :: [Command] - block
  compBlock = block_ . map compCommand

  compCommand :: Command - command

still gives

Could not deduce (Java block command1)
  from the context (Java block command)
  arising from use of `block_' at Bla.hs:7:14-19
Possible fix:
  add (Java block command1)
  to the class or instance method `compBlock'
In the first argument of `(.)', namely `block_'
In the expression: block_ . (map compCommand)
In the definition of `compBlock':
compBlock = block_ . (map compCommand)

which is /exactly/ the same error as I get w/o the fundep.



Yes, because command determines block but block doesn't determine 
command.


So in a usage of 'compBlock' it has no way of deciding which 'command' 
to use, although it can choose the block from the return type.


You could have command - block, block - command, if that is indeed 
true.


Jule
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe




--
Prof. Dr. Peter Padawitz
Informatik 1
University of Dortmund  
D-44221 Dortmund
Germany 
phone   +49-231-755-5108

fax +49-231-755-6555
secretary   +49-231-755-6223
email   [EMAIL PROTECTED]
internethttp://funlog.padawitz.de

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] RE: [Haskell] Nested guards?

2007-12-07 Thread Simon Peyton-Jones
| And I think that the solution is not to make the language larger and larger
| everytime someone wants a feature but to give people the tools to provide
| features without language changes.

Of course that would be even better!  (Provided of course the resulting 
programs were comprehensible.)  Haskell is already pretty good in this respect, 
thanks to type classes, higher order functions, and laziness; that's why it's 
so good at embedded domain-specific languages.  Template Haskell is another 
attempt to go further. Geoff Mainland's quasi-quoting idea is another.

If you have other ideas for such general tools, then it'd be great to hear 
about them.  They are much easier to wish for than to design.

But where such general tools are inadequate, well-designed syntactic sugar can 
have a powerfully beneficial effect, I think.  But it's a topic on which 
reasonable people can differ, and your judgement may well differ from mine.

Simon
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] fundeps and overlapping/undecidable instances

2007-12-07 Thread Jim Burton

I have some type-level sets using fundeps working whereby equality and
membership etc are predicate functions. This seems to leads to an explosion
of ugly code, with `If' class constraints etc getting out of hand -- I want
to treat these as relations instead so providing the definition describes
everything that is 'in' and nothing that is 'out'. I've been using Oleg's
paper on lightweight static resources [1] as a template for this. I want to
do something like this (supposing I have an EQ relation, (:::) for consing):

class Member x y 
instance EQ x y  = Member x (y:::ys) 
instance Member x ys = Member x (y:::ys)

But I can certainly see why this isn't possible (It's the equivalent of
pattern-matching on the constraints I suppose). Do type families provide a
way to do this kind of thing or do I need a different strategy altogether,
involving GADTs or whatever?

Thanks,

[1] http://okmij.org/ftp/Computation/resource-aware-prog/tfp.pdf
-- 
View this message in context: 
http://www.nabble.com/fundeps-and-overlapping-undecidable-instances-tf4962996.html#a14215583
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Re: type class question

2007-12-07 Thread Jules Bean

Peter Padawitz wrote:

Jules Bean wrote:


Peter Padawitz wrote:

Functional dependencies don't work in my case. Actually, I don't see 
why they should.



Ah well, it's cruel to say that without explaining to us why!


Cause I don't see why the instantiation conflicts pointed out by others 
would vanish then.


They would.

If it's really true that there is only one possible choice of b,c,d for 
any particular a, then there are no conflicts, so you'd get no errors.


So the fundep would solve the problem.


class Java (a,b,c,d) where 


Yeah... but ghc accepts only type variables here, not arbitrary 
polymorphic types.


Indeed, but there is a workaround:

class Java all a b c d |
   all - a, all - b, all - c, all - d, a,b,c,d - all


instance Java (a,b,c,d) a b c d where...

but I'm not sure you need this.

Jules
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: distinguish functions from non-functions in a class/instances

2007-12-07 Thread oleg

Philipp N. wrote:
 i'm trying to wrap functions (a - b - ... - z) of any arity to functions
 of type ([String] - y), where list of strings replaces the typed arguments.
 the problem is, that you cannot distinguish type (x-y) from z, so these
 instances are overlapping.

to which apfelmus replied
 Exactly. What you want to do is most likely impossible,

In fact, that distinction is possible. The following article

How to write an instance for not-a-function
http://okmij.org/ftp/Haskell/typecast.html#is-function-type

specifically describes a method of writing an instance which is
selected only when the type in question is NOT a function. The method
is quite general and has been extensively used (for example, to
implement deep monadic join).

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: distinguish functions from non-functions in a class/instances

2007-12-07 Thread Victor Nazarov
On Dec 7, 2007 4:46 PM, Luke Palmer [EMAIL PROTECTED] wrote:
 On Dec 7, 2007 6:27 AM, Victor Nazarov [EMAIL PROTECTED] wrote:
 
  nary 0 x [] = x
  nary n f (x:xs) | n  0 = nary (n-1) (f $ read x) xs

 Sometimes it helps to write type signatures for functions.  As in this
 case, where you'll find you won't be able to...  :-)

 Luke


Ok :)

 {-# OPTIONS -fglasgow-exts #-}
 {-# OPTIONS -fallow-undecidable-instances #-}

data Zero
data Succ a

class Nary n x y | n x - y where
  nary :: n - x - [String] - y

instance Nary Zero x x where
  nary _ x [] = x

instance (Nary n y z, Read x) = Nary (Succ n) (x-y) z where
  nary _ f (x:xs) = nary (undefined::n) (f $ read x) xs

-- 
vir
http://vir.comtv.ru/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: distinguish functions from non-functions in a class/instances

2007-12-07 Thread Victor Nazarov
On Dec 7, 2007 2:52 PM,  [EMAIL PROTECTED] wrote:

 In fact, that distinction is possible. The following article

 How to write an instance for not-a-function
 http://okmij.org/ftp/Haskell/typecast.html#is-function-type

 specifically describes a method of writing an instance which is
 selected only when the type in question is NOT a function. The method
 is quite general and has been extensively used (for example, to
 implement deep monadic join).


Cool solution and not so complicated and ad-hoc. But I'd like to ask
isn't the following definition is more natural and simple?

nary 0 x [] = x
nary n f (x:xs) | n  0 = nary (n-1) (f $ read x) xs

--
vir
http://vir.comtv.ru/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] [OT] A nice organized collection of threads in Haskell-Cafe

2007-12-07 Thread Vimal
Hi,
I am working on a product to analyze posts made in Forums, Usenet and
discussion mailing lists like Haskell-Cafe. For this, I require the
messages to be accessible in this format:

forum (* example: Haskell-cafe *)
 [  list of -
 thread
   [ list of -
 post
 /post
   ]
 /thread
 ]
/forum

as XML.

However, I find that that the messages (in haskell-cafe/usenet)
themselves aren't organized in this fashion.

I would like to know if there is any way in which I can get the
archives in this fashion.

Thanks,
-- 
~Vimal
RLE :)
encode = map (length  head) . group
decode = concatMap (uncurry replicate)
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Re: type class question

2007-12-07 Thread Benja Fallenstein
On Dec 7, 2007 6:57 PM, Peter Padawitz [EMAIL PROTECTED] wrote:
 Jules Bean wrote:
  Peter Padawitz wrote:
  Cause I don't see why the instantiation conflicts pointed out by
  others would vanish then.
 
  They would.
 
  If it's really true that there is only one possible choice of b,c,d
  for any particular a, then there are no conflicts, so you'd get no
  errors.

 How can ghci know this even if no instance has been defined?

Because there is only one possible choice of b,c,d for any particular
a is what the fundep means :-)

 If I omit the comp functions (see below), everything works. If I add
 them, all proposed solutions fail with error messages of the form

 Could not deduce (Java block1 ) from the context (Java block )
 arising from use of `prod' at ...

 (see also Ben Franksen's comment from yesterday).

If you add the cyclic functional dependencies to your code, it
compiles just fine:

type Block   = [Command]
data Command = Skip | Assign String IntE | Cond BoolE Block Block |
Loop BoolE Block
data IntE= IntE Int | Var String | Sub IntE IntE | Sum [IntE] | Prod [IntE]
data BoolE   = BoolE Bool | Greater IntE IntE | Not BoolE

class Java block command intE boolE | block - command, command -
intE, intE - boolE, boolE - block
  where block_ :: [command] - block
skip :: command
assign :: String - intE - command
cond :: boolE - block - block - command
loop :: boolE - block - command
intE_ :: Int - intE
var :: String - intE
sub :: intE - intE - intE
sum_ :: [intE] - intE
prod :: [intE] - intE
boolE_ :: Bool - boolE
greater :: intE - intE - boolE
not_ :: boolE - boolE

compBlock :: Block - block
compBlock = block_ . map compCommand

compCommand :: Command - command
compCommand Skip   = skip
compCommand (Assign x e)   = assign x (compIntE e)
compCommand (Cond be cs cs') = cond (compBoolE be) (compBlock
cs) (compBlock cs')
compCommand (Loop be cs)= loop (compBoolE be) (compBlock cs)

compIntE :: IntE - intE
compIntE (IntE i)   = intE_ i
compIntE (Var x)= var x
compIntE (Sub e e') = sub (compIntE e) (compIntE e')
compIntE (Sum es)   = sum_ (map compIntE es)
compIntE (Prod es)  = prod (map compIntE es)

compBoolE :: BoolE - boolE
compBoolE (BoolE b)  = boolE_ b
compBoolE (Greater e e') = greater (compIntE e) (compIntE e')
compBoolE (Not be)   = not_ (compBoolE be)

Best,
- Benja
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] ANN: Shu-thing 1.0 and Monadius 0.9

2007-12-07 Thread Andrew Coppin

Bit Connor wrote:

Hi,

Monadius is awesome! I've made a video of me playing it and kicking
some serious ass:

http://www.youtube.com/watch?v=zqFgQiPKtOI
  


No way! Hax!!

You edited the source code, didn't you? ;-)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: distinguish functions from non-functions in a class/instances

2007-12-07 Thread Luke Palmer
On Dec 7, 2007 6:21 PM, Dan Weston [EMAIL PROTECTED] wrote:
 This is great! Two questions:

 1) I want to make sure the function arity matches the list length (as a
 runtime check). I think I can do this with an arity function using
 Data.Typeable. I came up with:

 arity f = a (typeOf f) where
a tr | typeRepTyCon tr /= mkTyCon - = 0
 | otherwise = 1 + (a . fromJust . funResultTy tr . head
  . typeRepArgs $ tr)

 This looks awful. Is there a better way to get the function arity?

 2) Once I have say arity (+) == 2 at runtime, how can I get it reified
 into Succ (Succ Zero)) at compile time to be able to use it as the first
 argument in your nary function? Can/should I use Template Haskell for this?

You can project the compile time numbers into runtime ones:

 class ProjectN n where
 projectN :: n - Int

 instance ProjectN Zero where
 projectN _ = 0

 instance (ProjectN n) = ProjectN (Succ n) where
 projectN _ = 1 + projectN (undefined :: n)

And then make sure the length matches the projected number of
arguments.  Other disagreements will be resolved at compile time.

Luke

 Dan

 Victor Nazarov wrote:
 
  {-# OPTIONS -fglasgow-exts #-}
  {-# OPTIONS -fallow-undecidable-instances #-}
 
  data Zero
  data Succ a
 
  class Nary n x y | n x - y where
nary :: n - x - [String] - y
 
  instance Nary Zero x x where
nary _ x [] = x
 
  instance (Nary n y z, Read x) = Nary (Succ n) (x-y) z where
nary _ f (x:xs) = nary (undefined::n) (f $ read x) xs
 



 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Re: type class question

2007-12-07 Thread Jules Bean

Peter Padawitz wrote:

So the fundep would solve the problem.


But, actually, it doesn't :-(


But actually, it does!

Ben Franksen's answer from yesterday compiles fine for me if I add the 
missing fundep, block - command.


Your original code compiles without error, given the fundep. Exact code 
I compiled attached at the bottom of this document. You may have to 
repair long lines!


Incidentally, I question why the compFoo are methods. Why not just 
make them polymorphic functions? They don't look like you expect 
instances to change them. The code continues to compile if I make them 
functions and amend their signatures as required.


Jules

{-# LANGUAGE MultiParamTypeClasses, FunctionalDependencies #-}

type Block   = [Command]
data Command = Skip | Assign String IntE | Cond BoolE Block Block | Loop 
BoolE Block
data IntE= IntE Int | Var String | Sub IntE IntE | Sum [IntE] | Prod 
[IntE]

data BoolE   = BoolE Bool | Greater IntE IntE | Not BoolE

class Java block command intE boolE | block - command, command - intE, 
intE - boolE, boolE - block

  where block_ :: [command] - block
skip :: command
assign :: String - intE - command
cond :: boolE - block - block - command
loop :: boolE - block - command
intE_ :: Int - intE
var :: String - intE
sub :: intE - intE - intE
sum_ :: [intE] - intE
prod :: [intE] - intE
boolE_ :: Bool - boolE
greater :: intE - intE - boolE
not_ :: boolE - boolE
compBlock :: Block - block
compBlock = block_ . map compCommand
compCommand :: Command - command
compCommand Skip   = skip
compCommand (Assign x e)   = assign x (compIntE e)
compCommand (Cond be cs cs') = cond (compBoolE be) (compBlock 
cs) (compBlock cs')

compCommand (Loop be cs)= loop (compBoolE be) (compBlock cs)
compIntE :: IntE - intE
compIntE (IntE i)   = intE_ i
compIntE (Var x)= var x
compIntE (Sub e e') = sub (compIntE e) (compIntE e')
compIntE (Sum es)   = sum_ (map compIntE es)
compIntE (Prod es)  = prod (map compIntE es)
compBoolE :: BoolE - boolE
compBoolE (BoolE b)  = boolE_ b
compBoolE (Greater e e') = greater (compIntE e) (compIntE e')
compBoolE (Not be)   = not_ (compBoolE be)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Re: type class question

2007-12-07 Thread Luke Palmer
On Dec 7, 2007 5:57 PM, Peter Padawitz [EMAIL PROTECTED] wrote:
 type Block   = [Command]
 data Command = Skip | Assign String IntE | Cond BoolE Block Block | Loop
 BoolE Block
 data IntE= IntE Int | Var String | Sub IntE IntE | Sum [IntE] | Prod
 [IntE]
 data BoolE   = BoolE Bool | Greater IntE IntE | Not BoolE

 class Java block command intE boolE
where block_ :: [command] - block
  skip :: command
  assign :: String - intE - command
  cond :: boolE - block - block - command
  loop :: boolE - block - command
  intE_ :: Int - intE
  var :: String - intE
  sub :: intE - intE - intE
  sum_ :: [intE] - intE
  prod :: [intE] - intE
  boolE_ :: Bool - boolE
  greater :: intE - intE - boolE
  not_ :: boolE - boolE

  compBlock :: Block - block
  compBlock = block_ . map compCommand

  compCommand :: Command - command
  compCommand Skip   = skip
  compCommand (Assign x e)   = assign x (compIntE e)
  compCommand (Cond be cs cs') = cond (compBoolE be) (compBlock
 cs) (compBlock cs')
  compCommand (Loop be cs)= loop (compBoolE be) (compBlock cs)

  compIntE :: IntE - intE
  compIntE (IntE i)   = intE_ i
  compIntE (Var x)= var x
  compIntE (Sub e e') = sub (compIntE e) (compIntE e')
  compIntE (Sum es)   = sum_ (map compIntE es)
  compIntE (Prod es)  = prod (map kcompIntE es)

  compBoolE :: BoolE - boolE
  compBoolE (BoolE b)  = boolE_ b
  compBoolE (Greater e e') = greater (compIntE e) (compIntE e')
  compBoolE (Not be)   = not_ (compBoolE be)

I'm not sure what this is worth, since you seem to have your mind set
on using this strange four-parameter type class.

You can keep most of the design advantages of using this type at the
cost of being more explicit if you factor it into a data type
yourself:

 data Java block command intE boolE
   = Java { block_ :: [command] - block
  , skip   :: command
  , assign :: String - intE - command
  , ...
  , compBlock :: Block - block
  , ...
  }

For your default implementations:

 defCompBlock :: Java block command intE boolE - Block - block
 defCompBlock self = block_ self . map (compCommand self)

 .. etc

Then to define an example instance:

 javaAST :: Java Block Command IntE BoolE
 javaAST
   = Java { block_ = Block
  , ...
  , compBlock = defCompBlock javaAST
  , ...
  }

Your type errors will be resolved because you are saying explicitly
which instance to use by passing the instance data structure you want
explicitly.

Luke
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Re: type class question

2007-12-07 Thread Peter Padawitz

Jules Bean wrote:


Peter Padawitz wrote:


Jules Bean wrote:


Peter Padawitz wrote:

Functional dependencies don't work in my case. Actually, I don't 
see why they should.




Ah well, it's cruel to say that without explaining to us why!



Cause I don't see why the instantiation conflicts pointed out by 
others would vanish then.



They would.

If it's really true that there is only one possible choice of b,c,d 
for any particular a, then there are no conflicts, so you'd get no 
errors.


How can ghci know this even if no instance has been defined?


So the fundep would solve the problem.


But, actually, it doesn't :-(


class Java (a,b,c,d) where 



Yeah... but ghc accepts only type variables here, not arbitrary 
polymorphic types.



Indeed, but there is a workaround:

class Java all a b c d |
   all - a, all - b, all - c, all - d, a,b,c,d - all


Same problem.

If I omit the comp functions (see below), everything works. If I add 
them, all proposed solutions fail with error messages of the form


Could not deduce (Java block1 ) from the context (Java block ) 
arising from use of `prod' at ...


(see also Ben Franksen's comment from yesterday).

***

type Block   = [Command]
data Command = Skip | Assign String IntE | Cond BoolE Block Block | Loop 
BoolE Block
data IntE= IntE Int | Var String | Sub IntE IntE | Sum [IntE] | Prod 
[IntE]

data BoolE   = BoolE Bool | Greater IntE IntE | Not BoolE

class Java block command intE boolE
  where block_ :: [command] - block
skip :: command
assign :: String - intE - command
cond :: boolE - block - block - command
loop :: boolE - block - command
intE_ :: Int - intE
var :: String - intE
sub :: intE - intE - intE
sum_ :: [intE] - intE
prod :: [intE] - intE
boolE_ :: Bool - boolE
greater :: intE - intE - boolE
not_ :: boolE - boolE

compBlock :: Block - block

compBlock = block_ . map compCommand

compCommand :: Command - command

compCommand Skip   = skip
compCommand (Assign x e)   = assign x (compIntE e)
compCommand (Cond be cs cs') = cond (compBoolE be) (compBlock 
cs) (compBlock cs')

compCommand (Loop be cs)= loop (compBoolE be) (compBlock cs)

compIntE :: IntE - intE

compIntE (IntE i)   = intE_ i
compIntE (Var x)= var x
compIntE (Sub e e') = sub (compIntE e) (compIntE e')
compIntE (Sum es)   = sum_ (map compIntE es)
compIntE (Prod es)  = prod (map compIntE es)

compBoolE :: BoolE - boolE

compBoolE (BoolE b)  = boolE_ b
compBoolE (Greater e e') = greater (compIntE e) (compIntE e')
compBoolE (Not be)   = not_ (compBoolE be)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] fundeps and overlapping/undecidable instances

2007-12-07 Thread Jeff Polakow
Hello,

  You should be able to use fundeps to do exactly what you describe below. 


  Can you make a relatively small self-contained example which exemplifies 
the ugliness you see? 

-Jeff


[EMAIL PROTECTED] wrote on 12/07/2007 11:24:35 AM:

 
 I have some type-level sets using fundeps working whereby equality and
 membership etc are predicate functions. This seems to leads to an 
explosion
 of ugly code, with `If' class constraints etc getting out of hand -- I 
want
 to treat these as relations instead so providing the definition 
describes
 everything that is 'in' and nothing that is 'out'. I've been using 
Oleg's
 paper on lightweight static resources [1] as a template for this. I want 
to
 do something like this (supposing I have an EQ relation, (:::) for 
consing):
 
 class Member x y 
 instance EQ x y  = Member x (y:::ys) 
 instance Member x ys = Member x (y:::ys)
 
 But I can certainly see why this isn't possible (It's the equivalent of
 pattern-matching on the constraints I suppose). Do type families provide 
a
 way to do this kind of thing or do I need a different strategy 
altogether,
 involving GADTs or whatever?
 
 Thanks,
 
 [1] http://okmij.org/ftp/Computation/resource-aware-prog/tfp.pdf
 -- 
 View this message in context: http://www.nabble.com/fundeps-and-
 overlapping-undecidable-instances-tf4962996.html#a14215583
 Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
 
 ___
 Haskell-Cafe mailing list
 Haskell-Cafe@haskell.org
 http://www.haskell.org/mailman/listinfo/haskell-cafe


---

This e-mail may contain confidential and/or privileged information. If you 
are not the intended recipient (or have received this e-mail in error) 
please notify the sender immediately and destroy this e-mail. Any 
unauthorized copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: distinguish functions from non-functions in a class/instances

2007-12-07 Thread Dan Weston

This is great! Two questions:

1) I want to make sure the function arity matches the list length (as a 
runtime check). I think I can do this with an arity function using 
Data.Typeable. I came up with:


arity f = a (typeOf f) where
  a tr | typeRepTyCon tr /= mkTyCon - = 0
   | otherwise = 1 + (a . fromJust . funResultTy tr . head
. typeRepArgs $ tr)

This looks awful. Is there a better way to get the function arity?

2) Once I have say arity (+) == 2 at runtime, how can I get it reified 
into Succ (Succ Zero)) at compile time to be able to use it as the first 
argument in your nary function? Can/should I use Template Haskell for this?


Dan

Victor Nazarov wrote:



{-# OPTIONS -fglasgow-exts #-}
{-# OPTIONS -fallow-undecidable-instances #-}


data Zero
data Succ a

class Nary n x y | n x - y where
  nary :: n - x - [String] - y

instance Nary Zero x x where
  nary _ x [] = x

instance (Nary n y z, Read x) = Nary (Succ n) (x-y) z where
  nary _ f (x:xs) = nary (undefined::n) (f $ read x) xs




___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Literate HTML

2007-12-07 Thread Neil Mitchell
Hi

I want literate Haskell, but where the literate bit forming a document
is actually HTML, not latex. Does anyone have any idea how to go about
this?

For a start, how do I persuade GHC to run the file:

C:\Documents\Uni\tagsouprunhaskell index.html
Warning: ignoring unrecognised input `index.html'
interactive:1:108: attempting to use module `Main' (Main.hs) which
is not loaded

And leading on to that, how Cabal might be used to compile the file.

Secondly, what tricks can people think of for placing the code inside
- I have come up with:

  code
 foo = 1
  /code

And:

code
!--
\begin{code}
--
foo = 1
\end{code}
/code

I can then hide the trailing \end{code} with a margin-bottm: -2em.

Does anyone have any other ideas?

Thanks

Neil
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Point and link

2007-12-07 Thread Andrew Coppin

Hi guys.

Here's a fairly basic question. I have several ideas for programs that 
I'd like to write. They all involve placing units of some kind, and 
then drawing connections between those units. How feasible is it to 
program such a thing in Haskell? Where would you start? (Gtk2hs is the 
only graphics API I'm familiar with using at present.)


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] parser

2007-12-07 Thread Brent Yorgey
On Dec 7, 2007 6:04 PM, Chris Eidhof [EMAIL PROTECTED] wrote:

 On 7 dec 2007, at 23:51, Ryan Bloor wrote:
  i am using hugs and the isDigit and anything 'is' doesn't work...
  they must have forgot to add them in! Does GHC work with them.


Perhaps you need to import Data.Char?

-Brent
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: Point and link

2007-12-07 Thread Tom Davies
Andrew Coppin andrewcoppin at btinternet.com writes:

[snip]

You might like to look at OpenQuark: http://labs.businessobjects.com/cal/
 -- its 'GemCutter' provides a visual environment for linking together functions
written in a Haskell-like language.

I'm not sure if it would be flexible enough for you out of the box, but it's 
open
source so you might be able to adapt it.

Tom





___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] parser

2007-12-07 Thread Chris Eidhof


On 7 dec 2007, at 22:55, Ryan Bloor wrote:


hi

The thing is... it will be a simple parser really. The expressions  
are already defined and we can't use parsec imports. Below is the  
types I have.
I have a function that removes initial spaces from the start of a  
string. A function that checks if a substring is part of another  
string, which use the remove space function also.

The next function is: which uses the type:

type Parser = String - [(Expr,String)]
So what does this type really mean? You give it a string, and it will  
return a list of (Expr, String). I would guess that the list is all  
possible outcomes, and the String in (Expr, String) is probably the  
rest of the parsed data. If you want to find a correct parse, you  
probably want to select the first element from that list that has an  
empty rest.


You could write a parser that parses single digits:

parseDigit :: String - [(Int, String)]
parseDigit (x:xs) | isDigit x = [read x]

Of course you have to define the other cases for parseDigit. If you  
had a parseMany function that parses many things of the same type, you  
could combine that with parseDigit to parse natural numbers. The other  
thing you are really going to need is a choice-operator. In your  
example, you want to parse terms that are either numbers or term +  
term:


parseTerm = parseNaturalNumber `parseOr` parseAddition

It's probably best to read a good book or tutorial on parsers. There  
is an excellent textbook on grammars and parsing in Haskell [1], it  
probably explains exactly what you want.


-chris

[1] Johan Jeuring, Doaitse Swierstra: Grammars and Parsing: 
http://www.cs.uu.nl/docs/vakken/gont/diktaat.pdf




readExpression :: String - Expr

e.g. readExpression True = EBool True
e.g. readExpression (23 + 67) = EAdd (EInt 23) (EInt 67)


 
Types--


data Type = TNone -- badly typed values
| TInt -- integer values
| TBool -- boolean values
deriving Show
data Expr = EInt {vInt :: Int} -- integer values
| EBool {vBool :: Bool} -- boolean values
| EAdd Expr Expr -- (e1 + e2)
| EMin Expr Expr -- (e1 - e2)
| EMul Expr Expr -- (e1 * e2)
| EAnd Expr Expr -- (e1  e2)
| EOr Expr Expr -- (e1 || e2)
| ENot Expr -- not e1
| EComp Expr Expr -- (e1 == e2)
| ETest Expr Expr Expr -- if e1 then e2 else e3
| ENone -- badly formed expressions






 CC: haskell-cafe@haskell.org
 From: [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 Subject: Re: [Haskell-cafe] parser
 Date: Fri, 7 Dec 2007 22:17:54 +0100


 On 6 dec 2007, at 18:06, Ryan Bloor wrote:
  Can anyone advise me on how to check whether a string contains  
ints,

  chars, bools, etc
 
  2345 + 6767 shoudl give IntAdd (2345) (6767)
  2345 should give IntT 2345
 You need to write a parser. There are a lot of libraries that will
 help you write a parser. One library that is often used for writing
 parsers in Haskell is called Parsec [1]. There's good  
documentation on

 that site on how to use it. Parsec is already included in you
 distribution. Good luck!

 -chris

 [1]: http://legacy.cs.uu.nl/daan/parsec.html


Get closer to the jungle… I'm a Celebrity Get Me Out Of Here!


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] distinguish functions from non-functions in a class/instances

2007-12-07 Thread Philipp N.


oleg-7 wrote:
 
 
 In fact, that distinction is possible. The following article
 
   How to write an instance for not-a-function
   http://okmij.org/ftp/Haskell/typecast.html#is-function-type
 
 specifically describes a method of writing an instance which is
 selected only when the type in question is NOT a function. The method
 is quite general and has been extensively used (for example, to
 implement deep monadic join).
 
 

That's really incredible, and yet I don't quite understand how IsFunction
works.
Here is my very short but powerful solution (nary is renamed to wrap).
http://www.nabble.com/file/p14220591/wrap.hs wrap.hs 

-- 
View this message in context: 
http://www.nabble.com/distinguish-functions-from-non-functions-in-a-class-instances-tf4952209.html#a14220591
Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] fundeps and overlapping/undecidable instances

2007-12-07 Thread jim burton
On Fri, 2007-12-07 at 12:49 -0500, Jeff Polakow wrote:
 
 Hello, 

   You should be able to use fundeps to do exactly what you describe
 below. 
 
   Can you make a relatively small self-contained example which
 exemplifies the ugliness you see? 
 

Hi Jeff, as well as a minor code explosion if Member returns a (phantom
type representing a) boolean then you have to decide what to do if the
element is already in -- return the set unchanged I should think, but in
other cases it might not be obvious so I want this and other things to
be relations. So I had

class If p x y c | p x y - c
where if :: p - x - y - c
instance If True x y x
instance If False x y y

class Member el set b | el set - b
where member :: el - set - b
instance x Nil False
instance (Eq x y c
, Member x ys b 
, Or c d b) =
Member x (y ::: ys) b

(Eq and Or being more constraints that return True or False) I want a
version that is only defined where the element is in the set -- not a
test, an assertion. 

Jim

 -Jeff 
 
 
 [EMAIL PROTECTED] wrote on 12/07/2007 11:24:35 AM:
 
  
  I have some type-level sets using fundeps working whereby equality
 and
  membership etc are predicate functions. This seems to leads to an
 explosion
  of ugly code, with `If' class constraints etc getting out of hand --
 I want
  to treat these as relations instead so providing the definition
 describes
  everything that is 'in' and nothing that is 'out'. I've been using
 Oleg's
  paper on lightweight static resources [1] as a template for this. I
 want to
  do something like this (supposing I have an EQ relation, (:::) for
 consing):
  
  class Member x y 
  instance EQ x y  = Member x (y:::ys) 
  instance Member x ys = Member x (y:::ys)
  
  But I can certainly see why this isn't possible (It's the equivalent
 of
  pattern-matching on the constraints I suppose). Do type families
 provide a
  way to do this kind of thing or do I need a different strategy
 altogether,
  involving GADTs or whatever?
  
  Thanks,
  
  [1] http://okmij.org/ftp/Computation/resource-aware-prog/tfp.pdf
  -- 
  View this message in context: http://www.nabble.com/fundeps-and-
  overlapping-undecidable-instances-tf4962996.html#a14215583
  Sent from the Haskell - Haskell-Cafe mailing list archive at
 Nabble.com.
  
  ___
  Haskell-Cafe mailing list
  Haskell-Cafe@haskell.org
  http://www.haskell.org/mailman/listinfo/haskell-cafe
 
 ---
 
 This e-mail may contain confidential and/or privileged information. If
 you 
 are not the intended recipient (or have received this e-mail in
 error) 
 please notify the sender immediately and destroy this e-mail. Any 
 unauthorized copying, disclosure or distribution of the material in
 this 
 e-mail is strictly forbidden.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why is this strict in its arguments?

2007-12-07 Thread Henning Thielemann

On Fri, 7 Dec 2007, Andrew Coppin wrote:

 Ian Lynagh wrote:
  On Tue, Dec 04, 2007 at 03:07:01PM -0800, Ryan Ingram wrote:
 
  Is there a reason why strictness is defined as
 
  f _|_ = _|_
 
  instead of, for example,
 
  forall x :: Exception. f (throw x) = throw x

Errors and exceptions are two very different concepts.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: distinguish functions from non-functions in a class/instances

2007-12-07 Thread Luke Palmer
On Dec 7, 2007 8:39 PM, Dan Weston [EMAIL PROTECTED] wrote:
  compose f g = f . g
 
  compose' f g x = f (g x)
 
  Are you saying that these two exactly equivalent functions should have
  different arity?   If not, then is the arity 2 or 3?

 Prelude :t let compose f g = f . g in compose
 let compose f g = f . g in compose :: (b - c) - (a - b) - a - c
 Prelude :t let compose' f g x = f (g x) in compose'
 let compose' f g x = f (g x) in compose' :: (t - t1) - (t2 - t) - t2
 - t1

 The arity is the number of top-level -

 Both are arity 3.


Hmm, this still seems ill-defined to me.

compose :: (Int - Int - Int) - (Int - Int) - Int - Int - Int

Is a valid expression given that definition (with a,b = Int and c = Int - Int),
but now the arity is 4.

Luke
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Why is this strict in its arguments?

2007-12-07 Thread Andrew Coppin

Ian Lynagh wrote:

On Tue, Dec 04, 2007 at 03:07:01PM -0800, Ryan Ingram wrote:
  

Is there a reason why strictness is defined as


f _|_ = _|_
  

instead of, for example,


forall x :: Exception. f (throw x) = throw x
  


There's discussion along these lines in
http://hackage.haskell.org/trac/ghc/ticket/1171
  


Gotta love bug reports where nobody is even sure what the correct 
behaviour actually *is*... ;-)


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Point and link

2007-12-07 Thread Claude Heiland-Allen

Denis Bueno wrote:

On Dec 7, 2007 1:50 PM, Andrew Coppin [EMAIL PROTECTED] wrote:

Hi guys.

Here's a fairly basic question. I have several ideas for programs that
I'd like to write. They all involve placing units of some kind, and
then drawing connections between those units. How feasible is it to
program such a thing in Haskell? Where would you start? (Gtk2hs is the
only graphics API I'm familiar with using at present.)


A bit off-topic for this list, but seeing as I'm replying anyway, I've 
done something like that with SVG and Javascript:


http://www.blurty.com/users/claudiusmaximus/day/2007/10/03#419

I imagine (having never used it) Gtk has similar ways to bind event 
listeners to graphical elements to implement drag and drop type 
stuff, and it will most likely be much less error-prone than Javascript 
(I still haven't got the drag and drop right in Graphgrow...).



Do you need to update positions of the units in real time?  Do they
even evolve over time, or are you just trying to visualise?

If it's the latter, you might just take a collection of units and
connections between them, output them in the graphviz [0] format, and
see the resulting drawing.  Graphviz is for visualising arbitrary
graphs, and it's quite good at it.


Coincidentally I tried this the other day:

http://www.blurty.com/users/claudiusmaximus/day/2007/12/06#426


Claude
--
http://claudiusmaximus.goto10.org

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Literate HTML

2007-12-07 Thread Henning Thielemann

On Fri, 7 Dec 2007, Neil Mitchell wrote:

 Hi

 I want literate Haskell, but where the literate bit forming a document
 is actually HTML, not latex. Does anyone have any idea how to go about
 this?

The numeric-quest library was the first and only one that I have seen in
this style. I could only get it working both in GHC and a browser by
creating links. Say
   Module.lhs
   Module.html - Module.lhs

http://darcs.haskell.org/numeric-quest/
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Literate HTML

2007-12-07 Thread Brandon S. Allbery KF8NH


On Dec 7, 2007, at 14:07 , Neil Mitchell wrote:


I want literate Haskell, but where the literate bit forming a document
is actually HTML, not latex. Does anyone have any idea how to go about
this?


You could replace the unlit executable in the GHC library directory  
with one which knows how to extract Haskell code from HTML.


--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [EMAIL PROTECTED]
system administrator [openafs,heimdal,too many hats] [EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon universityKF8NH


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: Re: type class question

2007-12-07 Thread Peter Padawitz

Jules Bean wrote:


Peter Padawitz wrote:

Functional dependencies don't work in my case. Actually, I don't see 
why they should.



Ah well, it's cruel to say that without explaining to us why!


Cause I don't see why the instantiation conflicts pointed out by others 
would vanish then.


I'm not sure why a complete cyclic dep a - b - c - d - a isn't 
what you want.


What seems to be needed here is a type class construct with a kind of 
record parameter so that instance conflicts cannot occur.



I'm not entirely sure what you intend to mean by this, but if you mean 
what I guess you mean:


class Java (a,b,c,d) where 


Yeah... but ghc accepts only type variables here, not arbitrary 
polymorphic types.


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Literate HTML

2007-12-07 Thread Duncan Coutts

On Fri, 2007-12-07 at 19:14 +, Neil Mitchell wrote:
 Hi Brandon,
 
  You could replace the unlit executable in the GHC library directory
  with one which knows how to extract Haskell code from HTML.
 
 I want a solution so that I can write the tagsoup manual in an way
 that can actually be run - I'd rather not force any additional
 dependence on a custom unlit command. However, adding HTML-style
 literate code to a future Haskell standard might not be a bad idea.

In the mean time you could prototype it as a pre-processor in Cabal if
you write the unlit code.

Duncan

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] fundeps and overlapping/undecidable instances

2007-12-07 Thread Jeff Polakow
Hello,

  Does the following code work for you?

-Jeff

---

{-# OPTIONS_GHC -fglasgow-exts -fallow-undecidable-instances 
-fallow-overlapping-instances #-}
data Nil = Nil
data x ::: xs = x ::: xs
infixr 5 :::

data HTrue = HTrue deriving Show
data HFalse = HFalse deriving Show

class Member x xs b | x xs - b where member :: x - xs - b
instance Member x Nil HFalse where member _ _ = HFalse
instance Member x xs b = Member x (x ::: xs) HTrue where member _ _ = 
HTrue
instance Member x xs b = Member x (y ::: xs) b where member x (_ ::: xs) 
= member x xs

{-
member 'a' (() ::: a ::: '1' ::: Nil)  ==  HTrue
member 'a' (() ::: a ::: Nil)  ==  HFalse
-}

---

This e-mail may contain confidential and/or privileged information. If you 
are not the intended recipient (or have received this e-mail in error) 
please notify the sender immediately and destroy this e-mail. Any 
unauthorized copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: distinguish functions from non-functions in a class/instances

2007-12-07 Thread Dan Weston

Luke Palmer wrote:

You can project the compile time numbers into runtime ones:


Yes, that works well if I know a priori what the arity of the function 
is. But I want to be able to have the compiler deduce the arity of the 
function (e.g. by applying undefined until it is no longer a function), 
precisely so I don't have to supply it myself.


Function arity is (I think) something already known to GHC, so I don't 
know why we can't get at it too.



On Dec 7, 2007 6:21 PM, Dan Weston [EMAIL PROTECTED] wrote:

This is great! Two questions:

1) I want to make sure the function arity matches the list length (as a
runtime check). I think I can do this with an arity function using
Data.Typeable. I came up with:

arity f = a (typeOf f) where
   a tr | typeRepTyCon tr /= mkTyCon - = 0
| otherwise = 1 + (a . fromJust . funResultTy tr . head
 . typeRepArgs $ tr)

This looks awful. Is there a better way to get the function arity?

2) Once I have say arity (+) == 2 at runtime, how can I get it reified
into Succ (Succ Zero)) at compile time to be able to use it as the first
argument in your nary function? Can/should I use Template Haskell for this?


You can project the compile time numbers into runtime ones:


class ProjectN n where
projectN :: n - Int

instance ProjectN Zero where
projectN _ = 0

instance (ProjectN n) = ProjectN (Succ n) where
projectN _ = 1 + projectN (undefined :: n)


And then make sure the length matches the projected number of
arguments.  Other disagreements will be resolved at compile time.

Luke


Dan

Victor Nazarov wrote:

{-# OPTIONS -fglasgow-exts #-}
{-# OPTIONS -fallow-undecidable-instances #-}

data Zero
data Succ a

class Nary n x y | n x - y where
  nary :: n - x - [String] - y

instance Nary Zero x x where
  nary _ x [] = x

instance (Nary n y z, Read x) = Nary (Succ n) (x-y) z where
  nary _ f (x:xs) = nary (undefined::n) (f $ read x) xs




___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe







___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] parser

2007-12-07 Thread Chris Eidhof

On 7 dec 2007, at 23:51, Ryan Bloor wrote:
i am using hugs and the isDigit and anything 'is' doesn't work...  
they must have forgot to add them in! Does GHC work with them.
Yes, it's in base. Alternatively, you could write the functions  
yourself, they're not that hard.
p.s... that book looks fantastic... bette than the ones i got from  
the library; too specific.
also... when and where do i use my predetermined types(EBool) and  
how...?
I think that is explained in the book. It's also not too hard to  
figure out yourself. A good strategy for writing Haskell programs in  
general is to think of the types first: what types should your  
functions have? Often this will help you in thinking of a good  
implementation.


The first thing you want to do, is parametrize the return-type of your  
parser. Instead of having type Parser = [(ETerm, String)] you probably  
want: type Parser a = String - [(a, String)]. With that type, you  
could do something like:


parseDigit :: Parser Int
parseMany :: Parser a - Parser [a]
parseOr :: Parser a - Parser a - Parser a

You can use these types to come up with the functions.

-chris




Ryan





 CC: haskell-cafe@haskell.org
 From: [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 Subject: Re: [Haskell-cafe] parser
 Date: Fri, 7 Dec 2007 23:31:38 +0100


 On 7 dec 2007, at 22:55, Ryan Bloor wrote:

  hi
 
  The thing is... it will be a simple parser really. The expressions
  are already defined and we can't use parsec imports. Below is the
  types I have.
  I have a function that removes initial spaces from the start of a
  string. A function that checks if a substring is part of another
  string, which use the remove space function also.
  The next function is: which uses the type:
 
  type Parser = String - [(Expr,String)]
 So what does this type really mean? You give it a string, and it  
will

 return a list of (Expr, String). I would guess that the list is all
 possible outcomes, and the String in (Expr, String) is probably the
 rest of the parsed data. If you want to find a correct parse, you
 probably want to select the first element from that list that has an
 empty rest.

 You could write a parser that parses single digits:

 parseDigit :: String - [(Int, String)]
 parseDigit (x:xs) | isDigit x = [read x]

 Of course you have to define the other cases for parseDigit. If you
 had a parseMany function that parses many things of the same type,  
you
 could combine that with parseDigit to parse natural numbers. The  
other

 thing you are really going to need is a choice-operator. In your
 example, you want to parse terms that are either numbers or term +
 term:

 parseTerm = parseNaturalNumber `parseOr` parseAddition

 It's probably best to read a good book or tutorial on parsers. There
 is an excellent textbook on grammars and parsing in Haskell [1], it
 probably explains exactly what you want.

 -chris

 [1] Johan Jeuring, Doaitse Swierstra: Grammars and Parsing: 
http://www.cs.uu.nl/docs/vakken/gont/diktaat.pdf

 
 
  readExpression :: String - Expr
 
  e.g. readExpression True = EBool True
  e.g. readExpression (23 + 67) = EAdd (EInt 23) (EInt 67)
 
 
  
  Types--
 
  data Type = TNone -- badly typed values
  | TInt -- integer values
  | TBool -- boolean values
  deriving Show
  data Expr = EInt {vInt :: Int} -- integer values
  | EBool {vBool :: Bool} -- boolean values
  | EAdd Expr Expr -- (e1 + e2)
  | EMin Expr Expr -- (e1 - e2)
  | EMul Expr Expr -- (e1 * e2)
  | EAnd Expr Expr -- (e1  e2)
  | EOr Expr Expr -- (e1 || e2)
  | ENot Expr -- not e1
  | EComp Expr Expr -- (e1 == e2)
  | ETest Expr Expr Expr -- if e1 then e2 else e3
  | ENone -- badly formed expressions
 
 
 
 
 
 
   CC: haskell-cafe@haskell.org
   From: [EMAIL PROTECTED]
   To: [EMAIL PROTECTED]
   Subject: Re: [Haskell-cafe] parser
   Date: Fri, 7 Dec 2007 22:17:54 +0100
  
  
   On 6 dec 2007, at 18:06, Ryan Bloor wrote:
Can anyone advise me on how to check whether a string contains
  ints,
chars, bools, etc
   
2345 + 6767 shoudl give IntAdd (2345) (6767)
2345 should give IntT 2345
   You need to write a parser. There are a lot of libraries that  
will
   help you write a parser. One library that is often used for  
writing

   parsers in Haskell is called Parsec [1]. There's good
  documentation on
   that site on how to use it. Parsec is already included in you
   distribution. Good luck!
  
   -chris
  
   [1]: http://legacy.cs.uu.nl/daan/parsec.html
 
 
  Get closer to the jungle… I'm a Celebrity Get Me Out Of Here!



Can you guess the film? Search Charades!


___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: distinguish functions from non-functions in a class/instances

2007-12-07 Thread Luke Palmer
On Dec 7, 2007 7:57 PM, Luke Palmer [EMAIL PROTECTED] wrote:
 On Dec 7, 2007 7:41 PM, Dan Weston [EMAIL PROTECTED] wrote:
  Luke Palmer wrote:
   You can project the compile time numbers into runtime ones:
 
  Yes, that works well if I know a priori what the arity of the function
  is. But I want to be able to have the compiler deduce the arity of the
  function (e.g. by applying undefined until it is no longer a function),
  precisely so I don't have to supply it myself.
 
  Function arity is (I think) something already known to GHC, so I don't
  know why we can't get at it too.

 No, it is not.  Consider:

 compose f g x = f (g x)

 What is the arity of f?

Oh, you're saying at run-time, given an object.

Still no, by some definition.

compose f g = f . g

compose' f g x = f (g x)

Are you saying that these two exactly equivalent functions should have
different arity?   If not, then is the arity 2 or 3?

Luke
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] Re: distinguish functions from non-functions in a class/instances

2007-12-07 Thread Luke Palmer
On Dec 7, 2007 7:41 PM, Dan Weston [EMAIL PROTECTED] wrote:
 Luke Palmer wrote:
  You can project the compile time numbers into runtime ones:

 Yes, that works well if I know a priori what the arity of the function
 is. But I want to be able to have the compiler deduce the arity of the
 function (e.g. by applying undefined until it is no longer a function),
 precisely so I don't have to supply it myself.

 Function arity is (I think) something already known to GHC, so I don't
 know why we can't get at it too.

No, it is not.  Consider:

compose f g x = f (g x)

What is the arity of f?

Luke
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


Re: [Haskell-cafe] parser

2007-12-07 Thread Chris Eidhof


On 6 dec 2007, at 18:06, Ryan Bloor wrote:
Can anyone advise me on how to check whether a string contains ints,  
chars, bools, etc


2345 + 6767 shoudl give IntAdd (2345) (6767)
2345 should give IntT 2345
You need to write a parser. There are a lot of libraries that will  
help you write a parser. One library that is often used for writing  
parsers in Haskell is called Parsec [1]. There's good documentation on  
that site on how to use it. Parsec is already included in you  
distribution. Good luck!


-chris

[1]: http://legacy.cs.uu.nl/daan/parsec.html
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe