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

SCM Repository

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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3402 - (view) (download)

1 : jhr 3396 (* util.sml
2 :     *
3 :     * This code is part of the Diderot Project (http://diderot-language.cs.uchicago.edu)
4 :     *
5 :     * COPYRIGHT (c) 2015 The University of Chicago
6 :     * All rights reserved.
7 :     *)
8 :    
9 :     structure Util : sig
10 :    
11 :     (* `coerceType (dstTy, (e, srcTy))` checks to see if `srcTy` can be coerced to `dstTy`.
12 :     * If so, it returns the coerced expression and the resulting type. Otherwise it
13 :     * returns `NONE`.
14 :     *)
15 :     val coerceType : Types.ty * (AST.expr * Types.ty) -> (AST.expr * Types.ty) option
16 :    
17 :     (* `coerceType ((e1, ty1), (e2, ty2))` checks to see if either 1ty1` can be coerced
18 :     * to `ty2` or vice versa. If so, it returns the coerced expressions and resulting
19 :     * type, otherwise it returns `NONE`.
20 :     *)
21 :     val coerceType2 : (AST.expr * Types.ty) * (AST.expr * Types.ty)
22 :     -> (AST.expr * AST.expr * Types.ty) option
23 :    
24 :     end = struct
25 :    
26 : jhr 3402 structure Ty = Types
27 :    
28 : jhr 3396 (* build a coercion expression; for literal values, we can coerce directly *)
29 :     fun coerceExp (Ty.T_Tensor(Ty.Shape[]), Ty.T_Int, AST.E_Lit(Literal.Int n)) =
30 : jhr 3402 AST.E_Lit(Literal.Real(RealLit.fromInt n))
31 : jhr 3396 | coerceExp (ty1, ty2, e) = AST.E_Coerce{srcTy=ty2, dstTy=ty1, e=e}
32 :    
33 :     fun coerceType (dstTy, (e, srcTy)) = (case Unify.matchType(dstTy, srcTy)
34 : jhr 3402 of Unify.EQ => SOME(e, dstTy)
35 :     | Unify.COERCE => SOME(coerceExp (dstTy, srcTy, e), dstTy)
36 :     | Unify.FAIL => NONE
37 : jhr 3396 (* end case *))
38 :    
39 : jhr 3402 fun coerceType2 ((e1, ty1), (e2, ty2)) = (
40 : jhr 3396 (* first try to coerce ty2 to ty1 *)
41 :     case Unify.matchType (ty1, ty2)
42 : jhr 3402 of Unify.EQ => SOME(e1, e2, ty1)
43 :     | Unify.COERCE => SOME(e1, coerceExp (ty1, ty2, e2), ty1)
44 : jhr 3396 | Unify.FAIL => (
45 :     (* try to coerce ty1 to ty2 *)
46 : jhr 3402 case Unify.matchType (ty2, ty1)
47 : jhr 3396 of Unify.EQ => raise Fail "impossible"
48 : jhr 3402 | Unify.COERCE => SOME(coerceExp (ty2, ty1, e1), e2, ty2)
49 : jhr 3396 | Unify.FAIL => NONE
50 :     (* end case *))
51 : jhr 3398 (* end case *))
52 : jhr 3396
53 :     end

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