# HG changeset patch # User Adam Chlipala # Date 1217351781 14400 # Node ID 80192edca30d4d4cb8c6a24ef1f352dc5c00007a # Parent 06a98129b23f45d03d2acee81d360591c2c86a2b Datatypes through corify diff -r 06a98129b23f -r 80192edca30d src/compiler.sml --- a/src/compiler.sml Tue Jul 29 12:30:04 2008 -0400 +++ b/src/compiler.sml Tue Jul 29 13:16:21 2008 -0400 @@ -342,7 +342,7 @@ print ("Unbound named " ^ Int.toString n ^ "\n") fun testReduce job = - (case tag job of + (case reduce job of NONE => print "Failed\n" | SOME file => (Print.print (CorePrint.p_file CoreEnv.empty file); diff -r 06a98129b23f -r 80192edca30d src/core.sml --- a/src/core.sml Tue Jul 29 12:30:04 2008 -0400 +++ b/src/core.sml Tue Jul 29 13:16:21 2008 -0400 @@ -87,6 +87,7 @@ datatype decl' = DCon of string * int * kind * con + | DDatatype of string * int * (string * int * con option) list | DVal of string * int * con * exp * string | DValRec of (string * int * con * exp * string) list | DExport of export_kind * int diff -r 06a98129b23f -r 80192edca30d src/core_env.sml --- a/src/core_env.sml Tue Jul 29 12:30:04 2008 -0400 +++ b/src/core_env.sml Tue Jul 29 13:16:21 2008 -0400 @@ -119,9 +119,17 @@ NONE => raise UnboundNamed n | SOME x => x -fun declBinds env (d, _) = +fun declBinds env (d, loc) = case d of DCon (x, n, k, c) => pushCNamed env x n k (SOME c) + | DDatatype (x, n, xncs) => + let + val env = pushCNamed env x n (KType, loc) NONE + in + foldl (fn ((x', n', NONE), env) => pushENamed env x' n' (CNamed n, loc) NONE "" + | ((x', n', SOME t), env) => pushENamed env x' n' (TFun (t, (CNamed n, loc)), loc) NONE "") + env xncs + end | DVal (x, n, t, e, s) => pushENamed env x n t (SOME e) s | DValRec vis => foldl (fn ((x, n, t, e, s), env) => pushENamed env x n t NONE s) env vis | DExport _ => env diff -r 06a98129b23f -r 80192edca30d src/core_print.sml --- a/src/core_print.sml Tue Jul 29 12:30:04 2008 -0400 +++ b/src/core_print.sml Tue Jul 29 13:16:21 2008 -0400 @@ -290,6 +290,24 @@ Link => string "link" | Action => string "action" +fun p_datatype env (x, n, cons) = + let + val env = E.pushCNamed env x n (KType, ErrorMsg.dummySpan) NONE + in + box [string "datatype", + space, + string x, + space, + string "=", + space, + p_list_sep (box [space, string "|", space]) + (fn (x, n, NONE) => if !debug then (string (x ^ "__" ^ Int.toString n)) + else string x + | (x, _, SOME t) => box [if !debug then (string (x ^ "__" ^ Int.toString n)) + else string x, space, string "of", space, p_con env t]) + cons] + end + fun p_decl env (dAll as (d, _) : decl) = case d of DCon (x, n, k, c) => @@ -313,6 +331,7 @@ space, p_con env c] end + | DDatatype x => p_datatype env x | DVal vi => box [string "val", space, p_vali env vi] diff -r 06a98129b23f -r 80192edca30d src/core_util.sml --- a/src/core_util.sml Tue Jul 29 12:30:04 2008 -0400 +++ b/src/core_util.sml Tue Jul 29 13:16:21 2008 -0400 @@ -390,6 +390,15 @@ S.map2 (mfc ctx c, fn c' => (DCon (x, n, k', c'), loc))) + | DDatatype (x, n, xncs) => + S.map2 (ListUtil.mapfold (fn (x, n, c) => + case c of + NONE => S.return2 (x, n, c) + | SOME c => + S.map2 (mfc ctx c, + fn c' => (x, n, SOME c'))) xncs, + fn xncs' => + (DDatatype (x, n, xncs'), loc)) | DVal vi => S.map2 (mfvi ctx vi, fn vi' => @@ -458,6 +467,21 @@ val ctx' = case #1 d' of DCon (x, n, k, c) => bind (ctx, NamedC (x, n, k, SOME c)) + | DDatatype (x, n, xncs) => + let + val ctx = bind (ctx, NamedC (x, n, (KType, #2 d'), NONE)) + val t = (CNamed n, #2 d') + in + foldl (fn ((x, n, to), ctx) => + let + val t = case to of + NONE => t + | SOME t' => (TFun (t', t), #2 d') + in + bind (ctx, NamedE (x, n, t, NONE, "")) + end) + ctx xncs + end | DVal (x, n, t, e, s) => bind (ctx, NamedE (x, n, t, SOME e, s)) | DValRec vis => foldl (fn ((x, n, t, e, s), ctx) => bind (ctx, NamedE (x, n, t, NONE, s))) diff -r 06a98129b23f -r 80192edca30d src/corify.sml --- a/src/corify.sml Tue Jul 29 12:30:04 2008 -0400 +++ b/src/corify.sml Tue Jul 29 13:16:21 2008 -0400 @@ -75,6 +75,7 @@ ENormal of int | EFfi of string * L'.con val bindVal : t -> string -> int -> t * int + val bindConstructor : t -> string -> int -> t val lookupValById : t -> int -> int option val lookupValByName : t -> string -> core_val @@ -182,6 +183,25 @@ n') end +fun bindConstructor {cons, vals, strs, funs, current, nested} s n = + let + val current = + case current of + FFfi _ => raise Fail "Binding inside FFfi" + | FNormal {cons, vals, strs, funs} => + FNormal {cons = cons, + vals = SM.insert (vals, s, n), + strs = strs, + funs = funs} + in + {cons = cons, + vals = IM.insert (vals, n, n), + strs = strs, + funs = funs, + current = current, + nested = nested} + end + fun lookupValById ({vals, ...} : t) n = IM.find (vals, n) fun lookupValByName ({current, ...} : t) x = @@ -384,8 +404,44 @@ in ([(L'.DCon (x, n, corifyKind k, corifyCon st c), loc)], st) end - | L.DDatatype _ => raise Fail "Corify DDatatype" - | L.DDatatypeImp _ => raise Fail "Corify DDatatypeImp" + | L.DDatatype (x, n, xncs) => + let + val (st, n) = St.bindCon st x n + val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) => + let + val st = St.bindConstructor st x n + val co = Option.map (corifyCon st) co + in + ((x, n, co), st) + end) st xncs + in + ([(L'.DDatatype (x, n, xncs), loc)], st) + end + | L.DDatatypeImp (x, n, m1, ms, s, xncs) => + let + val (st, n) = St.bindCon st x n + val c = corifyCon st (L.CModProj (m1, ms, s), loc) + + val (xncs, st) = ListUtil.foldlMap (fn ((x, n, co), st) => + let + val (st, n) = St.bindVal st x n + val co = Option.map (corifyCon st) co + in + ((x, n, co), st) + end) st xncs + + val cds = map (fn (x, n, co) => + let + val t = case co of + NONE => c + | SOME t' => (L'.TFun (t', c), loc) + val e = corifyExp st (L.EModProj (m1, ms, x), loc) + in + (L'.DVal (x, n, t, e, x), loc) + end) xncs + in + ((L'.DCon (x, n, (L'.KType, loc), c), loc) :: cds, st) + end | L.DVal (x, n, t, e) => let val (st, n) = St.bindVal st x n diff -r 06a98129b23f -r 80192edca30d src/expl_print.sml --- a/src/expl_print.sml Tue Jul 29 12:30:04 2008 -0400 +++ b/src/expl_print.sml Tue Jul 29 13:16:21 2008 -0400 @@ -285,8 +285,10 @@ string "=", space, p_list_sep (box [space, string "|", space]) - (fn (x, _, NONE) => string x - | (x, _, SOME t) => box [string x, space, string "of", space, p_con env t]) + (fn (x, n, NONE) => if !debug then (string (x ^ "__" ^ Int.toString n)) + else string x + | (x, _, SOME t) => box [if !debug then (string (x ^ "__" ^ Int.toString n)) + else string x, space, string "of", space, p_con env t]) cons] end diff -r 06a98129b23f -r 80192edca30d src/monoize.sml --- a/src/monoize.sml Tue Jul 29 12:30:04 2008 -0400 +++ b/src/monoize.sml Tue Jul 29 13:16:21 2008 -0400 @@ -453,6 +453,7 @@ in case d of L.DCon _ => NONE + | L.DDatatype _ => raise Fail "Monoize DDatatype" | L.DVal (x, n, t, e, s) => SOME (Env.pushENamed env x n t (SOME e) s, (L'.DVal (x, n, monoType env t, monoExp (env, St.empty) e, s), loc)) | L.DValRec vis => diff -r 06a98129b23f -r 80192edca30d src/shake.sml --- a/src/shake.sml Tue Jul 29 12:30:04 2008 -0400 +++ b/src/shake.sml Tue Jul 29 13:16:21 2008 -0400 @@ -47,7 +47,9 @@ (fn ((DExport (_, n), _), page_es) => n :: page_es | (_, page_es) => page_es) [] file - val (cdef, edef) = foldl (fn ((DCon (_, n, _, c), _), (cdef, edef)) => (IM.insert (cdef, n, c), edef) + val (cdef, edef) = foldl (fn ((DCon (_, n, _, c), _), (cdef, edef)) => (IM.insert (cdef, n, [c]), edef) + | ((DDatatype (_, n, xncs), _), (cdef, edef)) => + (IM.insert (cdef, n, List.mapPartial #3 xncs), edef) | ((DVal (_, n, t, e, _), _), (cdef, edef)) => (cdef, IM.insert (edef, n, (t, e))) | ((DValRec vis, _), (cdef, edef)) => (cdef, foldl (fn ((_, n, t, e, _), edef) => IM.insert (edef, n, (t, e))) edef vis) @@ -68,7 +70,7 @@ in case IM.find (cdef, n) of NONE => s' - | SOME c => shakeCon s' c + | SOME cs => foldl (fn (c, s') => shakeCon s' c) s' cs end | _ => s @@ -100,6 +102,7 @@ | SOME (t, e) => shakeExp (shakeCon s t) e) s page_es in List.filter (fn (DCon (_, n, _, _), _) => IS.member (#con s, n) + | (DDatatype (_, n, _), _) => IS.member (#con s, n) | (DVal (_, n, _, _, _), _) => IS.member (#exp s, n) | (DValRec vis, _) => List.exists (fn (_, n, _, _, _) => IS.member (#exp s, n)) vis | (DExport _, _) => true) file diff -r 06a98129b23f -r 80192edca30d src/tag.sml --- a/src/tag.sml Tue Jul 29 12:30:04 2008 -0400 +++ b/src/tag.sml Tue Jul 29 13:16:21 2008 -0400 @@ -153,6 +153,7 @@ val count = foldl (fn ((d, _), count) => case d of DCon (_, n, _, _) => Int.max (n, count) + | DDatatype (_, n, _) => Int.max (n, count) | DVal (_, n, _, _, _) => Int.max (n, count) | DValRec vis => foldl (fn ((_, n, _, _, _), count) => Int.max (n, count)) count vis | DExport _ => count) 0 file diff -r 06a98129b23f -r 80192edca30d tests/datatypeMod.lac --- a/tests/datatypeMod.lac Tue Jul 29 12:30:04 2008 -0400 +++ b/tests/datatypeMod.lac Tue Jul 29 13:16:21 2008 -0400 @@ -2,19 +2,27 @@ datatype t = A | B end -val a = M.A +val ac = M.A datatype u = datatype M.t -val a : M.t = A -val a2 : u = a +val ac : M.t = A +val a2 : u = ac structure M2 = M structure M3 : sig datatype t = datatype M.t end = M2 structure M4 : sig datatype t = datatype M.t end = M -val b : M3.t = M4.B +val bc : M3.t = M4.B structure Ma : sig type t end = M structure Magain : sig datatype t = A | B end = M + +val page : M.t -> page = fn x => + Hi. + + +val main : unit -> page = fn () => + Link +