[Haskell] build an interpreter on top of GHCi?

2005-01-26 Thread WANG Meng
Hi All,

Does anybody has the experience to built an interpreter on top of GHCi?

What I want is to defined a my own interpreter as a Haskell module and
load it into GHCi. So this new interpreter will be running on top of GHCi
which accepts syntax extension of Haskell. For example:

Prelude> :l myInter
Prelude> myInter> new language source
Prelude> myInter> ...
Prelude> myInter> exit
Prelude>

In another word, I want to have a read-eval-print-loop running on GHCi.
Does anybody know how to do that?

 -W-M-
  @ @
   |
  \_/
___
Haskell mailing list
Haskell@haskell.org
http://www.haskell.org/mailman/listinfo/haskell


[Haskell] parser for "Typing Haskell in Haskell"

2004-11-17 Thread WANG Meng
Hi All,

Does anybody know what parser "Typing Haskell in Haskell" use? I am tring
to use the code for some type checking. But I cannot find a parser in the
distribution.


 -W-M-
  @ @
   |
  \_/
___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


constrained datatype

2004-01-14 Thread Wang Meng
Hi All,

Anybody knows why the following code does not work?

> class Foo n

> data Erk n = Foo n => Erk

test.hs:53:
All of the type variables in the constraint `Foo n' are already in
scope
(at least one must be universally quantified here)
When checking the existential context of constructor `Erk'
In the data type declaration for `Erk'
Failed, modules loaded: none.

Is there any reason for this error?

 -W-M-
  @ @
   |
  \_/


___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


import modules from a different directory

2003-12-11 Thread Wang Meng
Hi All,

Anybody has the experience of importing a module from a different
directory?In my code, i need to import a module that is not in the current
directory. How can I do that in GHC?


 -W-M-
  @ @
   |
  \_/

___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


Re: *safe* coerce, for regular and existential types

2003-08-01 Thread Wang Meng
I admire the elegancy of your code which makes the changes to add new data
types minimum. There is one question I want to ask: Does this technique
extend to polymophic types?

Let's say we have the following type:

> data D a = C | D a

Is it possible to index the type D a? Or there is some fundmental
limitations which make it not achievable by Haskell type classes?


 -W-M-
  @ @
   |
  \_/

On Thu, 31 Jul 2003 [EMAIL PROTECTED] wrote:

>
> This message describes functions safeCast and sAFECoerce implemented
> in Haskell98 with common, pure extensions. The functions can be used
> to 'escape' from or to existential quantification and to make
> existentially-quantified datatypes far easier to deal with. Unlike
> Dynamic, the present approach is pure, avoids unsafeCoerce and
> unsafePerformIO, and permits arbitrary multiple user-defined typeheaps
> (finite maps from types to integers and values).
>
> An earlier message [1] introduced finite type maps for
> purely-functional conversion of monomorphic types to unique
> integers. The solution specifically did not rely on Dynamic and
> therefore is free from unsafePerformIO. This message shows that the
> type maps can be used for a safe cast, in particular, for laundering
> existential types. The code in this message does NOT use
> unsafePerformIO or unsafeCoerce. To implement safe casts, we define a
> function sAFECoerce -- which works just like its impure
> counterpart. However the former is pure and safe. sAFECoerce is a
> library function expressed in Haskell with common extension. The
> safety of sAFECoerce is guaranteed by the typechecker itself.
>
> This whole message is self-contained, and can be loaded as it is in
> GHCi, given the flags
>   -fglasgow-exts -fallow-undecidable-instances -fallow-overlapping-instances
>
> This message was inspired by Amr Sabry's problem on existentials.  In
> fact, it answers an unstated question in Amr Sabry's original message.
>
>
> It has been observed on this list that existentially-quantified
> datatypes are not easy to deal with [2]. For example, suppose we have
> a value of a type
>
> > data EV = forall a. (TypeRep a TI)=> EV a
>
> (please disregard the second argument of TypeRep for a moment).
>
> The constructor EV wraps a value. Suppose we can guess that the
> wrapped value is actually a boolean. Even if our guess is correct, we
> *cannot* pass that value to any function of booleans:
>
> *> *Main> case (EV False) of (EV x) -> not x
> *>
> *> :1:
> *> Inferred type is less polymorphic than expected
> *> Quantified type variable `a' is unified with `Bool'
> *> When checking an existential match that binds
> *> x :: a
> *> and whose type is EV -> Bool
> *> In a case alternative: (EV x) -> not x
>
> A quantified type variable cannot be unified with any regular type --
> or with another quantified type variable. Values of existentially
> quantified types cannot be passed to monomorphic functions, or to
> constrained polymorphic functions (unless all their constrains have
> been mentioned in the declaration of the existential). That limitation
> guarantees safety -- on the other hand, it significantly limits the
> convenience of existential datatypes [2].
>
> To overcome the limitation, it _seemed_ that we had to sacrifice
> purity. If we are positive that a particular existentially quantified
> value has a specific type (e.g., Bool), we can use unsafeCoerce to
> cast the value into the type Bool [3]. This approach is one of the
> foundations of the Dynamic library. The other foundation is an ability
> to represent a type as a unique run-time value, provided by the
> methods of the class like TypeRep. Given an existentially quantified
> value and a value of the desired type, Dynamic compares type
> representations of the two values. If they are the same, we can
> confidently use unsafeCoerce to cast the former into the type of the
> latter.
>
> This works, yet leaves the feeling of dissatisfaction. For one thing,
> we had to resort to an impure feature. More importantly, we placed our
> trust in something like TypeRep and its members, that they give an
> accurate and unique representation of types. But what if they lie to
> us, due to a subtle bug in their implementation? What if they give the
> same representation for two different types? unsafeCoerce will do its
> dirty work nevertheless. Using the result would lead to grave
> consequences, however.
>
> This message describes sAFECoerce and the corresponding safe cast.
> Both functions convert the values of one type into the target type.
> One or both of these types may be existentially quantified.  When the
> source and the target types are the same, both functions act as the
> identity function. The safe cast checks that the type representations
> of the source and the target types are the same. If they are, it
> invokes sAFECoerce. Otherwise, we monadically fail. The function
> sAFECoerce does the conversion without any type checkin

dynamics on polymorphic datatype

2003-07-25 Thread Wang Meng
Hi All,

I am trying to perform dynamic casting on polymorphic types.
Let's say I have a data type like:

> data Foo a = Foo a

Is there any way to use dynamics to convert a value of type Foo a to a
type reprentation? I try to use the toDyn in the dynamic libray, it
complains for ambigours a. Is there a solution to it?

Thank you very much.


 -W-M-
  @ @
   |
  \_/


___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


polymorphic type in state of state monad

2003-03-10 Thread Wang Meng
Hi All,

Any one of your have the experience of defining a state of a state monad
as a polymorphic type?
I want to have:

> type State = Term a => [a]
> data M a = M (State -> IO(State,a))

GHC yields a error message "Illegal polymorphic type".
How to resolve this?

Thank you very much.
  
 -W-M-
  @ @  
   |
  \_/   



___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


overload lift function

2003-03-06 Thread Wang Meng
Hi All,

I have a data type defined as

> data D a = D1 Int | D2 a

Follow this definition, I have D2 Int,D2 Bool and D2 [D2 Int] as instances
of type D.

I want to write an overloaded function lift which lifts an atom type to D.
 
It is easily done for D2 Int and D2 Bool using type classes.I can have

> class Liftable a where
> lift :: a -> L a
>  
> instance Liftable Int where
>   lift x = D2 x 

However, I have problem with D2 [D2 Int].I want to lift [Int] to D2 [D2
Int].

Is there any solution to it?

Thank you very much.
  
 -W-M-
  @ @  
   |
  \_/   



___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


Re: int to float problem

2003-02-28 Thread Wang Meng
Try 

intToFloat :: Int -> Float
intToFloat n = fromInteger (toInteger n)
  
 -W-M-
  @ @  
   |
  \_/   

On Fri, 28 Feb 2003, Mike T. Machenry wrote:

> Hello,
> 
>   I am having a problem. I recently desided I wanted a bunch function to return
> float instead of Int. I changed their type and wrote a new function that
> returned a float. I figured it'd be okay if all the others still returned
> Int since it's trivial to convert Int to Float. Sadly Haskell won't let me do
> this. What should I do? I attempted to cast all of the values in the functions
> that returned Int to Float, but I found no way to do this. I found fromInteger
> but it didn't seem to work on the return value of the cardinality function for
> instance. I guess cardinality much return Int. This is one of my functions.
> 
> smallestSet :: GameState -> Float
> smallestSet s = (-1 * cardinality (fLocations s))
> 
> This function is an error because it infer's an Int.
> 
> Thanks,
> -mike
> ___
> Haskell mailing list
> [EMAIL PROTECTED]
> http://www.haskell.org/mailman/listinfo/haskell
> 


___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell


escape from existential quantification

2003-02-27 Thread Wang Meng
I understand that existentially bound types cannot escape.

For example, say we have
data Foo = forall a. Foo Int a

Then we cannot define a function
extract (Foo i a) = a

However,this limitation makes it extremly difficult to program with local
quantifications.Is there any way to by pass this?

 -W-M-
  @ @  
   |
  \_/   






___
Haskell mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell