Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions src/QsCompiler/Core/ConstructorExtensions.fs
Original file line number Diff line number Diff line change
Expand Up @@ -11,39 +11,39 @@ open Microsoft.Quantum.QsCompiler.SyntaxTokens
open Microsoft.Quantum.QsCompiler.SyntaxTree


type QsQualifiedName with
type QsQualifiedName with
static member New (nsName, cName) = {
Namespace = nsName
Name = cName
}

type UserDefinedType with
type UserDefinedType with
static member New (nsName, tName, range) = {
Namespace = nsName
Name = tName
Range = range
}

type QsTypeParameter with
type QsTypeParameter with
static member New (origin, tName, range) = {
Origin = origin
TypeName = tName
Range = range
}

type QsLocation with
type QsLocation with
static member New (pos, range) = {
Offset = pos
Range = range
}

type InferredExpressionInformation with
type InferredExpressionInformation with
static member New (isMutable, quantumDep) = {
IsMutable = isMutable
IsMutable = isMutable
HasLocalQuantumDependency = quantumDep
}

type LocalVariableDeclaration<'Name> with
type LocalVariableDeclaration<'Name> with
static member New isMutable ((pos, range), vName : 'Name, t, hasLocalQuantumDependency) = {
VariableName = vName
Type = t
Expand All @@ -52,63 +52,63 @@ type LocalVariableDeclaration<'Name> with
Range = range
}

type LocalDeclarations with
type LocalDeclarations with
static member New (variables : IEnumerable<_>) = {
Variables = variables.ToImmutableArray()
}

static member Concat this other =
static member Concat this other =
LocalDeclarations.New (this.Variables.Concat other.Variables)

member this.AsVariableLookup () =
member this.AsVariableLookup () =
let localVars = this.Variables |> Seq.map (fun decl -> decl.VariableName, decl)
new ReadOnlyDictionary<_,_>(localVars.ToDictionary(fst, snd))

type InferredCallableInformation with
type InferredCallableInformation with
/// the default values are intrinsic: false, selfAdj: false
static member New (?intrinsic, ?selfAdj) = {
IsIntrinsic = defaultArg intrinsic false
IsSelfAdjoint = defaultArg selfAdj false
}

type CallableInformation with
type CallableInformation with
static member New (characteristics, inferredInfo) = {
Characteristics = characteristics
InferredInformation = inferredInfo
}

type TypedExpression with
type TypedExpression with
/// Builds and returns a TypedExpression with the given properties.
/// The UnresolvedType of the given expression is set to the given expression type, and
/// the ResolvedType is set to the type constructed by resolving it using ResolveTypeParameters and the given look-up.
static member New (expr, typeParamResolutions : ImmutableDictionary<_,_>, exType, exInfo, range) = {
Expression = expr
TypeArguments = typeParamResolutions |> Seq.map (fun kv -> fst kv.Key, snd kv.Key, kv.Value) |> ImmutableArray.CreateRange
TypeArguments = TypedExpression.AsTypeArguments typeParamResolutions
ResolvedType = ResolvedType.ResolveTypeParameters typeParamResolutions exType
InferredInformation = exInfo
Range = range
}
type QsBinding<'T> with

type QsBinding<'T> with
static member New kind (lhs, rhs) = {
Kind = kind
Lhs = lhs
Rhs = rhs
}

type QsValueUpdate with
type QsValueUpdate with
static member New (lhs, rhs) = {
Lhs = lhs
Rhs = rhs
}

type QsComments with
type QsComments with
static member New (before : IEnumerable<_>, after : IEnumerable<_>) = {
OpeningComments = before.ToImmutableArray()
ClosingComments = after.ToImmutableArray()
}

type QsScope with
type QsScope with
static member New (statements : IEnumerable<_>, parentSymbols) = {
Statements = statements.ToImmutableArray()
KnownSymbols = parentSymbols
Expand All @@ -121,7 +121,7 @@ type QsPositionedBlock with
Comments = comments
}

type QsConditionalStatement with
type QsConditionalStatement with
static member New (blocks : IEnumerable<_>, defaultBlock) = {
ConditionalBlocks = blocks.ToImmutableArray()
Default = defaultBlock
Expand All @@ -134,49 +134,49 @@ type QsForStatement with
Body = body
}

type QsWhileStatement with
type QsWhileStatement with
static member New (condition, body) = {
Condition = condition
Body = body
}

type QsRepeatStatement with
type QsRepeatStatement with
static member New (repeatBlock, successCondition, fixupBlock) = {
RepeatBlock = repeatBlock
SuccessCondition = successCondition
FixupBlock = fixupBlock
}

type QsConjugation with
type QsConjugation with
static member New (outer, inner) = {
OuterTransformation = outer
InnerTransformation = inner
}

type QsQubitScope with
type QsQubitScope with
static member New kind ((lhs,rhs), body) = {
Kind = kind
Binding = QsBinding<QsInitializer>.New QsBindingKind.ImmutableBinding (lhs, rhs)
Body = body
}

type QsStatement with
type QsStatement with
static member New comments location (kind, symbolDecl) = {
Statement = kind
SymbolDeclarations = symbolDecl
Location = location
Comments = comments
}

type ResolvedSignature with
type ResolvedSignature with
static member New ((argType, returnType), info, typeParams : IEnumerable<_>) = {
TypeParameters = typeParams.ToImmutableArray()
ArgumentType = argType
ReturnType = returnType
Information = info
}

type QsSpecialization with
type QsSpecialization with
static member New kind (source, location) (parent, attributes, typeArgs, signature, implementation, documentation, comments) = {
Kind = kind
Parent = parent
Expand All @@ -194,7 +194,7 @@ type QsSpecialization with
static member NewControlled = QsSpecialization.New QsControlled
static member NewControlledAdjoint = QsSpecialization.New QsControlledAdjoint

type QsCallable with
type QsCallable with
static member New kind (source, location) (name, attributes, modifiers, argTuple, signature, specializations : IEnumerable<_>, documentation, comments) = {
Kind = kind
FullName = name
Expand Down Expand Up @@ -225,7 +225,7 @@ type QsCustomType with
Comments = comments
}

type QsDeclarationAttribute with
type QsDeclarationAttribute with
static member New (typeId, arg, pos, comments) = {
TypeId = typeId
Argument = arg
Expand All @@ -234,18 +234,18 @@ type QsDeclarationAttribute with
}

type QsNamespaceElement with
static member NewOperation loc = QsCallable.NewOperation loc >> QsCallable
static member NewOperation loc = QsCallable.NewOperation loc >> QsCallable
static member NewFunction loc = QsCallable.NewFunction loc >> QsCallable
static member NewType loc = QsCustomType.New loc >> QsCustomType

type QsNamespace with
type QsNamespace with
static member New (name, elements : IEnumerable<_>, documentation) = {
Name = name
Elements = elements.ToImmutableArray()
Documentation = documentation
}

type QsCompilation with
type QsCompilation with
static member New (namespaces, entryPoints) = {
Namespaces = namespaces
EntryPoints = entryPoints
Expand Down
21 changes: 13 additions & 8 deletions src/QsCompiler/DataStructures/SyntaxTree.fs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ type QsQualifiedName = {
/// the declared name of the namespace element
Name : NonNullable<string>
}
with
override this.ToString () =
with
override this.ToString () =
sprintf "%s.%s" this.Namespace.Value this.Name.Value


Expand Down Expand Up @@ -344,8 +344,8 @@ type InferredExpressionInformation = {
type TypedExpression = {
/// the content (kind) of the expression
Expression : QsExpressionKind<TypedExpression, Identifier, ResolvedType>
/// contains all type arguments implicitly or explicitly determined by the expression,
/// i.e. the origin, name and concrete type of all type parameters whose type can either be inferred based on the expression,
/// contains all type arguments implicitly or explicitly determined by the expression,
/// i.e. the origin, name and concrete type of all type parameters whose type can either be inferred based on the expression,
/// or who have explicitly been resolved by provided type arguments
TypeArguments : ImmutableArray<QsQualifiedName * NonNullable<string> * ResolvedType>
/// the type of the expression after applying the type arguments
Expand All @@ -361,13 +361,18 @@ type TypedExpression = {

/// Contains a dictionary mapping the origin and name of all type parameters whose type can either be inferred based on the expression,
/// or who have explicitly been resolved by provided type arguments to their concrete type within this expression
member this.TypeParameterResolutions =
member this.TypeParameterResolutions =
this.TypeArguments.ToImmutableDictionary((fun (origin, name, _) -> origin, name), (fun (_,_,t) -> t))

/// Given a dictionary containing the type resolutions for an expression,
/// returns the corresponding ImmutableArray to initialize the TypeArguments with.
static member AsTypeArguments (typeParamResolutions : ImmutableDictionary<_,_>) =
Comment thread
ScottCarda-MS marked this conversation as resolved.
typeParamResolutions |> Seq.map (fun kv -> fst kv.Key, snd kv.Key, kv.Value) |> ImmutableArray.CreateRange

/// Returns true if the expression is a call-like expression, and the arguments contain a missing expression.
/// Returns false otherwise.
static member public IsPartialApplication kind =
let rec containsMissing ex =
let rec containsMissing ex =
match ex.Expression with
| MissingExpr -> true
| ValueTuple items -> items |> Seq.exists containsMissing
Expand Down Expand Up @@ -564,7 +569,7 @@ and QsStatementKind =
| QsRepeatStatement of QsRepeatStatement
| QsConjugation of QsConjugation
| QsQubitScope of QsQubitScope // includes both using and borrowing scopes
| EmptyStatement
| EmptyStatement


and QsStatement = {
Expand Down Expand Up @@ -645,7 +650,7 @@ type QsSpecialization = {
/// Contains the location information for the declared specialization.
/// The position offset represents the position in the source file where the specialization is declared,
/// and the range contains the range of the corresponding specialization header.
/// For auto-generated specializations, the location is set to the location of the parent callable declaration.
/// For auto-generated specializations, the location is set to the location of the parent callable declaration.
Location : QsNullable<QsLocation>
/// contains the type arguments for which the implementation is specialized
TypeArguments : QsNullable<ImmutableArray<ResolvedType>>
Expand Down
77 changes: 77 additions & 0 deletions src/QsCompiler/Tests.Compiler/TestCases/TypeParameter.qs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.


// Identifier Resolution
namespace Microsoft.Quantum.Testing.TypeParameter {

operation Main() : Unit {
Foo<_, Int, String>(1.0, 2, "Three");
}

operation Foo<'A, 'B, 'C>(a : 'A, b : 'B, c : 'C) : Unit { }
}

// =================================

// Adjoint Application Resolution
namespace Microsoft.Quantum.Testing.TypeParameter {

operation Main() : Unit {
(Adjoint (Foo(1.0, 2, _)))("Three");
}

operation Foo<'A, 'B, 'C>(a : 'A, b : 'B, c : 'C) : Unit is Adj { }
}

// =================================

// Controlled Application Resolution
namespace Microsoft.Quantum.Testing.TypeParameter {

operation Main(qs : Qubit) : Unit {
(Controlled (Foo(1.0, 2, _)))([qs], "Three");
}

operation Foo<'A, 'B, 'C>(a : 'A, b : 'B, c : 'C) : Unit is Ctl { }
}

// =================================

// Partial Application Resolution
namespace Microsoft.Quantum.Testing.TypeParameter {

operation Main() : Unit {
(Foo(_, 3, "Hi"))(1.0);
}

operation Foo<'A, 'B, 'C>(a : 'A, b : 'B, c : 'C) : Unit { }
}

// =================================

// Sub-call Resolution
namespace Microsoft.Quantum.Testing.TypeParameter {

operation Main() : Unit {
(Foo(1, 2, 3))(4);
}

operation Foo<'A, 'B, 'C>(a : 'A, b : 'B, c : 'C) : ('C => Unit) { return Bar<'A, 'B, 'C>(a, b, _); }

operation Bar<'A, 'B, 'C>(a : 'A, b : 'B, c : 'C) : Unit { }
}

// =================================

// Argument Sub-call Resolution
namespace Microsoft.Quantum.Testing.TypeParameter {

operation Main() : Unit {
Foo(1.0, Bar(2), "Three");
}

operation Foo<'A, 'B, 'C>(a : 'A, b : 'B, c : 'C) : Unit { }

operation Bar<'A>(a : 'A) : 'A { return a; }
}
4 changes: 4 additions & 0 deletions src/QsCompiler/Tests.Compiler/Tests.Compiler.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
<None Include="TestCases\ClassicalControl.qs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="TestCases\TypeParameter.qs">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<Compile Include="..\..\Common\DelaySign.fs" Link="DelaySign.fs" />
<Compile Include="TextTests.fs" />
<Compile Include="SyntaxTests.fs" />
Expand All @@ -158,6 +161,7 @@
<Compile Include="ExecutionTests.fs" />
<Compile Include="LinkingTests.fs" />
<Compile Include="ClassicalControlTests.fs" />
<Compile Include="TypeParameterTests.fs" />
<Compile Include="RegexTests.fs" />
<Compile Include="SerializationTests.fs" />
<Compile Include="CommandLineTests.fs" />
Expand Down
Loading