cartazio / numbers

Other
29 stars 13 forks source link

Totally abstract numbers #20

Open dmcclean opened 7 years ago

dmcclean commented 7 years ago

I have a EDSL with lots of terms that are polymorphic over any Floating type. I wish to perform some abstract interpretation over them looking at properties other than what they do to the numbers (things like signal names and other metadata). Right now I have to pick a Floating type essentially arbitrarily, even though I end up ignoring it.

Since there isn't a Num instance for (), I propose a type like:

data AbstractNumber = AbstractNumber deriving (Eq, Ord)

binop :: AbstractNumber -> AbstractNumber -> AbstractNumber
binop AbstractNumber AbstractNumber = AbstractNumber

instance Num AbstractNumber where
  (+) = binop
  (*) = binop
  negate = id
  abs = id
  signum = id
  fromInteger = const AbstractNumber

-- similarly for Fractional and Floating

Is this a good fit for this package? If so I can draft a PR.

DanBurton commented 7 years ago

LGTM, but personally I'd make binop ignore it's arguments rather than pattern match on them.

dmcclean commented 7 years ago

Good idea. I was trying to maintain the same strictness behavior as the unary operations, but a strict wildcard would be more concise.

DanBurton commented 7 years ago

Not really sure, but I'd perhaps suggest making the unary operations lazier instead.

binop _ _= AbstractNumber
unop _ = AbstractNumber

instance Num AbstractNumber where
  (+) = binop
  (*) = binop
  negate = unop
  abs = unop
  signum = unop
  fromInteger = unop