adamc@29: (* Copyright (c) 2008, Adam Chlipala adamc@29: * All rights reserved. adamc@29: * adamc@29: * Redistribution and use in source and binary forms, with or without adamc@29: * modification, are permitted provided that the following conditions are met: adamc@29: * adamc@29: * - Redistributions of source code must retain the above copyright notice, adamc@29: * this list of conditions and the following disclaimer. adamc@29: * - Redistributions in binary form must reproduce the above copyright notice, adamc@29: * this list of conditions and the following disclaimer in the documentation adamc@29: * and/or other materials provided with the distribution. adamc@29: * - The names of contributors may not be used to endorse or promote products adamc@29: * derived from this software without specific prior written permission. adamc@29: * adamc@29: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" adamc@29: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE adamc@29: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE adamc@29: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE adamc@29: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR adamc@29: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF adamc@29: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS adamc@29: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN adamc@29: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) adamc@29: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE adamc@29: * POSSIBILITY OF SUCH DAMAGE. adamc@29: *) adamc@29: adamc@29: structure Cjrize :> CJRIZE = struct adamc@29: adamc@29: structure L = Flat adamc@29: structure L' = Cjr adamc@29: adamc@29: structure Sm :> sig adamc@29: type t adamc@29: adamc@29: val empty : t adamc@29: val find : t * (string * L.typ) list * (string * L'.typ) list -> t * int adamc@29: adamc@29: val declares : t -> (int * (string * L'.typ) list) list adamc@29: end = struct adamc@29: adamc@29: structure FM = BinaryMapFn(struct adamc@29: type ord_key = L.typ adamc@29: val compare = FlatUtil.Typ.compare adamc@29: end) adamc@29: adamc@29: type t = int * int FM.map * (int * (string * L'.typ) list) list adamc@29: adamc@29: val empty = (0, FM.empty, []) adamc@29: adamc@29: fun find ((n, m, ds), xts, xts') = adamc@29: let adamc@29: val t = (L.TRecord xts, ErrorMsg.dummySpan) adamc@29: in adamc@29: case FM.find (m, t) of adamc@29: NONE => ((n+1, FM.insert (m, t, n), (n, xts') :: ds), n) adamc@29: | SOME i => ((n, m, ds), i) adamc@29: end adamc@29: adamc@29: fun declares (_, _, ds) = ds adamc@29: adamc@29: end adamc@29: adamc@29: fun cifyTyp ((t, loc), sm) = adamc@29: case t of adamc@29: L.TTop => ((L'.TTop, loc), sm) adamc@29: | L.TFun (t1, t2) => adamc@29: let adamc@29: val (_, sm) = cifyTyp (t1, sm) adamc@29: val (_, sm) = cifyTyp (t2, sm) adamc@29: in adamc@29: ((L'.TFun, loc), sm) adamc@29: end adamc@29: | L.TCode (t1, t2) => adamc@29: let adamc@29: val (t1, sm) = cifyTyp (t1, sm) adamc@29: val (t2, sm) = cifyTyp (t2, sm) adamc@29: in adamc@29: ((L'.TCode (t1, t2), loc), sm) adamc@29: end adamc@29: | L.TRecord xts => adamc@29: let adamc@29: val old_xts = xts adamc@29: val (xts, sm) = ListUtil.foldlMap (fn ((x, t), sm) => adamc@29: let adamc@29: val (t, sm) = cifyTyp (t, sm) adamc@29: in adamc@29: ((x, t), sm) adamc@29: end) adamc@29: sm xts adamc@29: val (sm, si) = Sm.find (sm, old_xts, xts) adamc@29: in adamc@29: ((L'.TRecord si, loc), sm) adamc@29: end adamc@29: | L.TNamed n => ((L'.TNamed n, loc), sm) adamc@29: adamc@29: fun cifyExp ((e, loc), sm) = adamc@29: case e of adamc@29: L.EPrim p => ((L'.EPrim p, loc), sm) adamc@29: | L.ERel n => ((L'.ERel n, loc), sm) adamc@29: | L.ENamed n => ((L'.ENamed n, loc), sm) adamc@29: | L.ECode n => ((L'.ECode n, loc), sm) adamc@29: | L.EApp (e1, e2) => adamc@29: let adamc@29: val (e1, sm) = cifyExp (e1, sm) adamc@29: val (e2, sm) = cifyExp (e2, sm) adamc@29: in adamc@29: ((L'.EApp (e1, e2), loc), sm) adamc@29: end adamc@29: adamc@29: | L.ERecord xes => adamc@29: let adamc@29: val old_xts = map (fn (x, _, t) => (x, t)) xes adamc@29: adamc@29: val (xets, sm) = ListUtil.foldlMap (fn ((x, e, t), sm) => adamc@29: let adamc@29: val (e, sm) = cifyExp (e, sm) adamc@29: val (t, sm) = cifyTyp (t, sm) adamc@29: in adamc@29: ((x, e, t), sm) adamc@29: end) adamc@29: sm xes adamc@29: adamc@29: val (sm, si) = Sm.find (sm, old_xts, map (fn (x, _, t) => (x, t)) xets) adamc@29: adamc@29: val xes = map (fn (x, e, _) => (x, e)) xets adamc@29: val xes = ListMergeSort.sort (fn ((x1, _), (x2, _)) => String.compare (x1, x2) = GREATER) xes adamc@29: in adamc@29: ((L'.ERecord (si, xes), loc), sm) adamc@29: end adamc@29: | L.EField (e, x) => adamc@29: let adamc@29: val (e, sm) = cifyExp (e, sm) adamc@29: in adamc@29: ((L'.EField (e, x), loc), sm) adamc@29: end adamc@29: adamc@29: | L.ELet (xes, e) => adamc@29: let adamc@29: val (xes, sm) = ListUtil.foldlMap (fn ((x, t, e), sm) => adamc@29: let adamc@29: val (t, sm) = cifyTyp (t, sm) adamc@29: val (e, sm) = cifyExp (e, sm) adamc@29: in adamc@29: ((x, t, e), sm) adamc@29: end) adamc@29: sm xes adamc@29: val (e, sm) = cifyExp (e, sm) adamc@29: in adamc@29: ((L'.ELet (xes, e), loc), sm) adamc@29: end adamc@29: adamc@29: fun cifyDecl ((d, loc), sm) = adamc@29: case d of adamc@29: L.DVal (x, n, t, e) => adamc@29: let adamc@29: val (t, sm) = cifyTyp (t, sm) adamc@29: val (e, sm) = cifyExp (e, sm) adamc@29: in adamc@29: ((L'.DVal (x, n, t, e), loc), sm) adamc@29: end adamc@29: | L.DFun (n, x, dom, ran, e) => adamc@29: let adamc@29: val (dom, sm) = cifyTyp (dom, sm) adamc@29: val (ran, sm) = cifyTyp (ran, sm) adamc@29: val (e, sm) = cifyExp (e, sm) adamc@29: in adamc@29: ((L'.DFun (n, x, dom, ran, e), loc), sm) adamc@29: end adamc@29: adamc@29: fun cjrize ds = adamc@29: let adamc@29: val (ds, sm) = ListUtil.foldlMap cifyDecl Sm.empty ds adamc@29: in adamc@29: List.revAppend (map (fn v => (L'.DStruct v, ErrorMsg.dummySpan)) (Sm.declares sm), adamc@29: ds) adamc@29: end adamc@29: adamc@29: end