Advanced Functional Prog.

back - up


Algebraic Datatypes

  1. Type & function generalization:
    Expressions on a set of values with two operators: (boolean,or,and), (int,+,*), (set,union,intersection), ...
type 't Exp = 
  | Val  of 't
  | Plus of ('t Exp)*('t Exp)
  | Mult of ('t Exp)*('t Exp);;  

let e = Plus (Val [1],Mult (Val [1;2], Val [2;3;3]));;

General transformation function:

let rec interp v p m e = 
  match e with
  | Val x        -> v x
  | Plus (e1,e2) -> p (interp v p m e1) (interp v p m e2)
  | Mult (e1,e2) -> m (interp v p m e1) (interp v p m e2);;

interp (Set.ofList) (Set.union) (Set.intersect) e;;
interp string (fun x y->"("+x+" || "+y+")") (fun x y->"("+x+" && "+y+")") e;;

interp (List.length) (+) (*) e;; // = ?

11