annotate src/mono_env.sml @ 168:25b169416ea8

Storing datatype constructors in type references past monoize
author Adam Chlipala <adamc@hcoop.net>
date Tue, 29 Jul 2008 15:43:17 -0400
parents 6847741e1f5f
children eb3f9913bf31
rev   line source
adamc@25 1 (* Copyright (c) 2008, Adam Chlipala
adamc@25 2 * All rights reserved.
adamc@25 3 *
adamc@25 4 * Redistribution and use in source and binary forms, with or without
adamc@25 5 * modification, are permitted provided that the following conditions are met:
adamc@25 6 *
adamc@25 7 * - Redistributions of source code must retain the above copyright notice,
adamc@25 8 * this list of conditions and the following disclaimer.
adamc@25 9 * - Redistributions in binary form must reproduce the above copyright notice,
adamc@25 10 * this list of conditions and the following disclaimer in the documentation
adamc@25 11 * and/or other materials provided with the distribution.
adamc@25 12 * - The names of contributors may not be used to endorse or promote products
adamc@25 13 * derived from this software without specific prior written permission.
adamc@25 14 *
adamc@25 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
adamc@25 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
adamc@25 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
adamc@25 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
adamc@25 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
adamc@25 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
adamc@25 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
adamc@25 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
adamc@25 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
adamc@25 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
adamc@25 25 * POSSIBILITY OF SUCH DAMAGE.
adamc@25 26 *)
adamc@25 27
adamc@25 28 structure MonoEnv :> MONO_ENV = struct
adamc@25 29
adamc@25 30 open Mono
adamc@25 31
adamc@25 32 structure IM = IntBinaryMap
adamc@25 33
adamc@25 34
adamc@25 35 exception UnboundRel of int
adamc@25 36 exception UnboundNamed of int
adamc@25 37
adamc@25 38 type env = {
adamc@168 39 datatypes : (string * (string * int * typ option) list) IM.map,
adamc@25 40
adamc@25 41 relE : (string * typ) list,
adamc@109 42 namedE : (string * typ * exp option * string) IM.map
adamc@25 43 }
adamc@25 44
adamc@25 45 val empty = {
adamc@168 46 datatypes = IM.empty,
adamc@25 47
adamc@25 48 relE = [],
adamc@25 49 namedE = IM.empty
adamc@25 50 }
adamc@25 51
adamc@168 52 fun pushDatatype (env : env) x n xncs =
adamc@168 53 {datatypes = IM.insert (#datatypes env, n, (x, xncs)),
adamc@25 54
adamc@25 55 relE = #relE env,
adamc@25 56 namedE = #namedE env}
adamc@25 57
adamc@168 58 fun lookupDatatype (env : env) n =
adamc@168 59 case IM.find (#datatypes env, n) of
adamc@25 60 NONE => raise UnboundNamed n
adamc@25 61 | SOME x => x
adamc@25 62
adamc@25 63 fun pushERel (env : env) x t =
adamc@168 64 {datatypes = #datatypes env,
adamc@25 65
adamc@25 66 relE = (x, t) :: #relE env,
adamc@25 67 namedE = #namedE env}
adamc@25 68
adamc@25 69 fun lookupERel (env : env) n =
adamc@25 70 (List.nth (#relE env, n))
adamc@25 71 handle Subscript => raise UnboundRel n
adamc@25 72
adamc@109 73 fun pushENamed (env : env) x n t eo s =
adamc@168 74 {datatypes = #datatypes env,
adamc@25 75
adamc@25 76 relE = #relE env,
adamc@109 77 namedE = IM.insert (#namedE env, n, (x, t, eo, s))}
adamc@25 78
adamc@25 79 fun lookupENamed (env : env) n =
adamc@25 80 case IM.find (#namedE env, n) of
adamc@25 81 NONE => raise UnboundNamed n
adamc@25 82 | SOME x => x
adamc@25 83
adamc@164 84 fun declBinds env (d, loc) =
adamc@25 85 case d of
adamc@164 86 DDatatype (x, n, xncs) =>
adamc@164 87 let
adamc@168 88 val env = pushDatatype env x n xncs
adamc@164 89 in
adamc@168 90 foldl (fn ((x', n', NONE), env) => pushENamed env x' n' (TDatatype (n, xncs), loc) NONE ""
adamc@168 91 | ((x', n', SOME t), env) => pushENamed env x' n' (TFun (t, (TDatatype (n, xncs), loc)), loc) NONE "")
adamc@164 92 env xncs
adamc@164 93 end
adamc@164 94 | DVal (x, n, t, e, s) => pushENamed env x n t (SOME e) s
adamc@126 95 | DValRec vis => foldl (fn ((x, n, t, e, s), env) => pushENamed env x n t NONE s) env vis
adamc@109 96 | DExport _ => env
adamc@25 97
adamc@25 98 end