Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shared cases in F# discriminated unions

I want to write something like this:

type NumExp = Num of float

type Exp =
    | Num of float
    | Dot of NumExp * NumExp
    | Op of string * Exp * Exp

 let getValue (Num(n) : NumExp) = n

The compiler complains about a conflict between NumExp and Exp in getValue. Even the following fails:

let getValue (nn : NumExp) = match nn with | Num(n) -> n

Is there a way to use the same case in both discriminated unions that works with functions? The DU definitions themselves are OK.

I want to use the same case to avoid adding a level of indirection like

type Exp =
    | NumExpExp of NumExp
    | Dot of NumExp * NumExp
    | Op of string * Exp * Exp

in the Exp definition. I feel I'm missing something very basic here.

The reason I have NumExp is that I want to be able to 'plug' 2 Exps into a Dot (rather than 2 floats) because it makes generating expressions easier, but they can't be any Exp, just numerical.

EDIT: what I really wanted to know is whether the two cases in the two DUs could be treated as the same entity (kind of like Exp "including" NumExp). I realise now Exp.Num and NumExp.Num are completely separate entities. Tomas provides a nice way of discriminating the two cases below.

like image 644
Mau Avatar asked Jul 07 '10 12:07

Mau


2 Answers

If you have two discriminated unions with conflicting names of cases, you can use fully qualified name of the discriminated union case:

 let getValue (NumExp.Num(n)) = n  

A more complete example would look like this:

let rec eval = function
  | Exp.Num(f) -> f
  | Exp.Dot(NumExp.Num(f1), NumExp.Num(f2)) -> 
      // whatever 'dot' represents
  | Exp.Op(op, e1, e2) ->
      // operator

This always uses fully qualified names, which is probably a good idea if the names are simple enough and there are conflicting cases (which could lead to a confusion).

EDIT: Regarding sharing of cases - there is no automatic way of doing that, but you could have a case in your Exp that simply includes values of NumExp. For example like this:

type NumExp =
  | Num of float 

type Exp = 
  // first occurrence of NumExp is just a name, but F# allows us to reuse 
  // the name of the type, so we do that (you could use other name)
  | NumExp of NumExp  
  // other cases

When writing eval function you would then write (note that we no longer have the issue with name clashes, so we don't need fully qualified names):

| NumExp(Num f) -> f
| Op(op, e1, e2) -> // ...
like image 52
Tomas Petricek Avatar answered Oct 16 '22 05:10

Tomas Petricek


You can use interfaces as a substitute. This adds a bit of syntactic overhead, but is the best way I've found to do this.

type IExp = interface end

type NumExp =
        | Num of float
        interface IExp
type Exp =
        | Dot of NumExp * NumExp
        | Op of string * IExp * IExp
        interface IExp

// This function accepts both NumExp and Exp
let f (x:IExp) = match x with
    | :? NumExp as e -> match e with
        | Num v -> "Num"
    | :? Exp as e -> match e with
        | Dot (e1,e2) -> "Dot"
        | Op (op,e1,e2) -> "Op"
    | _ -> invalidArg "x" "Unsupported expression type"

// This function accepts only NumExp
let g = function
    | Num v -> "Num"
like image 3
dtech Avatar answered Oct 16 '22 06:10

dtech