home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / sml_nj / 93src.lha / src / basics / lambda.sml < prev    next >
Encoding:
Text File  |  1993-01-27  |  4.3 KB  |  137 lines

  1. (* Copyright 1989 by AT&T Bell Laboratories *)
  2. (* lambda.sml *)
  3.  
  4. (* We add a very simple set of type information to the lambda calculus 
  5.  * to do the representation analysis.   (zsh)
  6.  *
  7.  * We make the following assumptions in our typed lambda language LEXP:
  8.  *   (1) Each lvar is uniquely named, i.e., no two bound lvar use same names 
  9.  *       (integers). Note the only places where new variables get introduced 
  10.  *       are at the lambda abstraction FN or the mutually recursive function 
  11.  *       definition FIX.  
  12.  *   (2) All data constructors are marked with a type. How to interpret these
  13.  *       constructors depends on their conrep and lty. The current version is 
  14.  *       doing recursive wrap-unwrap on all non-zero arity datatypes. The lty
  15.  *       field for same constructors always gives the same type, e.g. cons 
  16.  *       will have the type 'a * 'a list -> 'a. In the future, this lty field
  17.  *       may contain the instantiated type at the different application site,
  18.  *       e.g. cons : (int * int) * (int * int) list -> (int * int) list in the
  19.  *       expression cons((1,1),nil). 
  20.  *   (3) Type variables in a polymorphic functions are now treated as BOXEDty.
  21.  *       Most of sum types (concrete datatypes except those transparent) are 
  22.  *       treated as BOXEDty. 
  23.  *   (4) All primops are also marked with a type, depending on how they are 
  24.  *       used in the program.
  25.  *   (5) BOGUSty is a type used to be filled in some place such as the * in 
  26.  *       APP(FN(_,*,_),_) in the lambda expression, but its content should be 
  27.  *       never used. 
  28.  *   (6) RBOXEDty is used to denote those type variables that should be 
  29.  *       recursively wrapped or unwrapped when matched by some ML types.
  30.  *       (e.g., when 'a list -> 'a is instantiated into "(int * int) list 
  31.  *       -> int * int", the type variable 'a is treated as an RBOXEDty). 
  32.  *)
  33.  
  34. signature LAMBDA = sig
  35.  
  36. type primop sharing type primop = Access.primop
  37.  
  38. datatype lty 
  39.   = INTty 
  40.   | BOOLty
  41.   | REALty
  42.   | BOXEDty
  43.   | RECORDty of lty list
  44.   | ARROWty of lty * lty
  45.   | CONTty of lty     
  46.   | RBOXEDty                    (* recursively boxed type variables *) 
  47.  
  48. datatype con
  49.   = DATAcon of Symbol.symbol * Access.conrep * lty
  50.   | INTcon of int
  51.   | REALcon of string
  52.   | STRINGcon of string
  53.   | VLENcon of int
  54.  
  55. datatype lexp
  56.   = VAR of Access.lvar
  57.   | FN of Access.lvar * lty * lexp
  58.   | FIX of Access.lvar list * lty list * lexp list * lexp
  59.   | APP of lexp * lexp
  60.   | INT of int
  61.   | REAL of string
  62.   | STRING of string
  63.   | SWITCH of lexp * Access.conrep list *
  64.                (con * lexp) list * lexp option
  65.   | CON of (Symbol.symbol * Access.conrep * lty) * lexp
  66.   | DECON of (Symbol.symbol * Access.conrep * lty) * lexp
  67.   | RECORD of lexp list
  68.   | VECTOR of lexp list 
  69.   | SELECT of int * lexp
  70.   | RAISE of lexp * lty
  71.   | HANDLE of lexp * lexp
  72.   | PRIM of primop * lty
  73.   | WRAP of lty * lexp
  74.   | UNWRAP of lty * lexp
  75.  
  76. val BOGUSty : lty
  77. val CON' : (Symbol.symbol * Access.conrep * lty) * lexp -> lexp
  78. val DECON' : (Symbol.symbol * Access.conrep * lty) * lexp -> lexp
  79.  
  80. end
  81.  
  82. structure Lambda : LAMBDA = struct 
  83.  
  84.  open Access
  85.  
  86.  type primop = Access.primop
  87.  
  88.  datatype lty 
  89.    = INTty 
  90.    | BOOLty
  91.    | REALty
  92.    | BOXEDty
  93.    | RECORDty of lty list
  94.    | ARROWty of lty * lty
  95.    | CONTty of lty  
  96.    | RBOXEDty                    (* recursively boxed type variables *)
  97.  
  98.  type dataconstr = Symbol.symbol * Access.conrep * lty
  99.  
  100.  datatype con
  101.    = DATAcon of dataconstr
  102.    | INTcon of int
  103.    | REALcon of string
  104.    | STRINGcon of string
  105.    | VLENcon of int    (* VLENcon is a kludge, it should go away
  106.                soon.  Don't assume it exists.  WEA 8/13/92 *)
  107.  
  108.  datatype lexp
  109.    = VAR of lvar
  110.    | FN of lvar * lty * lexp
  111.    | FIX of lvar list * lty list * lexp list * lexp
  112.    | APP of lexp * lexp
  113.    | INT of int
  114.    | REAL of string
  115.    | STRING of string
  116.    | SWITCH of lexp * conrep list * (con*lexp) list * lexp option
  117.    | CON of dataconstr * lexp
  118.    | DECON of dataconstr * lexp
  119.    | RECORD of lexp list
  120.    | VECTOR of lexp list
  121.    | SELECT of int * lexp
  122.    | RAISE of lexp * lty 
  123.    | HANDLE of lexp * lexp
  124.    | PRIM of primop * lty
  125.    | WRAP of lty * lexp
  126.    | UNWRAP of lty * lexp
  127.  
  128.  val BOGUSty = BOXEDty    
  129.  
  130.  fun CON' ((_,REF,lt),e) = APP(PRIM(Access.P.MAKEREF,lt),e)
  131.    | CON' x = CON x
  132.  
  133.  fun DECON' ((_,REF,lt),e) = APP(PRIM(Access.P.DEREF,lt),e)
  134.    | DECON' x = DECON x
  135.  
  136. end
  137.