aboutsummaryrefslogtreecommitdiff
path: root/snug-clean/src/Snug/Compile/Typing.icl
blob: 4d69aca717d0030073e3664ea05a0eb222fc9a9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
implementation module Snug.Compile.Typing

import Data.Error

import Snug.Syntax

instance type Expression
where
	type locals e = case e of
		BasicValue bv ->
			type locals bv
		Symbol sym -> // TODO
		Constructor cons ->
			lookupConstructorM ns cons >>= \(ConstructorDef _ args ret) ->
			foldr TyFun ret args
		Case _ alts ->
			checkSameTypes "case alternatives" [type locals e \\ CaseAlternative _ e <- alts]
		ExpApp e1 e2 ->
			type locals e1 >>= \t1 -> case t1 of
				TyFun t1arg t1ret ->
					type locals e2 >>= \t2 ->
					unify t1arg t2 ->
					resolve t1ret
				TyVar _ ->
					freshTyVar >>= \t1arg ->
					freshTyVar >>= \t1ret ->
					unify t1arg t2 ->
					resolve t1ret
				_ ->
					fail "ExpApp: first argument cannot be unified with a function type"