News for hackers. Stuff that matters.
Re:Revenge of the Types by berdario on Sunday August 24, 2014 @09:44PM (#8220232)
I'm not sure why, but your examples aren't working with me...

    1 *| meter |+| 35 *| centi meter
    1.35 m
works, but

    1 meter
fails with

    <interactive>:42:1:
        No instance for (Num (Value LengthDimension (U Meter) f0 -> t0))
          arising from the literal `1'
        Possible fix:
          add an instance declaration for
          (Num (Value LengthDimension (U Meter) f0 -> t0))
        In the expression: 1
        In the expression: 1 meter
        In an equation for `it': it = 1 meter
    
    <interactive>:42:3:
        No instance for (Fractional f0) arising from a use of `meter'
        The type variable `f0' is ambiguous
        Possible fix: add a type signature that fixes these type variable(s)
        Note: there are several potential instances:
          instance Fractional Double -- Defined in `GHC.Float'
          instance Fractional Float -- Defined in `GHC.Float'
          instance Integral a => Fractional (GHC.Real.Ratio a)
            -- Defined in `GHC.Real'
          ...plus four others
        In the first argument of `1', namely `meter'
        In the expression: 1 meter
        In an equation for `it': it = 1 meter
Re:Revenge of the Types by gizmo686 on Monday August 25, 2014 @12:49AM (#8220644)
Try running "import UnitTyped.NoPrelude" first.

  EDIT: you may also want to start ghci with the "-XNoImplicitPrelude" flag, or explicitly disambiguate operations such as "*" with "UnitTyped.NoPrelude.*" or "Prelude.*"
Re:Revenge of the Types by berdario on Monday August 25, 2014 @02:46AM (#8220851)
Thanks, but that's not enough. The error seems due to the fact that `meter` is passed as an argument to `1` which is not a function, and indeed I don't see how that could be valid haskell code (unless there's some language flag I should enable, but unfortunately the wiki/docs of unittyped doesn't seem to be comprehensive enough)
Re:Revenge of the Types by gizmo686 on Monday August 25, 2014 @03:22AM (#8220908)
Weird, it is enough on my machine (running ghc 7.6.3, unittyped 0.1), but unittyped does seem to use a lot of language extensions internally, it is seems possible that it would behave weirdly on different versions.

To answer the question of how it is possible, we can start by looking at the types. ":t" is a ghci command to show the type of an expression:

  ghci> :t 1
  1 :: Num a => a
This indicates that "1" can be any type that implements the "Num" typeclass.

Next, we want to determine what type "1" takes in the expression "1 meter". We can do this with:

  ghci> let a=1; b=a meter
  ghci> :t a
  a :: UnitTyped.Value Double LengthDimension Meter
     -> UnitTyped.Value
          Double
          ('UnitTyped.UnitCons
             * Length ('UnitTyped.Pos 'UnitTyped.One) 'UnitTyped.UnitNil)
          Meter
We can see that, in the expression "1 meter", "1" is actually a function. Looking at the source code [1], it seems like this is accomplished in a relativly hacky manner:

  instance (Fractional f, Convertable a b, t ~ Value f a b) => (Prelude.Num (Value f a b -> t)) where
	fromInteger i x = Prelude.fromInteger i . x
	(+) = error "This should not happen"
	(*) = error "This should not happen"
	abs = error "This should not happen"
	signum = error "This should not happen"
This works because the "+" being defined here is Prelude.+, not UnitTyped.NoPrelude.+ (which is defined seperatly).

[1] https://hackage.haskell.org/package/unittyped-0.1/docs/src/U...

Re:Revenge of the Types by berdario on Monday August 25, 2014 @05:39AM (#8221127)
I see. I tried to report this as a bug, fwiw:

https://bitbucket.org/xnyhps/haskell-unittyped/issue/3/num-i...

Btw, it's weird... I just tried

    ack-grep "Num\b"
and I cannot see any instance of Num defined anywhere inside unittyped's src

PS: ok, since I wanted to see exactly what was the problem with unittyped compiling under ghc7.8 I cloned the sources, but I forgot to checkout the actual release... thus running directly from tip was the cause

Installing it directly from hackage solved it, it's embarrassing how I was stunned by this in hindsight

Re:Revenge of the Types by tel on Monday August 25, 2014 @09:45AM (#8221684)
1 absolutely could be a function—all you need is a function type to instantiate Num.