Home My Page Projects Code Snippets Project Openings diderot
Summary Activity Tracker Tasks SCM

SCM Repository

[diderot] View of /branches/vis15/src/compiler/typechecker/util.sml
ViewVC logotype

View of /branches/vis15/src/compiler/typechecker/util.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3398 - (download) (annotate)
Wed Nov 11 01:17:58 2015 UTC (4 years ago) by jhr
File size: 1823 byte(s)
working on merge
(* util.sml
 *
 * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
 *
 * COPYRIGHT (c) 2015 The University of Chicago
 * All rights reserved.
 *)

structure Util : sig

  (* `coerceType (dstTy, (e, srcTy))` checks to see if `srcTy` can be coerced to `dstTy`.
   * If so, it returns the coerced expression and the resulting type.  Otherwise it
   * returns `NONE`.
   *)
    val coerceType : Types.ty * (AST.expr * Types.ty) -> (AST.expr * Types.ty) option

  (* `coerceType ((e1, ty1), (e2, ty2))` checks to see if either 1ty1` can be coerced
   * to `ty2` or vice versa.  If so, it returns the coerced expressions and resulting
   * type, otherwise it returns `NONE`.
   *)
    val coerceType2 : (AST.expr * Types.ty) * (AST.expr * Types.ty)
	  -> (AST.expr * AST.expr * Types.ty) option

  end = struct

  (* build a coercion expression; for literal values, we can coerce directly *)
    fun coerceExp (Ty.T_Tensor(Ty.Shape[]), Ty.T_Int, AST.E_Lit(Literal.Int n)) =
          AST.E_Lit(Literal.Float(FloatLit.fromInt n))
      | coerceExp (ty1, ty2, e) = AST.E_Coerce{srcTy=ty2, dstTy=ty1, e=e}

    fun coerceType (dstTy, (e, srcTy)) = (case Unify.matchType(dstTy, srcTy)
           of U.EQ => SOME e
            | U.COERCE => SOME(coerceExp (dstTy, srcTy, e))
            | U.FAIL => NONE
          (* end case *))

    fun coerceType2 ((ty1, e1), (e2, ty2)) = (
	(* first try to coerce ty2 to ty1 *)
	  case Unify.matchType (ty1, ty2)
	   of Unify.EQ => SOME(e1, e2)
	    | Unify.COERCE => SOME(e1, coerceExp (ty1, ty2, e2))
	    | Unify.FAIL => (
	      (* try to coerce ty1 to ty2 *)
		case Unify.match (ty2, ty1)
		 of Unify.EQ => raise Fail "impossible"
		  | Unify.COERCE => SOME(coerceExp (ty2, ty1, e1), e2)
		  | Unify.FAIL => NONE
		(* end case *))
	  (* end case *))

  end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0