Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Andrej Bauer <Andrej.Bauer@fmf.uni-lj.si>
To: caml-list <caml-list@inria.fr>
Cc: "Tom Primožič" <tom.primozic@gmail.com>
Subject: Re: [Caml-list] How to compute variance of a type
Date: Sun, 06 Jan 2008 16:31:00 +0100	[thread overview]
Message-ID: <4780F434.8000604@fmf.uni-lj.si> (raw)
In-Reply-To: <c1490a380801060219hc074ad0s46db833fa4410fe8@mail.gmail.com>

There is nothing strange or complicated about variance. Whenever you
descend to the left of an arrow, you have to switch the variance.

I enclose a simple example of how to compute variance for simple types.

Andrej

P.S. Feel free to knock on my office door when you have such questions :-)

--------------------------------------------------------
type name = string

type ty =
  | Var of name      (* type variable *)
  | Prod of ty * ty  (* product *)
  | Arrow of ty * ty (* function type *)

type variance =
  | Variant
  | Covariant
  | Mixed

(** Join variances *)
let join u v =
  if u = v then v else Mixed

let opposite = function
  | Variant -> Covariant
  | Covariant -> Variant
  | Mixed -> Mixed

(** Add a type variable [x] with variance [v] to a given association
    list of variances. *)
let rec add x v = function
  | [] -> [(x,v)]
  | (y,u)::lst ->
      if x = y then
	(x, join u v) :: lst
      else
	(y,u) :: (add x v lst)

(** Compute variances of all type variables appearing
    in a type. Return an association list mapping type parameters
    to their variances.
 *)
let variance t =
  let rec var c lst = function
    | Var v -> add v c lst
    | Prod (t1, t2) -> var c (var c lst t1) t2
    | Arrow (t1, t2) -> var c (var (opposite c) lst t1) t2
  in
    var Variant [] t

(** Examples. *)

let t1 = Arrow (Arrow (Var "a", Var "b"), Var "c")
let v1 = variance t1

let t2 = Arrow (Var "a", Arrow (Var "b", Var "c"))
let v2 = variance t2

let t3 = Arrow (Prod (Var "a", Var "b"), Var "a")
let v3 = variance t3
---------------------------------------------------------


      parent reply	other threads:[~2008-01-06 15:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-06 10:19 Tom Primožič
2008-01-06 12:44 ` [Caml-list] " rossberg
2008-01-06 15:31 ` Andrej Bauer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4780F434.8000604@fmf.uni-lj.si \
    --to=andrej.bauer@fmf.uni-lj.si \
    --cc=Andrej.Bauer@andrej.com \
    --cc=caml-list@inria.fr \
    --cc=tom.primozic@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox