on f4401ac help sign in
elk/compiler/src/Type/Instantiate.hs
77 lines 1.7 kB history raw
1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Type.Instantiate
4 ( FreeVars
5 , fromSrcType
6 )
7 where
10import qualified Data.Map.Strict as Map
11import Data.Map.Strict ((!))
12import qualified Data.Name as Name
14import qualified AST.Canonical as Can
15import Type.Type
19-- FREE VARS
22type FreeVars =
23 Map.Map Name.Name Type
27-- FROM SOURCE TYPE
30fromSrcType :: Map.Map Name.Name Type -> Can.Type -> IO Type
31fromSrcType freeVars sourceType =
32 case sourceType of
33 Can.TLambda arg result ->
34 FunN
35 <$> fromSrcType freeVars arg
36 <*> fromSrcType freeVars result
38 Can.TVar name ->
39 return (freeVars ! name)
41 Can.TType home name args ->
42 AppN home name <$> traverse (fromSrcType freeVars) args
44 Can.TAlias home name args aliasedType ->
45 do targs <- traverse (traverse (fromSrcType freeVars)) args
46 AliasN home name targs <$>
47 case aliasedType of
48 Can.Filled realType ->
49 fromSrcType freeVars realType
51 Can.Holey realType ->
52 fromSrcType (Map.fromList targs) realType
54 Can.TTuple a b maybeC ->
55 TupleN
56 <$> fromSrcType freeVars a
57 <*> fromSrcType freeVars b
58 <*> traverse (fromSrcType freeVars) maybeC
60 Can.TUnit ->
61 return UnitN
63 Can.TRecord fields maybeExt ->
64 RecordN
65 <$> traverse (fromSrcFieldType freeVars) fields
66 <*>
67 case maybeExt of
68 Nothing ->
69 return EmptyRecordN
71 Just ext ->
72 return (freeVars ! ext)
75fromSrcFieldType :: Map.Map Name.Name Type -> Can.FieldType -> IO Type
76fromSrcFieldType freeVars (Can.FieldType _ tipe) =
77 fromSrcType freeVars tipe