Skip to content

Instantly share code, notes, and snippets.

@joonazan
Created April 17, 2017 13:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joonazan/d4c0cfd9018719a9532dccba14bc4531 to your computer and use it in GitHub Desktop.
Save joonazan/d4c0cfd9018719a9532dccba14bc4531 to your computer and use it in GitHub Desktop.
typeSignature =
lazy <| \() ->
sepBy (whitespace *> string "->" *> whitespace) nonFunctionType
|> andThen (
reducer TArrow
>> Maybe.map succeed
>> Maybe.withDefault (fail "expected type, got nothing")
)
reducer : (a -> a -> a) -> List a -> Maybe a
reducer f list =
case list of
last :: [] ->
Just last
head :: tail ->
reducer f tail
|> Maybe.map (f head)
[] ->
Nothing
nonFunctionType : Parser s Type
nonFunctionType =
let
withArguments =
TUnion
<$> uppercaseName
<*> (many onePartType)
in
or withArguments onePartType
onePartType : Parser s Type
onePartType =
choice
[ typeName
, typeVar
, parentheses
--, tuple
--, record
]
typeName : Parser s Type
typeName =
(flip TUnion <| []) <$> uppercaseName
-- TODO: properly assign type variables
typeVar : Parser s Type
typeVar =
always (TAny 1) <$> lowercaseName
parentheses : Parser s Type
parentheses =
parens typeSignature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment